I am struggling with the YAML scripting concept quite a bit. Perhaps the decades of thinking in the context of c, c++ and other higher languages are getting in the way. It feels like I am trying to force a square peg through a round hole (perhaps my brain got into a square shepe? )
What I am trying to accomplish is writing a script that is switching loads off after a timeout since last command.
The timeout is expected to vary based on time of the day. (During the daytime the light simply acknowledges the automation and is switched off within seconds while at night the light has additional utility).
I could achieve almost what I want (albeit not in an elegant way) by two independent automations (for each of the lights).
One implements “long” trigger for the night and another automation might be faster switching the light off when sun is above horizon:
alias: Light Watchdog Feit_RGBW_01
description: Turn off after longer time
trigger:
- platform: state
entity_id:
- light.feit_rgbw_01_feit_rgbw_bulb_01
to: "on"
for:
hours: 0
minutes: 30
seconds: 0
condition: {}
action:
- service: light.turn_off
target:
entity_id: light.feit_rgbw_01_feit_rgbw_bulb_01
data: {}
mode: single
and additionally the day-time short timeout:
alias: Light Watchdog Feit_RGBW_01 daytime
description: Turn off after short time during daytime
trigger:
- platform: state
entity_id:
# Seems I cannot use "{{entities}}" in this context? :-(
- light.feit_rgbw_01_feit_rgbw_bulb_01
to: "on"
for:
# would love to use "{{timeout}}" here
hours: 0
minutes: 1
seconds: 0
condition:
- condition: state
entity_id: sun.sun
state: above_horizon
action:
- service: light.turn_off
target:
entity_id: "{{entities}}"
data: {}
mode: single
variables:
entities:
- light.feit_rgbw_01_feit_rgbw_bulb_01
timeout:
- 0
- 1
- 0
- 0
I intended to use list of entities for various lightbulbs to be covered by a common watchdog but that doesn’t look easy (or I just don’t know how to) (unless I instantiate a a++ class, right?).
So I may just copy the watchdog scripts and preferably use a variable for the entity to keep editing a single line.
The bigger problen - not addressed by this approach - is that the light (that has let’s say a timeout of 30 minutes) may be turned on by another automation while it is already on and if that happens (as en example) 29 minutes into the existing timeout the light won’t stay on for another 30 minutes as desired but will switch off in a minute when the previous timeout expires.
I have a dirty workaround for this as well - every automation may first switch the light off before switching it on but this is less than ideal.
I also entertained another approach: Writing a service with fields “entity”, “data” and “timeout” that would turn on the light defined by entity
setting color data
and wait for timeout
before turning the light off.
The idea was hat mode restart
may set new light parameter(s) and start new timeout as intended. I clearly have lack of some fundamentals since I can implement “{{entity}}” in some context and not other. Furthermore - to capitalize on the mode restart
idea I would have to make separate script for each of the lights (but that might be fine).
Any guidance will be much appreciated! I am seeing Message malformed: ...
way too often to be productive by just trying (and on-line YAML linter doesn’t see any fromal issues)> Thanks!