Getting human readable Labels for woirking states

Hi

I have just updated my Hom Assistant to 2023.7.3 and now struggle to get my operation mode labels for my heater to work again.

I came from a version of Dec 2022/Jan 2023 and had following sensor definition to get a Human readable labels to my heater operation mode:

sensor:
  - platform: template
    sensors:
      heizung_betriebsart_name:
        friendly_name: "Heizung Betriebsart Name"
        value_template: >-
          {% set mapper =  {
          '1' : 'Standby',
          '3' : 'Normal',
          '4' : 'Absenk',
          '5' : 'Sommer',
          '11' : 'Programm 1',
          '12' : 'Programm 2',
          '13' : 'Programm 3',
          '255' : 'Wie Leitstelle'
          } %}
          {% set state =  states('sensor.heizung_betriebsart') %}
          {{ mapper[state] if state in mapper else 'Unknown' }}

The integer value sensor.heizungbetriebsart comes via MQTT from a nodered shim which handles reading and writing to my Weishaupt Heater Setup.

This does now not longer work and in investigation this I tried to move this to the following select template

template:
  - select:
      - name: "Heizung Betriebsart Name"
        unique_id: heizung_betriebsart_name
        state: >
          {% set opt = states('sensor.heizung_betriebsart') %}
          {% set mapper =  {
          1 : 'Standby',
          3 : 'Normal',
          4 : 'Absenk',
          5 : 'Sommer',
          11 : 'Programm 1',
          12 : 'Programm 2',
          13 : 'Programm 3',
          255 : 'Wie Leitstelle'
          } %}
          {{ mapper.get(opt) }}
        options: "Standby,Normal,Absenk,Sommer,Programm 1,Programm 2,Programm 3,Wie Leitstelle,Unknown"
#        options: "{{['Standby','Normal',Absenk','Sommer','Programm 1','Programm 2',Programm 3',Wie Leitstelle','Unknown']}}"
#        options: "[Standby,Normal,Absenk,Sommer,Programm 1,Programm 2,Programm 3,Wie Leitstelle,Unknown]"
#        options: ["Standby","Normal","Absenk","Sommer","Programm 1","Programm 2","Programm 3","Wie Leitstelle"]
#        options:
#          - Standby
#          - Normal
#          - Absenk
#          - Sommer
#          - Programm 1
#          - Programm 2
#          - Programm 3
#          - Wie Leitstelle
        select_option:
          - service: mqtt.publish
            data:
              topic: home/weishaupt/Betriebsart/set
              payload: >
                {% set mapper =  {
                'Standby' : 1,
                'Normal' : 3 ,
                'Absenk' : 4,
                'Sommer' : 5,
                'Programm 1' : 11,
                'Programm 2' : 12,
                'Programm 3' : 13,
                } %}
                {{ mapper[option] if state in mapper else 'Unknown' }}                
              qos: "1"  

But this does not render the state correctly, I get the following error message:

home-assistant   | 2023-07-31 08:48:43.580 ERROR (MainThread) [homeassistant.helpers.template_entity] Error validating template result 'None' from template 'Template<template=({% set opt = states('sensor.heizung_betriebsart') %} {% set mapper =  { 1 : 'Standby', 3 : 'Normal', 4 : 'Absenk', 5 : 'Sommer', 11 : 'Programm 1', 12 : 'Programm 2', 13 : 'Programm 3', 255 : 'Wie Leitstelle' } %} {{ mapper.get(opt) }}) renders=4>' for attribute '_attr_current_option' in entity select.heizung_betriebsart_name validation message 'string value is None'  

My assuption is that the list of options is not correct, you see the different attempts to define them commented out in the snippet above.

Could someone shed some light on why this is not working?

cheerio
Steve

         {% set opt = states('sensor.heizung_betriebsart')|int(1) %}

State values are strings.

Set the default value you want in the brackets for when the sensor is undefined.

There doesn’t appear to be anything wrong with your original Template Sensor so what exactly does not work?

Ah, OK. I had also tried the map with the integers as string values but this doesn’t worked either.

Not I tried this, there is no error in the logs any more, but the value of heizung_betriebsart_name stays ‘unknown’

Copy-paste the following template into the Template Editor and let us know what it reports.

{{ states('sensor.heizung_betriebsart') }}

Good question, the error messages are lost from the restarts and now after re-adding this to my config, there is no error anymore…

It states Result type: number: 5

Copy-paste the following template into the Template Editor and confirm it reports Sommer (assuming the value of sensor.heizung_betriebsart is still 5).

{{ {1: 'Standby',
    3: 'Normal',
    4: 'Absenk',
    5: 'Sommer',
    11: 'Programm 1',
    12: 'Programm 2',
    13: 'Programm 3',
    255: 'Wie Leitstelle'}.get(states('sensor.heizung_betriebsart')|int(0), 'unknown') }}

If the template reports the correct name, you can use it in your Template Sensor.

sensor:
  - platform: template
    sensors:
      heizung_betriebsart_name:
        friendly_name: "Heizung Betriebsart Name"
        value_template: >-
          {{ {1: 'Standby',
              3: 'Normal',
              4: 'Absenk',
              5: 'Sommer',
              11: 'Programm 1',
              12: 'Programm 2',
              13: 'Programm 3',
              255: 'Wie Leitstelle'}.get(states('sensor.heizung_betriebsart')|int(0), 'unknown') }}
2 Likes

Many thanks @123,

it works and TIL to use the template Editor to check for templates in my config.

cheeiro
Steve

1 Like