On_value_range should trigger only after condition was true for a certain time

Dear all, I am building an esphome-driven chicken door and have the following problem:

Apart from manual operation, the door’s opening or closing will be driven by the amount of daylight measured. The following code works without problems.

sensor:
  - platform: bh1750
    name: "BH1750 Illuminance"
    address: 0x23
    update_interval: 90s 
    on_value_range:
      - below: 300
        then:
          - cover.close: chicken_door
      - above: 1000
        then:
          - cover.open: chicken_door

However, this might be a bit jumpy when the light scenario changes quickly (clouds moving in front of sun late at day), so you want the condition to be true for, say, at least 15 min.
There seems to be the FOR condition where you can achieve this, but I cannot get the syntax (and the logic) right in the context of on_value_range.
Something like:

on_value_range:
     - below: 300
        then:
          if:
            condition:
              sensor.in_range:
                id: lux_sensor_huhn
                below: 300
                for:
                  time: 90s # short just for testing
          then:
            cover.close: chicken_door

Is there a simpler way to achieve my goal? Any help appreciated :slight_smile:

Kind regards,

Chris

I’m just guessing since I can’t test it but I think you need to change the order:

on_...:
  if:
    condition:
      for:
        time: 5min
        condition:
          sensor.in_range:
                id: lux_sensor_huhn
                below: 300

It is straight copied from the documentation. For the on_... value I would suggest using on_loop.

EDIT: How cool is a automated chicken door? Will you post something about your project when finished? :slight_smile:
EDIT2: Maybe use on_time instead of on_loop.

Hey, I have exactly the same need, have you found the solution?

I had the same problem but apparently the sensor.in_range condition in correlation with for: time cannot work within on_value_range, as explained here On_value_range doesn't support "FOR" condition(?) · Issue #4044 · esphome/issues · GitHub

You could move your logic to a template binary sensor with a delayed_on and then trigger your actions from that?

Could you please give me an example? I was trying to “debounce” temporary, sudden spikes of power, so the intended light does not come on and off constantly. The relevant snippet is as follows:

sensor:
  - platform: hlw8012
    <<: !include common/devices/plugs/moesrgb/hlw8012_base.yaml
    power:
      name: $friendly_name power
      unit_of_measurement: W
      id: power
      on_value_range:
        - below: 7.5
          then:
            - if:
                condition:
                  - for:
                      time: 2s
                      condition:
                        sensor.in_range:
                          id: power
                          below: 7.5
                  - light.is_on: rgb_light
                then:
                  light.turn_off: rgb_light

This means that I’m trying to turn off the light only as long the power is under 7.5W for a total duration of 2 seconds (not immediately). But as stated above this does not work with on_value_range in combination with for: and sensor.in_range.

Edit: I’m just looking at delayed_on and indeed seems promising…

This is the general structure. I’ve quickly masehed together a few seperate configs of mine so the details of the sensors etc won;t be quite right.

binary_sensor:
  - platform: template
    id: desky_tof_seems_occupied
    name: Desky ToF Seems Occupied

    lambda: |-
      return (id(solar_plant_moisture_level).state < 7.5;
    filters:
      - delayed_on: 60s
      - delayed_off: 120s
    on_press:
      then:
        - delay: 2s
        - light.turn_on:
            id: desky_light_led_left_red
            brightness: 20%
    on_release:
      then:
        - light.turn_off:
            id: desky_light_led_left_red
        - delay: 1s
        - while:
          #If presence isn't detected (PIR/ToF) but height is in range and keyboard drawer is out, slow flash red led
            condition:
              and:
                - binary_sensor.is_on: desky_simple_sit_detection
                - binary_sensor.is_off: someone_is_sitting
            then:
              - light.turn_on:
                    id: desky_light_led_left_red
                    brightness: 100%
              - delay: 1s
              - light.turn_off:
                    id: desky_light_led_left_red
              - delay: 1s

Ok, I see where that’s going and thank you. Just to be clear the on_press trigger for a template binary sensor is fired when the sensor is true and the on_release when it’s false? The documentation for this kind of sensor is minimum.
I feel that ESPHome’s documentation with regard to automations/conditions/triggers is a little bit lacking, at least in relation to HA’s one.

Correct.

I also recall there’s on_state or similar which will trigger on either.

1 Like

It’s a bit varied. Like HA.

Generally better documentation on newer things.

But I think it also might depend on someone putting their hand up to do the docs.

Are there any news regarding this problem? I have also ran into a similar wall.