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.
Thanks for trying
The code is basically a copy-paste but modified to show the triggered entities last_triggered time, atleast thats what I hoped for ^^
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?
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”
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’.
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 }}
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
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!
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”