Daily automation repetition

Hello, everyone,
and I apologize in advance - I’m a complete beginner and I’m also new here on the forum - so I apologize for any naive questions and for any possible transgressions against forum ethics.
I wrote my first automation to do this: When the humidity reaches “X” in the room, turn on the outlet where the dehumidifier is plugged in, turn the outlet off again when the humidity drops to “Y”. Do this only between H1H1:M1M1 and H2H2:M2M2 each day.
Yaml file is here:
To turn on:

alias: dehumidifier ON
description: ""
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.sensor_temperature_and_humidity
    above: "55"
condition:
  - condition: time
    after: "06:00:00"
    before: "21:30:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun
action:
  - type: turn_on
    device_id: c59c2ab0c5cb3f80f3b07ecd6533927a
    entity_id: e08c373e155f812974df46e17d43cfee
    domain: switch
  - service: notify.persistent_notification
    metadata: {}
    data:
      message: dehumidifier ON
  - service: notify.notify
    metadata: {}
    data:
      message: dehumidifier ON
mode: restart

To turn off:

alias: dehumidifier OFF
description: ""
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.sensor_temperature_and_humidity
    below: "50"
condition:
  - condition: time
    after: "06:00:00"
    before: "22:00:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun
action:
  - type: turn_off
    device_id: c59c2ab0c5cb3f80f3b07ecd6533927a
    entity_id: e08c373e155f812974df46e17d43cfee
    domain: switch
  - service: notify.persistent_notification
    metadata: {}
    data:
      message: dehumidifier OFF
  - service: notify.notify
    metadata: {}
    data:
      message: dehumidifier OFF
mode: restart

Automation “dehumidifier ON” works fine, but only on one day. The following day, starting from H1H1:M1M1, the automation “dehumidifier ON” does not start, even though all conditions (humidity, hour, day) are met. If I start the automation manually, the cycles ON and OFF works normally throughout the day until H2H2:M2M2.
It’s obvious that I have a logical error somewhere, or that there must be a completely different way to do such an automation - and I can’t figure out how. Note: I could use some of the blueprints posted here, but I want to figure out how to go about creating my own scripts. Thank you very much for your help!

Using numeric_state with an above or below will only trigger when the humidity passes that threshold. Ie if your humidity is already above 55 at 6am, you will not get a trigger to turn this on.

Therefore, you need a slightly different logic.

In addition to what you already have, you could can add a trigger for for time at 6am and a condition that humidity is above 55 for your on and likewise a trigger for 21.30 in your off (presuming you dont want it running over night).

Oh right - my logical error … thanks for pointing it out! I’ll continue to debug …

If you still have patience with me …:-). I tried to find a way to add a “switch on at 06:00 in the morning” trigger, but I couldn’t figure it out. The time condition can only be specified as an interval of time after and time before, but not as if the time value reaches just 06:00 then trigger the switch.
Thus, I need to write in YAML notation: If the time is 06:00 and the humidity is >=55% then trigger the switch. How is this syntactically correct? For example, I tried this:

condition: time
 #    here I want to express "is just equal to"
    is: "06:00:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun

but this syntax is not valid. Apparently, the time condition always has to be before and after (Conditions - Home Assistant)

Could you please give me an example of how to specifically implement your proposed condition in my existing YAML? Thank you very much, it would help me to understand the logic of HA…

You need to add a time trigger and a matching condition.

trigger:
  - platform: time
    at: "06:00"
  - platform: numeric_state
    entity_id: sensor.sensor_temperature_and_humidity
    below: 55
condition:
  - condition: time
    after: "05:59"
    before: "22:00"
  - condition: numeric_state
    entity_id: sensor.sensor_temperature_and_humidity
    below: 55

Triggers:

  • at 06:00
  • whenever the humidity drops below 55;

The conditions then check:

  • it’s between 06:00 (I use 05:59 to be on the safe side to avoid any confusion between at and after) and 22:00
  • the humidity is below 55.

If both conditions are true, the action is run.

You could use something like this to handle both on and off.

alias: dehumidifier ON/OFF
description: ""
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.sensor_temperature_and_humidity
    above: "55"
    id: "on"
  - platform: numeric_state
    entity_id:
      - sensor.sensor_temperature_and_humidity
    below: "50"
    id: "off"
  - platform: time
    at: "06:00:00"
    id: "on"
  - platform: time
    at: "21:29:00"
    id: "off"
condition:
  - condition: or
    conditions:
      - condition: time
        after: "06:00:00"
        before: "21:30:00"
      - condition: and
        conditions:
          - condition: trigger
            id: "on"
          - condition: numeric_state
            entity_id: "sensor.sensor_temperature_and_humidity"
            above: 55
          - condition: time
            after: "06:00:00"
            before: "21:30:00"
action:
  - type: "turn_{{trigger.id}}"
    device_id: c59c2ab0c5cb3f80f3b07ecd6533927a
    entity_id: e08c373e155f812974df46e17d43cfee
    domain: switch
  - service: notify.persistent_notification
    metadata: {}
    data:
      message: "dehumidifier {{trigger.id}}"
  - service: notify.notify
    metadata: {}
    data:
      message: "dehumidifier {{trigger.id}}"

But perhaps a better way of doing it is to use the generic hygrostat

To Troon: yes, defining “trigger” makes sense, I didn’t know which way to go. Perhaps an even better way was suggested by Hellis81. Thank you both very much for the inspiration!
I’ve also already defined the generic hygrostat, I see it as an entity, but so far I haven’t managed to build it into any automation. Maybe the approaches you suggest will help me.

I have not set up a hygrostat, but I assume it has service calls for turn on and off.
If it does, then you just need to set it up to be between 50 and 55 and then have time triggers to turn on and off and that is all.

As @Hellis81 mentioned, the Generic Hygrostat integration can handle a lot of the repetitive functions in the automation examples shown so far. Once configured, the integration will create an entity with humidifier as its domain. The following automation for a humidifier entity would replace your two existing automations from the original post:

alias: Humidity Control - Time based
trigger:
  - platform: time
    at: "06:00:00"
    id: 'on'
  - platform: time
    at: "22:00:00"
    id: 'off'
condition: []
action:
  - service: humidifier.turn_{{trigger.id}}
    data:
      entity_id: humidifier.EXAMPLE
  - service: notify.persistent_notification
    metadata: {}
    data:
      message: dehumidifier {{ trigger.id | upper }}
  - service: notify.notify
    metadata: {}
    data:
      message: dehumidifier {{ trigger.id | upper }}
mode: single

It also makes changing the targeted humidity level easier from both the frontend and other automations since you can use the dashboard card and/or service calls.

Yes, this script works very well, the inclusion of the “generic hygrostat” or the entity “humidifier.XXXXX” created by it is very effective, moreover this entity can be placed in the UI and temporarily manually controlled - great, thank you very much @Didgeridrew for the inspiration! Especially valuable for me is the insight on using humidifier.turn_{{trigger.id}} variables - I have almost no programming experience (although I’ve been around computers for several years) - I’m a complete self-taught amateur. Thanks again!