Change numeric sensor states to text

Hello,

I have a modbus sensor that returns numeric state values, each value representing a specific mode of operation. I would like to translate those numeric values over to a text description of the operating mode. The following code in configuration.yaml returns the modbus numeric values as expected:

modbus:
  name: AXSPort
  type: tcp
  host: 192.168.1.37
  port: 502

sensor:
  platform: modbus
  scan_interval: 10
  registers:
    - name: Outback_Operating_Mode
      hub: AXSPort
      register: 41517

Then, based on this thread https://community.home-assistant.io/t/wanting-to-map-sensor-number-value-to-words/40663/7 I tried tacking on the " - platform: template" section as seen here.

modbus:
  name: AXSPort
  type: tcp
  host: 192.168.1.37
  port: 502

sensor:
  platform: modbus
  scan_interval: 10
  registers:
    - name: Outback_Operating_Mode
      hub: AXSPort
      register: 41517

  - platform: template
    sensors:
      outback_operating_mode_text:
      friendly_name: "Outback_Operating_Mode_Text"
        unit_of_measurement: "State"
        entity_id: sensor.outback_operating_mode
        value_template: >-
          {% set mapper =  {
              '0' : 'Off',
              '1' : 'Searching',
              '2' : 'Inverting',
              '3' : 'Charging',
              '4' : 'Silent',
              '5' : 'Float',
              '6' : 'EQ',
              '7' : 'Charger Off',
              '8' : 'Support',
              '9' : 'Selling',
              '10' : 'Pass Through',
              '11' : 'Offsetting' } %}
          {% set state =  states.sensor.outback_operating_mode.state %}
          {{ mapper[state] if state in mapper else 'Unknown' }}

Unfortunately, this configuration fails validation:
image

Guessing the problem is a fairly basic oversight on my part, but I’m pretty new at this. Any guidance would be appreciated.

Thanks!

Indent the first sensor’s configuration and include a hyphen.

sensor:
  - platform: modbus
    scan_interval: 10
    registers:
      - name: Outback_Operating_Mode
        hub: AXSPort
        register: 41517

  - platform: template
    sensors:
      outback_operating_mode_text:

In addition, unless you are running a very old version of Home Assistant, remove the following line from the Template Sensor:

        entity_id: sensor.outback_operating_mode

That convention was deprecated several versions ago.

Thanks for the quick reply and for the tips! I implemented those changes, but unfortunately I’m still getting a validation error. My code now look like this:

modbus:
  name: AXSPort
  type: tcp
  host: 192.168.1.37
  port: 502

sensor:
  - platform: modbus
    scan_interval: 10
    registers:
      - name: Outback_Operating_Mode
        hub: AXSPort
        register: 41517

  - platform: template
    sensors:
      outback_operating_mode_text:
      friendly_name: "Outback_Operating_Mode_Text"
        unit_of_measurement: "State"
        value_template: >-
          {% set mapper =  {
              '0' : 'Off',
              '1' : 'Searching',
              '2' : 'Inverting',
              '3' : 'Charging',
              '4' : 'Silent',
              '5' : 'Float',
              '6' : 'EQ',
              '7' : 'Charger Off',
              '8' : 'Support',
              '9' : 'Selling',
              '10' : 'Pass Through',
              '11' : 'Offsetting' } %}
          {% set state =  states.sensor.outback_operating_mode.state %}
          {{ mapper[state] if state in mapper else 'Unknown' }}

And here is the error I’m getting:
image

you need to indent “friendly_name:” under “outback_operating_mode_text:”

- platform: template
    sensors:
      outback_operating_mode_text:
        friendly_name: "Outback_Operating_Mode_Text"
        unit_of_measurement: "State"
        value_template: >-
          {% set mapper =  {
              '0' : 'Off',
              '1' : 'Searching',
              '2' : 'Inverting',
              '3' : 'Charging',
              '4' : 'Silent',
              '5' : 'Float',
              '6' : 'EQ',
              '7' : 'Charger Off',
              '8' : 'Support',
              '9' : 'Selling',
              '10' : 'Pass Through',
              '11' : 'Offsetting' } %}
          {% set state =  states.sensor.outback_operating_mode.state %}
          {{ mapper[state] if state in mapper else 'Unknown' }}
1 Like

Deleted because it was redundant. Didn’t see finity’s reply until web page refreshed in my browser. :man_shrugging:

Fantastic! That worked! Seems I need to find the manual on proper spacing. :slight_smile: Thanks again @123 and @finity!

Ought to give @123 the solution. He helped you with the majority of it.

He would have fixed the last bit too but I jumped in on it before he did. :slightly_smiling_face:

Thanks for that but it’s all good; I should’ve identified the second syntax error in my first post.

1 Like