Alert based on sensor duration (Solution at end of first post)

Good morning everyone. I’m no coder or developer but I’m one hell of a tinkerer and I’ve spent the last week or so elbow deep in this. And I’m fairly certain I love it. After figuring out some of the nuances of Python (took me waaaaay too long to figure out that it doesn’t like the TAB key), I’ve got a fairly functional setup going with Wink, Kodi, MQTT/Owntracks, and the like.

I’m now looking into some of the automation stuff and I hit a wall fairly early. Is there a way to trigger an action based on sensor duration? What I am trying to do is send an alert if a certain door is open for more than a set period of time, like 5 minutes.

I’m assuming what I am looking for would be something along the lines of:

alias: 'Garage Door Still Open'
   trigger:
     platform: state
     entity_id: sensor.garage_door
     from: 'closed'
     to: 'open'
   action:
    service: notify.HomeAssistantFE
    data:
      message: 'The garage door is still open'

This currently works to send me an alert whenever the door is opened. Is there a way to add a duration condition to that?

Thanks for any and all help.

SOLUTION BELOW WITH CODE

[code]automation:

  • alias: ‘Garage door still open’
    trigger:
    platform: state
    entity_id: sensor.garage_door
    from: ‘closed’
    to: ‘open’
    action:
    service: homeassistant.turn_on
    entity_id: script.garage_door_alarm

  • alias: ‘Garage Door was closed’
    trigger:
    platform: state
    entity_id: sensor.garage_door
    from: ‘open’
    to: ‘closed’
    action:
    service: homeassistant.turn_off
    entity_id: script.garage_door_alarm

script:
garage_door_alarm:
alias: “Garage door alarm”
sequence:
- delay:
minutes: 5
- execute_service: notify.HomeAssistantFE
service_data:
message: ‘The garage door is still open’
target: ‘channel/EDITEDOUT’[/code]

1 Like

Maybe you can do it with script. With script you can use delays and stop script from automation rule.
Something like this:

alias: 'Garage Door Still Open' trigger: platform: state entity_id: sensor.garage_door from: 'closed' to: 'open' action: service: homeassistant.turn_on entity_id: script.garage_door_alarm alias: 'Garage Door Still Open' trigger: platform: state entity_id: sensor.garage_door to: 'closed' action: service: homeassistant.turn_off entity_id: script.garage_door_alarm script: garage_door_alarm: alias: "Garage door alarm" sequence: - delay: minutes: 5 - execute_service: notify.HomeAssistantFE service_data: message: 'The garage door is still open'
The example may contain errors on layout. And I’m not entirely sure about message thing on script, but something like that should work.

Awesome! Thank you. I’ll play around with it and see if I can get it working with my setup.

It makes me feel better that it requires a script. I’m not as useless as I originally thought haha.

Well I am at the point of keyboard smashing and the like. Here is my current code:

[code] alias: ‘Garage door still open’
trigger:
platform: state
entity_id: sensor.garage_door
from: ‘closed’
to: ‘open’
action:
service: homeassistant.turn_on
entity_id: script.garage_door_alarm

alias: ‘Garage Door was closed’
trigger:
platform: state
entity_id: sensor.garage_door
from: ‘open’
to: ‘closed’
action:
service: homeassistant.turn_off
entity_id: script.garage_door_alarm

script:
garage_door_alarm:
alias: “Garage door alarm”
sequence:
- execute_service: notify.HomeAssistantFE
service_data:
message: ‘The garage door is still open’[/code]

If I manually trigger the script it works. Even with the delay (removed for now to help with testing).

When I close the door I see in the logging section “Garage door was closed has been triggered”. But when I open the door I only get the standard “Garage Door changed to open”. Nothing about the automation triggering and the notification never sends.

Am I missing something stupid here?

Thanks again for any help.

Hmm, I checked quickly your config and I don’t see nothing wrong on that. But then I take copy of config and pasted that to notepad++, at least this way there is between automation rules, on empty line there is some spaces so its not a empty line. If automation rule does not trigger then there is something wrong on that.
Does your config like this:

[code]
automation:

  • alias: ‘Rule 1’
    trigger:
  • alias: ‘Rule 2’
    trigger:[/code]
    With script did you also get message?

Thanks again! I added the hyphen to the start of each automation and everything is working exactly how I wanted it to. :mrgreen:

Great! Can you post solution (with correct layout, sorry about that) or maybe better way is edit first post and add solution to there. This way others can use your solution as example. I think this is basic solution to add some delay for automation rule.

Copy-pasted code added to the first post.

Thanks again.

Using the automation trigger you could easily use the for statement.

Ex: if a state has been y for x time then trigger.

something like:

automation:
  - alias: 'Garage door still open'
    trigger:
      platform: state
      entity_id: sensor.garage_door
      from: 'closed'
      to: 'open'
      for:
         minutes: 5
    action:
      service: notify.HomeAssistantFE
      data:
        message: 'The door is still open'
        target: 'channel/EDITEDOUT'
2 Likes

now there is the alert component

Hi All,

I am in the same boat, needed something like this, your code helped me, however, I want a small iteration, I want the message notification to work after every 5 minutes. So first notification comes after 5 minutes, the second should come again after another 5. This cycle should keep happening until the door is closed. Can anyone assist how this can be achieved?

below is my code which is working …

automation:

  • alias: “front door opened”
    trigger:
    • platform: state
      entity_id: binary_sensor.front_door
      from: ‘off’
      to: ‘on’
      for:
      minutes: 5
      action:
      service: notify.pover
      data:
      message: Front Door Opened since last 5 minutes
1 Like