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

Hi folks,
This has probably been covered, but couldn’t see it (my poor searching likely).
I have an automation I’d like to
run at a specified time (turn a bunch of stuff off/on) based on time of day.
BUT I only want that to run if no motion is detected (for say 7minutes) from 2x PIR sensors (up & downstairs).
If condition (2 x PIR’s clear) is not met, then wait say 10 minutes, then check again (or however many times needed), until clear.
Once clear execute turn stuff off/on.

Seems simple, have the parts working no problem, but can’t seem to combine them into a single script/automation…

I’ve looked at this too long and am no longer thinking sensibly.

Clues?

trigger:
  -  platform: time_pattern # trigger every ten minutes
      minutes: "/10"
  -  platform: time
     at: '10:10:00'  # Also trigger at 10:10am, or whatever time you want.
condition:
  - condition: state
    entity_id: sensor.movement_1 # must be no movement here for at least 7 minutes
    state: 'off'
    for:
      minutes: 7
  - condition: state
    entity_id: sensor.movement_2 # AND must be no movement here for at least 7 minutes
    state: 'off'
    for:
      minutes: 7
  - condition: time # AND must be after your specified time (minus 1 second to be sure the 10:10 trigger fires)
    after: '10:09:59'
action:
  service: do_your_stuff

Thank you.
I’ll give that a punt - will let you know.

I’m using the automation front end, I simply don’t know what’s possible without reading a tonne of stuff and other people’s posts (which is fine, but I end up in circles).

I likes - thanks again.

Note that after 10:10am will only be true until midnight, i.e. 10:10:00 to 00:00:00, if you need it to run longer say to 2 am, add a before condition as well.

  - condition: time
    after: '10:09:59'
    before: '02:00:00'
1 Like

Unless you do something else, won’t this run the action every 10 minutes once there’s no motion for 7 minutes?

If I understand what’s wanted, I think the easiest way to implement this is to add a Template Binary Sensor that indicates when there has been no motion from either motion sensor for 7 minutes. Then use that in a wait_template.

So something like this for the binary sensor:

binary_sensor:
  - platform: template
    sensors:
      no_motion_7min:
        value_template: >
          {{ is_state('binary_sensor.motion_1', 'off') and
             is_state('binary_sensor.motion_2', 'off) }}
        delay_on:
          minutes: 7

And something like this for the automation:

automation:
  - trigger:
      platform: time
      at: 'HH:MM:SS'
    action:
      - wait_template: "{{ is_state('binary_sensor.no_motion_7min', 'on') }}"
      # The rest of the actions go here...
3 Likes

Unless you do something else, won’t this run the action every 10 minutes once there’s no motion for 7 minutes?

That’s what this is for:

  - condition: time # AND must be after your specified time (minus 1 second to be sure the 10:10 trigger fires)
    after: '10:09:59'

That just means it will repeat every 10 minutes only from 10:10 until midnight.

Which does not matter if just turning something off.

Unless you want to be able to manually turn it back on, or there’s another automation that wants to, during that time period.

@tom_l I implemented your suggestion last night - actions worked a treat - Thanks!

I never considered the re-occurring turning off until midnight aspect…
I didn’t test for that.

I’ll save that automation and will build per @pnbruckner’s suggestion and test.
Thanks again folks - this community is awesome. I learn with every interaction.

Yeah Phil’s solution is more elegant. I’d go with that.

I’ve configured it - I’ll check it out tonight.
Wonderful.

@pnbruckner Seems to have done the trick with your suggestion (they both worked though which is fab).

Could I ask though… what does the “>” do on in the case of “value_template: >” ?

1 Like

See:

https://yaml-multiline.info/

1 Like

Hi @pnbruckner and @tom_l I seem to have a problem…

I had to do a restore of my environment due to an update which broke a bunch of my Hue sensors on my bridges.
Unsurprisingly, I hadn’t captured a full snapshot, so had to redo a bunch of stuff.

When I get up in the morning all of my ‘dailylights’ are still on, and have been since the rebuild (i.e the wai_template doesn’t seem to be working for me anymore).

This is the excerpt of the configuration.yaml
#this was to check my two motion sensors we idle for 7 minutes.

binary_sensor:
  - platform: template
    sensors:
      dailylights_no_motion_xmin:
        value_template: >
          {{ is_state('binary_sensor.livinglight_sensor_motion', 'off') and
             is_state('binary_sensor.entertainmentlight_sensor_motion', 'off') }}
        delay_on:
          minutes: 7

And automations.yaml

  alias: TurnDailyLightsOFF2
  description: ''
  trigger:
  - at: '22:00:00'
    platform: time
  condition: []
  action:
  - wait_template: "{{ is_state('dailylights_no_motion_xmin', 'on') }}"
  - data:
      entity_id: group.dailyplugs
    service: switch.turn_off
  - data:
      entity_id: group.dailylights, light.spare_room
    service: light.turn_off

I’ve seen in the logs that it fires @ 22:00…

Any assistance would be greatly appreciated.

Please format your pasted config correctly.

EDIT: Thanks, much easier to see what is going on. Looks ok (disregarding automation editor mangling).

Have you checked that the automation is on?

Have you checked your developer tools states menu to see if the entity ids have changed?

1 Like

I’ll double check the entity Ids when I get home.
Appreciate it.
Cheers,

Also, at a minimum, this:

  - wait_template: "{{ is_state('dailylights_no_motion_xmin', 'on') }}"

should be:

  - wait_template: "{{ is_state('binary_sensor.dailylights_no_motion_xmin', 'on') }}"
1 Like

And, FWIW, this:

  - data:
      entity_id: group.dailyplugs
    service: switch.turn_off
  - data:
      entity_id: group.dailylights, light.spare_room
    service: light.turn_off

could be:

  - data:
      entity_id: group.dailyplugs, group.dailylights, light.spare_room
    service: homeassistant.turn_off

oooo!
Nice.
I’ll hop to it.
Thanks.