Turn off 10 minutes prior to sunset and off at 10:30pm

I’m relatively new to automations and need clarification on handling multiple triggers and actions. Ideally, I’d like the following:

  • Turn on outside light 10 minutes prior to sunset
  • Turn off outside light at 10:30pm
  • Turn on outside light if motion is detected outside between 10:30pm and 5:00am
  • Turn off outside light between 10:30pm and 5:00am if motion ceases for 5 minutes
  • 5 minute timer should restart if motion is detected during the 5 minute count down.

Thank you for anyone that can point me in the right direction. I appreciate it.

You should be able to do this in a single automation using Trigger IDs. Trigger IDs let you do things based on what trigger activated the automation.
image
(3 dots on trigger item)

For triggers, you could have something like:

  • Trigger on sunset with -10 offset
  • Time trigger at 10:30pm
  • Trigger on motion
  • Trigger on no motion

Just give each trigger a unique ID.

For conditions, you could have an ‘AND’ statement that if triggered by motion AND time is between 10:30pm & 5:00am, do action. Same for no motion.

A trigger for motion should have a duration value. You can set this to 5min for the “motion ended” trigger. It will automatically reset if there is motion.

Hope this helps!

It can sometimes be helpful to do a separate automation for each action. Then you can temporarily disable the ones you don’t want, those times when your schedule changes a little.

I have some automations I wrote for “away” lighting patterns, but I leave some of them active even when I’m home. Turning a few lights on before sunset is handy, but I’ll turn them off manually. So I leave those “on” automations active and disable the associated “off” automations when I’m home.

Can you pls explain what a Trigger ID is and does ?
I am quite familiar with Triggers in Automations but Trigger ID is not something I know.

Trigger IDs are just a name (any string of characters) given by you to a certain trigger in your automation. It only applies to that automation.

The purpose of Trigger IDs are to allow you to know which trigger in the automation started the automation.

In OPs case, they could give your “10 minutes before sunset” trigger a Trigger ID of “sunset”.
image

You can then create an action that if triggered by “sunset”, turn on light.
image

1 Like

thanks for this explanation

For this part specifically, I would recommend an actual Timer helper, with Restore enabled, rather than trying to trigger on “not motion detection for 5 minutes”. You can then have motion detection events (in your desired time window) just start the timer (which will also reset the duration if it’s already running). The “turn off again” would be a timer.finished event trigger, rather than triggering on a lack of motion state specifically. One benefit there is that, even if HA goes offline for any reason, it’ll turn off the light after the timer runs out (once HA’s running again). It will also simplify the case of motion detection firing at 04:59. It’ll kick the light on, start the timer. At 5:04 the timer will run out, motion after 5am won’t have renewed the timer, and it’ll turn off. If you want motion to keep it going past 5am, you could adjust your conditional on “motion detected, is it in my time window?” to add “or is the timer already running?” … and then it’ll go until there’s no motion well into the morning.

And for that, sunrise/sunset (as well as solar elevation and azimuth) are common enough things of interest that the Sun got its own integration.

  alias: example
  trigger:
    - platform: sun
      event: sunset
      offset: "-00:10:00"
      variables:
        mode: 'on' 
        in_range: 'yes'
    - platform: time
      at: '22:30:00'
      variables:
        mode: 'off' 
        in_range: 'yes'
    - platform: state
      entity_id: binary_sensor.your_motion_detector
      to: 'on'
      from: 'off' 
      variables:
        mode: 'on' 
        in_range: >
          {% set (h,m) = (now().hour, now().minute) %}
          {{ iif((h,m) >= (22,30) or (h,m) < (5,0), 'yes', 'no') }}
    - platform: state
      entity_id: binary_sensor.your_motion_detector
      to: 'off'
      from: 'on'
      for:
        minutes: 5
      variables:
        mode: 'off' 
        in_range: >
          {% set (h,m) = (now().hour, now().minute) %}
          {{ iif((h,m) >= (22,30) or (h,m) < (5,0), 'yes', 'no') }}
  condition: "{{ in_range == 'yes' }}"
  action:
    - service: 'light.turn_{{ mode }}'
      target:
        entity_id: light.your_light

In the automation, change the following to your actual entities:

  • binary_sensor.your_motion_detector
  • light.your_light

Just finished this, ill see how it works tonight

alias: Turn on Outside Holiday Lights
description: ""
trigger:
  - platform: sun
    event: sunset
    offset: "-15:00"
    enabled: true
    id: SunSet
  - platform: time
    at: "22:30:00"
    id: 10:30 pm
  - platform: state
    entity_id:
      - binary_sensor.blink_frontyard_motion_detected
      - binary_sensor.front_door_motion
    to: "on"
    id: motion detected
  - platform: state
    entity_id:
      - binary_sensor.blink_frontyard_motion_detected
      - binary_sensor.front_door_motion
    to: "off"
    id: motion stopped for 5 mins
    for:
      hours: 0
      minutes: 5
      seconds: 0
condition: []
action:
  - if:
      - condition: trigger
        id:
          - SunSet
    then:
      - service: light.turn_on
        data: {}
        target:
          entity_id: light.attic_outlet_on_off
  - if:
      - condition: trigger
        id:
          - 10:30 pm
    then:
      - service: light.turn_off
        data: {}
        target:
          entity_id: light.attic_outlet_on_off
  - if:
      - condition: trigger
        id:
          - motion detected
      - condition: time
        after: "22:30:00"
        before: "05:00:00"
    then:
      - service: light.turn_on
        data: {}
        target:
          entity_id: light.attic_outlet_on_off
  - if:
      - condition: trigger
        id:
          - motion stopped for 5 mins
      - condition: time
        after: "22:30:00"
        before: "05:00:00"
    then:
      - service: light.turn_off
        data: {}
        target:
          entity_id: light.attic_outlet_on_off