I have a humidity sensor in my bathroom that will turn the fan on when a spike is detected. The automation then runs until the humidity returns to the value recored when it was triggered.
The issue is that due to the nature of having to wait a few minutes in order to detect a spike the humidity is already quite high.
I would love a way to get the humidity value from a few minutes prior to the automation triggering so I can ensure the humidity goes back down closer to the level it was at before the shower was turned on.
Don’t forget to vote for your own request. I think you can get it using a history stats sensor and some tinkering with templates for the start and end time, but it won’t be as straightforward as what you suggested. I base my trigger limits on a humidity sensor in an adjacent room that is not affected by the shower, but reflects the conditions in the home as a useful baseline.
I like your idea of using another room. I should be able to use the value from the hallway. Not as ideal as just being able to get the value based on a timestamp but it’s a good workaround.
Currently this can only be done via sql. Here is a mariadb query I use to find the last state of temperature sensor from 24 hours ago.
SELECT last_updated,state FROM states WHERE entity_id = 'sensor.upstairs_temperature' AND state != 'unknown' AND state != '' AND state != 'unavailable' AND last_updated > UTC_TIMESTAMP - INTERVAL 36 HOUR AND last_updated <= UTC_TIMESTAMP - INTERVAL 24 HOUR ORDER BY last_updated DESC LIMIT 1;
I do something similar with a humidity sensor in the bathroom (not for fan, just adjusting the motion timeout for the lights so they don’t turn off while we’re in the shower), though with AppDaemon. Here the “on” automation fires if the difference between the “old” and “new” state is above a certain value (meaning a spike).
I then just save the “old” (pre-spike) value to the attributes of a sensor (binary_sensor.shower_on) that I also create/update with AppDaemon. Perhaps you can do something similar with UI automations?
I’m not sure if UI automations can do this, but if it can, then you can just use that saved state to check against in the automation to turn off the fan (e.g. turn off when humidity drops, but with condition that the difference between humidity and that saved state/value is less than X).
[Note: the fan in my bathroom is purely humidity-based, no HA integration. And it actually works fine just using a static humidity cutoff set by the manufacturer. So while the way you prefer it’s really fancy (and I’m all for that! I’ve been called “Mr. Over-engineered” on more than once occasion), it might work just as well in practice to find a static humidity threshold]