Automation condition not working, lend me your eyes

Hio.

One of my DW1 433mhz door sensors, setup as binary_sensor, has the audacity to send it’s open codes twice at times. I’ve added an condition to my automation to prevent notification spam to counter this.
However the condition does not seem to apply. DW1 only has 1 code so I’ve setup the sensor to revert to “closed” after 5 seconds and set a 10 sec “timeout” on the notification automation. However after 5 sec I can open the door and send another notification before the >10 sec condition is met.

What am I missing?

Binary sensor:

- platform: mqtt
  state_topic: "SonoffBridge/tele/RESULT"
  name: 'entre'
  value_template: '{{value_json.RfReceived.Data}}'
  payload_on: '086129'
  device_class: opening
  off_delay: 5
  qos: 1

My all-in-one automation for 4 DW1 units.

- alias: "SensorNotify"
  hide_entity: true
  initial_state: 'on'
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.entre
    - binary_sensor.altan
    - binary_sensor.pannrum
    - binary_sensor.garage
    to: 'on'
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: input_boolean.notify_enabled
        state: 'on'
      - condition: template # only notify once every 10 seconds at most from the same sensor
        value_template: "{{ ( as_timestamp(now()) - as_timestamp(state_attr('{{trigger.entity_id}}', 'last_triggered')) |int(0) ) > 10 }}"
  action:
    - service: notify.all_devices
      data_template:
          title: "Varning, sensor utlöst"
          message: "Dörr vid {{ trigger.to_state.attributes.friendly_name }}"

As a guess as_timestamp gives a Unix time, e.g in milliseconds. You can verify this by printing it to log when you execute.

Try 10*1000 to get 10 seconds.

Edit: no Unix timestamp is in seconds :-[

Thanks for trying :smiley:
The code is basically a copy-paste but modified to show the triggered entities last_triggered time, atleast thats what I hoped for ^^

This is searching for an attribute in your binary_sensor labeled last_triggered. Does that exist?

{{ as_timestamp(state_attr('{{trigger.entity_id}}', 'last_triggered')) }}

I’m guessing you want last_updated or last_changed. Not sure the trigger.entity_id in below is correct syntax or not.

{{ as_timestamp(now()) - as_timestamp(states.trigger.to_state.last_updated) }}

Your conditions will never be true and the automation will never fire. Well, one of them will be true, the other will not.

      - condition: template # only notify once every 10 seconds at most from the same sensor
        value_template: "{{ ( as_timestamp(now()) - as_timestamp(state_attr('{{trigger.entity_id}}', 'last_updated')) |int(0) ) > 10 }}"

Even if you did fix it from ‘last_triggered’ to ‘last_changed’, it will never be true because the sensor literally just changed (it’s how the automation fired), so it will ALWAYS be < 10, thus be false.

I’ll have to think for a few mins of a way to rate limit this multi-id automation. My normal way would be to just turn off the automation, then back on after a timer…but we want the automation on to handle the other sensors.

EDIT:

Silly question, but why not just increase the off_delay to 10 seconds? If you’re not doing anything else with these sensors, do you care if it stays on for the entire 10 seconds?

- platform: mqtt
  state_topic: "SonoffBridge/tele/RESULT"
  name: 'entre'
  value_template: '{{value_json.RfReceived.Data}}'
  payload_on: '086129'
  device_class: opening
  off_delay: 10
  qos: 1

Then just remove the 2nd conditional.

Try adding a delay to your automation as a last step. An automation can only run once at a time and not in parallel, so it won’t fire again if it is still running (because of the delay). I seem to be wrong and should have tested it better.

Yeah, it’s worse than that! An automation waiting in a delay will actually finish and skip the current and all future delays, but still execute everything else in the sequence! Always causes problems lol.

Wish they had flag you could set after a delay that says “exit if delay was cancelled”

Bunch of things here.

  1. last_triggered is not an attribute on a state object, it’s only available on automation entitys. You only have binary_sensors. So you need to use ‘last_changed’.
  2. last_changed is not an attribute. It’s a property on the state object. Super confusing. So have to access it directly on the state object, you cannot use state_attr() to get it. Super annoying.

This should work for you. This solution won’t work, but i’ll keep it. This will have the most recent data as pointed out by @jocnnor.

      - condition: template # only notify once every 10 seconds at most from the same sensor
        value_template: >
          {% set entity = states | selectattr('entity_id','eq', trigger.entity_id) | first %}
          {{ as_timestamp(now()) - as_timestamp(entity.last_changed) > 10 }}

EDIT: I didnt see @popboxgun’s post. He is close to a solution that would work too, but he’s using the wrong state object.

This would be the only working solution

      - condition: template # only notify once every 10 seconds at most from the same sensor
        value_template: >
          {{ as_timestamp(now()) - as_timestamp(trigger.from_state.last_changed) > 10 }}
1 Like

Next suggestion, that actually worked in my test:

Create a timer, only trigger when the timer is idle, start the timer in the automation (so it won’t be idle on the next event).

- id: '1581087813609'
  alias: Test
  trigger:
  - device_id: a0397bdf1a3f489e8537cb7272e01118
    domain: binary_sensor
    entity_id: binary_sensor.motion_occupancy
    platform: device
    type: motion
  condition:
  - condition: state
    entity_id: timer.debounce_test
    state: idle
  action:
  - entity_id: timer.debounce_test
    service: timer.start
  - device_id: 4a0d561aa0f646e59d2850c178151672
    domain: light
    entity_id: light.test
    type: toggle

This would work fine, but would only allow one message every 10 seconds for all sensors combined.

So maybe he creates X number of timers, one for each sensor :slight_smile:

Thank you all for your contributions and time, the working solution has been selected

{{ as_timestamp(now()) - as_timestamp(trigger.from_state.last_changed) > 10 }}

Enables the behaviour I need.

In regards to the question above, this would not solve the issue im having. One of my sensor triggers twice sometimes when the door is opened, I want it to only send 1 notification not two.
Changing the off_delay does nothing to remedy the issue as it will still trigger notifications
because my automation reacts to state change and would fire twice because one of the sensor sent the payload_on twice

This would have been a horrible solution as it would have forced me to change multiple lines of code for a simple edit :wink:

Had my daughter help me test this

  1. I open door 1 → notification
  2. 1 sec wait
  3. She opens door 2 → notification
  4. I open door 1 → no notification
  5. 5 sec wait
  6. I open door 1 → notification

Works like a charm

The final code

# 1/ 4 similiar Binary sensors
- platform: mqtt
  state_topic: "SonoffBridge/tele/RESULT"
  name: 'entre'
  value_template: '{{value_json.RfReceived.Data}}'
  payload_on: '086129'
  device_class: opening
  off_delay: 5
  qos: 1
#Binary sensor automation
- alias: "SensorNotify"
  hide_entity: true
  initial_state: 'on'
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.entre
    - binary_sensor.altan
    - binary_sensor.pannrum
    - binary_sensor.garage
    to: 'on'
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: input_boolean.notify_enabled
        state: 'on'
      - condition: template # only notify once every 5 seconds at most from the same sensor
        value_template: "{{ as_timestamp(now()) - as_timestamp(trigger.from_state.last_updated) > 5 }}"
  action:
    - service: notify.all_devices
      data_template:
          title: "Varning, sensor utlöst"
          message: "Dörr vid {{ trigger.to_state.attributes.friendly_name|title }}"

Small side note, this timer thingy will reset Everytime you open or close the door. Open door, get message. Wait 4 seconds, close the door. Wait 3 seconds, open the door… no message. Wait 3 seconds, open again… no message. Etc etc until it’s closed for at least 5 seconds!

I’m sure this will be acceptable though lol

Actually since the automation only triggers when the state goes to “on/open”, the “from_state” object is not changed when the off_delay changes the state to “closed”

  1. Open door - > notification
  2. Hold open 10 s
  3. Close and reopen directly -> notification