Tracking runtime of a switch, with notification and reset

I’m new to Home Assistant and am thoroughly enjoying the system, documentation and community. I have plans for a setup which seemed easy in my programmer’s mind, but within the limitations of Home Assistant has led to a lot of searching and thinking of how to implement it.

I have an air filter that should be on only some of the time. I control it through a Z-Wave switching outlet (“the switch”). It should also be possible to pause it for 30 minutes. The tricky bit is that I want to log how much time it has been running, to let me know when it is time to change the filter. When the filter is changed it should be possible to reset the counter. I have found out that Home Assistant doesn’t like storing data persistently. This is my understanding of best how to implement it. I am grateful for any pointers or critiques.

Through the Home Assistant GUI I add a custom attribute to the switch where I store when the filter was last reset.

I have a script that sets the filter switch attribute on the switch to now(). This requires me to set up a new service to run the script from (as far as I understand it). I believe this can be a Home Assistant script, but it could also be done with a shell script.

Tracking runtime of the air filter is done by adding the sensor platform “history stats.” It is set up to watch the switch that controls the air filter. It considers the time between the custom attribute on the switch and now().

There is an automation that checks whether the “sensor” history stats has surpassed the maximum run time of the filter, in which case it should notify me.

Would this work? Can it be simplified? Am I missing any best practices?

You appear to be on the right track.

1 Like

@osja I’m interested in doing something like this for my air filter as well. Would you mind sharing your code implementations as well?