Wait until condition not met with Delay

Hi, Community!
I have few relays, which automatically turning off in under\over voltage cases, and doesnt turn on after it normalized.
I’ve created following automation:

      - id: "UPS Power Restore"
        description: Restore UPS after high voltage
        alias: ups_ac_restore
        initial_state: true
        mode: single
        trigger:
          - platform: state
            entity_id: binary_sensor.home_ac_in
            to: "off"
            for:
              hours: 0
              minutes: 10
              seconds: 0
              
        condition:
          - condition: state
            entity_id: switch.control_mode
            state: "on"
          - condition: state
            entity_id: switch.home_ups
            state: "off"
          - condition: numeric_state
            entity_id: sensor.zal_charger_voltage
            below: 260
        action:
          - service: switch.turn_on
            target:
              entity_id: switch.home_ups

in general, this automation will only work if the voltage is normal after 10 minutes, but if it is not, the device will remain off.

Please tell me how to create an automation that will check the voltage every ten minutes after turning off the power, and if it is OK, turn on the device, if not, wait 10 minutes and try again

Anyone can help? Tried many variants (even with chatgpt=))), but no luck

something like that doesnt works:

  action:
  - repeat:
      sequence:
      - condition: numeric_state
        entity_id: input_number.test_input_number
        below: 260
      - target:
          entity_id: switch.home_ups
        action: switch.turn_on
      - delay:
          hours: 0
          minutes: 10
          seconds: 0
      until:
      - condition: numeric_state
        entity_id: input_number.test_input_number
        below: 260

I would approach this differently. Generally if you have to keep re-checking something after a period of time you’re doing it wrong. Instead you should trigger the automation when whatever the thing you’re waiting for actually happens. Or, in the case of waiting for multiple things to be true, you trigger on each one of those things individually and then check if each one is true.

But the issue with your particular requirement is that you’re waiting for a numeric state to be past a threshold for a period of time, and that is not available as a condition. (Specifically, you can’t do the for: 10 minutes part if it’s a numeric state condition.)

So, I would create a template binary sensor helper in YAML and use the delay_on option (which is not an available option if created in the UI).

template:
  - binary_sensor:
      - name: UPS AC Off for 10 minutes
        state: "{{ is_state('binary_sensor.home_ac_in', 'off') }}"
        delay_on:
          minutes: 10

That will give you a sensor that is true when the home_ac_in sensor has been off for at least 10 minutes.

Then it’s a simple task to use that new sensor and your other two switches as triggers and also all three as conditions.

1 Like

Thanks for the help, but this is not quite what I needed.
I implemented it differently, using a timer, the end of which is a trigger for repeating the automation.

To be honest, no matter how much I tried, I still didn’t understand the sense of the function
repeat/until

- id: Восстановления питания UPS
  alias: ups_ac_restore
  description: Восстановления питания UPS после проблем с AC
  
  trigger:
  - platform: state
    entity_id: switch.home_ups
    to: 'off'
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.ups_ac_restore
    
  condition:
  - condition: state
    entity_id: switch.control_mode
    state: 'on'
  - condition: state
    entity_id: switch.home_ups
    state: 'off'
  - condition: not
    conditions:
    - condition: state
      entity_id: sensor.zal_charger_voltage
      state: unavailable

  action:
      - delay:
          hours: 0
          minutes: 10
          seconds: 0
      - choose:
        - conditions:
          - condition: numeric_state
            entity_id: sensor.zal_charger_voltage
            below: 260
          sequence:
          - target:
              entity_id: switch.home_ups
            action: switch.turn_on
            data: {}
        - conditions:
          - condition: numeric_state
            entity_id: sensor.zal_charger_voltage
            above: 260
          sequence:
          - service: notify.telegram
            data:
              message: "AC High. UPS Turn on delayed. Voltage: {{ states('sensor.zal_charger_voltage') }}"
          - service: timer.start
            target:
              entity_id: timer.ups_ac_restore
            data_template: 
                duration: >
                 00:01:00
        
  initial_state: true
  mode: single

Hey as long as you’re happy with it. As you use home asssistant more you’ll start to see why long delays and time-based re-checks of conditions are bad practice in automations.