thank you for pointing me in the right direction in getting my fan to turn on when humidity is detected!
I’ve got a problem with false triggers though. just a question about the bathroom humidity stat- if i increase the max age of the sample to say 2 hours, do i need to double the sample size?
I would double the sample size as well.
I have just started using these sensors and automations to control some bathroom ventilation as well. Its working pretty well but want to see if I can get the fans to run a bit less.
Is it possible, in the binary sensor value template to add some maths so that it turns on at 5-10% above average humidity level? My templating skills are non existent, any help would be much appreciated.
Would it just be:
value_template: '{{ sensor.your_bathroom_humidity_sensor.state | int > states.sensor.bathroom_humidity_stats.state | int +5 and states.sensor.bathroom_humidity_stats.state != "unknown" }}'
or is that too easy?
That’s the way it should work indeed!
Maybe this is a bit more safe:
value_template: '{{ sensor.your_bathroom_humidity_sensor.state | int > (states.sensor.bathroom_humidity_stats.state | int + 5) and states.sensor.bathroom_humidity_stats.state != "unknown" }}'
See the warning box here:
Use states("sensor.your_bathroom_humidity_sensor")
. So:
value_template: >-
{{ states("sensor.your_bathroom_humidity_sensor")|int >
states("sensor.bathroom_humidity_stats")|int + 5 and
states("sensor.bathroom_humidity_stats") != "unknown" }}
Note that this looks for a 5 percentage point increase, so if the average is 95%+, it’ll never trigger.
To throw another way to accomplish this out there. I use the Average Sensor (available via HACS [also here: GitHub - Limych/ha-average: Average Sensor for Home Assistant]).
- platform: average
name: Master Bathroom Humidity Average
duration:
days: 1
entities:
- sensor.master_bathroom_sensor_relative_humidity_measurement
I then created an input_number which saves a threshold. This is used so I can easily update this threshold without having to restart HA.
master_bath_humidity_threshold:
name: Master Bath Humidity Threshold
min: 0
max: 20
initial: 9
mode: box
step: 1
I use a binary_sensor to check if the current humidity minus the average humidity is greater than the input_number threshold.
master_bath_fan_sb:
friendly_name: Master Fan S/B Status
value_template: "{{ (states('sensor.master_bathroom_sensor_relative_humidity_measurement')|int - states('sensor.master_bathroom_humidity_average')|int) > states('input_number.master_bath_humidity_threshold')|int }}"
This automation then works to turn the fan on/off based on the value of the binary_sensor changing.
- id: '1555779101294'
alias: Master Bath Fan - TOGGLE
trigger:
- entity_id: binary_sensor.master_bath_fan_sb
for: 00:00:30
platform: state
condition: []
action:
- data:
entity_id: switch.master_bath_fan
service_template: switch.turn_{{trigger.to_state.state}}
Really all you need is the average sensor and the automation - I just use the input_number to allow me to change the ‘test’ without restarting / editing the automation. Same with the binary_sensor - this could be done in the automation itself with a trigger template and/or by breaking into two automations. Anyhow, more than one way to skin a cat, as always, with HA.
trying to follow along and the binary sensor is throwing errors in the confog. Would you be able to share more of your config around the binary sensor?
You most likely have incorrect formatting. Just post the error instead.
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.
sensors:
bath_humidity_change:
friendly_name: Bathroom Humidity Change
value_template: "{{ (states.sensor.bathroom_temp_humidity.state | int ) - (states.sensor.bedroom_temp_humidity.state | int)}}"
device_class: moisture
Humidity device class should still exist, see here.
Maybe this would help:
sensors:
bath_humidity_change:
friendly_name: Bathroom Humidity Change
value_template: "{{ (states('sensor.bathroom_temp_humidity') | int ) - (states('sensor.bedroom_temp_humidity') | int)}}"
device_class: humidity
thank you for the reply. i got there in the end!
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 PMTemplate 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)
.
Thanks!
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).
value_template: >-
{{ states("sensor.your_bathroom_humidity_sensor") | int(0) >
states("sensor.bathroom_humidity_stats") | int(0) + 5 and
states("sensor.bathroom_humidity_stats") != "unknown" }}
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?
That is what the binary sensor does and how the fan is controlled.
The binary sensor is on, based on the value template and calculation, when the sensor humidity is greater than the average humidity plus the threshold
The automation then uses this binary sensor to turn on and off the bathroom fan.
Also just posted an update to how I’m doing today, as this method above has changed, albeit only slightly: Quick Bathroom Fan Humidity Switch Control
What confused me is you said you only need the average sensor and the automation.
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:
sensor:
- platform: statistics
name: Bad Luftfeuchte Statistics 5min Change
entity_id: sensor.bad_raumsensor_bme280_humidity
state_characteristic: change
max_age:
minutes: 5
sampling_size: 30
The automation rule in question:
- 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