I recently migrated from Domoticz to Home Assistant. For the most part I am very happy about that, but there are small issues left.
On domoticz I had a script that would turn on my fan in the bathroom if the humidity sensor climbs up rapidly (i.e. someone is taking a shower). I made it store the old value of humidity (base humidity is not a static value) and it would keep the fan on until it reaches the old humidity value. If the specific value is not reached within 3 hours it would switch off the fan as well.
I tried to recreate this in HA and it does switch on the fan, but it does not switch the fan off when the desired (old) value is reached.
#this sensor would detect the quick climbing of the humidity
binary_sensor:
- platform: trend
sensors:
badkamer_hum_rising:
entity_id: sensor.badkamer_hum
sample_duration: 180
min_gradient: 0.0166666
device_class: moisture
#this sensor stores the old humidity level
sensor:
- platform: sql
scan_interval: 45
queries:
- name: badkamer_humhist
query: "SELECT MIN(state) as state FROM (SELECT state FROM states WHERE entity_id='sensor.badkamer_hum' AND state!='unknown' order by last_changed desc limit 5);"
column: 'state'
unit_of_measurement: '%'
#this automation should do the magic trick
- id: '1548155325103'
alias: vochtigheid badkamer
trigger:
- entity_id: binary_sensor.badkamer_hum_rising
from: 'off'
platform: state
to: 'on'
condition:
- condition: state
entity_id: switch.badkamer_afzuiging
state: 'off'
action:
- data:
entity_id: switch.badkamer_afzuiging
service: switch.turn_on
- condition: state
entity_id: sensor.badkamer_hum
state: '{{states.sensor.badkamer_humhist.state}}'
- data:
entity_id: switch.badkamer_afzuiging
service: switch.turn_off
Anyone has a clue as to why this is not working like I think it should?
I think the problem is that your base humidity level changes every 45 seconds. Inculding when the fan is on.
You need to store it once, when the fan is first switched on.
Or maybe not. I missed the "select minâ.
Your problem is that your turn off action is in the same automation as the turn on action. This automation is triggered by a rising humidity. Your humidity will be falling when you want to turn the fan off.
- condition: state
entity_id: sensor.badkamer_hum
state: '{{states.sensor.badkamer_humhist.state}}'
Are you trying to wait until the humidity goes back to what it was? If so, this wonât do it for a couple of reasons. First, a condition in a script either lets the script keep running, or aborts it immediately. Second, the state parameter of a state condition does not accept a template, just a string. So itâs testing if sensor.badkamer_humâs state is literally â{{states.sensor.badkamer_humhist.state}}â.
Yes, thatâs exactly what I was trying to do there. I thought I could use the templating language wherever I want. Thanks for the suggestion about the wait template. I will definitely try this in the evening and will let you know
Would be nice, but no, you canât. It is only accepted in certain places. You have to check the documentation (and, unfortunately, itâs sometimes unclear.)
I have incorporated the proposed settings and HA seems to accept it. Now letâs test over the upcoming days. Anyhow thank you very much for your assistance!
Dear sir,
It is somewhat working but it switches off too early. I think this is because the wait_template is âconstantlyâ being executed. Is that correct?
I was hoping it was executed only once and waits for that value since off course my himhist sensor will also climb up in a matter of 45 minutes. I think to solve this as a first action I need to store the current himhist value.
A wait_template is evaluated when the script first reaches its step. If it evaluates to true, then the script immediately continues. If it evaluates to false, then the script will be âpausedâ and the template will be re-evaluated whenever any of the referenced entities change. As soon as it evaluates to true, the script will continue on to the next step.
I had a suspicion that would be the case, but it wasnât entirely clear not knowing the full behavior. Yes, I would agree, you should record the current value of humhist before the wait_template and use that in the wait_template instead. I believe an input_number would serve nicely.
Thank you for point me in the right direction <again!>.
I have indeed created an input_number and set the value it in the automationâs first action.
Have tested it by manually setting device states and I think this finally does the trick.
Perhaps for reference for other folks struggling with the same issue:
Iâm already using the trend sensor as a input trigger for this event.
I have thought about using it as a switch-off trigger as well, but from tests Iâve done this is not very trustworthy. I have even seen my trend sensor switching on during the day without ppl in the bathroom.
So I guess Iâll stick with the historic humidity level as a switch-off trigger.
Nevertheless thanks for the suggestion.