How do I use state.last_changed in automation condition?

I saw @sjee mentioned that you can use state.last_changed as part of an automation here.

What I’m trying to achieve is to have the automation (notification) only occur if the state has not been updated in the last 10 seconds. I’m having a hard time integrating this into my automation. I tried adding a template condition to the automation, but then it never runs as it seems to always be low. Without, the notifications operate fine, but I’m trying to prevent the notification from firing when I trigger the door from the UI.

    condition:
      - condition: template
        value_template: '{{ as_timestamp(now()) - as_timestamp(states(switch.sensor.garage_door_control.last_updated)) < 10 }}'

The overall automation:

  - alias: Notify iOS Garage
    initial_state: true
    hide_entity: true
    trigger:
      - platform: state
        entity_id: cover.garage_door
        from: 'closed'
        to: 'open'
    # condition:
    #   - condition: template
    #     value_template: '{{ as_timestamp(now()) - as_timestamp(states(switch.sensor.garage_door_control.last_updated)) < 10 }}'
    action:
        service: notify.ios_dkphone
        data:
          title: "Garage Alert"
          message: "GARAGE IS OPEN"
          data:
            push:
              badge: 0
              category: 'garage1'

You should trigger on the state.last_changed with a condition garage door open.

How do I do it? I don’t know the syntax of how you trigger on state.last_changed.

    trigger:
      - platform: state
        entity_id: cover.garage_door.last_changed

Is it like this?

You would use something like:

     - condition: template
       value_template: >
           {{ (as_timestamp(now()) - as_timestamp(states.cover.garage_door.last_changed)) > 10 }}

I have lot of automations using last_changed here

8 Likes

Aweomse, thanks @arsaboo!

Do you know if there is a way I can test to see the output of {{ (as_timestamp(now()) - as_timestamp(states.cover.garage_door.last_changed)) > 10 }} as I’m working on automations? Especially trying to troubleshoot ones that aren’t working…?

Put it in the template editor (under dev tools) and you can see the output of the same.

3 Likes