Just wanted to say thank you so much for this code.
Super useful. I am using it for a bathroom fan to control humidity as well.
I also modified and made a point that gives me Indoor Air Temp/Outdoor Air Temp Delta-T
As an HVAC technician and an all around physics nerd being able to graph I.A./O.A. Delta-T vs. my AC/Furnace runtime makes me all warm and fuzzy inside.
thank you first for the code. iām not sure if a home assistant update has made "device_class: āhumidityā " depreciated, but i cant seem to configure it correctly. also with the unit of measure as well. Iāve adjusted the device class to āmoistureā , but cant get a unit of measure to stick, so only giving me an on/off sensor.
any pointers would be much appreciated as only using 1 sensor is giving me lots of false readings
this is what its currently looking like.
Using this value template, I am starting to see error messagesā¦
Logger: homeassistant.helpers.template
Source: helpers/template.py:1286
First occurred: 9:21:16 PM (5 occurrences)
Last logged: 9:21:17 PM
Template warning: āintā got invalid input āunknownā when rendering template ā{{ ( states(āsensor.your_bathroom_humidity_sensorā) | int ) > ( states(āsensor.bathroom_humidity_statsā) | int + 5 ) and ( states(āsensor.bathroom_humidity_statsā) != āunknownā ) }}ā but no default was specified. Currently āintā will return ā0ā, however this template will fail to render in Home Assistant core 2022.1
Template warning: āintā got invalid input āunavailableā when rendering template ā{{ ( states(āsensor.your_bathroom_humidity_sensorā) | int ) > ( states(āsensor.bathroom_humidity_statsā) | int + 5 ) and ( states(āsensor.bathroom_humidity_statsā) != āunknownā ) }}ā but no default was specified. Currently āintā will return ā0ā, however this template will fail to render in Home Assistant core 2022.1
Soā¦ is that because during restart the sensor.bathroom_humidity_stats state would be unknown (or is it unavailable?) for a few seconds?
If so, how do I modify my value_template block to remedy the situation?
Recent updates of Home Assistant require the int filter to provide a default value. So both times you see an int in the template above, change this to int(0).
So for other visitors coming to this thread, this is what we should have under the value template block, per the recent updates, at least as of today (24-Feb-2022).
I set up the average sensor. What I want is to trigger when there is a quick change over 5% and then to off when it comes back to close to the average. I donāt see how the automation does this? Am I missing something?
Hey all! There is another clean way to solve the task.
I am using the statistics component to act based on the change over given time of the humidity in my bathroom. This covers @Domoticon 's original question for a solution for āwhen humidity increases (by x%) within y minutesā.
The example below does this for turning my fan off but of course this can be adapted to other wishes.
The statistics sensor for humidity stats over the last 5min:
- alias: Regel Bad LĆ¼ftung aus bei gleichbleibender Luftfeuchte
trigger:
- platform: state
# Trigger for every humidity change
entity_id: sensor.bad_raumsensor_bme280_humidity
condition:
condition: and
conditions:
- condition: state
# Condition: fan on for at least 5 minutes
entity_id: switch.bad_luftung
state: "on"
for:
minutes: 5
- condition: numeric_state
# Condition: Humidity reached at least 60%
entity_id: sensor.bad_raumsensor_bme280_humidity
below: 60
- condition: numeric_state
# Condition: Humidity change over the last 5 minutes is smaller than 2%
entity_id: sensor.bad_luftfeuchte_statistics_5min_change
above: -2
action:
- service: switch.turn_off
entity_id: switch.bad_luftung
Further automation rules I have defined:
Turn the fan on when humidity exceeds 70%
Turn the fan off after 60 minutes maximum duration. This is a fallback rule in case the above rule does not work and this even is logged for error handling
Thatās interesting. Is the change under the statistics component the same or similar to the trend component?
Personal experience: a couple of weeks ago I tested using mean under statistics components vs trend components side by side, tweaking parameters, and found that statistics being more reliable and sensitive (to my setup / my humidity sensor.)
Wasnāt aware that average_linear being a thing until I see Markās remark above. So thank you Mark, Iām planning to test that also.
Iāve gotten some useful tips from this thread so thanks to all. Iāve noticed that the binary_sensor will turn to 'on' when no shower is running i.e. no humidity changes but just from the ambient temperature in my bedroom, therefore it interferes with my automation logic. Anyone else experience this and have any workaround?
Given 3 months of statistics, I notice that the absolute humidity in my house pretty much follows the humidity so Iāll probably update my automation to enable my fan base of an offset with the external absolute humidity.
Here is the graph - orange is the external absolute humidity, the other ones are elsewhere in my house (when the lines are flat I either had a flat battery or an internet connection issue):
Nice one! I had a similar revelation some time ago. The challenge was that, logically speaking, your bathroom humidity canāt go below the outside humidity. Therefore, if fan-off-humidity-level is lower than outside-humidity-level, the fan never turns off.
Instead of acting on absolute humidity levels (in relation to the outside humidity) I ended up using the humidity-change-rate - the topic of this thread
I see that you are talking about enabling the fan, so I guess your use case is slightly different and the logic probably makes sense. Congrats on your discovery
I currently have a couple of issues with this code:
Whenever I restart Home Assistant the binary sensor is turned on with a message of: āDetected triggered by state of Bathroom Humidity Stats changed to 0.0ā; and
For some reason, when the statistics sensor had a value of -4.0 it turned on the binary sensor with the message of āDetected triggered by state of Bathroom Humidity Stats changed to -4.0ā
Is anyone able to provide any assistance?
Thanks for your help.
EDIT: I think I need to remove the āstate_characteristic: changeā section of the statistics sensor.
I donāt know why this isnāt made a default for new users.
Furthermore, Iām not saying the binary sensor is a bad idea (I also like to have āflagsā to indicate e.g. showering) but with an automations based approach you would be a lot more empowered to troubleshoot the issues you are seeing. It is certainly better for learning and more scalable, you should consider it.
EDIT: I think I need to remove the āstate_characteristic: changeā section of the statistics sensor.
No I donāt think so. I think your state logic is wrong. But that really depends on your use case. What are you trying to accomplish?
Your state template says āhumidity > (humidity_change+5)ā which doesnāt make sense.
If your wish is to detect showering, you want to either trigger on high humidity. To detect the end of the shower, you can also just look at the absolute humidity. Imho no statistics sensor needed. The tricky part for me was to decide when to turn on and off a ventilation fan. That is where a statistics sensor with the āchangeā characteristic was invaluable. Have a look at my automations again
I think I did need to change the state_characteristic as this would then default to mean. This way the state logic would be better as it would be comparing an absolute measure of humidity (average over the last hour) against an absolute measure of humidity (current humidity). However, Iāve gone back to using the āchangeā state_characteristic and updated the template binary sensor to:
I was originally using an automation based approach in NodeRed - using absolute humidity states - that also triggered a counter in Home Assistant. However, I liked the idea of not using absolute humidity values, as these often struggled to pick up different showers within a relatively small space of time, which is why the template sensor relying on change in humidity appealed to me.