Apply action until condition is met

Hi there,

How would I go about applying ‘wait until a condition is met then apply action’ after an if-then statement?

if:
  - type: is_power
    condition: device
    device_id: device1
    entity_id: device1_ac_in_power
    domain: sensor
    below: 350
  - condition: and
    conditions:
      - type: is_battery_level
        condition: device
        device_id: device1
        entity_id: device1_main_battery_level
        domain: sensor
        below: 50
then:
  - type: turn_on
    device_id: device2
    entity_id: device2_ac_enabled
    domain: switch
else:
  - type: turn_off
    device_id: device2
    entity_id: devic2_ac_enabled
    domain: switch

Before the else condition and action I’d like for the ac power to stay enabled until either device1’s ac in power reaches 350 and above or battery level for device1 reaches 79 then disable device2 ac. Somehow it would have to wait until this criteria is met.

type: turn_on
device_id: device2
entity_id: device2_ac_enabled
 domain: switch
 else:

This?

1 Like

It looks like that may work. I gave it a go but I’m not sure if all makes sense.

Trigger

type: battery_level
platform: device
device_id: device1
entity_id: device1_main_battery_level
domain: sensor
below: 51
enabled: false

Conditions

type: is_power
condition: device
device_id: device1
entity_id: device1_ac_in_power
domain: sensor
below: 350
enabled: false

Actions

repeat:
  until:
    - type: is_battery_level
      condition: device
      device_id: device1
      entity_id: device1_main_battery_level
      domain: sensor
      above: 79
    - condition: or
      conditions:
        - type: is_power
          condition: device
          device_id: device1
          entity_id: device1_ac_in_power
          domain: sensor
          above: 350
  sequence:
    - type: turn_on
      device_id: device2
      entity_id: device2_ac_enabled
      domain: switch
enabled: false
type: turn_off
device_id: device2
entity_id: device2_ac_enabled
domain: switch
enabled: false

I donot get it indeed…what are you trying to do?

1 Like

i have 2 ecoflow batteries. i want device 2 to activate its ac power out when device 1 drops either below 50% or 350w input ac power.
once device 2’s ac power out is on, it must begin charging until either device 1 is 79% charged or input ac power exceeds 350w in which case, turn off ac power out on device 2.

Could you please format your code using the </> button in the editor?
The code is currently difficult to read and understand, and most people may not make the effort to decipher it in its current format.
Properly formatting the code will greatly improve its readability.

1 Like

Still not 100% clear to me but will try to reword it using terminology as power out and on in the same sentence is confusing (for me)

  1. you seem to have two triggers, DEVICE1 either %<50 or power<350, your automation shows % followed by a power condition, which is not what you wrote.
  2. if triggered then DEVICE2 to has to power on
  3. DEVICE2 has to stay on until DEViCE 1 reaches 79% or power>350

means for me

  1. two triggers
  2. action to put device2 on
  3. action to wait for template indicating ‘true’ if DEVICE1 either the %>75 or power>350
  4. action to put device2 off

Have a look at the wait template.

Yes this is exactly what I’m intending. Will the code I have achieve this? I’ve gone over it many times I cant quite tell what might be wrong.

  1. Trigger - if DEVICE1 is <50% charge
  2. Condition for trigger - DEVICE1 is also below 350w AC input power draw (i.e NOT charging from the grid) in addition to the <50% charge trigger. If both conditions are met.
  3. Action - Activate DEVICE2’s AC power output, and wait until the condition of either >79% battery charge is reached OR >350w AC input power draw (i.e NOW charging from the grid) occurs on DEVICE1.
  4. Once conditions for repeat is met in the third step, turn off AC power output on DEVICE2

I hope this makes more sense.

Try to build it step by step in the template editor

something like this but I don’t know your sensor, its value and/or attributes.

{{ states("device1") | int(0) > 50 and state_attr("device1","power") | int(0) < 350 }}

So this is not the solution, just something to start with and play.
Note the conversion to int, with a default value as it might be unknown or undefined

This is not close to what I proposed and not what you wrote, but as @Olivier1974 proposed, you can play around

EDIT, the other option is to write one automation for turning it on and another for turning it off, this prevents the need to ‘waiting’ a long(er) time, i.e. if your server has a hickup the automation will not continue where it was stopped

I agree, waiting is usually not a good practice.
I’m using it with TTS because I’ve to wait for the speaker to go back to idle to detect the end of the TTS sequence.
But this is obviously a small waiting time, maximum 5-10 seconds.

That is actually a brilliant idea. I will separate the automations into two separate automations.