Conditional automation based on whether switch has been activated in the last 5min or not

Hi all,

Sorry if this seems obvious but I am running around in circles, reading through posts of people looking at similar things, yet I can’t get anything to work here. I am looking for an elegant and simple solution, here’s my problem:

Basically I have an automation that opens my gate when I get inside the defined zone. However, at times the phone is a little slow to update location, and I end up having to open my gate with the old remote. I’ll drive in, close the gate, and then on occasion the automation will then finally be triggered, re-opening the gate…

I’ve been looking to add a condition to the automation : I need to make sure the gate status (open/close) has not been changed in the last 10min for the automation to continue.

I’ve tried helpers, I’ve tried a template (within template.yaml?) ; I’ve tried getting time/date… So far all failed miserably. “Gate status” does not have a “last_triggered” parameter.

What’s the simplest way to solve this?

Thank you for any input you may have.

1 Like

It would be helpful if you posted the automation, or at least described the available inputs… how does the switch mentioned in the title relate to the gate? Does the gate have a sensor that indicates when it is open or closed?

Generally speaking, a switch entity will have a last_changed property that indicates when the state last changed, this information can be accessed either indirectly by adding a for to your State condition OR directly in a template by using the state object method (states.switch.example.last_changed).

Sure thing, here goes:

alias: Gate open
description: ""
triggers:
  - trigger: zone
    entity_id: person.xx
    zone: zone.home
    event: enter
  - trigger: zone
    entity_id: person.yy
    zone: zone.home
    event: enter
    enabled: true
conditions:
  - condition: state
    entity_id: binary_sensor.gate_intrusion_2
    state: "off"
  - alias: Automation has not run in the last 10min
    condition: template
    value_template: >-
      {{ state_attr('automation.open_gate','last_triggered') is none or
      (now()-state_attr('automation.open_gate','last_triggered')).total_seconds()
      > 600 }}
  - condition: template
    value_template: >-
      {{ now() - state_attr(this.entity_id, 'last_triggered') >
      timedelta(minutes=10) }}
    enabled: false
  - condition: template
    value_template: >-
      {{ state_attr('binary_sensor.gate_intrusion_2','last_triggered') is none
      or
      (now()-state_attr('binary_sensor.gate_intrusion_2','last_triggered')).total_seconds()
      > 600 }}
    enabled: false
actions:
  - type: turn_on
    device_id: 73cc7b85ade117438821d83d0ceef2f0
    entity_id: 07c1d7a28ff1f96ede76211137d2480e
    domain: switch
  - action: notify.mobile_app_phone
    metadata: {}
    data:
      message: Gate opened automatically - We are on our way home
  - delay:
      hours: 0
      minutes: 5
      seconds: 0
    enabled: false
mode: single

You can tell by the code my different approaches. Currently the automation.last_triggered works a treat, but of course if I open the gate with the physical remote, that logic no longer works.

This is the sensor “states” info, no last_triggered. This sensor detects any gate opening.

Thank you for your advice in finding the best logic here.

  - condition: state
    entity_id: binary_sensor.gate_intrusion_2
    state: "off"
    for:
      minutes: 10

You can drop the automation checks.

2 Likes

Wow thanks this is perfect… I knew there would be a much more elegant and simple solution, I stuck looking too close.
Thank you again.

1 Like