Is it possible to nest variables in payload?

I have a variable here:

{{trigger.from_state.attributes.friendly_name.split(" ")[1]}}

The trigger is this (with different numbers for different sensors, of course):

sensor.termostat8_temperature

And the friendly name is Termostat 8, so the result is the number 8, or whatever number that particular thermostat has. But then there’s a variable in another entity which also comes from the same thermostats:

{{states.climate.Termostat8_dry_air.temperature}}

And I would like to get that into the same MQTT message. Which is why I tried this, which of course didn’t work:

{{states.sensor.termostat{{trigger.from_state.attributes.friendly_name.split(" ")[1]}}_temperature_dry_air.attributes.node_id}}

So the nested variable should in my attempt return a number (for this sensor that would be 8) that fills up the variable, so it becomes like this:
{{states.sensor.termostat8_temperature.attributes.node_id}}

Does the failure mean that I can’t nest variables like this (which I am used to doing in EventGhost, Girder and Node-RED) or is it (crossing my fingers) my syntax that’s messed up? The error I get is this, which doesn’t really tell me all that much, since I’ve been using Hass for a week…

Invalid config for [automation]: invalid template (TemplateSyntaxError: expected token ‘end of print statement’, got ‘{’) for dictionary value @ data[‘action’][0][‘data_template’][‘payload_template’]. Got None. (See /home/homeassistant/.homeassistant/configuration.yaml, line 90). Please check the docs at Automation - Home Assistant

Just so I get this right. You’re trying to send a payload with template data yes?
can you post your automation? That would help understand a bit more.
but for now you could try to build your payload with a variable:

{% set test='states.sensor.termostat'+trigger.from_state.attributes.friendly_name.split(" ")[1]+'trigger.from_state.attributes.friendly_name.split(" ")[1]' %}

You payload would then look like:

  action:
    - service: mqtt.publish
      data_template:
        topic: "topic goes here"
        payload_template: "{{test}}"

Thanks for answering! I know when I’m on deep water. :slight_smile: But I can see that you know this. Here is my automation, just so you can see that before I try it on somet5hing that maybe is different then what you thought:

- alias: Floor temperature on all sensors
  action:
    data_template:
      payload_template: '{{trigger.from_state.attributes.friendly_name.split(" ")[1]}},{{trigger.from_state.attributes.node_id}},{{trigger.from_state.state}},{{states.sensor.termostat{{trigger.from_state.attributes.friendly_name.split(" ")[1]}}_temperature_dry_air.attributes.node_id}}'
      topic: eg/Gulvtemperatur
    service: mqtt.publish
  condition: []
  id: '1105425764'
  trigger:
  - entity_id: sensor.termostat1_temperature
    platform: state
  - entity_id: sensor.termostat2_temperature
    platform: state
  - entity_id: sensor.termostat3_temperature
    platform: state
  - entity_id: sensor.termostat4_temperature
    platform: state
  - entity_id: sensor.termostat5_temperature
    platform: state
  - entity_id: sensor.termostat6_temperature
    platform: state
  - entity_id: sensor.termostat7_temperature
    platform: state
  - entity_id: sensor.termostat8_temperature
    platform: state
  - entity_id: sensor.termostat9_temperature
    platform: state
  - entity_id: sensor.termostat10_temperature
    platform: state
  - entity_id: sensor.termostat11_temperature
    platform: state
  - entity_id: sensor.termostat12_temperature
    platform: state
  - entity_id: sensor.termostat13_temperature
    platform: state
  - entity_id: sensor.termostat14_temperature
    platform: state

Thanks. Can you also post an example of the payload that you expect to see/post?

Embarassing… I see that I have put in a wrong result! What I’m looking for is this:

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

The result of that is either ‘Cool’, ‘Heat Econ’, ‘Heat’ or ‘Off’.

And what I would like to see in EventGhost, which is going to receive this, is the topic:
eg/Gulvtemperatur
and the payload:
8,3,24.0,Heat
Or whatever the values say. They are the result of the split, which is the number of the thermostat (8), the node ID in ZWave (3), the temperature from the floor sensor (24.0) and the operating mode (Heat).

ok try this:
I think that should work…

  action:
    data_template:
      payload_template: >-
        {[- 
        trigger.from_state.attributes.friendly_name.split(" ")[1]
        +','+
        trigger.from_state.attributes.node_id
        +','+
        trigger.from_state.state
        +','+
        trigger.from_state.attributes.operation_mode
        -}}
      topic: eg/Gulvtemperatur
    service: mqtt.publish

I can’t get it to work, I’m afraid. I get no error messages in Hass and it’s sending, but the message is like this:

eg/Gulvtemperatur {[- trigger.from_state.attributes.friendly_name.split(" ")[1] +','+ trigger.from_state.attributes.node_id +','+ trigger.from_state.state +','+ trigger.from_state.attributes.operation_mode -}}

So it’s sending the text in the code, not variables. Here’s the full code now:

- alias: Gulvtemperatur alle sensorer - ny verson
  action:
    data_template:
      payload_template: >-
        {[- 
        trigger.from_state.attributes.friendly_name.split(" ")[1]
        +','+
        trigger.from_state.attributes.node_id
        +','+
        trigger.from_state.state
        +','+
        trigger.from_state.attributes.operation_mode
        -}}
      topic: eg/Gulvtemperatur
    service: mqtt.publish
  condition: []
  id: '11025764'
  trigger:
  - entity_id: sensor.termostat1_temperature
    platform: state

Also another question: Is it possible to consentrate it on one line, or will that mess up the payload parsing?

ah forgot quotes…
you may need to concatenate everything in a single line…

- alias: Gulvtemperatur alle sensorer - ny verson
  action:
    data_template:
      payload_template: "{[- trigger.from_state.attributes.friendly_name.split(" ")[1]+','+trigger.from_state.attributes.node_id+','+trigger.from_state.state+','+trigger.from_state.attributes.operation_mode -}}"
      topic: eg/Gulvtemperatur
    service: mqtt.publish
  condition: []
  id: '11025764'
  trigger:
  - entity_id: sensor.termostat1_temperature
    platform: state

I’ve no payload templates on my own install, but do they usally start with {[- and end with -}}

Should all the brackets be squiggly?

above post will simply publish 8,3,24.0,Heat as per above requirement/request.
Can always add ‘{{’ at the beginning and ‘}}’ at the end.
I’m guessing you’re referring to JSON format, in which case this is quite different than above request…

1 Like

I may be doing something wrong here. The payload isn’t working , but it is probably my mistake somewhere. The language is incredibly ticklish, and now it has stopped Hass from loading at all. Commenting out the ayload line makes it load, so there is something iun that line that isn’t working. :frowning: I have double and tripple checked the indentation, that’s very specific in this language. Here’s the line as it is now:

payload_template: "{[- trigger.from_state.attributes.friendly_name.split(" ")[1]+','+trigger.from_state.attributes.node_id+','+trigger.from_state.state+','+trigger.from_state.attributes.operation_mode -}}"

OK, I see a bigger problem here. The line does not take out the Heat part, because that’s not in the same entity. As I showed the trigger is sensor.termostat8_temperature, and what I need is something that can take out climate.termostat8_dry_air.attributes.operation_mode So it’s another entity, the common thing is the number 8. So what I was looking for was a way to use the 8 (which comes out of the split code) to get a variable from the other entity. I thought I made it clear, but I’m probably not clear enough. Sorry.

payload_template: "{[ should be payload_template: "{{
You need 2 curly braces, not a curly+square :wink:
what I provided above is a way to concatenate strings. so all you need to do is to list what you want to keep in a string (in single quotes), add a +, your template that retrieves the 8 (or other number) then another + and the rest of your string:
'beginning of string'+state.entry.to.retrieve.number+'end of string'

I’m not sure any more what state or attribute you’re trying to retrieve, but basically it’ll look something like that for 1 sensor:
'states.sensor.termostat'+trigger.from_state.attributes.friendly_name.split(" ")[1]+'_dry_air.attributes.operation_mode'

That is the one I’ve beem struggling with, really. So if I could get that one working, it should be possible for me to get the rest by myself (I hope…). But when I use this line, which is how I understand that you mean (and I’m probably misunderstanding), Hass won’t even start:

payload_template: '{{'states.sensor.termostat'+trigger.from_state.attributes.friendly_name.split(" ")[1]+'_dry_air.attributes.operation_mode'}}'

I’m fighting with the problem line. I have tried all placements of the quotes and double quotes I can think of. I have tried this, which gives nothing:

payload_template: '{{- states.climate.termostat+trigger.from_state.attributes.friendly_name.split(" ")[1]+_dry_air.attributes.operation_mode -}}'

And I’m still trying to make it invoke the entity attribute

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

Which is in climate, not sensor, where the trigger comes from.

But this one gave an interesting result:

      payload_template: '{{- "states.climate.termostat"+trigger.from_state.attributes.friendly_name.split(" ")[1]+"_dry_air.attributes.operation_mode" -}}'

It said:

 Invalid config for [automation]: required key not provided @ data['action']. Got None required key not provided @ data['trigger']. Got None. (See /home/homeassistant/.homeassistant/configuration.yaml, line 90). Please check the docs at https://home-assistant.io/components/automation/ Invalid config for [automation]: required key not provided @ data['trigger']. Got None. (See /home/homeassistant/.homeassistant/configuration.yaml, line 90).

Line 90 is invoking groups. Weird…

I’m trying a different approach too in case this is impossible, opening a new thread for that.

that won’t work as you use a single line on the outer.
Needs to be this way:
payload_template: "{{'states.sensor.termostat'+trigger.from_state.attributes.friendly_name.split(" ")[1]+'_dry_air.attributes.operation_mode'}}"
" encases the whole template which is made of strings (enclosed in ') and values

Actually you may want to replace it with this:
payload_template: "{{-'states.sensor.termostat'+trigger.from_state.attributes.friendly_name.split(" ")[1]+'_dry_air.attributes.operation_mode'-}}"

The additional - will remove blank spaces…

It locks up Hass for me. But I think it’s the indentations. The forum software is totally hopeless, because it eats the indentations when using the formatting. And I can’t paste my code, because it gets changed with bullet points and stuff, no matter what I do right now! So here is the code I have at the moment as a txt file, does that look right? It can’t be since it locks up.Code.yaml (296 Bytes)

I’ll try pasting the code again in the message, now with Chrome:

- alias: Gulvtemperatur med modus
- action:
    data_template:
      payload_template:
        "{{-'states.sensor.termostat'+trigger.from_state.attributes.friendly_name.split(" 
        ")[1]+'_dry_air.attributes.operation_mode'-}}"
      topic: eg/Gulvtemperatur
    service: mqtt.publish

And that seems to work…