How do I use a timer in an automation to delay an action with Ring

Hello - I have a Ring Spot Camera that I want to trigger my backyard lights based on motion. I’m a newbie and seeking help to configure the right settings to accomplish this. The goal is to have the lights stray on 15mins and then turn off.

Here’s the info I have in the Configuration.yaml file which turns on the lights based on motion. Now I need to build a timer that shuts the lights off after 15mins of being on.

alias: Backyard Motion Lights
trigger:

entity_id: binary_sensor.backyard_camera_motion
platform: state
condition: []
action:
data:
entity_id: switch.backyard_lights
service: switch.turn_on

I have read through several of the posts in this forum but there are so many ways of doing this that I’m getting confused.

Thanks.

Check these out
Trigger:

And you would use “to:” and “for” parts.

condition:
If you like, understanded not required here

Action:
This part you already handle with the turnin on the light. Just remember to use turn_off.

And here full example, just modify to your needs:

And in the future please use three tick marks before and after your code to highlight syntax and most important identation (```).

Thanks. I was able to trigger the light on but IT does not go off in 5 minutes. Any suggestions?

Here’s my entry in the automatons.yaml

  # Motion detector - Backyard
- alias: Light on - motion
  id: 'Backyard Light'
  initial_state: true
  trigger:
    platform: state
    entity_id: binary_sensor.backyard_camera_motion
    to: 'on'
  action:
    service: switch.turn_on
    entity_id: switch.backyard_lights

- alias: Light off - no motion
  id: 'Backyard Light'
  initial_state: true
  trigger:
    platform: state
    entity_id: binary_sensor.backyard_camera_motion
    to: 'off'
    for:
      minutes: 5
  action:
    service: switch.turn_off
    entity_id: switch.backyard_lights
    
'''

Thx!

You could delay 5 minutes and then turn the lights off.

  # Motion detector - Backyard
- alias: Light on - motion
  id: 'Backyard Light'
  initial_state: true
  trigger:
    platform: state
    entity_id: binary_sensor.backyard_camera_motion
    to: 'on'
  action:
    - service: switch.turn_on
      entity_id: switch.backyard_lights

    - delay:
        minutes: 5

    - service: switch.turn_off
      entity_id: switch.backyard_lights

Thanks for the help. That worked. For extra credit, I thought could add a condition to only trigger after sunset but I’m getting errors when I check the config. Here’s my script with the condition added. Did I miss something?

  # Motion detector - Backyard
- alias: Light on - motion
  id: 'Backyard Light'
  initial_state: true
  trigger:
    platform: state
    entity_id: binary_sensor.backyard_camera_motion
    to: 'on'
    
- condition: sun
  after: sunset
  
  action:
    - service: switch.turn_on
      entity_id: switch.backyard_lights

    - delay:
        minutes: 1

    - service: switch.turn_off
      entity_id: switch.backyard_lights
1 Like

You had this twice. The code is ok but id must be unique. I would suggest not to use id if you are not using the front end automation editor.
So in this case HA doesn’t understand there is also another automation. Same thing with alias if you use the same twice.

For this the identation is wrong. And you are missing one line of code saying “condition”. See here example and added tips :slight_smile:
“trigger:”, “condition:” and “action:” must have same identation.

Nicely formatted the code with the tick marks!

Thanks for the help but I’m struggling here. I looked at my history and the “Sun” section is showing above_horizon and below_horizon.

I google it and saw several posts that referenced this string of code.

      condition:
        condition: state
        entity_id: sun.sun
        state: above_horizon

So I took this info and updated my code to:

- id: '1556842002317'
  alias: 'backyard light on motion'
  initial_state: true
  
  trigger:
   platform: state
   entity_id: binary_sensor.backyard_camera_motion
   to: 'on'
    
  condition:
    condition: state
    entity_id: sun.sun
    state: above_horizon
        
  action:
    - service: switch.turn_on
      entity_id: switch.backyard_lights

    - delay:
        minutes: 1

    - service: switch.turn_off
      entity_id: switch.backyard_lights

The condition does not seem to work because my lights still come on whether I set the state to above or below horizon.

Any help to fix this issue would be much appreciated.

This is easily solved by just modifying the two original automations you created.

Your first automation remains largely unchanged. Motion continues to activate the lights. However, we add a condition to check the sun’s elevation. It must be less than 0 degrees (between sunset and sunrise) to allow the action to be executed.

  # Motion detector - Backyard
- alias: Light on - motion
  id: 'Backyard Light'
  initial_state: true
  trigger:
    platform: state
    entity_id: binary_sensor.backyard_camera_motion
    to: 'on'
  condition:
    - condition: template
      value_template: "{{ state_attr('sun.sun', 'elevation') < 0 }}"
  action:
    service: switch.turn_on
    entity_id: switch.backyard_lights

Your second automation is triggered when the lights have been on for 15 minutes then turns the lights off.

- alias: Light off - 15 minutes
  id: 'Backyard Light'
  initial_state: true
  trigger:
    platform: state
    entity_id: switch.backyard_lights
    to: 'on'
    for:
      minutes: 15
  action:
    service: switch.turn_off
    entity_id: switch.backyard_lights

Thanks 123. I tried your suggestion and it seems to work but I can’t tell because it’s day here.

2 questions:

  1. Is there a way to test this with some sort of setting to fake sunset?
  2. I’m not really understanding how this condition is working. How does the 2nd automation know that the light has been on for 15mins?

I’m new to HA and it’s a very steep learning curve. I’m trying to be sufficient to limit the number of times I need to reach out to the community for help. Any suggestions on ways to learn HA coding?

Thx.

You can temporarily replace the condition with something that you can easily toggle on/off to pretend it’s sunset/sunrise. For example, use some other light in your house (or even a door sensor).

  condition:
    - condition: state
      entity_id: light.whatever
      state: 'off'

So when light.whatever is off, we pretend that’s past sunset and when it’s on it’s past sunrise. This will allow you test the automation without having to wait for real sunset/sunrise.

How does the 2nd automation know that the light has been on for 15mins?

The trigger is effectively saying this:
I will be triggered when the backyard lights have been turned on, and have remained turned on, for at least 15 minutes.

So it won’t trigger at the moment when the backyard lights turn on but only after they’ve been on for at least 15 minutes. Effectively, it’s a 15-minute timer. When the timer expires, it triggers the automation. However, if you turn the backyard lights off while this 15-minute timer is counting down, it’ll cancel the timer. The 15-minute timer will only finish its countdown if the backyard lights remain on for an uninterrupted period of 15 minutes.

Thanks for the explanation – helps me get a better understanding of the trigger function.

Regarding value_template: "{{ state_attr('sun.sun', 'elevation') < 0 }}"

If change the value 0 to any other number would that represent something other than sunset and trigger the lights on?

Are there different degrees of this value (i.e: 0 - 10) the tack the status of the sun?

It’s my understanding that elevation represents the angle of the sun’s position relative to the horizon. So 0 is the horizon, positive numbers indicate the sun is above the horizon and negative numbers mean below it.

There’s a good example in the Cookbook: Automations for lights and blinds based on solar elevation

Thanks for the explanation. It’s evening here and the automation worked.

Glad to hear it works.

You may wish to consider tagging the post as the ‘solution’ so that others can find it quickly in this long thread.

Just tagged it. Thanks again!

Um, yes, well I meant tagging the post that shows the actual solution and not the post confirming the suggested solution works. Anyway, it’s your thread so you’re free to tag it however you see fit.

Done. Let me know if that’s was the right post to mark. I’m a newbie at this.