Use value obtained in an earlier action of automation

Didn’t quite know what to title this post, but here’s basically what I want to do. Forgive my pseudo-yaml:

automation:
  - alias: Volume up then back down
    trigger: <whatever>
    action:
      - service: variable.set
        entity_id: variable.previous_volume
        value_template: >
          {{ state_attr('media_player.google_home', 'volumelevel') }}

     - service: media_player.set_volume
       entity_id: media_player.google_home
       value: <something loud>

     - service: tts.google_say
       entity_id: media_player.google_home
       data:
         message: "Some announcement"

     - service: media_player.set_volume
       entity_id: media_player.google_home
       value_template: >
         {{ states('variable.previous_volume') }}

I don’t need “variable.previous_volume” to be accessible outside of that automation. I don’t really want to have to create this variable in any configuration outside this automation. Just a throw-away variable that I can set in a sequence, and use later in the same sequence.

Is there anything similar to this existing in HA today? Or do I have to create an input_number just to be used for the run of this automation, and useless all other times? I don’t particular like this because I’d have to create one for each instance of this pattern, or otherwise run the risk of the value being changed during the run of an automation containing a ‘delay’ or being generally long-running.

Alternatively, I could use the template-style variables, if only we could call services from inside templates. I would love to be able to do something like:

action_template:
  {% set previous_volume = state_attr('media_player.google_home', 'volumelevel') %}
  {% media_player.set_volume('media_player.google_home', '0.75') %}
  {% tts.google_say('media_player.google_home', 'some announcement') %}
  {% media_player.set_volume('media_player.google_home', previous_volume) %}

Just discovered this thread: Custom component to declare/set variables

It may be exactly what I’m looking for.

To whom ever might read this.

In a script you can use {% set variablename = "Hello world" %} to define a variable inside a script or an automation. that vaiablename will only be available in that script or automation.

UPDATE: Or that’s what I thought until @SteveDinn corrected me in the next post.

Unfortunately, this is exactly what I’m getting at.
If I have more than one service called in a script or automation, that variable will only be accessible in that service. Imagine the following script:

script:
  foo:
    sequence:
      - service_template: >
          {% set on_off = <some template logic> %}
          switch.turn_{{ on_off }}
        data:
          entity_id: switch.my_switch
      - service_template: >
          light.turn_{{ on_off }}   <--- Can't use this variable here. You need to calculate it again.
        data:
          entity_id: light.my_light

I know there are other ways to get around this sometimes, but you get the idea.

Edit: fixing script errors

1 Like