Sensor from MQTT Autodiscovery + Templating (value to string)

I got the sensor from MQTT Auto-discovery, I have used Customize to set a friendly Name.
The values I get are 0-2. When I manually add the MQTT topic I use:

value_template: "{{ ['Ekonomi', 'Normal', 'Lyx'][value_json.data] }}"

(Got help here… MQTT Sensor - value_template?)

But now I have the sensors added automatically by the auto-discovery. As I understand, it’s not possible to do:

homeassistant:
  customize:
    sensor.nibe_hot_water_comfort_mode:
      friendly_name: Varmvattenläge
      value_template: "{{ ['Ekonomi', 'Normal', 'Lyx'][data] }}"

How can I do it in a good way?

I think you have two options:

  1. Create a manual mqtt sensor from the topic.
  2. Create a template sensor
1 Like

I see!

I experimented a while with the template editor in dev tools. Is there any conversion I am missing?
Line 1 are ok, it gives 1 as output.
Line 2, no output

Yepp, try

{{ ['Min','Mid','Max'][states('sensor.nibe_hot_water_comfort_mode')|int] }}

and please post code, not pictures of it.

1 Like

Ah, I see, didn’t find out how to convert.

I’ll try to add a Template Sensor then! :slight_smile: Just to learn… Maybe I’ll go back to manually define the MQTT sensors (instead of auto-discovery).

Maybe one could explain the “magic” happening here, this register have a map-object:

{
    "register": "47260",
    "factor": 1,
    "size": "u8",
    "mode": "R/W",
    "titel": "Fan Mode",
    "info": "Fan mode",
    "unit": "",
    "min": "0",
    "max": "4",
    "map": [
        {
            "0": "Normal"
        },
        {
            "1": "Hastighet 1"
        },
        {
            "2": "Hastighet 2"
        },
        {
            "3": "Hastighet 3"
        },
        {
            "4": "Hastighet 4"
        }
    ],
    "logset": false,
    "data": "Normal",
    "raw_data": 0,
    "timestamp": 1599724178940
}

Compared to the register I need to do a template for:

{
    "register": "47041",
    "factor": 1,
    "size": "s8",
    "mode": "R/W",
    "titel": "Hot water comfort mode",
    "info": "Setting in menu 2.2. 0=Economy,1=Normal,2=Luxury,4=Smart Control 0=Economy 1=Normal 2=Luxury",
    "unit": "",
    "min": "0",
    "max": "4",
    "data": 1,
    "raw_data": 1,
    "timestamp": 1599724232919,
    "logset": false
}

I see the difference of data. It seems to make some mapping-magic. I have not found any documentation in HA about the mapping, but it seems to produce the same result as the template. Am I correct? Maybe can try to implement the mapping on the publishing node instead…