Is_state not matching in sensor template


I want to translate a sensor value. Here’s what I’ve done so far:

  - platform: template
        friendly_name_template: "{{ state_attr('sensor.rollladen_status_bad', 'friendly_name') }}"
        value_template: >-
          {% if is_state('sensor.rollladen_status_bad', 'Up') %}
          {% elif is_state('sensor.rollladen_status_bad', 'Down') %}
          {% elif is_state('sensor.rollladen_status_bad', 'Locked') %}
            Sperre ist aktiv
          {% elif is_state('sensor.rollladen_status_bad', 'Alarm End') %}
            Alarm beendet
[... removed additional similar translations...]
          {% else %}
            unbekannt: {{ states('sensor.rollladen_status_bad') }}
          {% endif %}
        icon_template: mdi:information-outline

It seems that my if/elif cases never match, as it always returns the value in the else statement (unbekannt: xxx).

Developer tools shows this:

I already checked if there are any invisible characters/whitespaces but could not find any in the state of this sensor.

I’ve tried the code in developer tools template debugger, but it has the same behaviour there.

I also tried it like that:

          {% set state = states('sensor.rollladen_status_gaste_bad_fenster') %}
          {% if state == 'Up' %}
          {% elif is_state('sensor.rollladen_status_gaste_bad_fenster','Locked') %}
            Sperre ist aktiv
          {% elif is_state('sensor.rollladen_status_gaste_bad_fenster', 'Alarm End') %}
            Alarm beendet
          {% elif is_state('sensor.rollladen_status_gaste_bad_fenster', 'Wind Alarm') %}
            Windalarm wurde ausgelöst
          {% else %}
            unbekannt: {{ state }}
          {% endif %}

By the way, is the latter form the ‘better’ solution because it only fetches the state a single time?

best regards


{{ states('sensor.rolladen_xx') | length }}

give you the correct string length?

interesting, it says string length is 14 for ‘Alarm End’:

Is there a way to see and/or strip those additional characters? I can’t see them in the developer tools.

trim filter should do the trick. So do this:

{% set state = states('sensor.rollladen_status_gaste_bad_fenster') | trim %}

And then compare the value of state in each of those ifs

1 Like

You can see if the string contains the word(s)

          {% set state = states('sensor.rollladen_status_gaste_bad_fenster') %}
          {% if 'Up' in state %}

And so on…

1 Like

thank you all. I got it working with the trim method @CentralCommand mentioned. The string is coming from my KNX bus where it is a 14 * 8 byte string in hex. It seems that every byte needs to be set to something so the string is filled up with blanks.

here’s the template using a mapper, which makes it better readable, and easy to add conditions and translations:

{% set state = states('sensor.rollladen_status_gaste_bad_fenster')|trim %}
{% set mapper = {'Up':'Auffahrt',
                 'Locked':'Sperre ist aktiv',
                 'Alarm End':'Alarm beendet',
                 'Wind Alarm':'Windalarm wurde ausgelöst'} %}
{{mapper[state] if state in mapper else 'unbekannt: ' ~ state}}
1 Like

wow, thx @Mariusthvdb :slight_smile: that’s very useful!

Is it possible to have the ‘mapper’ globally available? Would make it easier for my other covers.

not sure what you mean by ‘globally’?
do you wish to create other translations using other base sensors, and use the same framework? Don’t think there is a way out of the core HA box, other than creating several of these template sensors.

yes, I’ve multiple sensors for my other covers, all in need of the same translation. I agree to that, since it’s Jinja foo.

I see, and I know what you mean in that case… I have tried very long to use a ‘global’ setup for all of my motion sensors (20+) which use a friendly_name template:

        value_template: >
        friendly_name_template: >
          Garden backyard buiten:
          {% set light_level = state_attr('sensor.garden_backyard_buiten_sensor_light_level','lightlevel')|int %}
          {% if light_level < 1 %} dark
          {% elif light_level < 3000 %} bright moonlight
          {% elif light_level < 10000 %} night light
          {% elif light_level < 17000 %} dimmed light
          {% elif light_level < 22000 %} 'cosy' living room
          {% elif light_level < 25500 %} 'normal' non-task light
          {% elif light_level < 28500 %} working / reading
          {% elif light_level < 33000 %} inside daylight
          {% elif light_level < 40000 %} maximum to avoid glare
          {% elif light_level < 51000 %} clear daylight
          {% else %} direct sunlight
          {% endif %}

not possible, until we have a variable option in the template sensors, or some other way we can create these on the fly. Even considered Yaml anchors, but that wont help in this case either. Or maybe a Python script of sorts.

Long story short: I have 1 package with all motion sensors and their friendly names :wink:

Or, you have to be prepared to use custom_ui and customize the state. But that is against my own default advise to people using custom_ui and only for the adventurous.

in which case you can do something like this, and _glob it for all sensors you throw at it:


        friendly_name: >
          function capitalizeFirstLetter(string) {
              return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
          var id = entity.entity_id.split('.')[1].split('_sensor_light_level_raw')[0].replace(/_/g,' ');
          var id = capitalizeFirstLetter(id);
          if (state < 1) return id + ': dark';
          if (state < 3000 ) return id + ': bright moonlight';
          if (state < 10000) return id + ': night light';
          if (state < 17000) return id + ': dimmed light';
          if (state < 22000) return id + ': `cosy` living room';
          if (state < 25500 ) return id + ': `normal` non-task light';
          if (state < 28500) return id + ': working / reading';
          if (state < 33000) return id + ': inside daylight';
          if (state < 40000) return id + ': maximum to avoid glare';
          if (state < 51000) return id + ': clear daylight';
          return id + ': direct sunlight';


If you’d be willing to consider using these translations only in the Frontend, you might want to have a look at a template-entity-row and create a template for the sensor (above).

You could then use that in an auto-entities card, in which you list all of your covers.

1 Like

Can’t you use a rest sensor with the values as json?


{% sensor.json_rest_sensor.json. {{value }} %}