How to trigger automation based on last_triggered?

So, long story short, I have a cat and a girlfriend who’s allergic to cats and she needs to take a pill once every 24 hours to keep he allergies in check. She might visit my home for a few days, so I wanted to create an automation to remind her to take her medicine. It’s supposed to be a simple notification, but I’m stuck at making it work the way I want it to.
What I’m trying to do is to make a notification pop up once she is home (so a simple trigger based on the home state), but a second trigger would be the time of the last_triggered atrribute of this exact automation so it would fire in 24 hours after first notification. Also I tried to add a condition so that the automation would not fire more then once a day in case she leaves home and returns the same day.

I’ve created a template sensor “sensor.medicine_last_triggered” to display the last_triggered attribute value and use as a time trigger, but I’m not sure it is possible to use this sensor in a trigger this way.

System check goes fine, but there are a few Template Errors in the log, so I guess I did something wrong. I’m really bad at templating.

I believe there is a simpler solution here, but I just can’t see it. Any help would be appreciated.

- id: '1619382485710'
  alias: Medicine Notification
  description: ''
  trigger:
  - platform: state
    entity_id: person.test
    from: not_home
    to: home
  - platform: time
    at: sensor.medicine_last_triggered
  condition:
  - condition: template
    value_template: >
      {{as_timestamp(state_attr('automation.medicine_notification','last_triggered'))|timestamp_custom('%-d') != as_timestamp(now())|timestamp_custom('%-d')}}
  - condition: state
    entity_id: person.test
    state: home
  action:
  - service: notify.mobile_app
    data:
      title: bla-bla
      message: bla-bla-bla

Your condition template is fine. It is the time trigger that’s the problem, I believe. If you can format sensor.medicine_last_triggered as hh:mm either when you create it or in a template in this automation, you can compare against sensor.time (time & date integration) as a template trigger.

I don’t see why you could not use that. Other way would be to skip the sensor and make it a trigger template. Whole template could just be as simple as:

{{ now() > state_attr('automation.medicine_notification','last_triggered') + timedelta(days=1) }}

You’re right: I was getting confused by remembering a feature request to add it to a condition rather than the trigger. Here’s the docs: the sensor needs to have device_class: timestamp set.

Ok, I managed to fix my automation thanks to you guys. Now it looks like this.

- id: '1619382485710'
  alias: Medicine Notification
  description: ''
  trigger:
  - platform: state
    entity_id: person.test
    from: not_home
    to: home
  - platform: template
    value_template: '{{now() > state_attr(''automation.medicine_notification'',''last_triggered'')
      + timedelta(days=1)}}'
  condition:
  - condition: template
    value_template: '{{as_timestamp(state_attr(''automation.medicine_notification'',''last_triggered''))|timestamp_custom(''%-d'')
      != as_timestamp(now())|timestamp_custom(''%-d'')}}'
  - condition: state
    entity_id: person.test
    state: home
  action:
  - service: notify.mobile_app
    data:
      title: bla-bla
      message: bla-bla-bla

Now there is only one caveat with condition. The condition in the automation above blocks the automation from triggerring within the same day twice.
What I need is for a person to get a notification once she arrives home and then receive another notification the next day at the same time. But in this state the condition blocks the autometion to be triggered within specific day, so if, for example, she gets home at 7 p.m. and recieves a notification, the next day she can leave home at 11 a.m. and she will recieve another notification once she returns at 11:30 a.m., because the current condition blocks the automation from triggering only within the same day before midnight, not within 24 hours.
Is there any template condition that would render “true” specifically after 24 hours and not the next day after midnight?
Yes, I know it’s possible with additional timers and/or input_booleans, I just don’t want to create additional entities :slight_smile:

value_template: "{{ (now()|as_timestamp - state_attr('automation.medicine_notification','last_triggered')|as_timestamp) > 86400 }}"

86400 seconds is 24 hours.

Recently added ‘this’ variable…

{{ ( as_timestamp(now()) - as_timestamp(this.attributes.last_triggered) ) < 5 }}
4 Likes