Remembering entity from previous action

Hi all,

I currently have a template that changes the lights in my front garden repeatedly until some conditions are not met anymore. This works fine, but as I use a template to define a random entity (out of 6 options) and want to do two color changes to the same entity with each repeat, I need to use a helper to remember which entity I was changing.

My current logic is as follows:
Action 1 - Set input text with random entity:

service: input_text.set_value
target:
  entity_id: input_text.light_select_helper_test
data:
  value: >
    light.{{["fg_door_lamp","fg_spot_catalpa_right","fg_spot_catalpa_left","fg_spot_acer","fg_path_left","fg_path_right"]|random}}

Action 2 - Change the light entity color with a zero second transition:

service: light.turn_on
target:
  entity_id: |
    {{ states('input_text.light_select_helper_test') }}
data:
  transition: 0
  hs_color:
    - 262.798
    - 98.04

Action 3 - Change same light entity color with a long transition:

service: light.turn_on
target:
  entity_id: |
    {{ states('input_text.light_select_helper_test') }}
data:
  hs_color:
    - 27.73
    - 88
  transition: 3.5

Now this works, but needs to use three steps and needs to use an input text helper which I would like to avoid. Is there a way to combine those actions, so that I do not have to use the helper. Is there a way to define a random entity and then use this entity in 2 actions that are triggered directly after each other?

Would love to hear if this is possible or if the way I do it now is the best/only possible?

If all of this is within one script / automation you can use a variable instead of the input_text.

  • so instead of Action 1 you do:
- variables:
     light_entity:  'light.{{["fg_door_lamp","fg_spot_catalpa_right","fg_spot_catalpa_left","fg_spot_acer","fg_path_left","fg_path_right"]|random}}'

and use {{ light_entity}} wherever you call it later :slight_smile:

Thanks, I completely missed that on the Scripts info-page.
I do think this doesn’t work for me yet, as I have this repeat in 1 script, and every time it loops it needs to get a new random entity. The variable you mention is set once per script and stays like that until the script is executed again.
I don’t see a way to set a variable every time the loop repeats?

variables are local…not global.
So if you set the variable WITHIN the loop it is set newly every time :wink:

Wow great! I just got the time to implement it and it works flawlessly. Thanks for those quick and clear responses!