Threashold in automation not playing ball!

Hi all,
I am kinda new to home assistant, I have had it installed for a long time but now starting to create automations.
Equipment using:
Shelly 2pm to open and close window
ESPhome multi sensor with temperature to trigger the open and close
Aquara flood sensor (extended outside) to trigger rain

My requirements are:
Temperature over 24c = open window (Via a threashold)
Temperature under 24c = close window (Via a threashold)
Floor sensor is wet = close window

Rain sensor works fine when I wet it, closes the window.
However, when it’s dry again AND the temperature of the room is over 24c the window will not open as the threashold is like it’s not telling the automation it’s in the “ON” state and to reopen the window.

I was using the “From” and “To” in the open window trigger, however it’s already set to “on” as it has not dropped it’s the rain that’s closed the window.

My code below, hopefully it makes sense?

alias: Office Window
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.office_temp_under_24_degrees_office_window
    enabled: true
    id: Temp Under - Close
    alias: Temp - Close Window
    from: "off"
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.office_temp_over_24_degrees_office_window
    enabled: true
    id: Temp Over - Open
    alias: Temp - Open Window
  - platform: state
    entity_id:
      - binary_sensor.lumi_lumi_sensor_wleak_aq1_moisture
    from: "off"
    to: "on"
    id: Rain
condition:
  - type: is_open
    condition: device
    device_id: 6b922591151e73c85a8a6bd5e195ef00
    entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_on_off
    domain: binary_sensor
    enabled: true
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Temp Under - Close
        sequence:
          - service: script.window_close
            data: {}
    alias: Close Action
  - choose:
      - conditions:
          - condition: trigger
            id: Temp Over - Open
          - condition: and
            conditions:
              - condition: state
                entity_id: binary_sensor.lumi_lumi_sensor_wleak_aq1_moisture
                state: "off"
        sequence:
          - service: script.window_open
            data: {}
    alias: Open Action
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Rain
        sequence:
          - service: script.window_close
            data: {}
    alias: Close - Rain Action
mode: single

You’re not actually triggering on becoming dry, only when it starts raining.
Besides, as you probably know, your temperature triggers will only trigger when the state changes, i.e. when going from above 24 to below and the inverse.

Triggers are things happening, when should HA consider taking action. that is not that same as conditions that need to be met. Those should be in the conditions. Only if there is one trigger you generally do not need a condition, because the trigger happening assures the condition is met.

So the triggers you need are changes of the temperature threshold and changes of the wet sensor.

Then using conditions you should decide if the change should lead to action. Not by testing on the trigger, but by looking at the state. That is because a change in wetness doesn’t tell you anything about the temperature, and changes in temperature don’t tell you anything about rain.

Further suggestions: Personally I do not put opposite actions in one automation. It almost always leads to an extra choose statement and more clutter. Two automations, when should it close and when should it open, are cleaner and simpler.

Also, a threshold using the same temp for both open and close is not wise. if the temperature varies slightly above and below the window will open and close in short succession. The treshold is there to create what is called a Schmitt-trigger - Wikipedia. So for instance open when over 24.5 and close when below 23.5. That way the window is stable at 24 degrees and slightly fluctuating.

My suggestion, including the previous comments, would be:

  • 4 triggers total: You already have 3, just need to add the water sensor changing state from wet to dry
  • Set the trigger_id to “open” for the two triggers that could cause the windows to open (temp above, and sensor becoming dry). Yes, two different triggers can use the same trigger_id
  • Set the trigger_id to “close” for the two triggers that could cause the windows to close (temp below, and sensor becoming wet)
  • Remove all conditions from the conditions section
  • In your “choose” section, you will want two options. The first will have three conditions listed: trigger_id is “open”, water sensor is dry, and temp is above. The action is obviously to open the window. The second option for the choose section only has one condition: trigger_id is “close”. The action for that is obviously to close the window.

Adding hysteresis to your temperature threshold is still necessary.

The other thing you need to consider is that your water sensor is inside and affected by the window. When the window is closed and the water sensor changes from wet to dry, you don’t actually know if it stopped raining. The sensor will dry when the window is closed and could cause the window to open during a rain storm. It will get wet and close the window, then dry out, and open the window again while it is still raining. The cycle will repeat. You would need a way to know whether it stopped raining while the window was closed in order to avoid this.

Thank you for this, I really appreciate it.
Regarding the rain sensor, I soldered wires to the contact of the Aquara and ram them outside to a rain sensor module, so it’s outside.
Touch wood the reporting of moisture has not let me know down yet :slight_smile: