Convert sensor output of number value to text information from list in lovelace

Hey all - Haven’t had much luck finding anything to help me with this.

I have a Victron inverter which through MODBUS gives me the state it is in via a number which corresponds to what it is doing. I’d like to convert that number output to the appropriate text to display in lovelace.

Can this be done in the lovelace entity yaml? or can I do it within the sensor before it gets to the lovelace point?

Conditional entity cards are not what I’m after, the sensor is with other sensors on a entity card.

Thanks

1 Like

I have this in template editor which gives me what I want but in adding to config it logs an error that value template is invalid option

- platform: template
    sensors:
      MultiplusCurrentState:
        friendly_name: "Multiplus Current State"
        value_template: >-
            {% if is_state('sensor.multiplus_state', '0') %}
              Off
            {% elif is_state('sensor.multiplus_state', '1') %}
              Low Power
            {% elif is_state('sensor.multiplus_state', '2') %}
              Fault
            {% elif is_state('sensor.multiplus_state', '3') %}
              Bulk
            {% elif is_state('sensor.multiplus_state', '4') %}
              Absorption
            {% elif is_state('sensor.multiplus_state', '5') %}
              Float
            {% elif is_state('sensor.multiplus_state', '6') %}
              Storage
            {% elif is_state('sensor.multiplus_state', '7') %}
              Equalise
            {% elif is_state('sensor.multiplus_state', '8') %}
              Passthru
            {% elif is_state('sensor.multiplus_state', '9') %}
              Inverting
            {% elif is_state('sensor.multiplus_state', '10') %}
              Power Assist
            {% elif is_state('sensor.multiplus_state', '11') %}
              Power Supply
            {% elif is_state('sensor.multiplus_state', '252') %}
              Bulk Protection
            {% endif %}

I’m new to this too, but as I understand it, the value_template is there for extracting responses from REST (web services) queries. You just want a template sensor, so follow the examples here:

At quick glance, I think replacing friendly_name with just name and value_template with state should do it.

1 Like

Thanks Matt - The change to name and state did the trick :slight_smile:

For the sake of those in future:

        name: "Multiplus Current State"
        state: >-
            {% if is_state('sensor.multiplus_state', '0') %}
              Off
            {% elif is_state('sensor.multiplus_state', '1') %}
              Low Power
            {% elif is_state('sensor.multiplus_state', '2') %}
              Fault
            {% elif is_state('sensor.multiplus_state', '3') %}
              Bulk
            {% elif is_state('sensor.multiplus_state', '4') %}
              Absorption
            {% elif is_state('sensor.multiplus_state', '5') %}
              Float
            {% elif is_state('sensor.multiplus_state', '6') %}
              Storage
            {% elif is_state('sensor.multiplus_state', '7') %}
              Equalise
            {% elif is_state('sensor.multiplus_state', '8') %}
              Passthru
            {% elif is_state('sensor.multiplus_state', '9') %}
              Inverting
            {% elif is_state('sensor.multiplus_state', '10') %}
              Power Assist
            {% elif is_state('sensor.multiplus_state', '11') %}
              Power Supply
            {% elif is_state('sensor.multiplus_state', '252') %}
              Bulk Protection
            {% endif %}
2 Likes

Hey Guys i am new to Home assistant and i have tried to follow your examples without success.
I have heat pump and the heat pump compressor gives a value of ‘0’ if off and ‘1’ if on.

On the entity card is shows either 0 or 1.

I have tried to copy and modify some of the code examples above and put into the card yaml but no success. Any help would be appreciated.

Here is my code:

type: custom:mushroom-entity-card
entity: sensor.heatpump_compressor_status
name: Heatpump Compressor
icon_color: red
icon: mdi:heat-pump
state: >-
{% if is_state('sensor.heatpump_compressor_status', '0') %}
Off
{% elif is_state('sensor.heatpump_compressor_status', '1') %}
On
{% endif %}

And here is the error i get:

  • At path: state – Expected a value of type never, but received: "{% if is_state('sensor.heatpump_compressor_status', '0') %} Off {% elif is_state('sensor.heatpump_compressor_status', '1') %} On {% endif %}"

Another option instead of a series of elifs is to use a dictionary.

template:
  - sensor:
      name: "Multiplus Current State"
      state: >-
        {% set d = {
        '0': 'Off', '1': 'Low Power', 
        '2': 'Fault', '3': 'Bulk',
        '4': 'Absorption', '5': 'Float',
        '6': 'Storage', '7': 'Equalise',
        '8': 'Passthru', '9': 'Inverting',
        '10': 'Power Assist', '11': 'Power Supply',
        '252': 'Bulk Protection' } %}
        {% if states('sensor.multiplus_state') in d.keys() %}
          {{ d.get(states('sensor.multiplus_state')) }}
        {% else %}
          Unknown
        {% endif %}

state: is not a valid configuration varaible for a mushroom entity card. If you want to use templates you will need to use a mushroom template card.

type: custom:mushroom-template-card
entity: sensor.heatpump_compressor_status
icon_color: red
icon: mdi:heat-pump
secondary: >
  {{ iif(is_state(entity, '0'), 'Off', 'On') }}
primary: Heatpump Compressor
1 Like

Thanks! Amazing! That seems to have done the trick. I will use this format for my other sensors!

How would I list them if there is more than one value?

10: Off,
20: Hot Water,
30: Heat,
40: Pool,
60: Cooling

Similar to what you had above?
I cheated and did this but i know its not right! :sob:

type: custom:mushroom-template-card
entity: sensor.heatpump_operating_prioritisation
icon_color: red
icon: mdi:heat-pump
secondary: |
{{ iif(is_state(entity, '10'), 'Off','',) }}
{{ iif(is_state(entity, '20'), 'Hot Water', '')}}
{{ iif(is_state(entity, '30'), 'Heating', '') }}
{{ iif(is_state(entity, '40'), 'Pool', '') }}
{{ iif(is_state(entity, '60'), 'Cooling', '') }}
primary: Heatpump Priority

That might work, but the iif() function is an if-then-else construction, so it’s not really appropriate for when you have more than 2 possible outputs. Using a dictionary is more efficient than a lot of if-thens.

type: custom:mushroom-template-card
entity: sensor.heatpump_operating_prioritisation
icon_color: red
icon: mdi:heat-pump
primary: Heatpump Priority
secondary: |
  {% set d ={
  '10': 'Off',
  '20': 'Hot Water',
  '30': 'Heat',
  '40': 'Pool',
  '60': 'Cooling' 
  } %}
  {{ d.get(states(entity)) }}
2 Likes

Thank you!!

For my Itho fan I want to replace the precentages, attribuut of my fan.itho_fan, to a text value. My itho does not give me a status of high, medium or low, but only a percentage.
So I created therefore a template sensor like below;

sensor:
  - platform: template
    sensors:
......      
      fan_stage:
          friendly_name: Ventilatorstand
          value_template: >-
              {% if is_state_attr('fan.itho_fan', 'percentage', '0') %}
                off-
              {% elif is_state_attr('fan.itho_fan', 'percentage', '10') %}
                Low-
              {% elif is_state_attr('fan.itho_fan', 'percentage', '100') %}
                High-
              {% endif %}

The result should be visible in the following card, but I get no result:

type: custom:minimalistic-area-card
title: Bathroom
image: local/img/bathroom.jpg
tap_action:
  action: navigate
  navigation_path: /lovelace-bathroom
entities:
  - entity: sensor.fan_stage
    icon: mdi:fan

Attributes of fan.itho_fan:
preset_modes:
percentage: 10
percentage_step: 1
preset_mode: null
Speed status: 4.5
Internal fault: 0
Frost cycle: 0
Filter dirty: 0
friendly_name: Itho fan
supported_features: 1

Hi all, while I know enough HA to be dangerous :wink: I’m pretty much n00bing it here with value templates…

I am trying to convert a numeric value pulled via SNMP from my UPS, however none of the examples above seem to yield the expected result. If I remove the value_template, it works fine and just reads a value between 1 and 3, but with the value_template tacked on, the sensor just returns blank as if there is no match, even though the value is 2?

While the Yaml checks out, it does not work. Can anybody spot what I’m doing wrong here? Is it because the sensor has not been created at the time I am trying to read it? Is that why it’s failing to read anything out?

sensor:
  - platform: snmp
    host: 172.16.0.1
    community: somethingsomething
    accept_errors: true
    baseoid: 1.3.6.1.4.1.318.1.1.1.2.1.1.0
    name: "SmartUPS basic battery status"
    unique_id: smartups1500_basic_battery_status
    icon: mdi:state-machine
    value_template: >
      {% if is_state('sensor.smartups1500_basic_battery_status', '1') %} Unknown 
      {% elif is_state('sensor.smartups1500_basic_battery_status', '2') %} Normal 
      {% elif is_state('sensor.smartups1500_basic_battery_status', '3') %} Low
      {% endif %}

Second, if there is a way to convert this thing to a template so I can reload it without having to restart HA all the time would be capital.

Hi all,

I’m a relative new HA user. I get from my Varta energy storage system the status per number back into HA. Now I want instead of the number the correct wording. So I created the following code in the developer tool template:

sensor:
- platform: template
  sensors: 
      Varta_Status_Text:
        name: "varta_text"
        unit_of_measurement: "State"
        value_template: >-
          {% set mapper =  {
              '0' : 'Hochfahren',
              '1' : 'Betrieb',
              '2' : 'Laden',
              '3' : 'Entladen',
              '4' : 'Standby',
              '5' : 'Fehler',
              '6' : 'Service',
              '7' : 'Inselbetrieb' } %}
          {% set state =  states.sensor.vartastatus.state %}
          {{ mapper[state] if state in mapper else 'Unknown' }}

Now to my questions, where should this code be, in the configuration.yaml it wouldn’t work.

KR Gerald

It can go in configuration.yaml or sensors.yaml, but you will need to remove the unit_of_measurement line since your sensor’s state is not numeric.

Hi Didgeridrew

now I deleted this line, but it wouldn’t work.
Any other ideas?

sensor:
- platform: template
  sensors: 
      Varta_Status_Text:
        name: "varta_text"
        value_template: >-
          {% set mapper =  {
              '0' : 'Hochfahren',
              '1' : 'Betrieb',
              '2' : 'Laden',
              '3' : 'Entladen',
              '4' : 'Standby',
              '5' : 'Fehler',
              '6' : 'Service',
              '7' : 'Inselbetrieb' } %}
          {% set state =  states.sensor.vartastatus.state %}
          {{ mapper[state] if state in mapper else 'Unknown' }}

Now I found the correct code and it works.

sensor:
  - platform: template
    sensors:
      vartastatustext:
        friendly_name: "varta_text"
        value_template: >-
          {% set mapper =  {
              '0' : 'Hochfahren',
              '1' : 'Betrieb',
              '2' : 'Laden',
              '3' : 'Entladen',
              '4' : 'Standby',
              '5' : 'Fehler',
              '6' : 'Service',
              '7' : 'Inselbetrieb' } %}
          {% set state =  states.sensor.vartastatus.state %}
          {{ mapper[state] if state in mapper else 'Unknown' }}
1 Like