Is there a way to assign names to entity states?

I have a 3rd gen august lock on my door. I have it connected via Z-Wave to HA and it works fine. One of the sensors (that actually reports something) is the “Access Control” sensor, it gives a 0 - 11 value that indicates what the last status change was caused by (as well as some other status related stuff). I was wondering if there was a way to tag those numbers with what they mean early on in the process. For example in the History tab it will show a bar of the days activities with the various status changes, but they are just numbers, and you have to refer to a table to figure out what was going on. Also on cards it just shows the number. I know I can make up cards that will do what I want, but it would be simpler and solve both problems if this could be addressed before the data is passed to the card and logged to the history chart. I tried searching for answers to this, but everything was relating to binary sensors and do not apply as far as I can tell.
Thank you!

You can make a template sensor interpret the numbers so
0 - Daffy
1 - Goofy
2 - Bugs
3 - Donald
4 - Elmer Fudd
Etc.
Then use that sensor in your cards

1 Like

Your voices are to create a Template Sensor (whose template maps the numbers to their associated text) or, if you have the requisite skills, modify the integration’s python code.

EDIT
Ninja’d by Mutt.

1 Like

I ended up with this for my August lock:

sensor:
  - platform: template
    sensors:
      august_lock_state:
        friendly_name: "August Lock State"
        value_template: >-
          {% if is_state('sensor.august_asl_03_smart_lock_access_control', '0') %}
            Mystery 0
          {% elif is_state('sensor.august_asl_03_smart_lock_access_control', '1') %}
            Manually Locked
          {% elif is_state('sensor.august_asl_03_smart_lock_access_control', '2') %}
            Manually Unlocked
          {% elif is_state('sensor.august_asl_03_smart_lock_access_control', '3') %}
            Z-Wave Locked
          {% elif is_state('sensor.august_asl_03_smart_lock_access_control', '4') %}
            Z-Wave Unlocked
          {% elif is_state('sensor.august_asl_03_smart_lock_access_control', '5') %}
            Keypad Locked
          {% elif is_state('sensor.august_asl_03_smart_lock_access_control', '6') %}
            Keypad Unlocked
          {% elif is_state('sensor.august_asl_03_smart_lock_access_control', '7') %}
            App Locked
          {% elif is_state('sensor.august_asl_03_smart_lock_access_control', '8') %}
            App Unlocked
          {% elif is_state('sensor.august_asl_03_smart_lock_access_control', '9') %}
            Auto Locked
          {% elif is_state('sensor.august_asl_03_smart_lock_access_control', '11') %}
            Lock Jammed
          {% else %}
            Unknown State
          {% endif %}

Thanks for your help!

1 Like

On long lists like this I’ve also used arrays, which, while completely unnecessary, can clean things up a bit:

{% set val = states('sensor.august_asl_03_smart_lock_access_control') %}
{% set lock_array = {0:'Mystery 0',1:'Manually Locked',2:'Manually Unlocked',3:'Z-Wave Locked',4:'Z-Wave Unlocked',5:'Keypad Locked',6:'Keypad Unlocked',7:'App Locked',8:'App Unlocked',9:'Auto Locked',11:'Lock Jammed'} %}
{% if val >= 0 and val <= 9 %}
  {% set output = lock_array[val] %}
 {% else %}
  {% set output = 'Unknown State' %}
{% endif %}
{{ output }}

Believe you can also build the array on multiple lines, fwiw:

{% set lock_array = {
0:'Mystery 0',
1:'Manually Locked',
2:'Manually Unlocked',
3:'Z-Wave Locked',
4:'Z-Wave Unlocked',
5:'Keypad Locked',
6:'Keypad Unlocked',
7:'App Locked',
8:'App Unlocked',
9:'Auto Locked',
11:'Lock Jammed'} %}
1 Like

Markus99’s suggestion is the preferred approach in this situation (as opposed to using a lengthy if-else chain). It can also be streamlined further, like this:

sensor:
  - platform: template
    sensors:
      august_lock_state:
        friendly_name: "August Lock State"
        value_template: >-
          {% set value = states('sensor.august_asl_03_smart_lock_access_control') | int %}
          {% set values = { 0:'Mystery 0', 1:'Manually Locked', 2:'Manually Unlocked', 3:'Z-Wave Locked', 
                            4:'Z-Wave Unlocked', 5:'Keypad Locked', 6:'Keypad Unlocked', 7:'App Locked', 
                            8:'App Unlocked', 9:'Auto Locked', 11:'Lock Jammed' } %}
          {{ values[value] if value in values.keys() else 'Unknown State' }}
2 Likes

I ended up with this (I wanted to change the icon as well):

  - platform: template
    sensors:
      august_lock_state:
        friendly_name: "August Lock State"
        value_template: >-
          {% set value = states('sensor.august_asl_03_smart_lock_access_control') | int %}
          {% set values = { 0:'Mystery 0', 1:'Manually Locked', 2:'Manually Unlocked', 3:'Z-Wave Locked', 
                            4:'Z-Wave Unlocked', 5:'Keypad Locked', 6:'Keypad Unlocked', 7:'App Locked', 
                            8:'App Unlocked', 9:'Auto Locked', 11:'Lock Jammed' } %}
          {{ values[value] if value in values.keys() else 'Unknown State' }}
        icon_template: >-
          {% set icon_value = states('sensor.august_asl_03_smart_lock_access_control') | int %}
          {% set icon_values = { 1:'mdi:lock-outline', 2:'mdi:lock-open-outline', 3:'mdi:lock-check', 
                            4:'mdi:lock-open-check', 5:'mdi:login', 6:'mdi:logout', 7:'mdi:lock', 
                            8:'mdi:lock-open', 9:'mdi:lock-clock', 11:'mdi:lock-alert' } %}
          {{ icon_values[icon_value] if icon_value in icon_values.keys() else 'mdi:lock-question' }}

Thank you for the tips guys! I have experiance with Java and C++, but YAML is taking me a while to figure out the “Grammar”.

1 Like

One side question, do variables exist only in the domain they are set? (ie: is “value” global or does it only exist under value_template?) Could I define it further up and re-use it for both the icon and value templates?

Believe they exist only w/in the _template where they’re defined.

2 Likes

No, it’s not a belief, it’s a fact.
Though of course only true at this moment in time in the current development of HA

:rofl:

Markus, this is not a dig at you, but a reference to the ‘future of yaml’ thread

1 Like

Thank you for marking my post with the Solution tag but I believe you should mark Markus99’s post (instead of mine) because it was first to present a functional solution. My example simply reduces the template’s size.