Problem with variable in payload

Dear all.

A very beginner question.

I want to be able to use a variable (text_input.some_text) in a payload (using mqtt and iotlink).

Below is a example of something that does not “compile” with the error (I guess the reaon for this is that I’m using a ’ to “expand” the variable and then payload thinks the string is finished?):
bad indentation of a mapping entry at line xx, column xx:
… \Program Files (x86)\Magic RDS", “user”: “”, “visible”: true, " …

  - service: mqtt.publish
      topic: "iotlink/workgroup/lydtelelap02/commands/run"
      payload: '{ "command": "C:\\Program Files (x86)\\Some Directory\\programname.exe", "args": "send:MSG={{ sensor('text_input.some_text')}}", "path": "C:\\Program Files (x86)\\Some Directory", "user": "", "visible": true, "fallback": true }'

If I try to use:
payload: ‘{ “command”: “C:\Program Files (x86)\Some Directory\programname.exe”, “args”: “send:MSG={{ sensor.text_input.some_text.sensor }}”, “path”: “C:\Program Files (x86)\Some Directory”, “user”: “”, “visible”: true, “fallback”: true }’

Then I get “send:MSG={{ sensor.text_input.some_text.sensor }}” to the MQTT and not “send:MSG=example” whick I hope to achieve.

Could someone please enlighten me on how I can use a variable in the payload?

Thanks in advance.

data_template: not data:

Thank you. That worked!

1 Like

Please mark @tom_l’s reply as the solution.


That lets the rest of us know you no longer need help. Also, if someone else is having the same issue it helps them know this topic contains a solution. Thanks!

with which of the options did it work? with sensor('text_input.some_text'), or with sensor.text_input.sometext.sensor ?

also, the answer marked as valid solution could be a little bit more thorough - would definetely not make it on a slackoverflow…

This three year old topic is no longer relevant due to changes made to home assistant.

That one is the one you want to use. The second example not valid. See