Trying to create action in parallel with delays for certain actions that need to occur sequentially

Happy Saturday everyone! I’m trying to work around a problem that I encountered after updating to the latest HA (was several back). Anyways, the light fader scripts started having nonzero division errors. So, I’m trying to figure out how to have an automation that, in parallel, controls multiple lights but has a delay a bit longer than the transition time to turn off the light instead of setting it at 0 (which used to work).

I have two scenarios - one light needing to be turned off after transition or multiple lights turned off and others left dimmed.

Here’s an example where I tried to think it through and my newb mind didn’t get there. Parallel means that all this runs at the same time so the delay and light.turn_off won’t work correctly for the preceding light dimming instance. Just can’t think of how to get around it. Not sure if an automation can have multiple action entries or something. My Googling is weak on this issue I suppose.

  action:
  - parallel:
    - service: script.fade_light_v3
      data:
        light: light.kitchen_main_lights
        end_pct: 5
        transition: 200
    - delay:
        seconds: 300
    - service: light.turn_off
      target:
        entity_id: light.kitchen_main_lights
    - service: script.fade_light_v3
      data:
        light: light.kitchen_under_cabinet
        end_pct: 30
        transition: 180
    - service: light.turn_off
      target:
        entity_id: light.kitchen_sink_light
        seconds: 15
    - service: light.turn_off
      target:
        entity_id: light.kitchen_main_lights

Thank you anyone in advance for setting me straight.

You can call a script like this:

  - service: script.example

Or like this:

  - service: script.turn_on
    target:
      entity_id: script.example

The first version waits for the script to finish before executing the next action, the second one doesn’t.

Reference: Waiting for script to complete

You may wish to leverage that to make things execute the way you want.

Aha that makes sense. So I figure this can be a simple script and keep the other tasks in parallel on the action based on what you provided. I skipped creating a script_2 in the example tutorial.

How far off is my first attempt at making the script?

To drop into scripts.yml:

kitchen_main_to_zero:
  description: Fades kitchen lights and then turns off
  script_1:
    sequence:
#      - service: script.turn_on
#        target:
#          entity_id: script.script_2
      - service: script.fade_light_v3
        data:
          light: light.kitchen_main_lights
          end_pct: 5
          transition: 200
      - delay:
          seconds: 300
      - service: light.turn_off
        target:
          entity_id: light.kitchen_main_lights

Actually, if it’s sequential then I guess the delay isn’t needed if it waits for fader to complete and then turns off the light? Not sure how that would work - script in a script.

Adjustment for the automation:

  action:
  - parallel:
    - service: script.turn_on
      target:
        entity_id: script.kitchen_main_to_zero
    - service: script.fade_light_v3
      data:
        light: light.kitchen_under_cabinet
        end_pct: 30
        transition: 180
    - service: light.turn_off
      target:
        entity_id: light.kitchen_sink_light
        seconds: 15
    - service: light.turn_off
      target:
        entity_id: light.kitchen_main_lights

Well dang, I added it to scripts.yml to see what would happen and system’s log has an error:

Script with object id ‘kitchen_main_to_zero’ could not be validated and has been disabled: extra keys not allowed @ data[‘script_1’]. Got {‘sequence’: [{‘service’: ‘script.fade_light_v3’, ‘data’: {‘light’: ‘light.kitchen_main_lights’, ‘end_pct’: 5, ‘transition’: 200}}, {‘delay’: {‘seconds’: 300}}, {‘service’: ‘light.turn_off’, ‘target’: {‘entity_id’: ‘light.kitchen_main_lights’}}]} required key not provided @ data[‘sequence’]. Got None

Okay, error is gone now in the log by deleting script_1 stuff.

kitchen_main_to_zero:
  description: Fades kitchen lights and then turns off
  sequence:
  - service: script.fade_light_v3
    data:
      light: light.kitchen_main_lights
      end_pct: 5
      transition: 200
  - delay:
      seconds: 300
  - service: light.turn_off
    target:
      entity_id: light.kitchen_main_lights

Maybe this is progress.

EDIT - worked. Had the nonzero error I messaged about elsewhere, but seems that I found some random numbers that don’t cause it…since some real numbers (nonzeroes) were problematic.