SIngle UI toggle for two automations

I have a couple wifi switches which I would like to turn on -30:00 before sunset, and turn off at midnight. It currently works with two separate automations. What is the best practice for combining these into a single automation with a single UI toggle?

  • Create a group with both automations and create a third automation to trigger the goup?
  • Create a single automation with multiple triggers? (don’t want to use wait or delay, because time will vary with sunset)
  • Create a script triggered by an automation (having trouble with sun and time platforms in scripts…)

I feel like this should be simpler than it is, but I’m just learning HA…
Thanks!

- id: evening_lights_on
  alias: Evenging Lights On
  trigger:
    - platform: sun
      event: sunset
      offset: -30:00
  action:
    service: switch.turn_on
    entity_id: group.all_switches

- id: evengin_lights_off
  alias: Evenging Lights Off
  trigger:
    - platform: time
      at: 00:00
  action:
    service: switch.turn_off
    entity_id: group.all_switches

You can do this with just one automation. It triggers and turns on the lights like you have it now. Then, add a wait as the next step. This will wait until midnight. Then add the service call to turn the lights off.

The wait needs to wait on something that generates state changes. Unfortunately, the now() function doesn’t do that. But you can create a time sensor that changes every minute and use that.

Here your go. Let me know if you have any questions.

sensor:
  - platform: time_date
    display_options:
      - 'time'
automation:
  - id: evening_lights
    alias: Evening Lights
    trigger:
      platform: sun
      event: sunset
      offset: '-00:30:00'
    action:
      - service: switch.turn_on
        entity_id: group.all_switches
      - wait_template: "{{ is_state('sensor.time', '00:00') }}"
      - service: switch.turn_off
        entity_id: group.all_switches
1 Like

That works perfect, thanks for you help!

1 Like

I had a similar pair automations and your post got me to do the same consolidation but with example from this Reddit post since I used offset with my sunrise/sunset events.