Change state (number / int) to text (string)

Hi

This makes me crazy - can anyone help me understand what i am doing wrong?

I have a heatpump giving me a operation status of 1 - 7, 98, 99 and 100.
I want those to be human readable.
So, whit a litlte help i wrote:

modbus:
  - type: tcp
    name: Thermia
    host: nnn.nnn.nnn.133
    port: 502
    sensors:
      - name: wp_Operation_state
        scan_interval: 10

And

- sensor:
      - name: wp_operationmode_tekst
        unit_of_measurement: state
        state: >-
          {%- if is_state('sensor.wp_Operation_state', '0') %}
          'None'
          {%else%}
          {%- if is_state('sensor.wp_Operation_state', '1') %}
          'Manual operation'
            {%- elif is_state('sensor.wp_Operation_state', '2') %}
            'Defrost'
            {%- elif is_state('sensor.wp_Operation_state', '3') %}
            'Hot water'
            {%- elif is_state('sensor.wp_Operation_state', '4') %}
            'Heat'
            {%- elif is_state('sensor.wp_Operation_state', '5') %}
            'Cool'
            {%- elif is_state('sensor.wp_Operation_state', '6') %}
            'Pool'
            {%- elif is_state('sensor.wp_Operation_state', '7') %}
            'Anti legionella'
            {%- elif is_state('sensor.wp_Operation_state', '98') %}
            'Standby'
            {%- elif is_state('sensor.wp_Operation_state', '99') %}
            No demand
            {%- elif is_state('sensor.wp_Operation_state', '100') %}
            'Off'
          {% else %}
          Ukendt
          {%- endif %}
          {%endif%}

When using “Templates” in Developer tools, it seems to work. But when inserting it to configuration.yaml it gives state “Unknown”.

Can anyone help me not loosing my mind?

Best regards
Thomas

Has the state of sensor.wp_Operation_state changed since you created the text sensor?

yes:

What happens, if you comment out the line unit_of_measurement: state and reload the template?

And could you test this:


        state: |
          {% set states = states('sensor.wp_Operation_state') |int %}
          {% set text = {
            0: 'Null',
            1: 'Manual operation',
            2: 'Defrost',
            3: 'Hot water',
            4: 'Heat',
            5: 'Cool',
            6: 'Pool',
            7: 'Anti legionella',
            98: 'Standby',
            99: 'No demand',
            100: 'Off'} %}
          {{ text[states] if states in text.keys() else 'Ukent' }}

Remove this line from your sensor’s configuration:

        unit_of_measurement: state

That option is meant for a sensor that reports a numeric value. Your sensor reports a string value.