I’ve been digging around the source code and it appears that I can pass variables to scripts when I call them with script.turn_on. I’m just not sure what the proper format is and how to actually use them in the script.
For example, I’m trying to write a generic script for turning something off automatically after some period of time following when it was turned on. (My kids leave lights on all over the house all time time.)
Here’s a snippet of what I’ve been trying to do:
automation:
- alias: "Turn off upstairs sink light after a delay"
trigger:
platform: state
entity_id: switch.upstairs_bathroo_switch_13
from: "off"
to: "on"
action:
service: script.turn_on
entity_id: script.turn_off_after_delay
data:
desired_delay: "00:00:30"
device_entity_id: switch.upstairs_bathroo_switch_13
script:
turn_off_after_delay:
alias: "Turn something off after a delay"
sequence:
- delay: "{{ variables.desired_delay }}"
- service: homeassistant.turn_off
entity_id: "{{ variables.device_entity_id }}"
This gives me errors like:
Invalid config for [script]: Entity ID {{ XXXX }} is an invalid entity id for dictionary value...
I’m trying to avoid writing one of these scripts for each of my lights. Any suggestions?
The name of the variable should be accessible directly in the template. I don’t think that the entity_id key supports templating though. I’d be happy to be corrected.