Trigger automation at specific time if conditions are met, otherwise wait until met

Thank you, oops. I can see that wouldn’t be helping me at all.

Well that certainly made a difference.
the automation triggered @ 22:00.
Unfortunately it triggered while I was moving in the room with the PIR in it, and it still turned the lights off.
I’ll have a bit more of a deeper dive on the binary_sensor to see what it’s reported state is…

Thanks again.

Hi Again,
Okay, so this works however the behaviour isn’t quite what I was after.
It appears, the wait_template makes it’s assessment at a point in time (when the timer expires I guess).
If at that instant both motion sensors are off, then make the binary_sensor ‘on’.
If we’re not moving at that point in time (but was for the last ten mintes) it still resets the binary_sensor, even though i’m in the room.

Is it possible to have a trigger to be say 22:00 AND (it’s an OR I believe) binary_sensor from ‘off’ to ‘on’ for 00:20:00 like you can for a condition?

Otherwise If I make my trigger the binary_sensor the condition based on time will need to be start and end time (which has the problem already discussed in this thread).?

Could I use the timeout function in the Wait Action? Would that help?

@pnbruckner, @tom_l Wouldn’t this do what he’s looking for?

@CrouchingWorm Maybe give it a whirl and see if it does what you want.

alias: TurnDailyLightsOFF2

trigger:
  # run every 10 minutes
  - platform: time_pattern
    minutes: '/10'

condition:
  # run between 10pm and 2am
  - condition: time
    after: '22:00:00'
    before: '02:00:00'

  # run when the no motion sensor is on
  - condition: state
    entity_id: binary_sensor.dailylights_no_motion_xmin
    state: 'on'

action:
  # turn off all the stuff
  - service: homeassistant.turn_off
    entity_id:
     - group.dailyplugs
     - group.dailylights
     - light.spare_room

Thanks, I’ll load it up and will let you know.
Thanks for taking the time.

Here’s an approach that doesn’t use a Time Pattern Trigger.

This first automation turns off the lights when ‘no_motion’ binary_sensor changes to on.

- alias: 'Turn Lights Off'
  trigger:
    platform: state
    entity_id: binary_sensor.dailylights_no_motion_xmin
    to: 'on'
  action:
    service: homeassistant.turn_off
    entity_id:
      - group.dailyplugs
      - group.dailylights
      - light.spare_room

This second automation enables/disables the first automation at 22:00 and 02:00, respectively. In addition, at the scheduled hour, it turns off the lights but only if ‘no-motion’ is ‘on’.

- alias: 'Turn DailyLights Off'
  trigger:
  - platform: time
    at: '22:00:00'
  - platform: time
    at: '02:00:00'
  action:
  - service_template: "automation.turn_{{ 'on' if trigger.now.hour == 22 else 'off' }}"
    entity_id: automation.turn_lights_off
  - condition:
      condition: template
      value_template: "{{ is_state('binary_sensor.dailylights_no_motion_xmin', 'on' }}"
  - service: homeassistant.turn_off
    entity_id:
      - group.dailyplugs
      - group.dailylights
      - light.spare_room

EDIT
Removed:

    for: '00:10:00'

from first automation. The binary_sensor already waits 7 minutes before declaring a state of ‘no_motion’ so there’s no need to wait 10 minutes in the automation.

There is probably a better way to do this now that I think about it. Take the time pattern trigger out and replace it with a time trigger for 10pm and add a trigger for the no-motion sensor turning on. The 10pm trigger is necessary because if the no-motion sensor is already on at 10pm it won’t trigger the automation because it is looking for the sensor to turn on. The condition for the no-motion sensor being on prevents everything from turning off here if motion is still detected (the situation you ran into previously).

Doing it this way there would be no delay in turning everything off after the no-motion sensor turn on and you won’t have the automation firing every 10 minutes.

So anytime between 10pm and 2am everything should shut off after 7 minutes of no motion. It should also not fire if you’ve triggered a motion sensor within the 7 minute delay on the binary sensor.

BTW I put 22:00:01 in the trigger on purpose to be safe. I’m not exactly sure what the behaviour is of a time trigger firing exactly at the same time as the after parameter of the time condition. Perhaps if someone more knowledgeable than myself sees this they can jump in and clarify.

trigger:
  # run at 10 pm
  - platform: time
    at: '22:00:01'

  # run whenever the no-motion sensor turns on (7 min delay)
  - platform: state
    entity_id: binary_sensor.dailylights_no_motion_xmin
    state: 'on'

condition:
  # only run between 10pm and 2am
  - condition: time
    after: '22:00:00'
    before: '02:00:00'

  # only run when the no motion sensor is on
  - condition: state
    entity_id: binary_sensor.dailylights_no_motion_xmin
    state: 'on'

action:
  # turn off all the stuff
  - service: homeassistant.turn_off
    entity_id:
     - group.dailyplugs
     - group.dailylights
     - light.spare_room
1 Like

Ha you beat me too it. I was also working on getting the time pattern out of there. I just put up a better solution than my first one I think. I believe it accomplishes the goal at hand without having two automations? Did I miss something?

I don’t think so.

I can think of only two minor differences:

  • By disabling the first automation, it isn’t triggered by the binary_sensor outside the time range (i.e. no processing outside of 22:00 to 02:00).
  • The lights are explicitly turned off at 02:00 (unless there’s motion detected).

As you can see, the differences are minor indeed.

One of the issues with using the 22:00 to 02:00 time-range is if I’ve manually turned on lights after they’ve been turned off via this automation, the automation will always be active until 02:00, probably continually fighting with me to turn them off, even though I’ve manually toggled them on.

I’m using Hue PIR’s, as they’re battery operated the polling cycle isn’t brilliant (can be a few seconds before they register motion), as such the behaviour isn’t always what I’d expect. Sometimes it hasn’t sensed me when I think it should have. This is adding some behavioural confusion I think.

I’ll build this out and will experiment.
Thanks!

The behavior you described isn’t unique to the example I provided. All other proposals will do the same thing because they employ a condition that makes them active during the 22:00 to 02:00 time-range.

All proposals are designed according to your original requirements, namely to turn the light off after no motion is detected for a several minutes and only during certain hours.

1 Like

You’ve now added another layer of complexity. I actually have a quick and dirty solution for this that I hacked together a long time ago that involves booleans, timers, and goups to indicate when a user has manually changed a device (on or off) so automations leave it alone for the specified duration.

While it works “good enough” for my needs it isn’t very flexible. In this case if it’s just this one area you’re worried about just create a timer for however long you want the lights to stay on that starts if someone turns on the lights you’re talking about after 10pm. Then put a condition in your motion automation that says if the timer is active don’t run the automation. This will stop the motion sensors from turning off the lights for as long as you set the timer duration to be.

Edit: You could also add another automation that will cancel the timer if you turn the lights off manually before it expires so the motion automation will kick back in.

I think a better solution to accomplish what you want might be this though. It’s available in HACS.

I’ll have a look through that comprehensive post!
I’m somewhat glad it is indeed complex to achieve a seemingly simple outcome, I’m not going nuts or overthinking the logic and constraints. Add to this my limited knowledge of what’s possible in the platform.

This community is awesome!
Many thanks.

Hi again,
I’ve ended up settling with @pnbruckner suggestion. I can live with the slight nuances of it.

Thanks everyone.

I didn’t have any luck using the homeassistant.turn_on(off)…
If I changed my config back to light.turn_on(off) it worked fine.
So that’s what I’m doing.

Thanks again.