Inverter State Integer Value to Text Not Wokring

Hi all,

New to this whole HA thing still and I am struggling to get my output from my inverter from a integer value to a text value.

My sensor the integer value comes from is called “sensor.inverter_state”
This is working - I get the different values based on the modbus connector.

The values it can display are as follows:

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

Under my configuration.yaml I have the following:

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

Not sure I am understanding this template correctly but I’d essentially take the input from ‘sensor.inverter_state’ and output it to ‘sensor.inverter_state_text’ after the mapping takes place.

My issue is I don’t see sensor ‘sensor.inverter_state_text’

Update:

In my log I am seeing this:

2022-08-25 08:49:54.356 ERROR
(MainThread) [homeassistant.config] Invalid config for [sensor.template]:
invalid template (TemplateSyntaxError: expected token ',', got ':') for dictionary value @ data['sensors']['sensor_inverter_state']['value_template']. Got "{% set mapper = {\n '0' : 'Off',\n '1' : 'Low Power',\n '2' : 'Fault',\n '3' : 'Bulk',\n '4' : 'Absorbtion',\n '5' : 'Float',\n '6' : 'Storange',\n '7' : 'Equalize',\n '8' : 'Passthrough',\n '9' : 'Inverting',\n '10' : 'Power',\n '11' : 'Power Supply' \n '12' : 'Bulk Protection' } %}\n{% set state = states.sensor.inverter_state_text.state %} {{ mapper[state] if state in mapper else 'Unknown' }}". (See /config/configuration.yaml, line 132).
Please check the docs at https://www.home-assistant.io/integrations/template

TIP: Copy your templates into Dev Tools/Templates and play with it.
You are missing a comma after '11' : 'Power Supply'.


Not sure how the value from states.sensor.inverter_state_text.state will work.

Thanks for the pointer!

Maybe I am completely misunderstanding how to do this.

All I want to do is take those values → convert to a string and use that value on my dashboard to display something easier to understand instead of integers.

If you’re interested you can reduce the template to this:

sensor:
  - platform: template
    sensors:
      sensor_inverter_state:
        friendly_name: "Inverter State Text"
        unit_of_measurement: "State"
        value_template: >-
          {{ {0: 'Off',
              1: 'Low Power',
              2: 'Fault',
              3: 'Bulk',
              4: 'Absorption',
              5: 'Float',
              6: 'Storage',
              7: 'Equalize',
              8: 'Passthrough',
              9: 'Inverting',
              10: 'Power',
              11: 'Power Supply',
              12: 'Bulk Protection'}.get(states('sensor.inverter_state_text') | int(0), 'Unknown') }}

I found the solution on another post:

https://community.home-assistant.io/t/convert-sensor-output-of-number-value-to-text-information-from-list-in-lovelace/390156

The other post’s Solution suggests changing the Template Sensor’s configuration from legacy format to modern format (which employs different option names among other things). Is that what you did? (It wasn’t necessary)

Or did you change your template to use a long chain of if-elif-elif statements like shown in the linked thread? (It’s less efficient than using a dictionary lookup)

Or did you do both? (Unnecessary and inefficient)


Or did you use Didgeridrew’s suggestion posted in that thread (which isn’t marked as the Solution but does use a dictionary lookup)? If you did then it’s effectively what I suggested above.

I ended up doing the “Unnecessary and inefficient” way because it was a long day and I was tired.
I then went back and changed it.

This is what my code looks like currently:

# Inverter State to Text
  - platform: template
    sensors:
      sensor_inverter_state_text:
        friendly_name: "Inverter State Text"
        unit_of_measurement: "State"
        value_template: >-
          {% set mapper =  {
            '0' : 'Off',
            '1' : 'Low Power',
            '2' : 'Fault',
            '3' : 'Bulk',
            '4' : 'Absorption',
            '5' : 'Float',
            '6' : 'Storage',
            '7' : 'Equalize',
            '8' : 'Passthrough',
            '9' : 'Inverting',
            '10' : 'Power',
            '11' : 'Power Supply', 
            '12' : 'Bulk Protection' } %}
          {% set state =  states('sensor.inverter_state') %}
          {{ mapper[state] if state in mapper else 'Unknown' }}

For future reference, it’s customary to assign the Solution tag to the first post that identifies the cause of the original problem and explains how to correct it. It’s meant to be a learning guide for other users who may have a similar question.

In this case, you took the example I suggested, made it slightly longer, then marked your own post as the Solution.

For more information refer to guideline 21 in the FAQ

I did test what you gave me and it didn’t work.
Thanks for your help though :slight_smile:

Then you may have made a copy-paste error because the template definitely works. :man_shrugging: