Using the contents of an inputtext helper in a service

I have an occupancy simulator that turns on random lights at random times when I am marked as “away”.

The randomly generated name of the light (or switch) is stored in an input_text helper so the script knows what item to turn off before getting the next number when the random time expires.

When I arrive “home” a script runs that stops the occupancy simulator script but whatever light was on, remains on. So the “arrived home” script must lookup the name stored in the input_text helper and turn it off.
I have tried various versions of the following but can’t get it to work, can it be done?

action:
  - service: homeassistant.turn_off
    target:
      entity_id: {{ states.input_text.current_random_light.state }}

you forgot to quote the template:

action:
  - service: homeassistant.turn_off
    target:
      entity_id: "{{ states.input_text.current_random_light.state }}"

and it’s usually better to use the states() function to retreive the state instead of “states.xx…”

action:
  - service: homeassistant.turn_off
    target:
      entity_id: "{{ states('input_text.current_random_light') }}"

Note the use of two different styles of quotation marks (single & double).

Oh wow, it works!
Thanks, I was trying all sorts and just couldn’t get the right syntax.
That’s made my day, thanks finity, I’ll leave it here as I’m sure it will help others too.
TNX Bob.

1 Like