Nesting attribute in expression list

I’m using the Nest integration for my thermostat, and as part of the automations where it is automatically turned down I would like to do a tts.cloud_say notification over my speaker(s). I’m picking a random message from a list (some examples below).

Problem is that the {{ states.climate...temperature }} is actually read out as if it were text, not the parsed value of the attribute.

  - service: script.jarvis
    data:
      message: >-
        {{ [
          "Temp is set to {{ states.climate.nest.attributes.temperature }} degrees celsius.",
          "Setting changed to {{ states.climate.nest.attributes.temperature }} degrees celsius.",
          "We'll be heating to {{ states.climate.nest.attributes.temperature }} degrees celsius."
        ] | random }}

What can be done to parse these nested expressions so the actual value is used?

Try this

- service: script.jarvis
    data:
      message: >
        {{ [ "Temp is set to ", "Setting changed to ", "We'll be heating to " ] | random state_attr('climate.nest', 'temperature') }} degrees celsius.

That should work for the example I supplied, so kudos for that.

There’s always a… but; what I’m after is being able to use it inside of a sentence. In the examples I gave it’s pretty simple, but in other automations I’d like to nest it halfway through a sentence that does not always end the same.

So like this:

- service: script.jarvis
    data:
      message: >
        Any other text you want to start with. {{ [ "Temp is set to ", "Setting changed to ", "We'll be heating to " ] | random state_attr('climate.nest', 'temperature') }} degrees celsius.

No, not really. That is basically the same thing :upside_down_face:

What I’m looking for is more diversity in the deliverance of sentences. So it could be 25 random sentences that all have different structures.

  • {temp} has been reached so it is turned down
  • Now that the door opened, {temp} is too high so I turned it down
  • The thermostat has now been turned down from {temp}
  • Etc…

So basically I want a placeholder in the expression in where I can place a variable of sorts?

I have an answer, but you may move the goalposts again …

Come again?

Each time tom_I has made a suggestion, the requirements change and invalidate the suggestion (i.e. the “goalposts” keep moving).

Not familiar with that expression, not my maiden language… Also not at all trying to move the goalposts, just trying to figure out how to use a parsed value inside of an existing expression. How would you reform my original question in a better way:

What can be done to parse these nested expressions so the actual value is used?

I hope I used the correct terms here… I’ve been struggling with finding the right words.
I understand now that I made the mistake of ending the sentences with the same words, I guess that made it a bit unclear what my goal is.

Also not looking for a quick fix, just trying to learn and understand how to do stuff like this in YAML.

So if you have an answer, that would be much appreciated. And if not, I hope my goal is a bit more clear for others to chime in.

Try this:

  - service: script.jarvis
    data:
      message: >-
        {{ ["Temp is set to TT degrees celsius.",
            "Setting changed to TT degrees celsius.",
            "We'll be heating to TT degrees celsius.",
            "TT has been reached so it is turned down.",
            "Now that the door opened, TT is too high so I turned it down.",
            "The thermostat has now been turned down from TT."
            ] | random | replace('TT', state_attr('climate.nest', 'temperature')) }}
6 Likes

That’s exactly what I was hoping to get!
Thanks a lot @123. Didn’t know you could chain those pipeline commands like that.

1 Like