Hi All,
I’m having some issues with creating what in my head is a fairly simple automation, but I just can’t work out how home assistant deals with time.
What I want to happen is:
When the bathroom light turns on, a timer starts counting up.
When the bathroom light turns off, a fan turns on, and the same counter should start counting down.
If this is interrupted and the light switches on again, the fan should turn off, and it should start counting up again from whenever the counter got to.
When the counter reaches 0, the fan should turn off.
The Timer integration doesn’t support changing the direction of how it counts elapsed time. Your options are:
Change your design to work with a timer entity.
Invent your own directional “time counter” (perhaps by using a repeat to increment/decrement a counter every second plus a fair bit of code to manage it based on what the fan and light are doing).
As the other poster mentioned, timers don’t support counting up, stopwatch style. But you don’t need one. When the bathroom light turns on, that should trigger storing the current time in a datetime helper (configure it to store date and time so that this all will work if the light turns on before midnight and goes off after:
You should already have that timestamp as attribute, no need for a helper entity. The light should have an attribute last_changed, that should work equally.
If i understand correctly, this would mean that if someone came back into the bathroom for 10 seconds, the light would turn on so the fan stops, then they leave and the fan only runs for 10 seconds, when maybe it should have run for 20mins.
I guess this could be solved with some conditions though.
In general you don’t want to make anything in home assistant change continuously with time, such as a stopwatch or countdown timer. That would force HA to process changes on some very short time interval wasting processor time and writing a bunch of pointless data to the database. And for the most part HA has been coded to prevent this, or at least rate-limit to once/minute if you attempt something like that. The timer helper itself doesn’t even function that way internally; it will determine the datetime it should end and then do nothing until that datetime occurs. It doesn’t actually “count down” and change its state every second.
Anyway, the way to do what you want is to do it all with evaluating datetimes when the state of the light or fan changes. I took a crack at this, and it ended up being a little more involved than I had hoped. Someone can probably come along and simplify it a bit.
In English, the walkthrough is this:
(Prerequisite: create an input number helper named “fan time”; this will track the number of seconds the fan needs to run)
trigger if the light turns on or off
if the light turns on while the fan is on
take the “fan time” and subtract the length of time the fan has been on. If the number after subtraction is less than zero, use zero.
turn the fan off
if the light turns off
take the “fan time” and add the length of time the light has been on
turn the fan on
wait the number of seconds equal to the “fan time” helper
set “fan time” to zero
turn the fan off
if the light is turned on while the fan is off (this is the “default” option when no other condition matches)
set the “fan time” to zero
The mode of the automation is set to restart so the delay can be interrupted.
The downside of this automation is that if you restart during the delay (while the fan is running) the fan will never turn off when HA comes back up. You could add another trigger for HA startup and turn the fan off.