Do I need multiple scripts/rules for light on/off at sunset and sunrise?

I’m just getting started with understanding how the automation works with HA, and setting up a basic outside lights on/off script.

So I want my lights to turn on 15 mins before sunset. Then turn off at midnight. Then turn on later the next morning about about 2 hours before sunrise and then back off at sunrise.

Can you do all of that in 1 script/rule? Or do I need to set up each one separately? I read that you case use ‘for’ to change the state for an XX minutes. But can you change the light to ON and then have go off later at a specific time all in 1 rule?

This is the first have for sunset. Do you need 2 separate?

automation:
  - alias: Outside Lights on at Sunset
    trigger:
      platform: sun
      event: sunset
      offset: "-00:15:00"
    action:
      service: switch.turn_on
      entity_id: switch.outside_garage_light_switch
      entity_id: switch.frontporchlight_switch
  - alias: Outside Lights off at 11pm midnight
    trigger:
      platform: time
      at: "23:00:00"
    action:
      service: switch.turn_off
      entity_id: switch.outside_garage_light_switch
      entity_id: switch.frontporchlight_switch

You could use choose blocks to do it all in one, but I would probably just use one for on and one for off. An automation can have multiple triggers, so you would just make your “on” automation trigger at 15 minutes before sunset, and at two hours before sunrise.

FYI this should probably be in the “configuration” section of the forum.

Moved.

Apologies for posting in the wrong place and thank you for moving it.

So I just set up 4 separate ones and should work fine. Just trying to learn the different ways of doing things.

Thanks.

Separate automations is a good way to start. Easy to do, easy to debug.

You can always come back and combine them later when you are more familiar with the advanced automation features like templating.

1 Like