Templates misbehaving in 2023.5

I have such template and after update it return always “Unknown”, num sensor is showing correct value (1 or 0).

sensor:
  - platform: template
    sensors:
      komfovent_on_off:
        friendly_name: "Komfovent On Off"
        unit_of_measurement: "State"
        value_template: >-
          {% set mapper =  {
              '0' : 'Off',
              '1' : 'On'} %}
          {% set state =  states('sensor.komfovent_on_off_num') %}
          {{ mapper[state] if state in mapper else 'Unknown' }}

Replace all of that with this in your configuration.yaml file:

template:
  - sensor:
      - name: "Komfovent"
        device_class: # optional pick one from here https://www.home-assistant.io/integrations/binary_sensor/#device-class
        state: "{{ states('sensor.komfovent_on_off_num')|bool }}"
        availability: "{{ states('sensor.komfovent_on_off_num')|is_number }}"

This will create a binary sensor. Which is what on/off sensors should be.

What about such template, where there are various states, not only 1 or 0 ?

      komfovent_operation_mode:
        friendly_name: "Komfovent Operation Mode"
        unit_of_measurement: "State"
        value_template: >-
          {% set mapper =  {
              '0' : 'Standby',
              '1' : 'Away',
              '2' : 'Normal',
              '3' : 'Intensive',
              '4' : 'Boost',
              '5' : 'Kitchen',
              '6' : 'Fireplace',
              '7' : 'Overide',
              '8' : 'Holiday',
              '9' : 'Air Quality',
              '10' : 'Off' } %}
          {% set state =  states('sensor.komfovent_mode_num') %}
          {{ mapper[state] if state in mapper else 'Unknown' }}

There’s nothing in that template that would make it fail to load. I’d start looking at your source sensor to verify it still exists. 'sensor.komfovent_on_off_num'

It exists and shows state in numeric format. In previous ha core version everything was working perfectly.

There’s nothing that will make that template fail. Check your logs for errors. You don’t need to switch the template over from legacy to the ‘new’ style. It should work out of the box without fail. Something else is causing the entity to no longer load.

Looking at that specific template, the only way it would fail is if your source sensor was missing. And even then, it wouldn’t fail, it would just be unknown until it was found again.

It doesn’t fail, just it always states “unknown” state.

Then your source sensor most likely doesn’t exist… Please share a screenshot of the source sensor in developer tools → states page.

A screenshot of this entity 'sensor.komfovent_on_off_num'

i reverted back to previous ha core version right now, but i will update it once again tommorrow. Meanwhile configuration of that sensor itself looks like this:

  sensors:
    - name: "Komfovent on off num"
      address: 0
      input_type: holding
      data_type: uint16
      scan_interval: 5

That’s not what I asked for. I’m very confident that your entity was not being created and you were looking at the template being the problem when you should have been looking at the source. So when you update, please look at developer tools → states page for your sensor.komfovent_on_off_num sensor and verify it’s working.

1 Like

After the update, that sensor was showing numeric state as intended, but it seems that mapper didn’t map that next sensor based on numeric state, but i will double check that again.

Sorry, but a template wouldn’t just fail like that. I know I keep hammering on this, but did you look at the sensor in developer tools → states page? Or did you look at it elsewhere? If the entity_id changed, that template wouldn’t know about it. The entire UI hides the entity_id behind the friendly name except for the developer tools → states page. So that’s where you need to look.

1 Like

Like that but use an availability template. IF you need help with it start a new post.

Here are the screenshots from devtools → states page:

Please show a screenshot with the entit_id. That’s still showing the friendly name, not the entity_id. Just take a screenshot of the entity and it’s states and attributes in the table, not in the set state area.

is this want you want ?

1 Like

Please paste {{ states('sensor.komfovent_on_off_num') }} in developer tools → states.

Remove that. You’ll probably find a related message in your logs.

Note that your sensor state is "unknown", not the "Unknown" fallback value from your template.

As another hint, it’s a bit dangerous using a reserved word like state for a variable in your template. It seems to work for me in the template editor, but can be a bit risky.

Please also paste your template into the template editor and see if it returns what you’d expect. Like this, which returns Unknown for me because I don’t have your sensor:

My guess is it’ll work there, but it’s failing as a sensor because of your unit_of_measurement in the config.

Then turn that one into a binary sensor as recommended.

state should be good, states is the risky override.

Perfect. Now works. Appreciate the help.