Using the trigger as a condition

Should this be possible? Doesn’t seem to trigger at the moment, I wonder if it’s because I reference the trigger as a condition, too!

  - id: lunch
    alias: 'Steve home for lunch'
    trigger:
      entity_id: group.steve
      platform: state
      to: 'home'
    condition:
      - condition: state
        entity_id: binary_sensor.workday_sensor
        state : 'on'
      - condition: template
        value_template: '{{ (as_timestamp(now()) - as_timestamp(states.automation.steve_home_for_lunch.attributes.last_triggered | default(0)) | int > 72000)}}'
      - condition: state
        entity_id: group.steve
        state : 'not_home'
        for:
          hours: 2
      - condition: time
        after: '11:55:00'
        before: '14:10:00'
      - condition: template
        value_template: "{{ (as_timestamp(now()) - as_timestamp((states.binary_sensor.kitchen_door.last_updated)) <180)}}"
      - condition: template
        value_template: "{{ (as_timestamp(now()) - as_timestamp((states.binary_sensor.front_door.last_updated)) <180)}}"
    action:
      - service: timer.start
        entity_id: timer.lunch

Of course it won’t trigger… You have a condition that contradicts the trigger.

    trigger:
      entity_id: group.steve
      platform: state
      to: 'home'
      - condition: state
        entity_id: group.steve
        state : 'not_home'
        for:
          hours: 2

You want this to trigger when it changes to home. But then you check to see if it’s been not home for 2 hours. It literally just changed to ‘home’, therefore it cannot be ‘not home’ for 2 hours.

I just posted about a similar thing I’m trying to do. I wish there was a way to access state history from templates. How did you implement your binary_sensor.kitchen_door.last_updated sensor?

There is a way to access state history from templates.

1 Like

That’s fantastic! Learn something new every day on these forums :slight_smile:

last updated is an attribute that is on all state objects. I.E. All devices in all domains (outside of automations and scripts) have the last_updated and last_changed attribute

Check out the that doc. It tells you want is inside a state object. State objects are fancy ways of saying “a device in home assistant”

Thanks. I am aware that was likely to be the problem… I just wasn’t sure what order the execution/checks were made in. Back to the drawing board. :slight_smile:

Yeah, I’ve used state objects before… Somehow I just never noticed that they all have last_updated fields.

Well, they all don’t have that field :frowning: My MQTT-based device tracker doesn’t have this field despite being able to see “1 hour ago” when I click on the pop-up on the dev-state page.

Is there any way you can get this value for entities that don’t have it explicitly in their attributes?

last_updated isn’t inside the attributes. It’s a property of the state object. All device_trackers have it.

{{ states.device_tracker.mqtt_tracker.last_updated }}

not

{{ states.device_tracker.mqtt_tracker.attributes.last_updated }}
                                     \         /
                                      \       /
                                       \     /
                                       Remove

Don’t confuse the last_updated attribute with last_updated on the state object. It’s rather annoying that the 2 fields have the same name, but they are not related in any way.