Automation with template condition not working

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.

Code

- id: '1581974397015'
  alias: xxxx
  description: ''
  trigger:
  - entity_id: switch.shelly_shplg_s_2677f8
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: template
    value_template: "{{ (as_timestamp(now()) - as_timestamp(states.switch.shelly_shplg_s_2677f8.last_changed | default(0)) | int > 30)}}"
  action:
  - data:
      message: "Shelly on"
    service: notify.telegramchat

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.

Debug from Developer Tool

Now: {{now()}}
Updated: {{states.switch.shelly_shplg_s_2677f8.last_changed}}
Diff: {{ as_timestamp(now()) - as_timestamp(states.switch.shelly_shplg_s_2677f8.last_changed)}}
{{ (as_timestamp(now()) - as_timestamp(states.switch.shelly_shplg_s_2677f8.last_changed)|int) > 20 }}

It prints:

Now: 2020-02-17 22:43:14.257762+00:00
Updated: 2020-02-17 22:39:48.753027+00:00
Diff: 205.5052261352539
True

Can someone give me a help here? I’m becoming a bit crazy about this.

Thanks guys.

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:

- alias: 'test last_changed'
  trigger: 
    platform: state
    entity_id: input_boolean.toggler
  action:
    service: persistent_notification.create
    data_template:
      title: "now and last_changed"
      message: >
        Now: {{now().timestamp()}}, Last_changed: '{{states.input_boolean.toggler.last_changed.timestamp()}}'

When I change set the input_boolean to on the following notification is generated:
Screenshot from 2020-02-18 09-32-36

The two times are identical (to within a second).

Try it using trigger.from_state.last_changed.timestamp() like this:

  condition:
  - condition: template
    value_template: "{{ (now().timestamp() - trigger.from_state.last_changed.timestamp() | default(0)) | int > 30)}}"
1 Like

Dear Taras, thank you so much, it worked like a charm. I’m still giving the first steps in hass, so the programming is a bit challenging for me.

Another question. Regarding these kind of automations, please consider the following scenario:

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?

Thanks for all your support.

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.

- alias: 'zone example'
  trigger:
    platform: zone
    entity_id: device_tracker.life360_bjr, device_tracker.life360_mafalda
    zone: zone.lisboa
  action:
    service: system_log.write
    data_template:
      level: warning
      message: "Name: {{trigger.to_state.name}}, Entity_id: {{trigger.entity_id}} Event:  {{trigger.event}}"
1 Like

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?

Any advise to debug it?

I don’t know how to debug this…

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?

Going to test it tomorrow.

Hi Taras, just to say that everything is solved, thank you so much for your help on this :slight_smile:

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.