Issues with Template sensors and mapping

So my inverter states its state numerically. I would like to map those values to a word. I have attempted to follow what I can find online but I am not coming right nor can I find official documentation on this.

  - platform: template
    sensors:
      multiplus_inverter_state:
        friendly_name: "Inverter State"
        unit_of_measurement: "State"
        entity_id: sensor.multiplus_inverter_state_numeric
        value_template: >-
          {% set mapper =  {
              '0' : 'Off',
              '1' : 'Low Power',
              '2' : 'Fault',
              '3' : 'Bulk',
              '4' : 'Absorbtion',
              '5' : 'Float',
              '6' : 'Storage',
              '7' : 'Equalize',
              '8' : 'Passthru',
              '9' : 'Inverting',
              '10' : 'Power Assist',
              '11' : 'Power Supply',
              '252' : 'Bulk Protection' } %}
          {% set state =  states.sensor.multiplus_inverter_state_numeric %}
          {{ mapper[state] if state in mapper else 'Unknown' }}

Any idea why this doesn’t work?

Ahh, the sneaky spacing.
Still never fixed for me, unfortunately. Still get an unknown value.

Use the states method, not the states machine

Ela Petro. You lost me here. Any examples of docs that I can follow?

https://www.home-assistant.io/docs/configuration/templating/#states

Even with those changes, seems like no matter what I do I always get an unknown. Clecking for more details I can see that it is pulling the correct numeric state though.

image

Using the template editor, what result does this give:

{{ states('sensor.multiplus_inverter_state_numeric') }}

Hi
It gives a 4.

Post exactly what you tried when it’s unavailable. Typically, people make mistakes on templates and it’s impossible to tell if they fixed it correctly.

But in case you want to know what the original problem was…

{% set state =  states.sensor.multiplus_inverter_state_numeric.state %}

notice the “.state” at the end.

  - platform: template
    sensors:
      multiplus_inverter_state:
        friendly_name: "Inverter State"
        unit_of_measurement: "State"
        entity_id: sensor.multiplus_inverter_state_numeric
        value_template: >-
          {% set mapper =  {
              '0' : 'Off',
              '1' : 'Low Power',
              '2' : 'Fault',
              '3' : 'Bulk',
              '4' : 'Absorbtion',
              '5' : 'Float',
              '6' : 'Storage',
              '7' : 'Equalize',
              '8' : 'Passthru',
              '9' : 'Inverting',
              '10' : 'Power Assist',
              '11' : 'Power Supply',
              '252' : 'Bulk Protection' } %}
          {% set state = states('sensor.multiplus_inverter_state_numeric')|int(0) %}
          {{ mapper[state] if state in mapper else 'Unknown' }}

      # Inverter States, 0=Off;1=Low Power;2=Fault;3=Bulk;4=Absorption;5=Float;6=Storage;7=Equalize;8=Passthru;9=Inverting;10=Power assist;11=Power supply;252=Bulk protection   

Remove that. Your dictionary (what you cal mapper) uses umbers that are wrapped in quotes. That means they aren’t actually numbers but strings. So he mapper will fail. You want to keep your state a string, I.e. remove the int cast

1 Like

Petro is that man. Thank you.
Now I can study this so I understand it better + copy pasta for future.