Automation: turn on device untill some value is reached

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.

What exactly are you trying to do with this?

  - 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}}’.

I think you want to use a wait_template:

  - wait_template: >
      {{ states('sensor.badkamer_hum')|float <
         states('sensor.badkamer_humhist')|float }}

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 :+1:

1 Like

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!

1 Like

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.

though I can’t pinpoint it yet, maybe the trend sensor could help here?

for the sake of it, I have defined some trend sensors, bt never saw good use for them, this might well be it :wink:

1 Like

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:

- 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_template:
      entity_id: input_number.target_badkamer_hum
      value: "{{ states('sensor.badkamer_humhist')|float }}"
    service: input_number.set_value
  - data:
      entity_id: switch.badkamer_afzuiging
    service: switch.turn_on
  - delay: 00:10:00
  - timeout: 04:00:00
    wait_template: "{{ states('sensor.badkamer_hum')|float <= states('input_number.target_badkamer_hum')|float }}"
  - data:
      entity_id: switch.badkamer_afzuiging
    service: switch.turn_off

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.