Reed Switch, 60 second check until state changes back to close

Hi There,

I have taken a look at a bunch of posts but can’t get my Automation to operate as needed

Aim - I have a side gate that I’ve placed a reed switch on, I’m trying to work out how to make it send an alert every time the gate is left open for longer than 1 minute. I can make the automation run once and it will send an alert but I would like it to keep checking and sending the alert every 60 seconds until the state changes back to close

Code I am trying to use

- id: '1558930450283'
  alias: SideGate
  - platform: time_pattern
    seconds: '/60'
  - condition: state
    entity_id: binary_sensor.esp8266_gate1
    state: 'on'
  - condition: template
    value_template: '{{ ((as_timestamp(now()) - as_timestamp(binary_sensor.esp8266_gate1) | default(0)) | int) > 60 }}'
  - data:
      message: Side Gate is open and should be checked
      title: Warning
    service: notify.smtp

When I compare the above code to others working examples my code is correct but it doesn’t work. I can manually run the automation and it sends the alert email but when I open the side gate the automation doesn’t run

I am sure it is something simple I am just not seeing it

Thanks, Nick

I wonder if it is because the seconds never reach a value divisible by 60. (58, 59, 0, 1, 2, 3…). And yes I know technically 0 is divisible by 60.

Humour me. Try:

- id: '1558930450283'
  alias: SideGate
  - platform: time_pattern
    minutes: '/1'

The way your automation is written it could take longer than 60 seconds (up to 119 seconds) to fire. Consider what happens if you open the gate when your system time is hh:mm:30 . The trigger does not fire for another 30 seconds when the pattern matches. Then the second condition fails (has only been 30 seconds so far), so you have to wait another 60 seconds for the trigger to fire again. That’s 90 seconds since the gate opened before you get a message.

You could use an alert instead, a lot simpler:

Thank you tom_I for your suggestion but it didn’t make a difference, same result

I will take a look at Alert and see if I can make that work

Thanks, Nick

I had a closer look at your conditions. This is incorrect:

Shoudl be

as_timestamp(states.binary_sensor.esp8266_gate1.last_changed) | default(0)) | int)

You will still have the problem of the first alert occurring anywhere between 60 and 119 seconds. But if that’s not an huge issue it should now work.

1 Like

Yes okay, I see that but didn’t know what the “state” and “last_changed” was for so I removed them. After that change, I am now seeing the following in the logs

> 2019-05-27 22:21:00 ERROR (Recorder) [homeassistant.components.recorder] Error saving event: <Event state_changed[L]: entity_id=binary_sensor.esp8266_gate1, old_state=<state binary_sensor.esp8266_gate1=on; friendly_name=ESP8266_gate1, device_class=door @ 2019-05-27T22:17:02.621488+10:00>, new_state=<state binary_sensor.esp8266_gate1=off; friendly_name=ESP8266_gate1, device_class=door @ 2019-05-27T22:21:00.956245+10:00>>

So I will spend some time researching the forums to work out what is going on before I can continue

thanks, Nick

Actually physically use the gate to trigger the automation. If you manually trigger the automation using the development tools there will be no last_changed timestamp for your gate sensor. Thus, the error you see.

It seems my local database was corrupt and needed flushing. After completing that the automation worked as expected

Thank you for your assistance