Sensor value renaming

I have an Envisalink on my DSC alarm integrated to HA. From the attributes I made 2 sensors: Last armed by user and Last disarmed by user.
These give numbers as values (the user number on Envisalink). Can I change somehow my code to get defined names instead of the numbers as a value? I mean somehow define in the template sensor that:
1 means John
2 is Marie
3 is Jack
etc.

My current code which gives the numbers is:

  - platform: template
    sensors:
      riaszto_disarmed_by:
        value_template: ‘{{ states.sensor.riaszto_keypad.attributes.last_disarmed_by_user }}’
      riaszto_armed_by:
        value_template: ‘{{ states.sensor.riaszto_keypad.attributes.last_armed_by_user }}’

Something like this:

  - platform: template
    sensors:
      riaszto_disarmed_by:
        value_template: >-
                        {% set names = {
                                        0: "N/A",
                                        1: "John",
                                        2: "Marie",
                                        3: "Jack"
                         } %}

                        {{ names[states.sensor.riaszto_keypad.attributes.last_disarmed_by_user] }}

And the same for armed.

Thanks a lot! I’ll check it. I have an issue to solve. After a while the keypad sensor becomes cleared even nobody pressed anything on the Keypad, and it clears the attribute too:
riaszto2

riaszto1

I must say I feel rather sceptical about this, but the template tool says it works.

Also in the image there seems to be some ' around the values. I assume you need to add them to the list.

Yes it would be OK. But somehow I’d like to show the last acting user whiich was identified instead of the cleared value. So something like the last history event which had defined user. E.g. if John disarmed 6 hours ago and the sensor went to “” 3 hours ago, then show John instead of “”.

You could send the initial result to a helper string - and ensure that it doesn’t change in the case of “”

 - platform: template
    sensors:
      riaszto_disarmed_by:
        value_template: >-
                        {% set names = {
                                        0: "N/A",
                                        1: "John",
                                        2: "Marie",
                                        3: "Jack"
                         } %}
                        {% if is_state_attr("sensor.riaszto_keypad", "last_disarmed_by_user", "''") -%}
                             {{ states("sensor.riaszto_disarmed_by") }}
                        {%- else -%}
                             {{ names[states.sensor.riaszto_keypad.attributes.last_disarmed_by_user] }}
                        {%- endif %}

Perhaps…

There is a syntactical issue, but I can’t find :frowning:

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected '}', expected ')') for dictionary value @ data['sensors']['riaszto_disarmed_by']['value_template']. Got '{% set names = {\n 0: "N/A",\n 1: "John",\n 2: "Marie",\n 3: "Jack"\n } %}\n{% if is_state_attr("sensor.riaszto_keypad", "last_disarmed_by_user", "\'\'") -%}\n {{ states("sensor.riaszto_disarmed_by" }}\n{%- else -%}\n {{ names[states.sensor.riaszto_keypad.attributes.last_disarmed_by_user] }}\n{%- endif %}'. (See ?, line ?).

OK, I found :slight_smile:
Here is the syntactical corrected:

        value_template: >-
                        {% set names = {
                                        0: "N/A",
                                        1: "John",
                                        2: "Marie",
                                        3: "Jack"
                         } %}
                        {% if is_state_attr("sensor.riaszto_keypad", "last_disarmed_by_user", "''") -%}
                             {{ states("sensor.riaszto_disarmed_by") }}
                        {%- else -%}
                             {{ names[states.sensor.riaszto_keypad.attributes.last_disarmed_by_user] }}
                        {%- endif %}

So, after a day, it works perfectly! Thanks a lot again!
My new issue to somehow show only that sensor state which is the latest. E.g. it was disarmed 9 hours ago and then armed 4 hors ago, than show only the Armed_by sensor value but hide the Disarmed sensor.
Or armed 3 hours ago but disarmed 1 hour ago, than show only the Disarmed sensor state.

You can probably set up a new sensor that has a template like this:

{% if as_timestamp(sensordisarm) - as_timestamp(sensorarmed) >= 0  %}
  latest action was disarmed, so insert your newly created disarmed sensor here.
{%- else -%}
  latest action was armed, so insert your newly created armed sensor here.
{%- endif %}

Unfortunately the original solution doesn’t work well. After a HA restert the sensor shows empty value. In the history the last known is shown. Somehow the last not empty history state would be the solution,

riaszto3

That is what sensors do. They update when HA reboots.
If you want it to persist across a reboot then you need to save it somewhere and bring it back somehow after the reboot.