State value publishes on MQTT as string literal

Hello community.

In reference to the slider example for controlling temperature here:

  - alias: Temp slider moved
    trigger:
      platform: state
      entity_id: input_number.target_temp
    action:
      service: mqtt.publish
      data:
        topic: 'Heater/cmd/TempDesired'
        retain: true
        payload: "{{ states('input_number.target_temp') | int }}"
    

{{ states(‘input_number.target_temp’) | int }} is being published as a string literal which the MQTT subs don’t know what to do with.

If I change it to a hardcoded **payload: 9** then it works as intended. For some reason I can’t get the state of the input_number in the recommended way.

Any advice?

The output of any template is always a string value.

You’ve demonstrated that a non-templated value can maintain its type. In this case, the type is an integer. In contrast, the type of whatever is generated by a template is always a string.

Thanks @Taras. It seems that this line, lifted directly from the documentation example, is meant to render the state of input_number.target_temp as an | int and then publish that to the MQTT topic.

It is literally publishing {{ states(‘input_number.target_temp’) | int }} which breaks the system as the subsciber is expecting an integer.

I’ve pretty much just done what is in the example but it doesn’t behave as expected.


input_number:
  target_temp:
    name: Temperature
    min: 8
    max: 24
    step: 1
    unit_of_measurement: °C
    icon: mdi:thermometer


automation:
  - alias: Set temp slider
    trigger:
      platform: mqtt
      topic: 'heater/sts/TempDesired'
    action:
      service: input_number.set_value
      data:
        entity_id: input_number.target_temp
        value: "{{ trigger.payload }}"


# This second automation script runs when the target temperature slider is moved.
  - alias: Temp slider moved
    trigger:
      platform: state
      entity_id: input_number.target_temp
    action:
      service: mqtt.publish
      data:
        topic: 'heater/cmd/TempDesired'
        retain: true
        payload: "{{ states('input_number.target_temp') | int }}"
        # payload: 9

I think I now understand the problem. Are you using version 0.115.X? If you are not, you must use data_template: and not data: in the line after service:.

I’m using version 0.114.4

Will try your suggestion

I believe the examples in the documentation may have been updated to reflect the deprecation of data_template: now that data: serves the same purpose in 0.115 (I.e. data: supports templates). However it becomes misleading for users of older versions where data: doesn’t support the use of templates.

Thanks. I upgraded to 0.115 and now it’s working. I can use a slider to control the thermostat over MQTT. Happy days.

Glad to hear it. Please consider marking my post (above) with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has an accepted solution. This helps other users find answers to similar questions.