Keep lights on for 10 minutes with no motion

I’m trying to turn off some lights after there is no motion detected for 10 minutes. I seem to be struggling with the format of time in the for: field, as I’m getting the following error:

  • Error rendering ‘Light - Back Deck - Off’ for template: offset ‘00:10:00’ should be format ‘HH:MM’, ‘HH:MM:SS’ or 'HH:MM:SS.F

I’ve tried this 3 ways, no quotes, single quotes and double quotes. None are working.

 alias: Light - Back Deck - Off
  description: ''
  trigger:
  - entity_id: binary_sensor.aarlo_motion_back_deck
    for: 00:10:00
    from: 'on'
    platform: state
    to: 'off'
  - entity_id: binary_sensor.aarlo_motion_pool
    for: '''00:10:00'''
    from: 'on'
    platform: state
    to: 'off'
  - entity_id: binary_sensor.aarlo_motion_side_yard
    for: '"00:10:00"'
    from: 'on'
    platform: state
    to: 'off'
  condition:
  - condition: template
    value_template: '{{is_state(''sun.sun'',''below_horizon'')}}'
  action:
  - device_id: 386aaaa1642f4c229bf626189699fd87
    domain: light
    entity_id: light.ls_master_bedroom_patio
    type: turn_off
  - device_id: 55f1998d972a49d5b109cff8cb654dfa
    domain: light
    entity_id: light.hue_pool_lights
    type: turn_off
  mode: single

Any suggestings?

Not sure why you use all those single and double quotes:

for: '''00:10:00'''
and
for: '"00:10:00"'

Just use single quotes like in the rest of the automation:

for: '00:10:00'

Also, be aware that this will turn the lighs off is any of the sensors have not detected movement for 10 minutes.

I’m not, that’s what HA is doing. For the first camera I put 00:10:00, for the second I put ‘00:10:00’ and for the third I put “00:10:00”. All three error, so I’m not sure what’s wrong.

As for the ANY condition, I’m not sure how to make this work with an ALL. I simplified this a bit for posting, there are also 3 doors that are closed, so the logic would be when all doors have been closed for 10 min and no motion for 10 min then turn off the lights.

Should I change the camera’s to conditions?

You can create a group of binary_sensors, and by default the group turn “off” when all sensors are “off”.

As for the formatting, you would at least have to be consistent. Try a simpler version first, with just one sensor, and when you have the syntax for that in order, you can add more sensors.

I’ll try the group thing. I am consistent with the quotes, I was trying the combinations because they all error. So, for illustration purposes, I showed all three in one automation. In prod, they’re all single quotes in this automation and double in another and none in the third. All fail. What is the correct way to say keep the lights on for 10 minutes? It works great with devices, but these state trigger in the “for” field require something else.

I’ve got the group thing working with contacts and motion, but still errors with the for field.

Example of a more simple example with 1 motion and 1 contact. Goal is to turn off the lights when the door is closed and no motion for 5 min.

In the log:

Error rendering ‘Light - Entryway - Off’ for template: offset ‘00:05:00’ should be format ‘HH:MM’, ‘HH:MM:SS’ or ‘HH:MM:SS.F’

Automation:

- id: '1601152113509'
  alias: Light - Entryway - Off
  description: ''
  trigger:
  - device_id: 73073c1b75b5463aa6555501864ad9a8
    domain: binary_sensor
    entity_id: binary_sensor.front_door_contact
    for:
      hours: 0
      minutes: 5
      seconds: 0
    platform: device
    type: not_opened
  - entity_id: binary_sensor.aarlo_motion_front_entry
    for: '00:05:00'
    from: 'on'
    platform: state
    to: 'off'
  condition:
  - condition: template
    value_template: '{{is_state(''sun.sun'',''below_horizon'')}}'
  action:
  - device_id: 7de6fc89b8944cad9df954ae62ef194c
    domain: light
    entity_id: light.ls_entry
    type: turn_off
  - device_id: e7da1b945d4d4a5fbbe0030aea7d71cf
    domain: light
    entity_id: light.ls_front_porch
    type: turn_off
  mode: single

Note: to get the ‘00:05:00’ in the automation, I had to modify the automations.yaml directly, otherwise HA is adding more quotes, as seen in the post above.

Maybe there is an easier way to do this, or a specific way of denoting time in the “for” field?

Going back to the first post.
This is a strange mixture of GUI and handwritten code
Two things strike me though.
Why are you checking that the sun is down before turning off a lamp, is it okay to keep it on all day if the sun is up ?
The yaml spacing looks off to me (this wouldn’t work for me) where is this code ?
I’m guessing not in a package, and from the looks of it not in automation.yaml either ( no “-” list identifier for the automation (or did you just delete the id: ? But the the yaml spacing would be even screwyer (is that even a word ?) ) where is it?, can you post another working automation from the same file? (to check)

1st, the initial post, as I said, was just to illustrate that the HA Gui is adding extra quotes when I add ‘’ or “” around the time, screwing it up. This is cut directly from the automation.yaml, except I didn’t include the first line, just an oversight. There is no hand coded elements. I was trying to show that no matter how I enter the time in the for field, it’s erroring. I did none, single and double in one automation just to show what it looks like in the automation.yaml.

2nd, the condition of the sun being down is because the lights shouldn’t be turned on during the day. I simply included the same condition for turning them off. If I turn them on during the day for some reason, I’d not want them to turn off in the automation. Just a time limiter for the automation.

As Olen suggested, I’m now using groups, which I’ll post below. Using groups seems to work better, but needs more testing of scenarios. What I’m looking to do is turn on the lights if it’s dark and I open a door. Some times I approach from the back yard and it’s dark, so also turn on the lights. Or, I leave through the door to take dogs out. If I’m out too long, the lights shut off, so turn them back on when I approach. Finally, shut off the lights when all the doors have been closed for 5 min AND when no motion has been detected for 5 min. I actually have a camera in the back yard, so I was hoping me moving around back there would keep the lights on. Again, I need more testing.

Here is the new automation.yaml for using groups for turning the lights on and off based on motion and doors.

- id: '1601330898887'
  alias: TEST - Groups - On
  description: ''
  trigger:
  - entity_id: group.back_deck_motion
    from: 'off'
    platform: state
    to: 'on'
  - entity_id: group.back_deck_doors
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - device_id: 386aaaa1642f4c229bf626189699fd87
    domain: light
    entity_id: light.ls_master_bedroom_patio
    type: turn_on
  mode: single

- id: '1601340142667'
  alias: TEST - Group - Off
  description: ''
  trigger:
  - entity_id: group.back_deck_motion
    for: 00:02:00
    from: 'on'
    platform: state
    to: 'off'
  - entity_id: group.back_deck_doors
    for: 00:02:00
    from: 'on'
    platform: state
    to: 'off'
  condition:
  - condition: state
    entity_id: group.back_deck_motion
    state: 'off'
  - condition: state
    entity_id: group.back_deck_doors
    state: 'off'
  action:
  - device_id: 386aaaa1642f4c229bf626189699fd87
    domain: light
    entity_id: light.ls_master_bedroom_patio
    type: turn_off
  mode: single

This seems to be working, but I’m still testing. Right now, I don’t have any quotes around the time, and I’m also not getting errors about the format. Odd, as what made me go down the quote track was the error saying I needed to follow the format.

I hope I’ve explained this well enough.

In WebCore, I’d simply say if:
IF door1 AND door2 AND door3 are ALL closed AND
motion1 AND motion2 AND motion3 have no motion THEN
turn off light

I can’t see directly how to do this in HA.

Just a note from my earlier post. It appears this is all working now, no special quotes needed. What really seems to have helped, and I do not know why, is in the Off automation, I’ve included conditions that the groups are off, even though the trigger that they’re off has fired. Once I added this, not even sure why I thought of it (probably another poster I read), it all seemed to work.

I have several other automations that are having the time frame issue in the for field, so I’m on to that next.

That is, because as I said, the triggers are “OR”., while the conditions are “AND”.

So your “Off” automation will trigger if either the doors have been closed for two minutes OR there has been no motion for two minutes.
If the doors have been closed for two minutes, but there is still motions outside - or if the motion has stopped but the doors are still open, the automation will trigger, but the condition ensures that the lights will not turn off.

In WebCore, I’d simply say if:
IF door1 AND door2 AND door3 are ALL closed AND
motion1 AND motion2 AND motion3 have no motion THEN
turn off light

Since they are all binray sensors, you could add them all to a group, and it would be behave the same way, but with only one trigger.

Home Assistant is more event-driven (something happens, and we want that to cause something else to happen), while it sounds like WebCore is more state-driven (when we reach a certain state, do something). Different approaches, and they both have their merits.

I’m glad you are making progress, and missing copying the first line makes a bit more sense with what we’ve read. The quotes around times is really weird (I always use single quotes for these, even though technically (to follow convention) I should use double. Both should work). The gui does its own thing but is getting much better. I don’t use it though, just a text editor (though VSC is more than just a text editor)

The problem I have with this is, if it is triggered say 2 minutes before sunrise, it will never go off again until 10 minutes after its triggered again the next night.

Something to consider
:man_shrugging:

I have a motion controlled light in my kitchen.
It has an on to off timer of 150 minutes (for normal use)
But if all the lights are off and motion gets triggered (assuming light levels permit), I set a motion triggered boolean which has a different on to off time (actually motion clear to light off) of 2.5 minutes. when the light goes off the input boolean is reset.

I definitely can’t read those automotions. :slight_smile:
I find Node Red 10 times easier to implement, to read, and to get a global overview of many automotions in a few seconds.
I know this won’t probably help you with your current problem but if haven’t ever try Node Red, maybe it’s worth having a look at it. It’s perfectly integrated in HA.

So your “Off” automation will trigger if either the doors have been closed for two minutes OR there has been no motion for two minutes.

So, the way I did the trigger coupled with condition that they’re all closed is the correct way to have AND triggers? This wasn’t very intuitive, but now that I know it’s workable.

Since they are all binray sensors, you could add them all to a group, and it would be behave the same way, but with only one trigger.

I have the 3 camera’s in one group as their motion and the 3 doors in a group as they’ve all contact. I didn’t think I could put motion and contacts in the same group, but if I can it would simplify it a bit.

The gui does its own thing but is getting much better. I don’t use it though, just a text editor (though VSC is more than just a text editor)

Yes, I’m seeing that. Being new to HA I was trying to not manipulate the yaml files directly, but it seems this is a better way. Currently, I’m using SSH/Vi to edit them.

The problem I have with this is, if it is triggered say 2 minutes before sunrise, it will never go off again until 10 minutes after its triggered again the next night.

This is just really a night light. Sunset isn’t immediately dark here, so I’m ok with a 10min lapse. I’ll see what winter brings and if it’s a problem I’ll change the condition to say -30 before sunset or something.

Actually, after switching to Hubitat from SmartThings, I installed NodeRED to try and use it to queue off of Arlo motion. I couldn’t get this working and haven’t looked at it again. It seems fairly intuitive for middleware, but I’m starting to overload my RPi server with Pi-Hole, Home Assistant, NodeJS and sonos-http-api. I may visit it again, so if you have some good tips/pointers or a site you like please share.

This works for me:

  # Garage Lights ON As Long as There Is Motion - TESTED
  - id: garage_lights_on_when_motion_atm
    alias: Keep Garage Lights ON As Long as There Is Motion (ATM)
    description: Keep Garage Lights ON As Long as There Is Motion
    trigger:
    # Motion Detected in Garage
     -  platform: state
        entity_id: 
          - binary_sensor.motion_garage_bs
          - binary_sensor.motion_garage_2_bs
          - binary_sensor.motion_garage_3_bs
        to: 'on'
    mode: restart
    action:
    - service: switch.turn_on
      data:
        entity_id: switch.zooz_zen21_switch_garage
    - delay: 
        minutes: 10
    - service: switch.turn_off
      data:
        entity_id: switch.zooz_zen21_switch_garage

Hi,

I am still searching also for an elegant solution. But I don’t think this will ever work.

Why? Because it won’t change the status to “on” again from anything during motion in front of the sensor. It will stick with that status all the time.

You can test by yourself. Set the timer down to e.g 20-30 Seconds. Result will be that the light turns off. I was just in front of the sensor and had some fun while jumping up and down meanwhile checking my phone the whole time for the status. The Status was “on”. but the light turned off.

So I am thinking of something like
If state of sensor = on, THEN turn light on, ELSE turn light off. (For sure with IF before sunrise and after sunset. Because otherwise the automation won’t work for turning the lights on between 00:00:00 and let say sunrise at 06:00:00AM )
But since im playing just some days with HA, I am sadly even not sure how to implement this by now.

Cheers

fastboot

Then how do you explain all the people here that have working solutions.
I admit mine is a bit complicated given the dual timing nature’s and the conditions for firing but I recently saw a post by Taras that did it all in one automation

It’s the mode: restart that is responsible for making it work correctly (i.e. GrizzlyAK’s example).

If the mode were single (which is the default) then subsequent triggers would be ignored by the automation while the delay is counting down. In other words, the countdown would not be reset by the detection of additional movement.

1 Like