MQTT Sensor - value_template?


I have the following in my configuration:

  - platform: mqtt
    name: "Example"
    state_topic: "topic/foo/bar/json"
    value_template: "{{ }}"

The field json contains the numbers 1, 2 or 3.

As I understand I can add some conditions to the template to convert the three values as follows:
1 = “Min”
2 = “Mid”
3 = “Max”

But I don’t catch the syntax here.
Please advice me.

Try this:

    value_template: >
      {% set mapper = { 1:'Min', 2:'Mid', 3:'Max' } %}
      {% set digit = %}
      {% set word = mapper[digit] %}
      {{ word }}


    value_template: >
      {% set word = ['Min', 'Mid', 'Max'] %}
      {% set digit = - 1 %}
      {{ word[digit] }}

Or if you get all that and don’t mind a bit of obscurity:

    value_template: "{{ ['Min', 'Mid', 'Max'][ - 1] }}"
1 Like

I used the one-liner, but I learned the multi-line now. I also saw I have the first value at 0, not 1. So just skipped the -1…

1 Like

If I get the idea of doing the reverse…
I mean, from a card be able to select (Min, Mid, Max), and send it to the topic. Maybe select from a dropdown (or have 3 radio-buttons?).

There are no radio buttons (that I know of) so a drop-down (input_select) would be the way to go.

Define it with Min, Mid, Max as selection options. Trigger an automation on any change of the input select (state trigger with no to: or from:).

In the actions you can use an if/else template, e.g.

- trigger:
  - platform: state
    entity_id: input_select.example
  - service: switch.turn_on
      entity_id: >
        {% if states('input_select.example') == 'Max' %}
        {% elif states('input_select.deck_scene') == 'Mid' %}
        {% else %}
        {% endif %}

You could also do this with the new choose action option if you don’t want to use a template.

EDIT: Posted this more clear question instead:


I got it working when configured the sensor manually in the configuration.

But for learning I switched on the MQTT Discovery, and removed the manual configuration. Now I get the following data.

Topic: homeassistant/sensor/47041/config

    "name":"Nibe Hot water comfort mode",

Topic: nibe/modbus/47041
Value (1 = Normal):


Topic: nibe/modbus/47041/json

    "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": 1599639764959,
    "logset": false

Is there a way to add a value_template from the UI, or can I add something to a config-file?
Also, I have all data how to translate 0 to 4 into text in the info-JSON-object. Can I use it?
Just some questions to get wiser…