Mqtt payload including a variable based on other states

I have a requirement to send a particular string based on some other states.

I have a mode that can be 1 or 0, and a value ie 23. So the string will be 123 or 023 depending on the states. The value here 23, is going to be an adjustable number in HA, this is already in place.

How can I determine this string and then use it as a variable in an mqtt payload?

I currently have some automations that trigger scripts based on the the states that would ultimately select the mode, so I imagine it would make most sense that in those scripts a variable will be updated, and then call a thirds script to send the mqtt message?

So would it make sense that I create a helper string, and from either of the two scripts (that would be triggered when the mode needs to be turned on or off), update the string with the appropriate 1 or 0 + the value.

I may be answering my own question but Ive literally just got the first automation and scripts set up and working. Thanks

What are the sources of “mode” and “value”? Are they from sensor entities or something else?

It may help to clarify the situation if you post the automation and scripts you’ve created for this application.

The mode isnt a value that exists but will be determined based on some states within HA and the value is a number helper.

When the states change automations are triggered.

automations

alias: TRV Boost On
description: ""
trigger:
  - platform: state
    entity_id:
      - schedule.anti_condensation_heating_schedule
    to: "on"
condition:
  - condition: state
    entity_id: sensor.hotwater_state
    state: "OFF"
action:
  - service: script.trv_boost_on
    data: {}
mode: single
alias: TRV Boost Off
description: ""
trigger:
  - platform: state
    entity_id:
      - schedule.anti_condensation_heating_schedule
    to: "off"
  - platform: state
    entity_id:
      - sensor.hotwater_state
    to: "ON"
condition: []
action:
  - service: script.trv_boost_off
    data: {}
mode: single

scripts

alias: TRV_Boost_On
sequence:
  - service: hive.boost_heating_on
    data:
      entity_id: climate.master_bedroom
      time_period: "01:00:00"
      temperature: 28
  - service: hive.boost_heating_on
    data:
      entity_id: climate.olivias_room
      time_period: "01:00:00"
      temperature: 28
  - service: hive.boost_heating_on
    data:
      entity_id: climate.play_room
      time_period: "01:00:00"
      temperature: 28
mode: single
icon: mdi:radiator
alias: TRV_Boost_Off
sequence:
  - service: hive.boost_heating_off
    data:
      entity_id: climate.master_bedroom
  - service: hive.boost_heating_off
    data:
      entity_id: climate.olivias_room
  - service: hive.boost_heating_off
    data:
      entity_id: climate.play_room
mode: single
icon: mdi:radiator-off

when trv boost on is triggered the mode will need to be “1” and when boost off is triggered is needs to be “0”. The value will be from the helper which selects a value with no decimal places but the resulting output needs to be a string for the mqtt message.

Thanks

Please post your automation and scripts as formatted YAML.

For more information, refer to FAQ Guideline 11 - Format it properly


What is its entity_id?

input_number.anti_condensing_heating_setpoint

For Boost On, here’s the service call needed to publish a string that concatenates 1 and the integer value of the Input Number helper.

  - service: mqtt.publish
    data:
      topic: your/topic/goes/here
      payload: >
        {{ '1' ~ states('input_number.anti_condensing_heating_setpoint')|int(0) }}

You can test the template by copy-pasting it into the Template Editor.

Thanks very much. I wasnt too far off with my attempt but I dont think I wouldve got here easily.

Ive sent the value to a text helper to keep track of it and can see its just right. :ok_hand:

1 Like

For future reference, the tilde operator performs concatenation. The plus operator also performs concatenation but only if the values are strings. If the values are numbers then the plus operator will (obviously) perform an arithmetic addition.