tfsjohan
(Johan)
September 14, 2022, 1:41pm
1
I’d like to use a humidity sensor to detect if someone is showering. I’d like to check if the humidity is increasing fast in the last couple of minutes. Let’s say that if the humidity is increasing 10 percent units in 2 minutes I know someone is showering.
I’ve declare the sensor as this:
- platform: trend
sensors:
bathroom_humidity_increasing:
entity_id: sensor.bathroom_climate_humidity
sample_duration: 120
min_gradient: 0.16
But it never triggers even though I can clearly see that the tracked sensor is increasing quickly.
petro
(Petro)
September 14, 2022, 1:47pm
2
The gradient needs to be greater than 0.16 over the 2 minute duration. You can see the calculated gradient as an attribute in the trend sensor to debug it. If it’s not higher than 0.16, it won’t trigger.
Personally, I ended up making a derivative sensor to track the rate of change and made a simple trigger off that.
tfsjohan
(Johan)
September 14, 2022, 1:51pm
3
Thanks.
I’d started out with a derivative sensor, but could get that to work well. Might very well be how I configured it that’s wrong.
Would you mind sharing how you did yours and what it does?
tfsjohan
(Johan)
September 14, 2022, 6:47pm
4
I think you’re right. Seems like the trend sensor becomes far too sensitive and a derivative sensor is better.
I’ve also have a magnet sensor on the door to better know when the shower is over.
petro
(Petro)
September 14, 2022, 10:26pm
5
Here’s my setup that I use to trigger the fan automation:
Derivative sensor
- platform: derivative
name: Master Bathroom Humidity Change
source: sensor.master_bathroom_filtered_humidity
unit: '%'
Template
- trigger:
- id: rising
platform: template
value_template: >
{%- set change = states('sensor.master_bathroom_humidity_change') | float(none) %}
{{ change is not none and change > 200 }}
- id: falling
platform: template
value_template: >
{%- set change = states('sensor.master_bathroom_humidity_change') | float(none) %}
{{ change is not none and change < -150 }}
- id: constant
platform: template
value_template: >
{%- set change = states('sensor.master_bathroom_humidity_change') | float(none) %}
{{ change is not none and -100 <= change <= 125 }}
- id: start
platform: homeassistant
event: start
- platform: state
entity_id: binary_sensor.templates_reloaded
to: 'off'
binary_sensor:
- unique_id: master_bathroom_humidity_status
name: Master Bathroom Humidity Status
state: >
{%- if trigger.id == 'rising' %}
True
{%- elif trigger.id == 'falling' %}
False
{%- else %}
{%- set entity_id = 'binary_sensor.master_bathroom_humidity_status' %}
{%- if states(entity_id) in ['on', 'off'] %}
{{ is_state(entity_id, 'on') }}
{%- else %}
False
{%- endif %}
{%- endif %}
auto_off:
minutes: 40
device_class: moisture
Automation
- alias: Bathroom - Turn on fan when humidity is high.
id: bathroom_turn_on_fan_when_humidity_is_high
mode: restart
trigger:
- platform: state
entity_id: binary_sensor.master_bathroom_humidity_status
variables:
check: >
{{ trigger | default(none) is not none and trigger.to_state is defined and trigger.from_state is defined }}
continue: >
{%- set valid = ['on', 'off'] %}
{{ check and trigger.to_state.state in valid and trigger.from_state.state in valid }}
service: >
switch.turn_{{ trigger.to_state.state if continue else none }}
add_delay: >
{{ trigger.to_state.state == 'off' if continue else False }}
condition:
- condition: template
value_template: "{{ continue }}"
action:
- choose:
- conditions: "{{ add_delay }}"
sequence:
- delay: "00:20:00"
- service: "{{ service }}"
target:
entity_id: switch.master_bathroom_fan
- delay:
seconds: 1
2 Likes
I want to use this approach, its a really nice job @petro could you tell me where do you use the start and the constant triggers in your template?
edit: Nvm I saw is for handle the unknown states and the reboots… I think jejeje
also do you mind explain me what this part of the code does?
{%- else %}
{%- set entity_id = 'binary_sensor.master_bathroom_humidity_status' %}
{%- if states(entity_id) in ['on', 'off'] %}
{{ is_state(entity_id, 'on') }}
1 Like