Sensor or variable to check if automation has been triggered today

Working from home has been good for me so far, but it messed up my automations based on presence in home assistant.

I have a vacuum robot that is triggered for a scheduled cleanup every weekday at 9:30 am IF I’m not working from home (presence detection via router).
Now as I’m home almost 5 days a week the automation is rarely triggered. That’s why I want a second automation for a cleanup job during a different time (e.g. 7pm) but only if I’m home.
So far no problem. But now I have the case where the first automation in the morning was triggered due to me being at the office and returning before 7pm in the evening. This triggers the second automation.

I need a sensor or variable that is set to e.g. a boolean value or a string value (0 / not cleaned and 1/ cleaned) that is set when the first automation is triggered and used as a condition for the second automation. Ideally this sensor/variable is reset every midnight to 0/not cleaned.

How do I achieve that? I have no clue what to google and where to look. Has someone an example so that I can adapt that to my needs or reverse engineer? I’m not looking for finished code but rather a place to start :slight_smile:

Thanks in advance!!

If I’m understanding your use case correctly, the condition should stop the second (evening clean) automation from firing where the first (cleaning when not at home) has fired that day?

If so, you could leverage existing attributes by comparing the first automation’s last_triggered attribute against the current day.

Using my doorbell automation (last triggered yesterday) in template editor as an example, the below returns true currently but would return false if the doorbell had been pushed today.

{{ not state_attr('automation.doorbell_pushed', 'last_triggered').day==now().day }}

It should work as a condition on the second automation - please note that I’m running 0.114.4 so have only tested on that version.

** EDIT **
I’ve got my caveat above back to front - this will only work properly in 0.115.x and above as Eliminate unnecessary automation state changes by pnbruckner · Pull Request #39323 · home-assistant/core · GitHub changed last triggered to only update where the automations’ actions were run.

From the release notes:

Automations

Previously an automation’s last_triggered attribute was updated, and an automation_triggered event was fired, whenever a trigger fired and the conditions (if any) were true, regardless if the actions actually ran.

For example, in single mode, the actions won’t run if they are still running from a previous trigger event.

Now the attribute will be updated, and the event fired, only if the actions actually run.

1 Like

Thanks @TazUk!! I will give it a try! Meanwhile I found the option to create an Input Boolean.
But your solution seems to be more elegant than creating a separate variable.

Hope it works out for you - I’m not sure what version you are on so do bear in mind my edit about v0.115.x changes to the last_triggered attribute. I believe that my suggestion will only work properly on 0.115.x and above.

Usually the latest version.
Will try it this evening, but I’m sure this is the right way to start! Thanks again!