Script that is switching loads off after a timeout since last command

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? :slight_smile: )

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!

Few more hours of trying and this seems to be workable. This implements the latest thought mentioned earlier. Turning the LED on through a script that waits for the timeout and then switches the light off. Contrary to my expectations this worker script is OK with mode: single while the calling “test” script needs to be in the mode: restart. That may mean that the Test script is actually waiting for the completion of the light_on_for_duration, right?

Am I on the right path?
Perhaps I need to launch the script as a parallel process(?)

alias: light_on_for_duration
description: Set light_rgbw and switch off after timeout
sequence:
  - service: light.turn_on
    target:
      entity_id: "{{entities}}"
    data:
      rgbw_color: "{{light_rgbw}}"
  - delay: "{{light_timeout}}"
  - service: light.turn_off
    target:
      entity_id: "{{entities}}"
    data: {}
mode: single
fields:
  light_rgbw:
    selector:
      text: null
    name: light_rgbw
    description: payload "[r,g,b,w]"
    required: true
  light_timeout:
    selector:
      text: null
    name: timeout
    description: time "hh:mm:ss" to switch off
    required: true
variables:
  entities:
    - light.feit_rgbw_01_feit_rgbw_bulb_01

Test “automation” (manually triggered for now):

alias: Test - Light on for duration
description: Test - Light on for duration
trigger: []
condition: []
action:
  - service: script.1704235368470
    data:
      light_rgbw: "[10,10,10,0]"
      light_timeout: "00:00:10"
mode: restart

Comments, pointers and improvements will be appreciated.
Thanks!