Combined and, or, not conditions in ESPHome

I have a hot water recirc pump that I want to run 2 minutes out of every 20 minutes during certain parts of the day. That all works with an interval, an template switch, and a time automation to set the template switch:

switch:
  - platform: gpio
    name: "Hot Pump Relay"
    pin: GPIO12
    id: relay
    
  - platform: template
    name: "Run Pump Allowed"
    id: runok
    optimistic: on
    
interval:
  - interval: 20min
    then:
      if:
        condition:
          switch.is_on: runok
        then:
          - switch.turn_on: relay
          - delay: 2min
          - switch.turn_off: relay
 
time:
  - platform: homeassistant
    id: homeassistant_time
    on_time:
      #Every morning at 6:30am
        - seconds: 0
          minutes: 30
          hours: 3
          then:
            - switch.turn_on: runok
      #Every night at 10:45pm
        - seconds: 0
          minutes: 45
          hours: 22
          then:
            - switch.turn_off: runok
  

Now I would like to add a few switches that HA can set to affect the operation:
disable_pump : This would allow HA to prevent the pump from running when the alarm is set (indicating nobody home)
override_pump: This would allow HA to force the pump to cycle out-of-hours, either by setting a switch, or if lights go on in the house, or perhaps motion detectors in the house (exiting)

These would be two additional template switches, no problem there.

This means the condition in the interval now has to have the following logic:

(runok AND NOT(disable_pump)) OR override_pump

I can see how to do this in HA if this were an HA automation, but how do I do this on ESPHome? Is there an option of nested AND/OR conditionals? Or do I have to build a lambda condition? Not good at Lambdas yet, but I can learn with a little help!

I have not seen any examples of nested AND/OR for ESPHOME, yet.

Thanks
Chuck

Disable pump and override pump, are they booleans?

Yes I think you need to go lambda. At least I believe it will be easier to read than doing it in the normal if else that is used in ESP-Home.

Thanks

Here is what I am trying:

switch:
  - platform: gpio
    name: "Hot Pump Relay"
    pin: GPIO12
    id: relay
    
  - platform: template
    name: "Run Pump Allowed"
    id: runok
    optimistic: on
    
  - platform: template
    name: "Run Pump Disabled"
    id: pump_disable
    optimistic: on
    
  - platform: template
    name: "Run Pump Forced"
    id: pump_override
    optimistic: on
    
interval:
  - interval: 20min
    then:
      if:
        condition:
          lambda: !lambda |-
            return (id(runok).state && !id(pump_disable).state) || id(pump_override).state;
        then:
          - switch.turn_on: relay
          - delay: 2min
          - switch.turn_off: relay

time:
  - platform: homeassistant
    id: homeassistant_time
    on_time:
      #Every morning at 6:30am
        - seconds: 0
          minutes: 30
          hours: 3
          then:
            - switch.turn_on: runok
      #Every night at 10:45pm
        - seconds: 0
          minutes: 45
          hours: 22
          then:
            - switch.turn_off: runok
 

So far it looks good, need to try it overnight, but I think this was simpler than I imagined. Thanks to help from ssieb on Discord

Chuck

That is how I would do it also.
I will be surprised if it doesn’t work