ESPHome - how do I use the OR condition here?

Hi y’all,

I have this code:

sensor:
  - platform: seeed_mr24hpc1   
    custom_spatial_static_value:
      name: "Existence Energy"
      id: eval
      on_value:
        if:
          condition:
            sensor.in_range:
              id: eval
              above: 20.0        
          then:
            - script.execute: toggle_a0_with_cooldown
    custom_spatial_motion_value:
      name: "Motion Energy"
      id: mval

I’m trying to execute the script, “toggle_a0_with_cooldown”, when either Existence energy (id: eval) is above 20 or Motion energy (id: mval) is above 5. I’m not sure how to type the syntax for this. Here’s my attempt :

sensor:
  - platform: seeed_mr24hpc1    
    custom_spatial_static_value:
      name: "Existence Energy"
      id: eval
      on_value:
        if:
          condition:
            sensor.in_range:
              id: eval
              above: 20.0
              or:
                -sensor.in_range:
                  id: mval
                  above: 5             
          then:
            - script.execute: toggle_a0_with_cooldown
    custom_spatial_motion_value:
      name: "Motion Energy"
      id: mval

That def. doesn’t look right. Thanks a lot for your help.

The or should be below the condition key, with the two sensor conditions nest under it.

ESPHome - Automations - Conditions

2 Likes

You can also just do this:

          - if:
              any:
                - sensor.in_range:
                    id: eval
                    above: 20.0
                - sensor.in_range:
                    id: mval
                    above: 5
              then:
3 Likes

Thanks - both solutions work.

One final, sort of similar syntax related question (I can create another topic on this if it’s out of this thread’s scope)

I have this code where I’m trying to execute the script toggle_a1_with_cooldown using the “for” condition when Motion energy (id: mval) crosses and goes below 5 and stays below 5 for 30 consecutive seconds.

custom_spatial_static_value:
      name: "Motion Energy"
      id: mval
      on_value_range:
        if:
          condition:
            for:
              time: 30sec
              condition:
                below: 5                
          then:
            - script.execute: toggle_a1_with_cooldown

Esphome gives me an error that says “Unable to find condition with the name ‘below’.”

A regular google search isn’t giving me many examples where I’m able to see the for condition in action, which is so frustrating. And I’m not able to figure this out even using esphome docs.

Thanks again very much for the help.

Clyde may know a way… but I don’t think you can do it directly like that.

The only thing that comes to mind for me would be to set up a template binary sensor with delayed_on set for your desired duration.

1 Like

Analog Threshold Binary Sensor — ESPHome combined with a delayed_on filter will do it.

2 Likes

Works! I also had to add the invert: filter

Thank you!