#Automation condition not working as expected

1 messages · Page 1 of 1 (latest)

waxen wharf
#

I've used this many times to only allow an alert to be sent if it hasn't happened in the last hour.

I've been pulling my hair out why it's not working in a new automation I created. In the Dev -> Template tool it evaluates to True but not in automation conditions.

Any ideas? (BTW I usually use this.entity_id rather than the hardcoded id)

true if state_attr('automation.alert_dispatches_monitoring_offline','last_triggered') is none else (as_timestamp(now())-as_timestamp(state_attr('automation.alert_dispatches_monitoring_offline','last_triggered')))>3600
tender storm
#

Can you post the full conditions: block from the automation in yaml? (edit entire Automation in yaml to get it)
Took me a moment to assemble, but "works on my machine", so it's likely a minor typo

feral cypress
#

I would use something like this:

{%- set timeout = timedelta(hours=1) %}
{{ now() - this.attributes.last_triggered | default(as_datetime(0), true) > timeout }}
#

I used this for testing

Current time: {{ utcnow() }}

** Test case 1: automation triggered in last half hour, check on 1 hour **
{#- create test this-variable #}
{%- set test_entity = 'automation.f2_xiaomi_cube' %}
{% set this = states[test_entity] %}

{#- Current last_triggered var -#}
{{ this.attributes.last_triggered }}

{#- actual condition #}
{%- set timeout = timedelta(hours=1) %}
{{ now() - this.attributes.last_triggered | default(as_datetime(0), true) > timeout }}

** Test case 2: automation triggered in last half hour, check on 15 minutes **
{# Current last_triggered var -#}
{{ this.attributes.last_triggered }}

{#- actual condition #}
{%- set timeout = timedelta(minutes=15) %}
{{ now() - this.attributes.last_triggered | default(as_datetime(0), true) > timeout }}

** Test case 3: automation never triggered **
{#- create test this-variable #}
{%- set test_entity = 'automation.f2_status_messages_for_printer' %}
{% set this = states[test_entity] %}

{#- Current last_triggered var -#}
{{ this.attributes.last_triggered }}

{#- actual condition #}
{%- set timeout = timedelta(hours=1) %}
{{ now() - this.attributes.last_triggered | default(as_datetime(0), true) > timeout }}
#

Result:


Current time: 2025-05-12 10:12:00.273200+00:00

** Test case 1: automation triggered in last half hour, check on 1 hour **
2025-05-12 09:48:28.842702+00:00
False

** Test case 2: automation triggered in last half hour, check on 15 minutes **
2025-05-12 09:48:28.842702+00:00
True

** Test case 3: automation never triggered **
None
True
waxen wharf
tender storm
#

My working variant looks like:

conditions:
  - condition: template
    value_template: >-
      {{state_attr(this.entity_id,'last_triggered') is none or
      (as_timestamp(now())-as_timestamp(state_attr(this.entity_id,'last_triggered')))>60}}

I think you need the {{ ... }} in the value_template. Which is slightly confusing
Though I also like the slightly nicer ways to do this in TheFes's post

waxen wharf
feral cypress
#

They are not both strings, they are both datetime objects

waxen wharf
feral cypress
#

You are creating 2 separate templates there, which both output the datetime object

#

You need to do the calculation within one pair of curly brackets

#
The EV dispatches monitoring service is offline. {{ this.attributes.last_triggered - now() }} 

Btw, this will result in a negative timedelta, as now() will be always later in time than the last time your automation triggered

waxen wharf
feral cypress
#

Jinja outputs a datetime object as an isoformat string