I’m stumbling my way though setting up some automated lights, I managed to build a set of Automations that do what I need them to, but then I realized I’m going to be making duplicates of all of those automations for each of my different rooms, so I started looking into scripts, and I’m trying to pass in the relevant switch as a parameter (basically the light.light_name device).
I figured out how to check the basic state using the value template and my field:
- if:
- condition: template
value_template: "{{ is_state(var_lightswitch, 'on') }}"
alias: If var_lightswitch is ON
However now I’m struggling to figure out how to set a condition so that it will only run if the light (represented by the var_lightswitch parameter) has been on for over 2 seconds. I have this YAML from the working Automation, but I can’t figure out how I would turn it into a condition in my script that can take a field as a parameter
- condition: device
type: is_on
device_id: 8b0a28328acd1f1369c8b77b7aaed69f
entity_id: afa23799d89afd7723fc19b85ceae1ba
domain: light
for:
hours: 0
minutes: 0
seconds: 2
If it makes any difference, these are Inovelli Red switches… I’m also curious if there’s some sort of console somewhere that I can use for quickly testing out all of the “state_attr” commands and the like with intellisense, because I have no idea what these objects look like under the hood. I found the Developer Tools > States screen, which is helpful, but nothing there seems to tell me when the light was last turned on… I’m guessing because it’s stored elsewhere, but I have zero idea how to even begin tracking that down.
Full example script that I’m trying to get working (I got the first if statement working, so I’m able to tell if the light I’m passing in is on currently, but the second if statement is where I’m struggling, I can’t figure out the value_template to check if the light has been on for at least 2 seconds):
alias: Temp Override Button Pressed
sequence:
- if:
- condition: template
value_template: "{{ is_state(var_lightswitch, 'on') }}"
then:
- target:
entity_id: "{{ var_light_lockstate }}"
data:
option: Automatic
action: input_select.select_option
else:
- target:
entity_id: "{{ var_light_lockstate }}"
data:
option: Manual
action: input_select.select_option
- if:
- condition: state
entity_id: "{{ var_lightswitch }}"
state: "on"
for:
seconds: 2
then: []
fields:
var_lightswitch:
selector:
device: {}
name: Var_Lightswitch
description: Select the Inovelli light switch object
required: true
example: light.light_hallway
var_light_lockstate:
selector:
entity: {}
name: Var_Light_LockState
description: Select the input_select object that stores the LockState for the light
required: true
example: input_select.light_hallway_lockstate
description: ""