Loop on a timer OR an entity

Hi,
In an automation that is triggered by a humidity sensor I want to do the following

  • turn on the heating
  • wait until either 20 minutes have passed or a certain temperature is reached.

I haven’t found a good way of coding this second step

What do you want to happen when this second step is achieved?

Turn it off again? If so, I’d suggest a second automation with two triggers (which are OR by default):

  • the heating entity has been on (or whatever state you see) for 20 minutes
  • the temperature goes above your threshold (numeric state trigger)

Of course, the first automation might turn it straight back on again…

There are a bunch of actions running before this and another bunch of actions after this.
And no, I don’t want to put this in a second automation as this is not something that should run independently

Long-running actions are often not a good idea as they’re prone to interruptions with HA restarts, reloads etc.

If you must do this, try wait_for_template or wait_for_trigger to test the temperature (docs), with a 20 minute timeout.

The most reliable way to incorporate an extended period of waiting is to use a timer instead of adding delays or other wait actions to your automation. This can be combined with other triggers by using a Choose action or templates.

trigger:
  - platform: numeric_state
    entity_id: sensor.humidity
    above: 60
    for: "00:01:00"
    id: 'on'
  - platform: numeric_state
    entity_id: sensor.temperature
    above: 75
    for: "00:01:00"
    id: 'off'
  - platform: event
    event_type: timer.finished
    event_data:
      event_id: timer.humidity
    id: 'off'
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
             id: 'on'
        sequence:
          - service: HOWEVER YOU TURN ON YOUR HEATING
          - service: timer.start
            target:
              entity_id: timer.humidity
            data:
              duration: "00:20:00"
      - conditions:
          - condition: trigger
            id: 'off'
        sequence:
          - service: HOWEVER YOU TURN OFF YOUR HEATING
1 Like

Just a small clarification, as of version 2022.11, automations and scripts that are in-progress are not terminated by reloading automations/scripts.

Reference: Smarter Reloading of Automations and Scripts

Restarting Home Assistant will, of course, terminate in-progress automations and script.

1 Like

Hi,
My current trigger is the closing of the window. As mentioned I don’t want this to go off all the time so I’m not sure how to combine what I want with what you wrote…

I currently had the script below with 2 parallel actions but this complete both fully before continuing…

alias: Onze Slaapkamer ontvochtigen
description: ""
trigger:
  - type: not_opened
    platform: device
    device_id: d911041a0e31de814c11ca3ba36ed206
    entity_id: binary_sensor.raam_onze_slaapkamer_on_off
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 10
condition:
  - condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.vochtsensor_onze_kamer_humidity
        above: 60
      - condition: numeric_state
        entity_id: sensor.airco_onze_slpk_room_temperature
        below: 16
action:
  - service: script.airco_onze_slaapkamer_heat
    data: {}
  - parallel:
      - delay:
          hours: 0
          minutes: 20
          seconds: 0
          milliseconds: 0
      - repeat:
          while:
            - condition: numeric_state
              entity_id: climate.airco_onze_slpk
              attribute: current_temperature
              below: 16
          sequence:
            - delay:
                hours: 0
                minutes: 1
                seconds: 0
                milliseconds: 0
  - service: script.airco_onze_slaapkamer_off
    data: {}
mode: single

It’s all a matter of setting up your conditions to keep your scripts from being fired when you don’t want them to. The basic outline would be something like what follows. You may need to add other conditions to the second option of the Choose to control when the “turn off” script is fired.

alias: Onze Slaapkamer ontvochtigen
description: ""
trigger:
  - type: not_opened
    id: 'on'
    platform: device
    device_id: d911041a0e31de814c11ca3ba36ed206
    entity_id: binary_sensor.raam_onze_slaapkamer_on_off
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 10
  - platform: numeric_state
    id: 'off'
    entity_id: climate.airco_onze_slpk
    attribute: current_temperature
    below: 16
  - platform: event
    event_type: timer.finished
    event_data:
      event_id: timer.slpk_airco
    id: 'off'
condition: []
action:
  - choose:
    - conditions:
        - condition: trigger
          id: 'on'
        - condition: numeric_state
          entity_id: sensor.vochtsensor_onze_kamer_humidity
          above: 60
        - condition: numeric_state
          entity_id: sensor.airco_onze_slpk_room_temperature
          below: 16
      sequence:
        - service: script.airco_onze_slaapkamer_heat
          data: {}
        - service: timer.start
          target:
            entity_id: timer.slpk_airco
          data:
            duration: "00:20:00"
    - conditions:
        - condition: trigger
          id: 'off'
      sequence:
        - service: script.airco_onze_slaapkamer_off
          data: {}
        - condition: template
          value_template: "{{ trigger.platform == 'numeric_state'}}"
        - service: timer.cancel
          target:
            entity_id: timer.slpk_airco