Loop within condition

Hi, my automation works as follows:

trigger (at a specific time)
condition (if a specific state is true)
action (do things).

it works well except it happened that the state checked in the condition was unavailable and the automation of course didn’t run. so I’m looking for a fix to that.
My idea is to have a condition such as:

if state is available (i.e. not unavailable)
  then check if it's true
else
  start a loop to check every 2 minutes if the state is still unavailable.

as soon as the state becomes available then the else is not executed anymore thus everything should be working fine.

I’m not able to implement the else block though, I’m currently using te UI.

No do not do that.

Do this:

trigger:
  - platform: time
    at: <specific time>
  - platform: state
    entitiy_id: <your entity here>
    to: <your wanted state>
condition:
  - condition: state
    entity_id: <your entity here>
    state: <your wanted state>
  - condition: time
    after: <specific time>
action:
  - service: <do things>

ok, not sure I understand the logic.
My automation:

  • at 6:40 every morning, if the calendar state is off, then turn the alarm off

the issue is that yesterday the calendar state was “unavailable” at 6:40 in the morning so the action wasn’t exectued.

with your solution I assume everything is in logical “OR” so I’m saying:

  • at 6:40

  • OR

  • when the calendar state changes to off

  • confirm the calendar state is off

  • OR

  • confirm it is after 6:40

  • turn the alarm off

I don’t get it…

Recommend reading the docs — conditions are an AND:

This

  • triggers at 06:40; also triggers when your calendar changes to the correct state
  • checks it’s after 06:40 AND the calendar is in the correct state
  • if the checks pass, run the action.

In other words, the action runs when both criteria are correct, at the point that the second one becomes correct.

In general, if your automation has timed “polling” loops in it, you’re not doing it the best way. As always, there are exceptions, but this isn’t one of them.

If the issue is the difference between false and unavailable, you can check those individually too. Describe the logic you want to use.

1 Like