Wanting to map sensor number value to words

I’m using the Dyson fan sensors to measure temp and humidity, but is also has a sensor for air quality “sensor.nursery_air_quality” which are returns as a number. I’d like the number to display as the air quality state as per the Dyson app eg: “Good”, “Fair”, “Poor” and Very Poor" instead of 1,2,3 or 4.
Can anyone please suggest how I might map the digits to the words so they display in the sensor? I can’t see that customize is the right place, so I’m guessing templates? Not sure now they work so sample code would be appreciated. thanks

My current Group config:
nursery:
name: Nursery
view: yes
icon: mdi:baby-buggy
entities:
- fan.nursery
- sensor.nursery_air_quality

I think this is what you are looking for…

1 Like

thanks @silvrr looks like it might be, not sure where exactly to put the value_template, Dyson has multiple sensors and it need to apply to just to air quality

Try the following…

  - platform: template
    sensors:
      airquality:
        entity_id: sensor.nursery_air_quality
        value_template: "{%if states.sensor.nursery_air_quality.state == '1 %}Good{% elif states.sensor.nursery_air_quality.state == '2' %}Fair{% elif states.sensor.nursery_air_quality.state == '3' %}Poor{% elif states.sensor.nursery_air_quality.state == '4' %}Very Poor{% endif %}"

This is a new template sensor. It only updates when sensor.nursery_air_quality and outputs a text status based on what you gave above.

You should be able to test the template by pasting it into the template dev tool located in the side panel of the HA front end.

Click the grey icon that looks like a page with the top right corner folded. It has a <> in the middle also. Delete the text in the box and paste only the text in red above into the box. It should return with “Good”, “Fair”, “Poor” or Very Poor" based on the sensor reading 1,2,3 or 4.

Cheers for help!
there’s a tiny typo in there with only one ’ on the 1, but when corrected the dev tool returned only " " and my sensor is showing blank. sensor.nursery_air_quality shows 1 on the frontend
I tried removing the quotes on the numbers but to no avail… I think we are very close…

Hmm, not sure what is going wrong there. I have the same template (with a different sensor) working in my setup.

what do you get when you put in ‘{{ states(“sensor.nursery_air_quality”) }}’ ?

That should return just the state, ie 1, 2, 3, 4.

It’s working - I think it’s the the Dyson fan that’s at fault, it does not always update the air quality status.

sensor:
  - platform: template
    sensors:
      nursery_air_quality_name:
        friendly_name: "Air Quality"
        unit_of_measurement: "State"
        entity_id: sensor.nursery_air_quality
        value_template: "{%if states.sensor.nursery_air_quality.state == '1' %}Good{% elif states.sensor.nursery_air_quality.state == '2' %}Fair{% elif states.sensor.nursery_air_quality.state == '3' %}Poor{% elif states.sensor.nursery_air_quality.state == '4' %}Very Poor{% endif %}"

Hi I just joined @reynos’s solution with the one provided by @Mariusthvdb which I wanted to share, because I find it more extensible

sensor:
  - platform: template
    sensors:
      nursery_air_quality_name:
        friendly_name: "Air Quality"
        unit_of_measurement: "State"
        entity_id: sensor.nursery_air_quality
        value_template: >-
          {% set mapper =  {
              '1' : 'Good',
              '2' : 'Fair',
              '3' : 'Poor',
              '4' : 'Very Poor' } %}
          {% set state =  states.sensor.nursery_air_quality.state %}
          {{ mapper[state] if state in mapper else 'Unknown' }}
2 Likes

yes, mappers are always very nice to use, and prevent many double code.
Be ware though that you ideally should have an ‘else’ in the template. In your case you could leave out the ‘4’, and use

{% set id = mapper[state] if state in mapper else 'Very Poor' %}

or maybe better (signaling none of the numeric states are met):

{% set id = mapper[state] if state in mapper else 'Unknown' %}

btw, I always use

{% set state = states('sensor.nursery_air_quality') %}

cheers!

Thanks, the else branch is a good input though. :slight_smile:

you can omit that last line and output the line before with out the set

          {% set mapper =  {
              '1' : 'Good',
              '2' : 'Fair',
              '3' : 'Poor',
              '4' : 'Very Poor' } %}
          {% set state =  states.sensor.nursery_air_quality.state %}
          {{ mapper[state] if state in mapper else 'Unknown' }}

and if you are feeling very crafty you can condense it some more. This uses a list instead of a dictionary and every unknown state is set to zero. However, if a severity level above 4 ever appears, this will error.

{% set mapper = ['Unknown', 'Good', 'Fair', 'Poor', 'Very Poor'] %}
{{ mapper[states.sensor.nursery_air_quality.state | int] }}
1 Like

@petro now it’s going to be religious… :wink:

just that I understand this correctly: you use the|int trick here to have each unknown state set to 0?

which means this way of mapping is only useable for lists with numbers, ints to be more precise? I really like the option not having to explicitly name the ‘else’ so if this would be possible for mappers that use non-numeric states Id like to use that.

ex:

have 4 modes:

          {{ states('input_select.mode') in ['Normal','Full house','Kiosk','Developer'] }}

using a mapper to have the ‘else’ be the fourth option ‘Developer’ not listed in the mapper:

        message: >
          {% set mapper = {'Normal': 'Normal operation, business as usual',
                        'Full house': 'Full house-mode, turn on the vents!',
                        'Kiosk': 'Wow, Kiosk-mode, showing off!',
                        } %}
          {% set state = states('input_select.mode') %}
            {{ mapper[state] if state in mapper else 'Ready for some serious development!' }}

Could this be niftied-up?

Correct.

Not really, you’ve got it set up pretty well.

1 Like

thanks for confirming.