How to implement STATE-based (as oppposed to EVENT-based) automation?

Something like this? (I agree with others, every minute is excessive and the hygrostat would be a better option).

trigger:
  - platform: time_pattern
    minutes: /1
condition:
  - condition: time
    after: "09:00:00"
    before: "23:00:00"
action:
  - if:
      - condition: numeric_state
        entity_id: sensor.indoor_average_humidity
        above: 50
    then:
      - service: switch.turn_on
        target:
          entity_id: switch.<your switch>
    else:
      - service: switch.turn_off
        target:
          entity_id:
            - switch.<your switch>
1 Like

(this is kind of minor but isn’t every minute excessive? you might also want to build in some hysteresis, so I feel the generic hygrostat might be solving all those nuances for you, thought I’ve never used it myself)

Thanks. I am guessing that I have to write a second automation for the upper-level ELSE, where I turn the dehumidifier OFF during the night, right?

Outside of the time period 09:00-23:00? You can just add a time trigger of 23:00 and use a trigger id and an if block to accomplish that.

trigger:
  - platform: time_pattern
    minutes: /1
  - platform: time
    at: "23:00:00"
    id: time_off
condition:
  - condition: time
    after: "09:00:00"
    before: "23:00:00"
action:
  - if:
      - condition: trigger
        id:
          - time_off
    then:
      - service: switch.turn_off
        target:
          entity_id: switch.<your switch>
    else:
      - if:
          - condition: numeric_state
            entity_id: sensor.indoor_average_humidity
            above: 50
        then:
          - service: switch.turn_on
            target:
              entity_id: switch.<your switch>
        else:
          - service: switch.turn_off
            target:
              entity_id:
                - switch.<your switch>
1 Like

Trigger on:

  1. Above 50
  2. HA startup
  3. Dehumidifier turns off

Condition:

  1. Above 50
  2. Dehumidifier off

Action:

  1. Turn on dehumidifier
3 Likes

If you believe that then you have misunderstood how to use “event-based” triggers. The failure scenarios described in your first post can be mitigated by a proper combination of triggers. In other words, it should detect more than one “event” to make it more fault-tolerant. There’s no need to revert to periodic polling (which is an inefficient use of resources in an event-based system like Home Assistant).

Nevertheless, what you’re requesting is trivial to implement with the Generic Hygrostat integration and an automation that limits its operation to the desired hours. It provides the added benefit of allowing for the humidity level to be controllable via the UI.

Effectively, this is the same kind of request by users who attempt to create an automation to control a heater (the solution is to use the Generic Thermostat integration).


NOTE

You may wish to peruse the following topic which demonstrates how to make an automation more fault-tolerant.

1 Like

Thanks. Prima facie, I find it impossible to predict what could go wrong, though I will attempt to study the issue further, when I can afford the time. For now, I will take the easy way out, and see how that goes.

Thanks again. I will have to devote some time to studying this Hygrostat integration.

I suggest your investment of time will provide you with better returns if you implement the Generic Hygrostat integration. After that, a simple automation can control when it is enabled/disabled.

I believe the generic hygrostat has the same bug as generic thermostat.
It will govern the humidity but not the device.

I noticed this when the room was warm and it allowed be to turn on the switch to the radiators, in my opinion the code should switch off the device in such cases.
I assume the hygrostat has the same issue since the code is probably based on each other.

@code-in-progress: Followed your instructions, and things work fine … except that the device does not get turned off @11PM. What am I doing wrong? Here’s the code:

alias: HisenseDehumidifier
description: ""
trigger:
  - platform: time_pattern
    minutes: /1
  - platform: time
    at: "23:00:00"
    id: time_off
condition:
  - condition: time
    after: "09:00:00"
    before: "23:00:00"
action:
  - if:
      - condition: trigger
        id:
          - time_off
    then:
      - service: switch.turn_off
        metadata: {}
        data: {}
        target:
          device_id: <some ID>
    else:
      - if:
          - condition: numeric_state
            entity_id: sensor.meter_plus_e84f_humidity
            above: 50
        then:
          - service: switch.turn_on
            metadata: {}
            data: {}
            target:
              device_id: <some ID>
        else:
          - service: switch.turn_off
            metadata: {}
            data: {}
            target:
              device_id: <some ID>
mode: single

What does the automation trace show?

1 Like

In addition to what @Tinkerer said, I suspect it might be using the device id versus the entity id. I would try it with entity_id: <your entity> and see if that works. There is very little confidence that the device id is actually correct (I avoid using them at all and favor entities).

EDIT: Never respond to forum posts on a Monday morning before you’ve had coffee. :coffee:

change

before: "23:00:00"

to

before: "23:00:01"
1 Like

I said nothing. :zipper_mouth_face:

omg… lmao. Sorry!!! Monday morning pre-coffee… that’s my excuse. :smiley:

2 Likes

As @fantangelo alludes the issue could just be timing, but I would address it a little differently:

...
condition:
  - or:
      - condition: time
        after: "09:00:00"
        before: "23:00:00"
      - condition: trigger
        id: time_off
action:
....

Thanks, guys, looks like @fantangelo’s solution might be what I need, as per this: leaving the before clause at 23:00:00, the automation will ignore the time_off event. By making it 23:00:01 I ensure that the time_off trigger is no longer ignored, hence the “Turn Off Dehumidifier” action gets performed. And that only happens once until 9:00 AM. Anyway, I will see what happens tonight, and report back.

Many thanks, again!

Alright, guys, here I am reporting back: things work fine … except that once in a while the automation just does not go through, and here’s a screenshot of the trace:

Just to remind you, the automation YAML code is this:

alias: MideaDehumidifier
description: ""
trigger:
  - platform: time_pattern
    minutes: /1
  - platform: time
    at: "23:00:00"
    id: time_off
condition:
  - condition: time
    after: "09:00:00"
    before: "23:00:40"
action:
  - if:
      - condition: trigger
        id:
          - time_off
    then:
      - type: turn_off
        device_id: <50>
        entity_id: <25>
        domain: humidifier
    else:
      - if:
          - condition: numeric_state
            entity_id: sensor.meter_plus_7ea4_humidity
            above: 50
        then:
          - type: turn_on
            device_id: <50>
            entity_id: <25>
            domain: humidifier
        else:
          - type: turn_off
            device_id: <50>
            entity_id: <25>
            domain: humidifier
mode: single

What could be going wrong? :thinking:

It’s working exactly how you configured it. It triggers every minute and at 23:00:00. That means it attempts to trigger twice at the exact same time at 23:00:00. Which one of the two simultaneous trigger events is processed first may not always be the same. While the automation is busy processing the first trigger event, it blocks the second trigger event because the automation’s mode is single.

Reference

Script Modes

Changing the value of mode to queued will prevent blocking (meaning that while busy processing actions for the first trigger, it queues the second trigger) but it can potentially cause undesirable behavior. Here’s what I mean:

Imagine a scenario where the humidity is above 50 at 23:00:00 and then the Time Trigger is processed first and then immediately followed by the Time Pattern Trigger. The initial trigger results in turning off the humidifier. The subsequent trigger results in turning the humidifier back on. The humidifier is left on overnight.

I still recommend you use the Generic Hygrostat integration.

2 Likes

Thanks. Just to keep things simpler, I yanked out all that time_off trigger stuff, and made a separate automation that kills both basement dehumidifiers at 11:00 PM. It’s as simple as that. Now the state of both dehumidifiers is checked every minute between 9:00 AM and 11:00 PM and acted accordingly based on the humidity level, while the second independent automation simply turns them off at 11:00 PM.

And yes, I plan to look into the Generic Hygrostat asap.

Many thanks for your input!