Publish {{trigger.event}} on mqtt with payload_template

My guess is, this should work, right? There’s a trigger and I’m going to push the trigger event name to MQTT. But somehow this does not work. Is it a bug? Or am I missing a hidden feature?

Or can I only use mqtt trigger stuff even though the actual trigger was not from MQTT?

- alias: 'Say who at home'
  trigger:
    - platform: state
      entity_id: group.greg
      to: 'home'
    action:
    - service: mqtt.publish
      data:
        payload_template: 'Hello there {{trigger.event}} welcome to our humble home'
        topic: 'homey/say'

I don’t think ‘payload_template’ exists. I could be wrong. But ‘data_template’ definitely exists, so this should work:

- alias: 'Say who at home'
  trigger:
    - platform: state
      entity_id: group.greg
      to: 'home'
    action:
    - service: mqtt.publish
      data_template:
        payload: 'Hello there {{trigger.event}} welcome to our humble home'
        topic: 'homey/say'

That would be really strange because I use payload_template in a lot of other automations and they all work… :thinking:

Going to try you alternative anyway :slight_smile:

if payload_template works, then your example should work. the action spacing is a little off though.

https://www.home-assistant.io/docs/mqtt/service/ it’s documented, the spacing was a copy-paste error I think. It’s ok in the original. Still it does not work… Weirdness :frowning:

Did you try the data_template? Also, is everything in group.greg a device_tracker?

1 Like

Hm… Not everything is a tracker. I’ve got BT tracking on a other device (a pi) and AFAIK it is not possible to transfer the tracker as is to an other HA instance (with mqtt) so it’s a sensor on the other side (where the user group lives).

I did try the data_template as well, also did not work.

Then your trigger must not be working. I would start looking at the status of your group and go from there.

1 Like

If I don’t use the {{trigger.event}} in the payload it works… :man_facepalming:

Funny, now that I look at it, you aren’t using an event. Your trigger is going to be a from_state and a to_state. You won’t have any event data. You need to actively go out and find who just arrived in order to place that information there. You may want to build this automation off individual devices or hard code the phrase to be ‘greg’.

1 Like

Hm… I’m nearly there… I didn’t show all of the code… It’s actually 4 triggers (4 persons) so I wanted to know what the trigger was to say “hello $name”.

But you DID help me a lot… It was a combinations of things… First there was also a condition that messed stuff up and second I do need to use data_template.

1 Like

Yes… You where right again… Although I think it’s only a to_state (I’m not using from). Well it works now! Thanks a million for your help!

all it takes is another set of eyes!