Estimating Electricity Cleanliness for Valuability of Electric Cars

The purpose of this post is to calculate the viable “dirtiness” of electricity for electric cars in order for them to be the better alternative to conventional diesel or petrol powered cars. It is a justification of my own information bubble as well as an answer to the rightly controversial and refuted article by Buchal, Karl and Sinn ​[1]​.

Let us note down the assumptions that will be made, respectively what we will neglect:

  • Neglect the construction, transport and sell of the car
  • Start at the point of “customer buys car” with the estimations
  • Neglect CO2 of fracking, refinery, storage and the transport of the fossil fuels
  • Assume electricity is composed of coal and water
  • Assume water energy has zero CO2 emissions
  • Neglect electricity grid construction

As for the data, we will calculate with the car models chosen in said paper: a Tesla Model 3 and a Mercedes C 220 d. The reference value for the Mercedes is a minimum of 0.134 kg CO2 per Kilometer ​[2]​. This is the value we will want to maximize the coal proportion to.

As for the Tesla Model 3, we calculate with a reported efficiency of 0.148 kWh per Kilometer ​[3]​. With a Li-ion battery efficiency of 99 % ​[4]​, we can safely neglect charge & discharge losses. Taking the global average of power loss by the power grid of 12 % ​[5]​, we require 0.168 kWh of produced power per Kilometer.

As for the CO2 production by coal power generation, we assume the worst case, the highest emissions, being by anthracite coal, of 0.354 kg CO2 per kWh (units converted) ​[6]​. With a proportion of 100 % coal power, our Tesla would be responsible for 0.059 kg CO2 per Kilometer. This is less than half of the emissions of the Mercedes, for the far-from-true assumption of 100 % of electricity coming from coal.

To summarize, a short calculation over the thumb supports the general impression that electric cars do indeed produce less emissions. It has to be noted though that quite a few assumptions were made, although most of them seem to cancel out in the direct comparsion. Also, only the carbon dioxide emissions were regarded, even though this is by no means the only nor a sufficient indicator of the environmental friendliness of an object. The only thing I might consider in a futher revision of these calculations is the production of the battery of the Tesla, as this is the major difference in the manufacture of the two cars.

Finally, if you find any mistakes in my calculations or have any doubts about my assumptions, do not hesitate to write me an e-mail or leave a comment.


  1. [1]
    C. Buchal, H.-D. Karl, and H.-W. Sinn, “Kohlemotoren, Windmotoren und Dieselmotoren: Was zeigt die CO2-Bilanz?” p. 15.
  2. [2]
    “C-Klasse: T-Modell, Preisliste gültig ab 1. Februar 2019.” Mercedes-Benz Schweiz AG.
  3. [3]
    “Tesla Model 3 Standard Range,” Electric Vehicle Database. [Online]. Available: [Accessed: 19-Jun-2019]
  4. [4]
    “Battery University,” BU-104a: Comparing the Battery with Other Power Sources, 28-Mar-2019. [Online]. Available: [Accessed: 19-Jun-2019]
  5. [5]
    “ 6. Efficiency and Power Grids,” Global CCS Institute. [Online]. Available: [Accessed: 16-Jun-2019]
  6. [6]
    “How much carbon dioxide is produced when different fuels are burned?,” U.S. Energy Information Administration, 04-Jun-2019. [Online]. Available: [Accessed: 19-Jun-2019]

My fix to`0 – Using $this not in object context` in Joomla!

After migrating, the Joomla! page would not show up. Instead, only the error “0 – Using $this not in object context” was showing up. In my case, I was able to fix the issue by replacing – in the code of the template – all JFactory::getMenu() with $application->getMenu(), where $application came from $application = JFactory::getApplication().

Note that this may not be the only possible fix for this error. It highly depends on the code you have. Generally, the errormessage indicates the underlying problem: somewhere in Joomla!s code, there is a $this inside a class which – in your code – is used, without the class being instantiated properly. In the case mentioned above, the class is initialized statically. This, because that was valid apparently in elder Joomla! versions.

To find the source of your problem, I recommend to enable Joomla! debugging as well as PHP logging in order to get the full stack trace and find the origin of the wrongly instantiated class.

How to Create a Third-Party Symfony Bundle

Have an awesome library you want to integrate in your Symfony application? Share your code between different Symfony instances? Share your code with other people? Create your bundle standalone and distribute it via Composer! Here is a short overview on how to do it.

To get started, you want to have a Composer & a git instance (if you follow the general practice to publish your bundle on a git-based central repository, such as GitHub or GitLab) running. All a Symfony third-party bundle has to be is a composer package. Everything else is bonus. To make your project a composer package, you add the `composer.json` file if you did not already. In it, you specify the name, properties and dependencies of your bundle. Comparsion with other bundles helps to get an impression of what to put in there.
Basically, it could look like mine for my PlaceholderBundle:

    "name" : "bernhard-webstudio/placeholder-bundle",
    "type" : "symfony-bundle",
    "description" : "Symfony bundle to generate placeholders for images",
    "keywords" : ["Symfony", "Placeholder"],
    "homepage" : "",
    "license" : "MIT",
    "authors" :
            "name" : "Tim Bernhard",
            "email" : "",
            "homepage" : "",
            "role" : "Developer"

After adding the boni, you will want to publish your bundle so other people can benefit from your bundle. There are various tutorials for this already so I will not write on.


To give the Symfony character to your package, you will want to integrate e.g. dependency injection, expose a class as a service or use the general Symfony configuration for your services. This can easily be achieved by adding the relevant Symfony components as dependencies via composer, e.g. use command composer require symfony/dependency-injection. Afterwards, you can use them the same way you are using them in your Symfony application. Pay attention not to limit the dependency versions too much, otherwise you may prevent a user from upgrading Symfony.

There are may boni you can add to your repository, if it is published on e.g. GitHub: to give users and contributors a chance to better understand the state of your repository, integrate some of the apps and add badges to your README file.

Finally, let’s talk testing: as your package should not ship with a Kernel oder similar, you have nothing to simply run. There are workarounds: either you let your bundle live in a complete application, e.g. as a submodule, and test the application as a whole. Or you use unit-testing. If your choice falls on the latter, I can recommend using ConfigTest if your bundle uses the configuration component. Also, at one point, you may have to have an AppKernel to test the execution of a command or the availability of a route. Depending on the actual need, you can either mock the Kernel or create a TestKernel. For latter are many examples online in other bundles, e.g. in mine.

My first third-party Symfony Bundle

I created my first open-source Symfony bundle: PlaceholderBundle. It is an abstraction of Primitive and/or SQIP for use in a PHP respectively Symfony application. This way, you can automatically generate nice & adaptive placeholders for images in the format you like, all in your PHP application or your Twig template. Refer to the documentation on how to use the PlaceholderBundle. For now, the underlying nodejs packages still have to be installed separately, depending on which you want to use. The configuration allows to personalise the placeholders as much as the underlying applications allow it.

I will write a follow-up on how to begin with developing a third-pary bundle, as I think the resources for it are rather limited. A general tip is to checkout existing bundles on how to do it.

How to Encode an SVG for the `src`-Attribute using PHP

As SVGs should preferably not be base64 encoded when setting them on an -tag src-attribute, the suggested alternative is to URL-encode them. The standard PHP urlencode function unfortunately is not suitable for this task, as the resulting value is not interpreted by any browser as a valid SVG image. Instead, the function rawurlencode has to be used. Took me some time to realize. As soon as you get this, you can also take over some other optimizations; maybe get inspired by Taylor Hunts mini-svg-uri to decode some characters manually to improve the overall size. A final function could possibly look like this:

function svgUrlEncode($svgPath)
$data = \file_get_contents($svgPath);
$data = \preg_replace('/\v(?:[\v\h]+)/', ' ', $data);
$data = \str_replace('"', "'", $data);
$data = \rawurlencode($data);
// re-decode a few characters understood by browsers to improve compression
$data = \str_replace('%20', ' ', $data);
$data = \str_replace('%3D', '=', $data);
$data = \str_replace('%3A', ':', $data);
$data = \str_replace('%2F', '/', $data);
return $data;

Getting started with elastica queries in Symfony

When integrating Elasticsearch with Symfony, there can be a few troubles for newcomers.

The easiest way to achieve integration is by following the setup instructions of the FOSElasticaBundle, in case you have a running Elasticsearch instance already at least. Symfony 3.1 or higher is required for this bundle. When following the setup instructions, there are a few caveats, which differed from what I expected. In the following post, I try to sketch some of the problems respectively solutions.

  • FOSElasticaBundle will not automatically recognize properties of an objectf, even when the serializer and the persistence model is defined. The properties you want to be mapped have to be listed in the configuration file.
  • To map relations such as ManyToOne, ManyToMany or OneToOne, the property gets a subkey “type” with value “nested”, after which the a new “properties” key is necessary followed by the properties of the related Entity
  • To create a development environment separat from the production index, simply set the index_name attribute of the relevant index to a environment-dependent value, such as “app_%kernel.environment%”

When you finally have the configuration set up, you have to run ./bin/console-dev fos:elastica:populate. This will show you errors in the configuration, if you have some, respectively sync your database with the elasticsearch instance.
The next step you will want to take is to use Elasticsearch to search, duh. This can get tricky at first if you are used to SQL search queries, especially as the relevant query classes are not yet as good documented as expected. The best way for me was to combine the knowledge of which classes exist with the elasticsearch documentation itself.

To translate from an SQL query to an Elastica-Query, keep the following in mind:

  • should means OR
  • must means AND
  • mustNot means XOR
  • to wrap these conditions together, use the Elastica\Query\BoolQuery
  • joins can be simulated with the Elastica\Query\Nested
  • to make comparsions with dates or numbers, use Elastica\Query\Range

With this knowledge, a query such as (...) WHERE (post.title LIKE %$search% OR creator.firstName LIKE %$search% OR creator.lastName LIKE %$search%) AND post.start < $date translates to (for example, in a custom search repository class, extends FOS\ElasticaBundle\Repository) :

setQuery($search)->setFields('firstName', 'lastName');
                $titleQuery = new Match('title', $search);
          $dateQuery = new Range('start', array('lt' => $date->format(ELASTICA_DATE_FORMAT)));

/** ... **/

This query can be executed like it is on the finder/repository yielding results. But. It will not work with a Paginator and a default number of 10 results will be returned instead. To successfully get the PaginatorAdabter, the whole BoolQuery has to be wrapped in an Elastica\Query Object.
This could be achieved e.g.:

$query = new Query();
// code from above

And to get the Pagination working:


Setting the default value of a Symfony FormType nested in CollectionType

There are a few proposed ways to hack a default value to form fields. The problen gets bigger when handling CollectionTypes, as the underlying objects don’t get constructed automatically. Workarounds are numerous, e.g. simply setting prototype_data on the CollectionType to an instantiated object. Unfortunately, for me, not one of these worked out when working with nested CollectionTypes.

The only way that worked out for me was adding a DataTransformer which checks in the reverseTransform function whether the object to transform is null, and if, set it to a new instance instead. The default values are than set in the constructor of the underlying object or in the reverseTransform function too, depending on your (performance) needs.

I sure hope this gets improved one day, but for now, this workaround is very comprehensible so I would not think about submitting an issue or pull request or even ask why this is necessary. This way, I even have more control over the new objects.

Vaccination controversy introduced by pharma industry because they don't earn enough by vaccination

Well, at least I would like this kind of controversy/conspiracy as it would lead to the opposite effect of what the vaccination-conspiracy is leading to today. Also, this theory would be a lot more supportable by evidence, numbers and logic than the dangerous conspiracy which is spread today.

I sincerly hope that one day, people can relax a bit more over their opinions, find a common midway, depending on the issue, and stop braging with unconventional, illogical and inconsequent ideas.

Cortana vs. Siri #4: Verfügbarkeit

Lange hat es gedauert, doch nun scheint dieser Punkt auch an Cortana zu gehen: Seit nicht allzu langer Zeit ist einerseits Cortana auf Windows 10 Geräten auch in der Schweiz standardmässig verfügbar, ohne dass man gross tricksen muss, und andererseits Siri verfügbar auf Desktop-Geräten und Laptops, die mit dem neusten MacOS ausgerüstet sind. Aber den wharen Punkt gibt es, da insbesondere Cortana bald auch auf iOs- und Adroid-Geräten verfügbar, was von Siri umgekehrt wohl nie behauptet werden kann.

Use Apple Superdrive with Windows

The Apple Superdrive drive is easy to use on Windows. However, the appropriate driver must be installed for this.

Here’s how to do it: Download the Apple Boot Camp Support Software . The file is a zip file, a “zip-compressed folder”, as Windows calls it. Unpack the file and then open the unzipped folder. Go to subfolder / BootCamp / Drivers / Apple. There you will find a file called “AppleODDInstaller64.exe” (you may not see the extension “.exe”). Use this file to install the driver by double-clicking it and following the installation wizard. That’s it! The next time you attach the Apple SuperDrive drive, your Windows should be able to recognize and work with it.

Dynamic grid from breakpoints

I dynamically generate MediaQueries from Zurb Foundation breakpoints for a grid with the following Sass code:

$i: 2;
@each $s,
$breakpoint in $breakpoints {
    @media (min-width: $breakpoint) {
        div.grid {
            .element {
                width: calc(100%/#{$i});
    $i: $i+1;

In my case, it generates the media queries for all breakpoints defined for the Zurb Foundation Framework in the Sass array $breakpoints. For each breakpoint, it increases the number of elements displayed in a row of the grid by adjusting its width, starting with two elements for the smallest screen width. Of course, for the .element, the display property must be set to inline-block.