Set duration of condition?

I’m fiddling with an automation to control roller shutters. The idea is to close them when sun is shining on the windows and it’s hot outside.

There are two possible scenarios for a successful trigger:

  • sun begins to shine and it’s allready hot → automation triggered by light intensity
  • it gets hotter while the sun has been shining for some time → triggered by temperature

So far it has been pretty straightforward task. But then I wanted to include another condition: due to possibility of clouds etc. the automation should trigger only if the duration of illumination is long enough.

Here is the code:

description: 
  - platform: numeric_state
    entity_id: sensor.light
    below: 0.4
    for:
      minutes: 10
  - platform: numeric_state
    entity_id: sensor.out_temperature
    above: 24.5
condition:
  - condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.light
        below: 0.4
      - condition: numeric_state
        entity_id: sensor.out_temperature
        above: 24.5
action:
 ### a lot of scripts etc. No problems there.

Now if the automation gets triggered by light intensity, it incorporates 10 min interval during which the light intensity has to be strong enough (LDR value lower than 0.4). But I would like to somehow incorporate that interval when the automation is triggered by temperature. Something like:

- condition: numeric_state
   entity_id: sensor.light
   below: 0.4
     for: 
       minutes: 10

But that doesn’t compile. Is there any workaround?

The workaround is to set up a template binary sensor that tracks your light intensity, then using a State condition which does allow a duration.

Thank you, that was it!

FYI, the working code is below…
In configuration.yaml add:

template:
  - binary_sensor
    - name: "sun duration"
      state: "{{states('sensor.light')|float(0) < 0.4}}"

And in the actual automation

condition:
  - condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.out_temperature
        above: 24
        below: 29
      - condition: state
        entity_id: binary_sensor.light_duration
        state: "on"
        for:
          hours: 0
          minutes: 10
          seconds: 0