Using condition:state within an if then block

Hey hey.
Im using a conditional inside an if then block:

In easy words this means:
if:
time = 08.00pm
then

  condition: state
  entity_id: binary_sensor.wassersensor_pflanzenbecken_feuchtigkeit
  state: "off"

do something WITHIN this if then block
do something else OUTSIDE the if then block

If the condition: state is false, does it skip the if block only (and keep doing whats after the if then block) or does it skip the whole script?

Hi Florian Vieten,
Chances are it will just skip the block but that is a pseudo answer based on the provided pseudo code.

Generally I use choose statements, they (usually) work better for me in most instances.

Ah sorry.
I didnt post code, because i thought it will make things more complex than needed.

So im checking if a moisture sensor is dry, to stop my robot vacuum from sucking too many water.

Here is a part of my code:

if:
  - condition: state
    entity_id: binary_sensor.wassersensor_pflanzenbecken_feuchtigkeit
    state: "off"
  - condition: state
    entity_id: input_boolean.roborock_flur_nicht_wischen
    state: "off"
then:
  - alias: wenn Batterie unter 100% warten
    if:
      - condition: numeric_state
        entity_id: vacuum.flur
        below: 100
        attribute: battery_level
    then:
      - wait_for_trigger:
          - platform: numeric_state
            entity_id:
              - vacuum.flur
            above: 99
            attribute: battery_level
        timeout:
          hours: 4
          minutes: 0
          seconds: 0
          milliseconds: 0
        continue_on_timeout: false
    enabled: true
  - condition: state
    entity_id: binary_sensor.wassersensor_pflanzenbecken_feuchtigkeit
    state: "off"
  - service: telegram_bot.send_message
    metadata: {}
    data:
      target:
        - xxxxx
      message: |-
        Robo Flur start wischen Aquaristik.
        Akku: {{state_attr('vacuum.flur','battery_level')}} %
      disable_notification: true
  - alias: Saugen aus
    service: vacuum.set_fan_speed
    target:
      entity_id:
        - vacuum.flur
    data:
      fan_speed: "off"
  - alias: Wischen intensiv
    service: vacuum.send_command
    target:
      entity_id:
        - vacuum.flur
    data:
      command: set_water_box_custom_mode
      params: 203
  - alias: Strecke grĂźndlich
    service: vacuum.send_command
    target:
      entity_id:
        - vacuum.flur
    data:
      command: set_mop_mode
      params: 301
  - alias: Aquaristik wischen
    service: roborock.vacuum_clean_segment
    metadata: {}
    data:
      segments:
        - 22
    target:
      entity_id:
        - vacuum.flur
  - alias: warten bis fertig
    wait_for_trigger:
      - platform: state
        entity_id:
          - vacuum.flur
        attribute: status
        to: charging
        for:
          hours: 0
          minutes: 0
          seconds: 40
    timeout:
      hours: 4
      minutes: 30
      seconds: 0
      milliseconds: 0
    continue_on_timeout: false
  - service: telegram_bot.send_message
    metadata: {}
    data:
      target:
        - xxxx
      message: |-
        Robo Flur fertig wischen Aquaristik.
        Akku: {{state_attr('vacuum.flur','battery_level')}} %
      disable_notification: true

 - will the following code here executed if the "wassersensor" is not "trocken"?

if:
  - condition: numeric_state
    entity_id: vacuum.flur
    below: 100
    attribute: battery_level
then:
  - wait_for_trigger:
      - platform: numeric_state
        entity_id:
          - vacuum.flur
        above: 99
        attribute: battery_level
    timeout:
      hours: 4
      minutes: 0
      seconds: 0
      milliseconds: 0
    continue_on_timeout: false
enabled: true
alias: Fan Speed normal
service: vacuum.set_fan_speed
target:
  entity_id:
    - vacuum.flur
data:
  fan_speed: balanced
[...]

The script checks if there is moisture.
If there is no moisture it waits until my robot vacuum gots a certain amount of battery.
To ensure that there is no moisture while loading I did insert the condition to check again.

if the condition block is false, it will execute the else block then continue.

so conceptually:

if
  false
then
   1
else
   2
endif
3

will do 2 then 3

if 
   true
then
   1
else 
   2
endif
3

will do 1 then 3

with out the else blocks:

if
   false
then 
   1
endif
3

just does 3

This random condition is in there, and you gave 2 if’s at the same level, I really cannot tell. Maybe someone else wants to take a shot. If you had this built as a choose I would probably be able to tell. As it stands, I would be surprised if this passes YAML config test. Did you perhaps run this thru an AI to help you?

1 Like

yeah, that conditional right in the middle is a good point from @Sir_Goodenough

if you put a test condition in the sequence like that, the condition will be tested, and if it fails, the sequence will stop.

ie, since this is not wrapped in an “if”, … then if:

  - condition: state
    entity_id: binary_sensor.wassersensor_pflanzenbecken_feuchtigkeit
    state: "off"

fails (the sensor is not “off”) then i believe it stops there and the rest of the code will not get executed.

if you have 2 conditions in an ‘if’, then i believe both conditions must be true for the if to be true. (unless you explicitly wrap the two 'if’s in with an ‘or’

hmm
I did a testing script. Therefore I added 3 helper switches:
AACondition, AATasksAfterIFThenExecuted and AATasksWithinIfThenExuted.
Should be obvious what they are doing.
To simulate my robot vacuum script I created following:

alias: AA Test
sequence:
  - if:
      - condition: state
        entity_id: input_boolean.aacondition
        state: "on"
    then:
      - delay:
          hours: 0
          minutes: 0
          seconds: 5
          milliseconds: 0
      - condition: state
        entity_id: input_boolean.aacondition
        state: "on"
      - service: input_boolean.turn_on
        metadata: {}
        data: {}
        target:
          entity_id: input_boolean.aataskswithinifthenexuted
  - service: input_boolean.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: input_boolean.aatasksafterifthenexecuted
description: ""

Starting the script with all three helpers being false.
Obviously the if then block gets skipped and AATasksAfterIffThenExuted gets true while AATasksWithinIfThenExuted stays false.

Interesting thing here is starting the script with “AACondition” being true and setting it to false within the 5 second delay time.
In this case “AATasksAfterIFThenExecuted” goes true, while “AATasksWithinIfThenExuted” stays false.

For me this means my robot script should work as expected.
If the moisture sensor is dry it will start the if block.
If neccesarry the script will wait for my robot to recharge the battery and check again if the moisture sensor did get wet in the meantime.
If not (still dry) script resums - if it got wet the then block will get skipped and the script will keep running (so my robot cleans the “dry” rooms.