Simpler way to automate my porch light?

I’m just getting started on automations and have been building everything from examples. I built this to automate my porch light. What I want is for the light to automatically come on 30 mins before sunset and turn off at 11pm, then back on at 30 mins before sunrise and off again 1 hour after sunrise. During the time between 11pm and sunrise-30, I want the motion detector to turn the light on for five minutes. What I came up with seems like a lot more code than necessary. Is there a way to streamline this?

automation:
  - alias: Turn on porch light at sunset-30
    trigger:
      platform: sun
      event: sunset
      offset: '-00:30:00'
    action:
      service: light.turn_on
      entity_id: light.porch_light

  - alias: Turn off porch light at 11pm
    trigger:
      platform: time
      after: "11:00:00"
    action:
      service: light.turn_off
      entity_id: light.porch_light

  - alias: Turn on porch light at sunrise-30
    trigger:
      platform: sun
      event: sunrise
      offset: '-00:30:00'
    action:
      service: light.turn_on
      entity_id: light.porch_light

  - alias: Turn off porch light at sunrise+30
    trigger:
      platform: sun
      event: sunrise
      offset: '+00:30:00'
    action:
      service: light.turn_off
      entity_id: light.porch_light

  - alias: Front Porch Motion Light
    trigger:
      platform: state
      entity_id: binary_sensor.front_porch_motion
      to: 'on'
    condition:
      condition: and
      conditions:
        - condition: time
          after: "11:00:01"
        - condition: sun
          before: sunrise
          before_offset: "-0:30:01"
    action:
      service: homeassistant.turn_on
      entity_id: script.timed_light

script:
  timed_light:
    alias: "Turn on light and set a timer"
    sequence:
      # cancel old timers
      - service: script.turn_off
        data:
          entity_id: script.timer_off_5
      - service: light.turn_on
        data:
          entity_id: light.porch_light
      # Set new timer
      - service: script.turn_on
        data:
          entity_id: script.timer_off_5

  timer_off_5:
    alias: "Turn off lamp after 5 minutes"
    sequence:
      - delay:
          minutes: 5
      - service: light.turn_off
        data:
          entity_id: light.porch_light
1 Like

If you want to shorten it you can place the automations for turn on together and have multiple triggers in each ‘on’ automation.

The automation for motion for 10 minutes you could shorten that by adding the delay option in the action section. Add a 5 minute delay and then add the off command. Im not sure how this will work as motion sensors often see a lot of on/off cycles for what would be a single person walking by. Something to try though.

When using multiple triggers or actions start with a hypen (-) before platform and service. The - needs to be at the same indentation level as the platform/service would be in a single option example.

See below for an example.

automation:
  - alias: Turn on porch light
    trigger:
      - platform: sun
        event: sunset
        offset: '-00:30:00'
      - platform: sun
        event: sunrise
        offset: '-00:30:00'
    action:
      service: light.turn_on
      entity_id: light.porch_light
      
  - alias: Front Porch Motion Light
    trigger:
      platform: state
      entity_id: binary_sensor.front_porch_motion
      to: 'on'
    condition:
      condition: and
      conditions:
        - condition: time
          after: "11:00:01"
        - condition: sun
          before: sunrise
          before_offset: "-0:30:01"
    action:
      - service: homeassistant.turn_on
        entity_id: light.porch_light
      - delay: '0:05
      - service: homeassistant.turn_off
        entity_id: light.porch_light
3 Likes

Just one question, aren’t the times supposed to be in 24 hour times?

Yep, in the conditions, replace 11:00:01 with 23:00:01.

1 Like

Good catch, thanks!

Excellent, that does make it a bit cleaner. Thanks

Wouldn’t this create a situation where you’d stack up a bunch of delayed ‘off’ commands if multiple motion events were detected? So the light would always turn off five minutes after the initial motion event and would only come back on if a new motion occurred after that? And then it would get shut off again if there were motion events in the interim?

It seems like that’s the purpose of the script. It resets the timer for new motion events.

Not sure how it will work. It was one to test. Easy to test to as you can manually trigger it.

What if you made a input_boolean that gets turned on when motion is detected, and have an automation on that boolean that after it’s been on for 5 minutes, resets the boolean to off and turns off the light. Would repeatedly setting the boolean to on cause the for logic to restart counting the 5 minutes?

you can do something like this:

script:
  front_door_timed_on:
    sequence:
      - service: script.turn_off
        data:
          entity_id: script.timed_off
      - service: light.turn_on
        data:
          entity_id:
            - light.front_door
      - service: script.turn_on
        data:
          entity_id: script.timed_off

  timed_off:
    sequence:
      - delay:
          minutes: 10
      - service: light.turn_off
        data:
          entity_id: light.front_door