MQTT Number - value_template issue

Hi,

I have a Hot Water System talking to HA via MQTT. I am coming across from OpenHAB so still learning.

I have the following defined.

- mqtt
 - number:
    - name: "Hot Water Main - Mode"
      unique_id: "hot_water_main_mode"
      command_topic: "evoheat/HotWaterMain/mode/command"
      state_topic: "evoheat/HotWaterMain/mode/state"
      min: 0
      max: 4
      value_template: >-
          {% set mapper =  {
              '0' : 'Off',
              '1' : 'Eco',
              '2' : 'Hybrid',
              '3' : 'Boost',
              '4' : 'Absorbtion' } %}
          {% set state = states('number.hot_water_main_mode') %}
          {{ mapper[state] if state in mapper else 'Unknown' }}

The above is throwing an error “Payload ‘2’ is not a Number”. I am a bit lost.

You have designed your template to receive a number, find a matching value in the mapper dictionary and then report a string value (Off, Eco, Hybrid, etc). An MQTT Number is meant for reporting numbers, not strings.

I think want you want is an MQTT Select. It receives a number and reports a corresponding string value. It allows you to select a different string value via the UI (or via a service call) and publishes a corresponding number to the command topic.

  - select:
      - name: "Hot Water Main - Mode"
        unique_id: "hot_water_main_mode"
        options:
          - 'Off'
          - 'Eco'
          - 'Hybrid'
          - 'Boost'
          - 'Absorbtion'
        state_topic: "evoheat/HotWaterMain/mode/state"
        value_template: >-
          {{ {  0: 'Off',
                1: 'Eco',
                2: 'Hybrid',
                3: 'Boost',
                4: 'Absorbtion' }.get(value|int(0), 'unknown') }}
        command_topic: "evoheat/HotWaterMain/mode/command"
        command_template: >-
          {{ {  'Off': 0,
                'Eco': 1,
                'Hybrid': 2,
                'Boost': 3,
                'Absorbtion': 4 }.get(value, 'unknown') }}

In Developer Tools → States


In the UI

image

1 Like

Any progress to report?

Missed your reply…about to try. Will report back!

It worked a treat, thank you :slight_smile:

1 Like