Is it possible to get payload from a trigger into the action as a variable?

Simple is good! :slight_smile: I don’t know if formatting or what, but I can’t get it to work. In the editor the action comes up like this:

{
“data_template”: null,
“entity_id”: “trigger.payload”
}

Something tells me it’s an erorr in the “data template” thing, probably when I pasted it into my automations file. Also it would be great if the code let me write in for instance switch.zwaveswitchnumber and then the payload could be simply the number (I name my switches with numbers, so it’s not the Z-Wave device number).

At its most basic…

trigger:
  platform mqtt
  topic: YOUR_TOPIC
action:
  service: home_assistant.turn_on
  data_template:
    entity_id: "{{ trigger.payload }}"

Thanks! And then I’m dense as usual, I’m not used to Hass yet. I’m trying, but it isn’t working, probably because I formatted something wrong when pasting into the automation file (I use leafpad to edit on the Rasberry). In the editor of Hass I see this in the action code:

{
“data_template”: null,
“entity_id”: “trigger.payload”
}

It’s probably an error I made there, but I have tried for 20 minutes without fixing it. Also, if possible, it would be great if I could use some static code in the identity ID, so like switch.zwaveswitchnumber and then I’d only need the payload to be for instance 4.

This is what it looks like in my automations file:

- action:
  - data:
      data_template:
      entity_id: trigger.payload
    service: homeassistant.turn_off
  alias: Skru av brytere
  condition: []
  id: '1524846616356'
  trigger:
  - platform: mqtt
    topic: EG/ZWaveBrytereAv

Even more dense than usual! I managed to edit the first post instead of replying the first time, then I didn’t see the reply and wrote another reply, and now I see the error… I must blame a week with work from 8 to 22 every day… :wink:

Ha - so are you sorted now??

I hope I’m a bit closer. I found a space error in the code, so now the code in the editor in the GUI looks like this:

{
“data_template”: {
“entity_id”: “trigger.payload”
}
}

That’s close, right? But it’s not yet working. I know it’s triggered (I have an almost identical one, with the payload different), but sending the code switch.zwavebryter4 (which is the name of the switch I’m testing with now) does not work.

I think using the UI editor for this sort of thing is a recipe for disaster tbh - just code it in to the file manually.

Yeah, I found out now, because I saw in the documentation that it neds no data , only data_template. The problem is that the code you gave me didn’t work when I pasted it in, again probably my fault. :blush: And I’m still struggling. I am sending the MQTT topic ZWaveBrytereAv and the payload switch.zwavebryter4. I know this is correct, because if I use that topic and payload as the trigger, and switch.zwavebryter4 as the entity_id in a regular “turn off” action it wil work. (I went from turn on to turn off for no apparent reason, but that doesn’t matter). So the problem is getting the payload switch.zwavebryter4 to the variable. This is the code I have at the moment, the way it looks in my automation file:

- action:
  - data_template:
      service: homeassistant.turn_off
      entity_id: trigger.payload
  alias: Skru av brytere
  condition: []
  id: '1524846616356'
  trigger:
    platform: mqtt
    topic: ZWaveBrytereAv

Feeling like a babe in the woods here… I noted that your code didn’t have the dash in front of the action and data template. Is there a reason for that?

entity_id: "{{ trigger.payload }}"

Duh… Bingo! It still didn’t work, then I saw that my other actions had entity_id in the line above service, and service with the same amount of spaces that data had, and that was the last piece of the puzzle! Thanks a lot!

So if you have the time, can you please tell me how I can use static text in the variable, in the way I wrote above? Static text like switch.zwaveswitchnumber and then I’d only need the payload to be for instance 4.

Edit: For me the logical would be something like this:
entity_id: "switch.zwavebryter"+"{{ trigger.payload }}"

But that doesn’t work, of course. Couldn’t be that easy. :wink:

entity_id: "switch.zwavbryter{{ trigger.payload }}"

:wink:

Incredible! It was even easier! :joy: Thank you very much! I can get a lot of fun out of this, using the payload for different things!

1 Like

No worries :+1: