How to show sensor as a custom state?

I created a sensor that shows an integer as state, fetched from a web api.
Now I’d like to give those integers a meaningful name in the ui, or even for use in automations. Is that possible?

sensor:
  - name: Door State
    platform: rest
    resource: 'apicall'
    value_template: '{{ value_json.state}}'
    scan_interval: 60

I’d like to set:
1 = locked
2 = unlocked
3 = open

Because currently the sensor os shown as “1” in the HA overview, and in a few weeks I will have forgotten which those numbers stand for. Or even in automations: when I check sensor.state = 1, this won’t help be when modifying the automation in a few weeks. But sensor.state = 'locked' would help.

sensor:
  - name: Door State
    platform: rest
    resource: 'apicall'
    value_template:>
     {% if  value_json.state == '1' %}
       Locked
     {% elif  value_json.state == '2' %}
       Unlocked
     {% elif  value_json.state == '3' %}
       Open
     {% else %}
       Unknown
     {% endif %}

You might have to delete the quotes round the numbers. Not sure.

1 Like

Did you use it with numbers quoted or without?

I used without quotes, as my webservice response contains an integer. I think quotes may be needed if a string is returned. But as well it might automatically work due to autoboxing.

1 Like