How to execute an "if" statement without triggering conditions?

Hello!
I have one questions related to “if” statement executed in ESPHome devices.

Recently I posted one question related to a control logic implemented in my dehumidifier (tanks @tom_l for the solution!). That was:

interval:
  - interval: 1min
    then:
      if:
        condition:
          or:
            or:
            - lambda: |-
                return id(h1).state >68;
            - lambda: |-
                return id(h2).state >68;
            - lambda: |-
                return id(h3).state >68;
            - lambda: |-
                return id(h4).state >68;
            - lambda: |-
                return id(h5).state >68;
            - lambda: |-
                return id(shth1).state >68;
            
        then:
          - switch.turn_on: relay

I have read through the documentation, and in most of the examples, the “if statement” routines are always triggered by a “triggering condition”. In the example above is “every minute” (interval).

My question is: If I want the device to execute this routine all the time, not just when some kind of triggering condition is set, how should I proceed?

I also went into the “script”, but also there, I always see a “triggering condition” which I don´t want to set.

Thanks in advance for help!

That doesn’t really make sense. You want to have a trigger, specifically to prevent looping forever testing conditions.

Unless you are simplifying, here, you can achieve the same with something like

sensor:
  - platform: foobar
    id: h1
    # ...
    on_value_range:
      - above: 68.0
        then:
          - switch.turn_on: relay
  - platform: foobar
    id: h2
    # ...
    on_value_range:
      - above: 68.0
        then:
          - switch.turn_on: relay

for each of the involved sensor.

Thanks @koying for the kind answer.

Why you said that it does not make sense? If a condition needs to be checked no matter what is happening on other triggering conditions, then this should be possible, isn´t it?
If I look at Arduino based programming, I can simply include an “if-then-else” statement in the main loop and this is checked all time, not necessarily affecting the overall device performances.

Looking at the solution you proposed, this will allow every sensor reading to trigger an action, but will not allow, as in my example above, to make an “and” or “or” logic between them. How to achieve that then, without a triggering event?

Not checked “all time”. It’s checked once per loop, which is basically an interval of 0 seconds (if possible), so it’s triggered as well.

You code is a plain “or”, and what show will achieve the same.

Anyway, at the end of the day, you always want something to happen in response to another event. That’s triggering and, ideally, you want to be as specific as possible as for the trigger rather than use “whenever the code is hit”.

It’s a matter of mindset, really…

1 Like

To be clear, my answer was about why your or condition was not working (with one lambda in an OR condition), it was not a solution to your programming issues.

What Chris is saying is correct. Use the single on_value_range action for each sensor to turn the relay on (any sensor can turn on the relay = OR logic).

To turn your relay off use another on_value_range action but with conditions that check all the other sensors are also in the “off” range (= AND logic).

1 Like

Thanks @tom_l and @koying ,

this has been a very educational discussion for me!

I got now what you mean, and I fully agree that is a matter of mindset.

As a matter of fact I don’t need to check “all the time” but better “what is changing” so that is the triggering condition that you suggested (on value range).

Fully clear. I know what to do now!

Cheers!