If...then in Template not working

Hi, I’ve written this template:

{% if sensor.tronity_q4_sportback_e_tron_plugged == true %}
ja
{% else %}
nein
{% endif %}

It worked for a short time, then it didn’t work any more (the red ‘ja’ was a first try, then I did change it to ja:

Can someone tell me, what I’m doing wrong? I’m not a coder but I’m trying to learn …

Regards,
Jogi

Try

{% if is_state('sensor.tronity_q4_sportback_e_tron_plugged','on') %}
ja
{% else %}
nein
{% endif %}

It is a bit of a guess that this is a binary_sensor, which is either on or off. When writing templates, it is always best to go to developer tools, states and see what the state really is. In the user interface you only see translations.

You can also use developer tools to check your template.

What are you trying to do? Your if logic is fine, but the condition you are testing is not. You cannot access the sensor like that.

If it is a binary_sensor:

{{ 'ja' if is_state('binary_sensor.tronity_q4_sportback_e_tron_plugged','on') else 'nein' }}

@JogiL — you need to read this:

Absolutely this. If you’re trying to learn templates, start with Developer Tools / Templates, not building sensors.

Thank you to both of you!

I’ve already prepared some templates and I’ve now prepared binary sensors for those two states as suggested by you:

{{ 'ja' if is_state('sensor.steckerzustand','plugged') else 'nein' }}

I did substitute ‘true’ by ‘plugged’, because this is one of the attributes of the source entity. Is this correct or should I use the German translation of it (“Ausgesteckt”?

image

Nethertheless it isn’t working yet. Currently Zoe is plugged, but the helper only says “Aus” (shouldn’t it be “nein” at least?):

Check your sensor’s real state in Developer Tools / States. Here’s one of mine reporting “Waiting for schedule”:

image

but the actual state that must be used in templates is waiting-for-schedule:

image

If it is a template binary sensor that you are creating, the state will be either 'on' or 'off' (always lower-case, always in English) — or potentially 'unknown' / 'unavailable' if there are problems.

Your binary sensor state template can return 'on' / 'off', true / false or a couple of other options:

so for your application, you can use:

{{ 'on' if is_state('sensor.steckerzustand','plugged') else 'off' }}

or simply:

{{ is_state('sensor.steckerzustand','plugged') }}

When you give that binary sensor a device class of “plug”, the front end will translate the underlying state of 'on' or 'off' into your language’s version of “Plugged in” or “Unplugged”.

image

Ah, OK, now I understand! Then “ja” und “nein” in {{ ‘ja’ if is_state(‘sensor.steckerzustand’,‘true’) else ‘nein’ }} won’t work in a binary sensor. If I want to have “ja” und “nein” as a result, I have to use a non-binary sensor.

Thank you!

1 Like

Now I did get the binary sensor working for the plugged state with that template:

{{ 'on' if is_state('sensor.tronity_q4_sportback_e_tron_plugged', 'True') else 'off' }}

The source entity has two attributes (false and true):

image

I’ve tried the same for the charging status with

{{ 'on' if is_state('sensor.tronity.Q4 Sportback e-tron.charging','Charging') else 'off' }}

but this doesn’t work. The source entity has no attributes:

image

Could that be the reason? How can I solve that?

'sensor.tronity.Q4 Sportback e-tron.charging' is not a valid entity_id. You need to click the cog button and copy the entity_id from within that window.

Thank you, but what is the cog button?

Do you mean that one?
image

The ID seems to be correct:

image

This is the id:
afbeelding

Got it, thank you!