Automation Last Triggered

Hi

Am trying to get an automation to only run if another automation has triggered in last 12hours.

I have this as a template but not entirely sure if it will do what I want:

{{ ( as_timestamp(now()) - as_timestamp(state_attr('automation.night_time', 'last_triggered')) |int(0) ) > 43200}}

Automation will be:
If someone arrives home
and automation.night_time has run
then switch on some lights.

1 Like

For all our lights I have automations that turn them off after it has been on for a certain length of time with no motion in the room.

HOWEVER in case my kids were simply really still for an hour, say reading a book, I have another automation that will trigger on motion IF the light is off and IF the automatic off automation ran within the last 60 seconds - this way if their light turns off and they were about to go turn it back on it instantly turns back on. This is the condition I wrote for checking if the turn off automation has run within 60 seconds:

{{ (now().timestamp() - state_attr('automation.georgia_s_light_timeout', 'last_triggered').timestamp()) < 60 }}

Looks extremely similar to yours but yet slightly different syntax. Hope this helps?

1 Like

Thank you I will give this a go and see what happens

Use the following in a Template Condition.

{{ now() - state_attr('automation.night_time', 'last_triggered') < timedelta(hours=12) }}
6 Likes

Gives me
TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'NoneType'

Probably because automation.night_time has never been triggered so it has no last_triggered attribute. As a result, state_attr() reports None and that’s what the error message is complaining about (cannot subtract None from the value of now()).

The error will go away after the automation has triggered and acquired a last_triggered attribute.

FWIW, it’s possible to mitigate this, by adding more filters to the template, but I chose not to do that here given that the issue only affects a new, un-triggered automation. If you’re interested, refer to the example in this post to learn how to use the default filter to provide a default value for the case where the automation has never been triggered yet.

Ah! Would you have any thoughts on, this? Surely if it’s used as a condition it will never trigger?
Used to work on previous versions of HA but not the most recent.

Do you have any tips on how to do the opposite? I only want an automation to run if another automation has not ran. I appreciate any help!

Not sure if this meets your requirements but, if used in a Template Condition, it checks if the last time an automation triggered was over 12 hours ago.

{{ now() - state_attr('automation.whatever', 'last_triggered') > timedelta(hours=12) }}

Really appreciate the fast response. I added it in a template condition but changed hours to minutes, I’m guessing that’s what broke it. Basically I’m trying to make garage door close when the Bluetooth beacon in my car is detected as “away”. It works, but I don’t want it to do it when I get home, only when I leave, so I’m trying to make it only close the door if the automation for it to open when the Bluetooth beacon is detected wasn’t ran in the last few minutes.

Alternatively, if I could get it to close only if home assistant did not open it.

Again I really appreciate the response and any input.

@hsuede87 I use this condition on a different automation, might help.

Uses a not, as I had issues with people not being home but in another zone, this just checks that they are not at home and nothing else.

    conditions:
      - condition: template
        value_template: "{{ not is_state('person.someone', 'home') }}"
{{ now() - state_attr('automation.something', 'last_triggered') > timedelta(minutes=5) }}

Thanks for your help everyone, I got it working with your input. Much appreciated!

Hi all! I’d like to do exactly the same but as a condition in the actions. However the delta is always zero because last triggered is “now” in the actions of the automation itself. Any solution?
The idea is to do something special if someone is ringing again my doorbell in the same minute.

Post your automation. Indicate where in the actions you want to insert the condition.

Here’s a reduced test case. If I hit run, a minute appart or not, it does not matter, the same path is always taken. I guess the last_triggered is taken as the triggering happening now, so the time delta is always 0. But I’m not sure how to verify this theory.

alias: New Automation test
description: ""
trigger: []
condition: []
action:
  - if:
      - condition: template
        value_template: >-
          {{ now() -
          state_attr('automation.new_automation_test','last_triggered') <
          timedelta(minutes=1) }}
    then:
      - service: switch.toggle
        data: {}
        target:
          entity_id: switch.tz3000_pnzfdr9y_ts0101_fab024fe_on_off
    else:
      - service: light.toggle
        data: {}
        target:
          entity_id: light.hue_live
mode: single

I need to see the automation’s triggers and conditions.

alias: Doorbell
description: ""
trigger:
  - device_id: 7790b4e2c9ff47d69b03abcc90552be5
    domain: zha
    platform: device
    type: remote_button_short_press
    subtype: remote_button_short_press
condition: []

Thanks for the help!

alias: Doorbell
description: ""
trigger:
  - device_id: 7790b4e2c9ff47d69b03abcc90552be5
    domain: zha
    platform: device
    type: remote_button_short_press
    subtype: remote_button_short_press
    variables:
      is_repeated: >-
        {{ now() - state_attr('automation.doorbell', 'last_triggered') |
        default(as_datetime(0), true) < timedelta(minutes=1) }}
condition: []
action:
  - service: homeassistant.toggle
    target:
      entity_id: "{{ iif(is_repeated, 'switch.tz3000_pnzfdr9y_ts0101_fab024fe_on_off', 'light.hue_live') }}"
mode: single

I have tested a similar version of this automation, using a Philips Hue Dimmer Switch to serve as the remote control, and confirmed it works.

The trigger variable named is_repeated reports true or false depending on whether the value of last_triggered was less than a minute ago.

The is_repeated variable is used in the service call to determine which entity should be toggled. It uses the homeassistant.toggle service because it works for both lights and switches.

2 Likes

Thanks, it’s a neat solution !

Meanwhile I had found that using “this.attributes.last_triggered” instead of state_attr(…) works too.

Thanks for the help :slight_smile: