MQTT publish script - using a helper as part of a payload

Hi

I’ve been using Wallpanel as in in home display for a while and have a few voice alerts set up using its speak function.

I was having fun using the script i set up to test it the other night by editing the text and running the script watching my toddler getting confused by the disembodied alexa like voice telling her its bed time, go get your pyjamas, no put that down etc.

Got me thinking about adding a test input helper on my dash & a button to run the script

So my question is in the script below how do i replace payload: “{‘speak’: ‘Hello’}” with something like payload: “{‘speak’: ‘{{states(input_text.wallpanel_say)}}’}” but valid

alias: Wallpanel_speak
sequence:
  - service: mqtt.publish
    data:
      topic: wallpanel/mywallpanel/command
      payload: "{'speak': 'Hello'}"
      qos: 0
      retain: false
mode: single

Untested

alias: Wallpanel_speak
sequence:
  - service: mqtt.publish
    data:
      topic: wallpanel/mywallpanel/command
      payload_template: "{'speak': '{{states(input_text.wallpanel_say)}}'}"
      qos: 0
      retain: false
mode: single

Thank you
I was sure id tried that and it didn’t like the yaml.
I’ll test it when I get home and let you know how it goes

Just for the record, JSON proper insists on double-quotes, so, even if Wallpanel doesn’t care, you should take the habit to write

payload: '{"speak": "Hello"}'

for that time when it will matter, somewhere else :wink:

Not sure that is correct. The entity inside states() is not enclosed in quotes. Think it should be:

payload_template: '{"speak": {{ states("input_text.wallpanel_say") }} }'

Too many quote imbrication :slight_smile:
Maybe

payload_template: >
  {"speak": "{{ states('input_text.wallpanel_say') }}"} }

EDITed following Francis-eagle-eye spotting :joy:

Then it should be

payload_template: >
  {"speak": "{{ states('input_text.wallpanel_say') }}" }
1 Like