Periodic check of the sensor state

Hello all,

I am trying to send a notification and trigger action if a sensor state is on for 30 minutes. The sample below works great, but how do I check for the sensor state every 30 minutes, not just once?

- id: Close_Garage_If_Open
  alias: Close garage door if open for longer than 30 minutes
  trigger:
    - platform: state
      entity_id: binary_sensor.garage
      to: 'on'
      for:
        minutes: 30
  action:
    - service: notify.telegram
      data:
        message: "Garage Door is Open for more than 30 minutes. Closing now. ({{ states('sensor.datetime') }})"
    - service: switch.turn_on
      entity_id: switch.garage_door

This happens not just once.
The system basically checks every second if the sensor has been in the ‘on’ state for the last 30 minutes.

On another note - might not be valid in your case - just food for thought:
I am always a little concerned when an action like ‘Closing the garage door!’ is carried out purely based on a time trigger.
My garage, e.g. is so small that the back hatch of my not so huge Mini Countryman touches the garage door and it would be a disaster to have the door close while that hatch is open. In other cases kids or toys might be in the way or an animal might get locked into the garage if the door closes automatically.

My recommendation would be to use an alert component that can remind you in defined intervals that the garage door is still open. I don’t need it, but I would imagine that with e.g. the notify.android service one could set up a notification with the option to manually trigger the action via Ariela.

You could use a time trigger every thirty minutes, and check the current state is on and that it was last changed at least 30 minutes ago. That though, will not perform the action at 30 minutes, it could be every 30 minutes starting at a random time.

  trigger:
    - platform: time_pattern
      minutes: '/30'
      seconds: 00
  condition:
    - platform: state
      entity_id: binary_sensor.garage
      state: on
    - condition: template
      value_template: '{{ ((as_timestamp(now()) - as_timestamp(states.binary_sensor.garage.last_changed) | default(0)) | int) > 1800 }}'

Edit: You could make it more complicated and track when the automation was last run, and block it if its less than 30 minutes, and run it every minute.

  trigger:
    - platform: time_pattern
      minutes: '/1'
      seconds: 00
  condition:
    - platform: state
      entity_id: binary_sensor.garage
      state: on
    - condition: template
      value_template: '{{ ((as_timestamp(now()) - as_timestamp(states.binary_sensor.garage.last_changed) | default(0)) | int) >= 1755 }}'
    - condition: template
      value_template: '{{(as_timestamp(now()) - as_timestamp(states.automation.xxx.attributes.last_triggered | default(0)) | int >= 1755 )}}'

So ideally you need to test for those eventualities. (Not saying such test is easy, perhaps an alert (as you suggest) with a pic is best.

I don’t need to :grin: - because I don’t want to close my garage door automatically for a number of reasons :astonished:

1 Like

What if the 30 minute mark was reached? Does it check for the sensor state beyond 30 minute mark? My garage has an optical sensor that prevents the garage from closing, but this check happens on the garage closer level and not in HA. So if there is an obstacle, the garage once started closing will go back to the open position. But from the HA perspective, the action was triggered and the job is done. For some reason it does not keep checking for the sensor state after that.

I guess that depends on how your open/close sensor triggers.

If it shows ‘closed’ only when your door is fully closed, it will not trigger another action when the door closes half way and then opens again.
If it shows closed as soon as the door moves and then goes back to ‘open’ again the 30min interval will be reset.

In the second case, you probably don’t need to do anything. It will try to run the closing action every 30 minutes.

In the first case, it should be enough to add a few more triggers, e.g.

    - platform: state
      entity_id: binary_sensor.garage
      to: 'on'
      for:
        minutes: 45
    - platform: state
      entity_id: binary_sensor.garage
      to: 'on'
      for:
        minutes: 60

Thanks everyone for your input. Here is the working configuration for my setup. I have a reed switch detecting the open/close state of the garage door. So the objective was to check if the garage door is open every 30 minutes (in case the first attempt did not work for example due to something preventing it from closing). This will keep bugging me via Telegram as well until it’s closed.

- id: Close_Garage_If_Open
  alias: Close garage door if open for longer than 30 minutes
  trigger:
    - platform: time_pattern
      minutes: '/30'
  condition:
    - condition: state
      entity_id: binary_sensor.garage
      state: 'on'
    - condition: template
      value_template: '{{ ((as_timestamp(now()) - as_timestamp(states.binary_sensor.garage.last_changed) | default(0)) | int) > 1800 }}'
  action:
    - service: notify.telegram
      data:
        message: "Garage Door has been open for {{ relative_time(states.binary_sensor.garage.last_changed) }}. Closing now. ({{ states('sensor.datetime') }})"
    - service: switch.turn_on
      entity_id: switch.garage_door
1 Like