What I want
I’m using automation to detect when a specific device change state and, if thats the case, I want to receive a telegram message. I don’t want to be flooded in telegram so, an additional template logic is being used to be notified ONLY if XX seconds have elapsed since the last state change.
The problem
The logic evaluating the variables above in the “condition/value_template” field is working well, as far as I can see it working on the Developer Tools - templates. For the first 30 seconds it returns False, after that it returns True.
The problem is that even when the return is True, after the initial 30 seconds, the automation never executes and I don’t receive any notification.
When switch.shelly_shplg_s_2677f8 changes state and triggers the automation, its last_changed property is set to the current time. In other words, it is set to the same value as now().
That’s why subtracting last_changed from now doesn’t produce a value greater than 20.
You can prove it to yourself using a simple automation like this:
Imagine I want to build a message to be delivered with the entity that triggered this automation (in this case imagine for instance it was “device_tracker.life360_bjr”, and want to know if it was a “leave or enter” to format properly the message.
Which variables may I use to accomplish that? Is there a list of available variables?
Finnally, debugging automations with conditional enter/leave zones ir hard. Is there a proper way to debug these scripts somehow?
When an automation is triggered, you can use what is called a Trigger State Object to get details about the trigger activity. The details can vary and it depends on the trigger’s platform.
For example, your automation’s trigger platform is Zone
Template variable
Data
trigger.platform
Hardcoded: zone
trigger.entity_id
Entity ID that we are observing.
trigger.from_state
Previous state object of the entity.
trigger.to_state
New state object of the entity.
trigger.zone
State object of zone
trigger.event
Event that trigger observed: enter or leave.
Here’s an example that reports to the system log whenever a device_tracker enters or leaves a zone.
Hello Taras, apologise for my lack of feedback but I’ve been away from home.
Unfortunately, the variable you mentioned above (Name: {{trigger.to_state.name}}, Entity_id: {{trigger.entity_id}} Event: {{trigger.event}}"), didn’t work on my side. If I put the variables, the automation don’t execute… maybe I’m missing some componente on my core?
Just figured out that I must change from “-data” to “-data_template”. When Using the gui, there is not that option available, right? You need to edit the file directly, correct?
Correct (and data_template is what I used in the example I posted). If you use a template then the option must change from data to data_template.
I use the VS Code editor to create/modify automations. Home Assistant’s Automation Editor is limited and doesn’t allow you to create more complex automations. It will probably improve in the future but it currently is better suited for simpler automations.
To help other users, who may have a similar question, please mark my post with the Solution tag. Only you, the author of this topic, can do that. It will help others find my answer quickly.