Can the MQTT payload be used as a variable in the action?

I have this code:

  • alias: Send modus
    action:
    data_template:
    payload_template: ‘{{trigger.payload}},{{states.climate.termostat8_dry_air.attributes.operation_mode}}’
    topic: eg/Gulvmodus
    service: mqtt.publish
    condition:
    id: ‘1476567232’
    trigger:
    platform: mqtt
    topic: EG/states

As you can see this is triggered by the MQTT message topic EG/Modus. The payload to this is

states.climate.termostat8_dry_air.attributes.operation_mode

(or whatever thermostate I’m looking for the mode for).

And theoretically the output from this should be “Heat, Heat”, since the second part of the action is what I’m sending as the payload. But what really comes is this:

  eg/Gulvmodus states.climate.termostat8_dry_air.attributes.operation_mode,Heat

So it’s sending the actual text of the payload, not the variable I’m trying to call. Sending the payload in {{}} did not change anything. Can somebody please tell me what I’m doing wrong here?

Don’t think that’s possible as, as you’d be in an infinite loop:
Say your payload is A but you set your payload to be [[payload]]+B, your payload now becomes A+B, which means your payload has just changed so it’s becoming A+B+B which means your payload has just changed so it’s becoming A+B+B+B an so on…
Hope that make sense…

Oh, sorry. The MQTT is not sendt from Hass but an external program. So there should be no loop because that MQTT message into Hass is triggered by something else.

I don’t see an issue because you are pushing the payload to another mqtt device. The only thing that you may need to do is piece each piece of the payload out to transfer it. It will most likely get dropped in as a string otherwise. Not sure how HA would handle that on the return. It’s worth a shot.

I’m taking this payload string IN to Hass:

states.climate.termostat8_dry_air.attributes.operation_mode

And what I want to send out again from Hass is the variable that this payload string points to back out on MQTT with the data_template. So when Hass gets that line over in it should send “Heat” out.

oh, I don’t think that will work. Jinja doesn’t have an exec() function like python. So the exec() function in python allows you to execute a string as a line of code. You’d need that function to actually use it as an object because MQTT will only return that as a string to HA.

Now you could parse that string and piece out the information, but this assumes that it will always have the full entity_id including the attribute.

    # replace 'states.climate.termostat8_dry_air.attributes.operation_mode' with the result from the payload
    {% set items = 'states.climate.termostat8_dry_air.attributes.operation_mode'.split('.') %} 
    {% set dummy1, domain, device, dummy2, attr = items %}
    {% set entity_id = '.'.join([domain, device]) %}
    {{ state_attr(entity_id, attr) }}

Where would I put this code? I have only been using Hass for a week or so, and I’m used to EventGhost (Python) and Girder (Lua) so it’s kind of confusing for me…

You’d put that into the data_template you are using for the other mqtt sensor. Do you have existing yaml? I can help you place it in your existing yaml.

Thanks! But MQTT sensor? OK, now you’re confusing me… I’ve never heard about MQTT sensors, only physical sensors. Is this a HA term? And here’s my full code for that automation so far. The second part of the payload is just to see that it’s actually sending something back, so that gives me the corect feedback, which is “Heat” . The first part so far sends the string it receives, so in EventGhost, which receives the MQTT-messages I see:

Topic: Gulvmodus Payload: {states.climate.termostat8_dry_air.attributes.operation_mode}},Heat

One weird thing about that is that there is only one of the wobbly paranthesis there.

- alias: Send modus
  action:
    data_template:
      payload_template: '{{trigger.payload}},{{states.climate.termostat8_dry_air.attributes.operation_mode}}'
      topic: eg/Gulvmodus
    service: mqtt.publish
  condition: []
  id: '1476567232'
  trigger:
    platform: mqtt
    topic: EG/termostatmodus

Ok, so I miss understood your original issue. I must have been tired. Also, MQTT sensors are pretty easy but you aren’t using those, I just used the wrong phase.

Anyways, try this in your configuration:

- alias: Send modus
  action:
    data_template:
      payload_template: '{{trigger.payload}}' , '{{states.climate.termostat8_dry_air.attributes.operation_mode}}'
      topic: eg/Gulvmodus
    service: mqtt.publish
  condition: []
  id: '1476567232'
  trigger:
    platform: mqtt
    topic: EG/termostatmodus

That should analyze both separately and payload_template will be set to a list of 2 items which should look like this: [ ‘Heat’, ‘Heat’ ]

As for mqtt sensors:

Sorry, but that didn’t wor. It sends nothing at all. I’m sending this in the payload in:

states.climate.termostat8_dry_air.attributes.operation_mode

I have also tried this:

{{states.climate.termostat8_dry_air.attributes.operation_mode}}

But none of them gets any response at all.

Ok, try this then:

payload_template: >
   {% set payload = ', '.join([ trigger.payload, states.climate.termostat8_dry_air.attributes.operation_mode]) %}
   {{ payload }}

No, I’m afraid not. It’s still giving me the actual payload, not the variable that the payload should fetch.

trigger.payload will always give you the whole payload. thats what payload is. if you want the entity_id from the trigger, then use trigger.entity_id

I guess I am confused as to what you actually want. Do you want what is triggering the payload? Do you want something inside the payload? Does the payload return the entity_id? Payloads are what you design so i’m taking wild guesses as to what the payload looks like.

Yeah, I think we’re misunderstanding each other here. I was probably unclear. I want the MQTT in topic to trigger the automation, and the automation to parse the variable in the payload {{states.climate.termostat8_dry_air.attributes.operation_mode}} and then send out again on MQTT (back to where the trigger came from) the value of the variable, which for now (until I change the mode on the thermostate) is Heat. So with this stuff it should be able to do it for all the thermostats.

Ah ok, I’m guessing you are populating that topic with some other automation. So if your payload is “states.climate.termostat8_dry_air.attributes.operation_mode”, then you just need to do this to get the operation mode out of it:

- alias: Send modus
  action:
    data_template:
      payload_template: >
        {% set items = trigger.payload.split('.') %} 
        {% set dummy1, domain, device, dummy2, attr = items %}
        {% set entity_id = '.'.join([domain, device]) %}
        {{ state_attr(entity_id, attr) }}
      topic: eg/Gulvmodus
    service: mqtt.publish
  condition: []
  id: '1476567232'
  trigger:
    platform: mqtt
    topic: EG/termostatmodus

Bingo! Thank you very much! :grinning: That worked!

Nice, glad it worked out.

If I could ask for one more thing: Would it be much trouble to amend that code so I only have to send the number (in this case 8) in with MQTT, and then have the code concatenate the rest of the stuff so it ends up with the same states.climate.termostat8_dry_air.attributes.operation_mode ?