#condition based on automation last_triggered always fails

1 messages · Page 1 of 1 (latest)

desert copper
#

I'm trying to sync one lock to another lock with an automation with a condition based on the automations last_triggered attribute. the problem is when the automation triggers the condition always fails. the same condition seems to work correctly when used in either a template under dev tools or when using 'test' on the automation trigger page.

I've tried using both

this.attributes.last_triggered

and

state_attr('automation.nfc_lock_frontdoor', 'last_triggered')

but both fail on the condition.

any suggestions?

alias: NFC Lock State change
description: ""
triggers:
  - entity_id:
      - lock.homekey_frontdoor_homekey_front_door
    trigger: state
conditions:
  - condition: template
    value_template: >-
      {{ this.attributes.last_triggered is not defined or now() -
      this.attributes.last_triggered >= timedelta(seconds=10)}}
    enabled: true
actions:
  - if:
      - condition: state
        entity_id: lock.homekey_frontdoor_homekey_front_door
        state:
          - unlocked
    then:
      - action: lock.unlock
        metadata: {}
        data: {}
        target:
          entity_id: lock.front_door_lock
    else:
      - if:
          - condition: state
            entity_id: lock.homekey_frontdoor_homekey_front_door
            state: locked
        then:
          - action: lock.lock
            metadata: {}
            data: {}
            target:
              entity_id: lock.front_door_lock
mode: single
bright chasm
#

Instead of using is not defined as a guard, just provide a default:

condition: template
value_template: >-
  {{ now() -
  this.attributes.last_triggered | default(0|as_datetime) 
  >= timedelta(seconds=10)}}
desert copper
#

i'll give that a shot, it also failed without that section in the condition though, i picked that up while googling around