How to re-trigger "number state above value for x minutes"?

Dear all:
New to Home Assistant and hope can get some hints on where to look for my issue with automation. I have made the below automation to “open” blinds (it actually closes it), when the outdoor light is above 50000lx for more than 15 minutes and weather condition allows to open it:

alias: Close Blind When Sunny
description: ""
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.garden_sunsensor_luminance
    for:
      hours: 0
      minutes: 15
      seconds: 0
    above: 50000
conditions:
  - condition: time
    after: "09:00:00"
    before: "18:00:00"
  - condition: numeric_state
    entity_id: sensor.carport_sensor_temperature
    above: 16
  - condition: numeric_state
    entity_id: sensor.nilan_outdoor_temperature
    above: 16
  - condition: template
    value_template: |
      {{
        (state_attr('weather.openweathermap', 'wind_speed') | float(0) < 30) and
        (state_attr('weather.openweathermap', 'precipitation') | float(0) == 0)
      }}
    alias: If not rainy or windy
actions:
  - action: cover.open_cover
    metadata: {}
    data: {}
    target:
      entity_id: cover.livingroom_blind
mode: single

During a day it only gets triggered once - that’s when the outdoor lx goes above 50000lx for 15 minutes. However in some cases the condition blocks the action, and I’d like to trigger again when the trigger condition is still met. How to do it then? I can make a trigger for every change of the sun sensor, however in condition I can’t specify number state above value “For” xx minutes.
Any suggestion where I should look? Thank you!

Add all the conditions as triggers, and all triggers as conditions.

Thanks - I can add a time pattern to check condition periodically - however the issue is I cannot say Number X is greater than Y for Z minutes under condition.

Having only this part in the triggers will make it trigger everytime that sensor change.

No, idea how to get the “for” part easily in the condition though.
Only thing I could come up with was another template sensor to indicate the time the sensor was in one condition.

Don’t add a time pattern. That will make it worse.
No you can’t do that in one go. But you can add a numeric condition is larger than something, and then a new condition state is empty in both to and from and for 15 minutes.
Those two combined will be the same

Thanks - is there a way to create a sensor that keeps the minimal value of another sensor in the past 15 minutes?

If you want a rolling number, then you will have to look into the history/statistic parts.
A rolling number is when you look back 15 minutes and select the lowest number, but when that number expires then you again look back and find the lowest number in the past 15 minutes.
For this search back in the past values you need to use the recorder and some history/statistic sensor.

Like this:

Thanks - sounds too complex for what I need. The only reason I have for is because I have another automation to open it when it’s dark, I’d like to avoid the senario that it keeps up and down during a cloudy day :slight_smile:

I just realized this is wrong.
The value will update with new values and reset the “timer”.

1 Like

It’s interestingly tricky for a simple problem :slight_smile:

Don’t make this more complex than it needs to be. Create a new “threshold” sensor (devices & services → helpers → threshold sensor).

Select your luminance sensor as the input, and use “above 50000” as the settings.

Now you have a new binary sensor that will be ON when lux is above 50000.

As hellis81 said earlier enter all your triggers as conditions and all your conditions as triggers, except replace the numeric state trigger & condition with a state trigger using your new threshold sensor. Since it is a state trigger, it allows “for” as an option in the conditions.

4 Likes

I believe what Wally said is the easiest.
Create a statistics sensor.
Use this as the trigger and condition.
And add all the triggers to conditions and conditions to triggers.

EDIT
I changed my mind.
See above

Good to learn that - I didn’t know I can use for for state in conditions.
And threshold sensor itself is interesting: would it be possible to say the binary sensor is ON when light > 50000 and OFF when light drops <30000?
So what I want is to close blind above 50000, and open blind below 30000.
Many thanks for your hint!

How can this Threshold Binary Sensor support the OP’s time duration requirement (must be greater than 50000 for more than 15 minutes)?


EDIT

It has a hysteresis option but that’s value-based, not time-based.

Yes, it supports upper and lower limits but, based on what I see in the documentation, it won’t support your time duration requirement.

The for is supported in condition for non-numbers - I think that solves all the problems with a price of additional sensor.
Just reading it up: I really wish something could allow me to:

  1. Turn on when value > 50000
  2. Stay on until value drops < 30000
  3. Turn off when value < 30000

It will solve so many things for me:

  1. Temperature warning - flipping between 19.9 and 20.0 (annoying)
  2. My ventilation summer/winter → 14 degree summer, stays until it goes down below 10.

Question is whether threshold sensor actually support it?
Edit: would this work?

binary_sensor:
  - platform: threshold 
    entity_id: sensor.sunsensor
    upper: 40000
    hysteresis: 10000
    name: testing

Got it; you can use a State Condition, which support the for option, to confirm the Threshold Binary Sensor’s value has been on for at least 15 minutes.

FWIW, this section explains the Threshold Binary Sensor’s behavior depending on which options are used.

Exactly, even better now I can see past 15 minutes the value is on when it reaches 50000, and not below 30000. Condition is sunny for 15 minutes.

I changed the automation to be triggered whenever lux changes:

alias: Close Blind When Sunny
description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.garden_sunsensor_luminance
conditions:
  .......
  - condition: state
    entity_id: binary_sensor.garden_sunny
    state: "on"
    for:
      hours: 0
      minutes: 15
      seconds: 0
actions:
  - action: cover.open_cover
    metadata: {}
    data: {}
    target:
      entity_id: cover.livingroom_blind
mode: single```

If I have understood the documentation correctly,
that configuration will turn on the binary_sensor when light level increases above 40000+10000=50000 and turn off when it decreases below 30000.

Why not trigger it when the Threshold Binary Sensor changes state? It’s already monitoring the light level.

I guess I will get the same problem why I posted here?
The trigger only runs once and stays on for the rest of the day, the condition will only be evaluated once :wink: