Change state from number to string

Hi guys!

I have a broadlink device which show me the value of the light sensor, which have only 4 states (0, 1, 2, 3). I configured a card in lovelace, but I want to see something else, not numbers. I want to see “Dark” is is 0, “Normal” if is 1, “Bright” if is 2, “Extremely Bright” if is 3. How can I do this? I don’t understand exactly, i’m very new to this Home Assistant world… So take me easy

Thank you in advance

Take a look at sensor templates

Please explain to me, i’m very new to this…

Try this (change the sensor to the one you have that gives the 0 to 3 reading):

sensor:
  - platform: template
    sensors:
      light_level:
        friendly_name: "Light Level"
        value_template: >
          {{ ['Dark', 'Normal', 'Bright', 'Extremely Bright'][states('sensor.your_light_sensor_here')] }}

It is not showing me any state, for this new sensor …

Try this:

sensor:
  - platform: template
    sensors:
      light_level:
        friendly_name: "Light Level"
        value_template: >
          {{ ['Dark', 'Normal', 'Bright', 'Extremely Bright'][states('sensor.your_light_sensor_here')|int] }}
1 Like

You are the man !!!

I was trying to make this possible with conditions, like if 3, then “Very Bright”, but your approaching with array it is far better… Thank you so much

No problem. Do you need an explanation for how it works?

No, it is clear… using an array, it is from first to last, like 0 to 3.

1 Like

Hi Tom, can you please help me with something similar?

I want to make a custom switch to toggle alarm panel to armed/disarmed, and I want the switch to have the real state, in on/off only

I have done something like this, but it is not working…

    alarm_toggle:
      value_template: >-
        {{ ['off', 'on', 'on']['disarmed', 'arming', 'armed_away'] }}
      turn_on:
        service: script.alarm_arm
      turn_off:
        service: script.alarm_disarm

Thank you in advance

I think I managed to do it like this

      value_template: >-
        {% if is_state('alarm_control_panel.ha_alarm', 'arming') or is_state('alarm_control_panel.ha_alarm', 'armed_away') %}
        on
        {% elif is_state('alarm_control_panel.ha_alarm', 'disarmed') %}
        off
        {% endif %}
      turn_on:
        service: script.alarm_arm
      turn_off:
        service: script.alarm_disarm

But I don’t know if this is the proper solution… If you have another, simplier alternative, please tell me. Thank you!