Help with conditional trigger to run dehumidifier at night

Hello, I have a humidity meter: sensor.basement_storage_humidity
I also have a switch that turns the dehumidifier on: switch.dehumidifier_basement
The dehumidifier is slightly noisy so I only want it to run at night (23:00 to 07:00) and only if the humidity is high.

The problem with the script below is that it sometimes keeps running after 7am.

alias: Basement dehumidifier trigger
description: ""
trigger:
  - type: humidity
    platform: device
    device_id: 131d3a564b02aa162819f4cdef223792
    entity_id: 2c45caabfe7e8c7faf53283b54430bb1
    domain: sensor
    above: 60
condition:
  - condition: time
    after: "23:00:00"
    before: "07:00:00"
action:
  - type: turn_on
    device_id: 83be0c10b41b64450fd7fd90b275c3b9
    entity_id: 89e12c907ac67757507a048346531845
    domain: switch
mode: single

Is there an elegant solution to this?

If the humidity rises above 60 at, say, 06:45, the automation is triggered, the condition evaluates to true and the humidifier is turned on.

There’s nothing in that automation to turn off the humidifier, only to turn it on.

Here’s a quick fix:

alias: Basement dehumidifier trigger
description: ""
trigger:
  - id: 'on'
    type: humidity
    platform: device
    device_id: 131d3a564b02aa162819f4cdef223792
    entity_id: 2c45caabfe7e8c7faf53283b54430bb1
    domain: sensor
    above: 60
  - id: 'off'
    platform: time
    at: '06:59:59'
condition:
  - condition: time
    after: "23:00:00"
    before: "07:00:00"
action:
  - service: 'switch.turn_{{ trigger.id }}'
    target:
      entity_id: switch.your_humidifier
mode: single

Replace switch.your_humidifier with the entity_id of the switch that controls the humidifier.


EDIT

Enhancement. Changed time as per Sir_Goodenough’s suggestion (see below).

1 Like

I would suggest ‘06:59:59’ to cover the case that it tries to start up again in the last minute. This will leave almost no time for that to happen.

1 Like

It didn’t work as the humidifier could stay on. Hence I am trying this double-automation recommendation by ChatGPT

alias: "Dehumidifier: Turn on if above 60%"
description: ""
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.basement_storage_humidity
    above: 60
condition:
  - condition: time
    after: "23:00:00"
    before: "07:00:00"
action:
  - service: switch.turn_on
    target:
      entity_id: switch.dehumidifier_basement
mode: single
alias: "Dehumidifier: turn off at 7am"
description: ""
trigger:
  - platform: time
    at: "07:00:00"
action:
  - service: switch.turn_off
    target:
      entity_id: switch.dehumidifier_basement
mode: single
1 Like

That’s very odd given that the example I posted contains a Time Trigger designed to turn off the switch at 06:59:59.

Here’s the Time Trigger set to trigger at 06:59:59. Note that its id is off.

  - id: 'off'
    platform: time
    at: '06:59:59'

Here’s the service call that will become switch.turn_off after the template replaces trigger.id with off.

  - service: 'switch.turn_{{ trigger.id }}'
    target:
      entity_id: switch.your_humidifier

Did you check the trace file of my version of the automation when it failed to turn off the switch at 06:59:59? Perhaps all that’s needed is to change the time to be slightly earlier (06:59:55) to ensure it passes the Time Condition (time must be before 07:00:00`).

In your first post you asked if there’s an “elegant solution”. Adding a second automation to do something that the first automation can do isn’t exactly the most elegant solution.

On a related note, have you considered using the Generic Hygrostat integration?

It allows you to set the desired humidity and it will automatically control the switch to maintain the value. You can set the desired value via the UI using the Humidifier card or via a service call.

The only thing your automation would need to do is enable it at 23:00 and disably it at 07:00. Between those hours, it will determine if it needs to turn on the switch or not.

In my opinion, that’s the optimal way to handle humidification or dehumidification. Let me know if you need help configuring it.

I wasn’t aware of that integration however I don’t see how to create it in HA (without doing direct coding which I have so far avoided).
It’s not anywhere under integrations, automations or helpers?