Translate numeric states to human readable values question

Hi all !

I try to convert numeric states my stove sends to human readable values.

What I’m trying to convert is the “device_status” this entity reports:

I’ve googled a bit and I’m trying to adapt this :

So I went to dev tools > templates, and here is what I entered in there :

{% set t = states('sensor.device_status') | float(-99) %}
{% set values =
    { 0: 'Insert éteint', 1: 'Démarrage', 2: 'Préchauffe',
      3: 'Chargement pellets pour allumage', 4: 'Allumage de la bougie', 5: 'Allumage initial (8 minutes)',
      6: 'Finallisation allumage', 7: 'Chauffage en cours', 8: 'Demande extinction',
      9: 'Netoyage final (8 minutes)', 10: 'Attente de Standby',
      12: 'Alerte: Plus de pellet', 13: 'Alerte: Nettoyer le brasier' } %}
{{ values.get(values.keys() | select('<', t) | first, 'Unknown') }}

Problem is, I cannot see anything properly read in the right pane, any clue ?

Your template assigns the value of your sensor to t, then looks up the first item in the dictionary that has a key less than t. This will return Insert éteint if t is positive, as 0 is the first key; if t is negative it will fail as the select list will be empty. This is working as expected:

image

However, your device_status in the screenshot above is an attribute, not a sensor (unless you’ve already created a template sensor.device_status that reads the attribute), and it looks like you want to match exact values. Try this, with a list instead of a dictionary:

{% set t = state_attr('YOUR_ENTITY','device_status')|int(0) %}
{% set values =
    ['Insert éteint', 'Démarrage', 'Préchauffe',
     'Chargement pellets pour allumage', 'Allumage de la bougie', 'Allumage initial (8 minutes)',
     'Finallisation allumage', 'Chauffage en cours', 'Demande extinction',
     'Nettoyage final (8 minutes)', 'Attente de Standby',
     'Alerte: Plus de pellet', 'Alerte: Nettoyer le brasier'] %}
{{ values[t] }}

You’ll need to replace YOUR_ENTITY with the entity ID for Insert montefiore: at a guess, 'climate.insert_montefiore' — or if sensor.device_status exists and is working, use:

{% set t = states('sensor.device_status')|int(0) %}

instead.

Hi there,

I indeed did create a template sensor I’m already using to read the states, and send notifications for specific events.

Your approach seems great though, as it will start from 0 state, and will then ‘translate’ each state starting from that point, incrementing from 1 at each comma. I’m trying this now ! and will obviously keep you posted. :slight_smile:

EDIT :

And it works ! Thanks, I’ve learned something today !

Now as I am still quite new to all that, how do I need to add this to my dashboard ?
Idea is to simply have a card showing the current state as it is reported by the template.

I guess I need to add this somehow to my configuration.yaml file first, then how do I do for the card ?

FYI, these are all the templates I already added in my conf file :

sensor:
  - platform: template
    sensors:
      current_temperature:
        unique_id: "current_temperature"
        friendly_name: "Temperature actuelle insert"
        unit_of_measurement: "°C"
        value_template: "{{ state_attr('climate.insert_montefiore', 'current_temperature')}}"
      temperature:
        unique_id: "temperature"
        friendly_name: "Temperature voulue insert"
        unit_of_measurement: "°C"
        value_template: "{{ state_attr('climate.insert_montefiore', 'temperature')}}"
      fan_mode:
        unique_id: "fan_mode"
        friendly_name: "Vitesse ventilateur insert"
        unit_of_measurement: "state"
        value_template: "{{ state_attr('climate.insert_montefiore', 'fan_mode')}}"
      hvac_action:
        unique_id: "stove_hvac_action"
        friendly_name: "Etat insert"
        unit_of_measurement: "state"
        value_template: "{{ state_attr('climate.insert_montefiore', 'hvac_action')}}"
      smoke_temperature:
        unique_id: "smoke_temperature"
        friendly_name: "Temperature Fumees insert"
        unit_of_measurement: "°C"
        value_template: "{{ state_attr('climate.insert_montefiore', 'smoke_temperature')}}"
      human_device_status:
        unique_id: "human_device_status"
        friendly_name: "Statut insert"
        value_template: "{{ state_attr('climate.insert_montefiore', 'human_device_status')}}"
      device_status:
        unique_id: "device_status"
        friendly_name: "Code statut insert"
        value_template: "{{ state_attr('climate.insert_montefiore', 'device_status')}}"
      hvac_activity:
        friendly_name: "Temps de fonctionnement nest"
        value_template: "{{ state_attr('climate.vestiaire_thermostat_nest', 'hvac_action') }}"

Top-right of your dashboard, click the three dots and “Edit dashboard”. Then at the bottom, click “+ ADD CARD”. Pick the card you want: probably Entities (if you want more than one) or Entity (for a card showing just that value). Select your new sensor and you’re done.

but I only played with this templace in the dev section. As the format is quite different from my other templates present in my conf.yaml, how am I suposed to add this to avoid yaml syntax issues ?

Thanks again Troon !!! :slight_smile:

Ah, I see. Like your others, but with the value_template like this:

        value_template: >-
           {% set t = states('sensor.device_status')|int(0) %}
           {% set values =
               ['Insert éteint', 'Démarrage', 'Préchauffe',
                'Chargement pellets pour allumage', 'Allumage de la bougie', 'Allumage initial (8 minutes)',
                'Finallisation allumage', 'Chauffage en cours', 'Demande extinction',
                'Nettoyage final (8 minutes)', 'Attente de Standby',
                'Alerte: Plus de pellet', 'Alerte: Nettoyer le brasier'] %}
           {{ values[t] }}

Looking at your others, there’s no need for unit_of_measurement: 'state'. You can remove those lines: the unit is optional and only useful for measurements that actually have a unit.

Also worth noting you have used the legacy template format here. It still works, but the recommendation is to use the modern format:

ok so here is my full code in configuration.yaml now

sensor:
  - platform: template
    sensors:
      current_temperature:
        unique_id: "current_temperature"
        friendly_name: "Temperature actuelle insert"
        unit_of_measurement: "°C"
        value_template: "{{ state_attr('climate.insert_montefiore', 'current_temperature')}}"
      temperature:
        unique_id: "temperature"
        friendly_name: "Temperature voulue insert"
        unit_of_measurement: "°C"
        value_template: "{{ state_attr('climate.insert_montefiore', 'temperature')}}"
      fan_mode:
        unique_id: "fan_mode"
        friendly_name: "Vitesse ventilateur insert"
        unit_of_measurement: "state"
        value_template: "{{ state_attr('climate.insert_montefiore', 'fan_mode')}}"
      hvac_action:
        unique_id: "stove_hvac_action"
        friendly_name: "Etat insert"
        unit_of_measurement: "state"
        value_template: "{{ state_attr('climate.insert_montefiore', 'hvac_action')}}"
      smoke_temperature:
        unique_id: "smoke_temperature"
        friendly_name: "Temperature Fumees insert"
        unit_of_measurement: "°C"
        value_template: "{{ state_attr('climate.insert_montefiore', 'smoke_temperature')}}"
      human_device_status:
        unique_id: "human_device_status"
        friendly_name: "Statut insert"
        value_template: "{{ state_attr('climate.insert_montefiore', 'human_device_status')}}"
      device_status:
        unique_id: "device_status"
        friendly_name: "Code statut insert"
        value_template: >-
           {% set t = states('sensor.device_status')|int(0) %}
           {% set values =
               ['Insert éteint', 'Démarrage', 'Préchauffe',
                'Chargement pellets pour allumage', 'Allumage de la bougie', 'Allumage initial (8 minutes)',
                'Finallisation allumage', 'Chauffage en cours', 'Demande extinction',
                'Nettoyage final (8 minutes)', 'Attente de Standby',
                'Alerte: Plus de pellet', 'Alerte: Nettoyer le brasier'] %}
           {{ values[t] }}
      hvac_activity:
        friendly_name: "Temps de fonctionnement nest"
        value_template: "{{ state_attr('climate.vestiaire_thermostat_nest', 'hvac_action') }}"

I can see the status is reported in a human readable state now :

I have two problems :

  1. the status is not updating in the entity card I’ve added to my dashboard. Here we can see that the stove is in this mode :

image

But the readable message remains “insert éteint” in the card, even if I refresh it.
Also I have another problem which must be related:

All the notifications I had created are based on triggers like that read device_status, and send notifications accordingly, an example :

alias: "Notification : Insert a pellet OFF (éteint)"
description: ""
trigger:
  - platform: state
    entity_id: sensor.device_status
    to: "0"
action:
  - service: notify.mobile_app_alf
    data:
      title: Insert Montefiore
      message: L'insert a pellets s'est éteint. Il faut le rallumer manuellement.
  - service: notify.mobile_app_cel_stelar_2
    data:
      message: L'insert a pellets s'est éteint. Il faut le rallumer manuellement.
      title: Insert Montefiore
mode: single

If I use the UI to try to select device_status statuses, here are the two only options displayed :

So there is something going wrong there. Did I make a mistake replacing my old/existing device_status value_template content ?

Yes. Your new sensor.device_status (readable output) is trying to read your old one (number)! Use:

{% set t = state_attr('climate.insert_montefiore', 'device_status')|int(0) %}

instead, or create a new sensor.readable_device_status with the old one producing the number (which your automation is expecting):

      device_status:
        value_template: "{{ state_attr('climate.insert_montefiore', 'device_status') }}"
      readable_device_status:
        value_template: >-
           {% set t = states('sensor.device_status')|int(0) %}
           {% set values =
               ['Insert éteint', 'Démarrage', 'Préchauffe',
                'Chargement pellets pour allumage', 'Allumage de la bougie', 'Allumage initial (8 minutes)',
                'Finallisation allumage', 'Chauffage en cours', 'Demande extinction',
                'Nettoyage final (8 minutes)', 'Attente de Standby',
                'Alerte: Plus de pellet', 'Alerte: Nettoyer le brasier'] %}
           {{ values[t] }}
1 Like

Thank you SO much Troon !

1 Like

Hi there,

Sorry, I’m struggling again. :frowning:

I want to read the values of “sensor.real_power” sensor, which reports the following values :
1, 3, 4, 5, 6, 7

and display this as a human readable text again.

here is what I try to use :

  readable_real_power:
    value_template: >-
      {% set t = states('sensor.real_power')|int(1) %}
      {% set values =
          [1: 'Eco', 3: 'Low',
           4: 'Medium', 5: 'High', 6: 'Tempest',
           7: 'Hurricane'] %}
      {{ values[t] }}

Unfortunately, it’s again, not working. I used numbers, as the sensors is not reporting “2” for some reason. What am I doing wrong this time ? :frowning:

EDIT : I fixed this by removing the numbers, and I entered the following code :slight_smile:

     readable_real_power:
        value_template: >-
          {% set t = states('sensor.real_power')|int(1) %}
          {% set values =
              ['Eco', 'Eco', 'Low',
               'Medium', 'High', 'Tempest',
               'Hurricane'] %}
          {{ values[t] }}