Convert numeric sensor output to text

Dear HA Forum,

I have a working Home Assistant configuration reading Modbus values from an Epever inverter.

Some of the returned values are numeric, I would like to convert these numeric values to text to make the dashboard easier to read. eg:
0=Normal, 1=Overvoltage, 2=Undervoltage, 3=Undervoltage Disconnect, 4=Fault

After reading many posts on the subject i still don’t get it, there are code snippets that i could modify but i have no idea where to place this code, templates, configuration.yaml or states?

Could someone give me some advice please?
Kind regards
Ade

I’m no expert, but I was at a loose end … I did a search and I believe this is what you are looking for?

LINK

Hope that works for you :slight_smile:

Regarding needing help, you could offer more information, such as the sensor, is it an attribute you’re trying to change. What have you come up with that doesn’t work?

Hi RoadkillUK,
Thanks for the tip but i have read this post previously. I tried adding my relevant values and adding it to configuration.yaml but i got errors. Not sure if it should go in configuration.yaml or somewhere else?
Many thanks
Ade

Regarding more info, i’m a bit embarrassed to post code as i see so many folks getting scalded for incorrect formatting of pasted code here. I tried the 3 backticks but it still looks a mess, more stuff i don’t understand.
Here is the beginning and the sensor from configuration.yaml

# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

modbus:
- name: epever
  type: rtuovertcp
  host: 192.168.1.170
  port: 8088
  delay: 5
  timeout: 10
  message_wait_milliseconds: 30
  sensors:
 - name: "Battery State 2"
# 0:Normal, 1:Overvoltage, 2:Undervoltage, 3:Undervoltage Disconnect, 4:Fault
    slave: 10
    address: 0x3589
    input_type: input

It is the numeric output from sensor Battery State 2 that i would like to show as a text value on my dashboard EG: Numeric value 0 to show ‘normal’ on the dashboard

If you have the numeric code in an existing sensor, create a template sensor, which you can do via the UI, under Helpers. State template should be (guessing the entity ID from the code above):

{{ {"0": "Normal",
    "1": "Overvoltage",
    "2": "Undervoltage",
    "3": "Undervoltage Disconnect",
    "4": "Fault"}.get(states('sensor.battery_state_2'), 'unknown') }}

To format code, type three backticks, paste your code after it, then three more backticks:

```
PASTED CODE
```

Here is the UI screen you want:

You wanna try formatting that again :slight_smile:

Thanks Troon,
I was using the wrong symbol :grinning:
Ade

Thanks RoadkillUK,
Got the code formatting sorted at least, was using the wrong symbol
Ade

1 Like

You can also make it an array and therefor the numbers are not needed.

{{ [ "Normal",
     "Overvoltage",
     "Undervoltage",
     "Undervoltage Disconnect",
     "Fault"][states('sensor.battery_state_2')|int] }}

I only went to make a coffee, came back and you’re sorted. Nice :slight_smile:

Thanks Troon,
Good clear explanation, got it working now :slight_smile:
Cheers
Ade