I haven’t done home assistant coding in a while, so please excuse the newbie question.
I have a gate with two scripts setup. One script (gate_ajar) opens the gate enough for a person to walk through. The second script (open_close_gate) opens the gate all the way and closes it. The second scrip closes the ajar gate as well.
I am installing a sensor on the gate to notify me when the gate opens and closes.
My question is:
How can I create an automation to send me a security alert if the gate was forcefully opened. I was thinking of coding an if/else if statement to check if gate_ajar and open_close_gate scripts were called. If either of the scripts were called then send a normal notification. If the scripts were not called and the gate was opened then send a danger alert, or something serious.
If your script to open the gate runs while the gate is opening (such that if you check the status of the script, it is in a running state until the gate finishes opening), you could have your automation that watches for the gate open sensor check the script status to see if it’s running, or has very recently run with it’s last run timestamp. That could give you a sufficiently accurate way to detect if it was an intended call to open, or someone forcing it open.
Below are my two scripts. They just send a short trigger to the gate controller and turn off. I don’t think they are while the gate is opening/closing.
What would be the best way to check against the timestamp of when either of the scripts ran?
If you have any way to poll those switches for a state, you can put a wait command immediately after the action and have it check the state until it’s complete. That would allow your script to keep running until the switch changed to an idle/finished state.
To check for the last time the script ran, that should be something like
I am using a Shelly Plus 1 as a relay to trigger the gate controller. I am not sure it has a polling feature.
So the logic would be to use {{ state_attr('script.gate_ajar', 'last_triggered') }} to check if the script was called in the last 30 seconds. If not then send an alert.
You can go in to Developer tools, States and filter the entities to switch.gate_opener_switch_0 in your browser on your system, then in mobile or other device (to make it easy; 2nd monitor in another browser tab or whatever, just so you can see the filtered entity at all times) trigger that switch. In the first browser, watch the entity state and properties for any changes.
That will show you if the state changes as it’s opening. It may be normalized by HA though to only be on/off, so you may be right that you can’t use the state of the switch.