Adding For (time delay) in If statement

I merged two automations in one, everything seems ok, but I still need to find a way to adapt the For (delay) in my code.
My first automation included the following code:

below: '1000'
entity_id: sensor.neo_extsud_lux
for: '00:05:00'
platform: numeric_state

So when my luminance sensor was under 1000 for 5 minutes it changed the input boolean value.

Now I simply want to add the 5 minutes delay in my new conditional code:

service_template: >
            {% if states('sensor.neo_extsud_lux') | float < 1000 %}
            input_boolean.turn_on
            {% else %}
            input_boolean.turn_off
            {%- endif -%}
          entity_id: input_boolean.blinds_clearance

Thanks in advance.

So you want to turn on the input boolean in this service call if the sensor is below 1000 and has been for at least 5 minutes, is that right?

How about:

service_template: >
  {% state = states.sensor.neo_extsud_lux %}
  {% if state.state|float < 1000 and
     (now() - state.last_changed).total_seconds() > 5*60 %}
    input_boolean.turn_on
  {% else %}
    input_boolean.turn_off
  {% endif %}
entity_id: input_boolean.blinds_clearance

Not sure that will do what he wants
As the time starts when (say) level goes to 900
It waits a minute and the value changes to 800
It waits a minute and the value changes to 900
It waits a minute and the value changes to 800
It waits a minute and the value changes to 900
It waits a minute and the value changes to 800
It waits a minute and the value changes to 900

Etc.
At no point does last changed get beyond 1 minute

Think you’d need an intermediary input boolean

And then you are back to 2 automations
(actually you could drop this in the service template. No, it needs context as Phil says in next post)

True, mainly because he doesn’t provide the full context. It will do what it appears he asked for: turn on the input boolean via that service call if 1) the sensor is under 1000, and 2) it has been for longer than 5 minutes. Whether or not it can ever meet both those criteria in the context in which it is used can’t be determined because, again, he didn’t provide that context.

totally right, will try that tomorrow and keep you informed, thanks!

Unless you can guarantee that you sensor will stay rock solid on ‘a’ value below 1000 you are going to be disappointed.
Did you not read the above ?
You really need to post both your old automations and what you currently have.
A little explanation about the sensor and what you think the automation does would also help.

No, I didn’t read, it was night for me, sorry, so I just told you I would check this tomorrow. As it’s not really ergonomic explaining the context with a phone interface, let’s do it now with my PC.

This extract of code is used to manage an external solar blind, I mean for solar protection, to protect overheating inside the house (most of the time, end of spring, autumn, and part of the summer). But in winter, this blind is up, allowing heating almost the whole house. This house is based on passive (constuction, windows) and active sun energy (thermal solar and photovoltaĂŻc).

Ok, that’s said. Now my problem, as I say, this is an external blind, sensible to high wind, I mean if wind is higher than 30km/h, blinds goes up, that’s easy because local meteo station where I’m connected via swissmeteo custom_component is very accurate with wind measurement. Blind could be destroyed in case of storm.

Now you could say, why don’t you manage your blind also according to the meteo station data, like “lightning, rainy, storm”? Because I must say, this is totally inacurate. Let’s say for example meteo indication is Lightning, that’s mean, a sequence of sun and clouds in a certain frequency. For example 10 minutes of sun, than 10 minutes of clouds and so on. Are you still reading? :grin:

So my solution is to use the photovoltaïc power sensor that gives me very accurate data about luminosity, and more reactive than my external luminance z-wave sensor battery based. My goal is to allow the blind to goes down and up one time per day, so I handle it wiht a flag. Because I do not want the blind to goes up and down 10 times per day. But what do I want to do with my "For “00:05:00”? This is just to filter the extreme or error value. For example I set my pwpower limit to 400W, that means ok, If < 400W very cloudy, blinds Up But as I explained above, this could be a cycle, so may be five minutes later, sun is back, and in summer this will be a problem. So If I the system waits for 5 or 10 minutes before running the script, the sensor will be again over the400W limit. It’s a kind of fuzzy logic, but it’s working.

This is my code:

service_template: >
            {% if states('sensor.neo_extsud_lux') | float > 400 and state_attr('sun.sun','azimuth') | int> 100 and state_attr('sun.sun','azimuth') | int < 260 %}
            input_boolean.turn_on
            {% else %}
            input_boolean.turn_off
            {%- endif -%}
          entity_id: input_boolean.blinds_clearance

I also declared an “average” value for the pvpower sensor:

# average pvpower
  - platform: statistics
    entity_id: sensor.pv_power
    name: avg_pvpower
    sampling_size: 20
    max_age:
      minutes: 10

So now you’ve got the real context :slightly_smiling_face:

ok, what I didn’t mention is that main triggers are inside and outside temp sensor.

Okay that’s more context than we needed but more is better than less.
Do you have a link to one of these covers so we have a sense of the thing ?
Now your logic seems to be all over the place. And one of the most limiting points being that you only want the blind deployed 1/day (you will loose a lot of sun) This kinda implies you have a counter an you allow raising if the number is less than x, which means you need to reset the counter at midnight (another automation)
You have azimuth but did I miss elevation ?
Given the above I think you are best to define a template binary sensor with delay on but not sure we have the logic clear.

The number of automations is irrelevant, if it requires 20 then so be it. We can always prune it later.

thanks for your answer. You must think Why does he need a luminance or PV power sensor, why not just the wind sensor? This is because I don’t find let’s say Cloud meteo sensors 100% reliable, so very low luminance is like a backup trigger to me.

had before a very complex “Scene” for this in my old Veralite, five aperture positions related to the wind speed, some counters but too much complexity.

In fact in my case, just wanted to play with a timer or delay that I can integrate in my if then else statement, don’t get the right Syntax as I’m still thinking in Lua… reading the last 2 values of the sensor with a delay of X minutes between. it’s quite the same approach I have now with the mobile avg sensor I created.
Yes, I also have elevation but in another automation. I need more order I know…

Here’s a pict, to have an idea of the wind problematic in case of storm when I’m not home…

Here is an example of a binary_sensor for your usage case.
This is very simple and can be made to do a LOT more things but you could use it in your Trigger / Condition / Action stages as you see fit.

binary_sensor:
  - platform: template
    sensors:
      bs_lolux:
        value_template: >
          {% set lux = states('sensor.neo_extsud_lux') | float %}
          {{ lux < states('input_number.lolux_val') | float }}
        friendly_name: Lux Below Threshold
        delay_on: "00:{{ states('input_number.lolux_delay_on') | int }}:00"
        delay_off: "00:{{ states('input_number.lolux_delay_off') | int }}:00"

You will also see that the ‘delay on’, ‘delay off’ and the ‘threshold’ are all templated to use external UI presentable elements for easy tweaking (you can later hard code these when you are happy)
If you need these to be seconds, move the value to the seconds location.
Once created this can be used in multiple locations and thus becomes very efficient as well as just clean.

1 Like

Without seeing your full automation (not just extracts) and/or or a clear statement of when you want the cover to open and when you want it to shut, it hard too help more.

I understand that the blinds may be damaged by wind, so opening (retracting) them in high wind is understandable.
Opening Cover (retract blind) : -

  1. time constraints ???
  2. internal temp < 19 and external temp > internal
  3. wind speed > 30 km/h

Closing Cover (extend blind) : -

  1. time constraints ???
  2. wind speed < 26 km/h
  3. internal temp > 26 and lux > 1000
  4. internal temp > 26 and external temp < internal temp

You need to collate reasons for opening and closing and any correlation between the reasons

1 Like

just perfect, now it’s just a question of tuning. thanks for your help!

Sorry, I think the quotes were wrong (corrected now)

Trigger parts, will do some optimizations now…

- id: '1586616451681'
  alias: 1000 HighTempBlindSouthDown
  description: If temp higher than 22 close blinds at 10h00
  trigger:
  - at: '10:00:00'
    platform: time
  condition:
  - condition: and
    conditions:
    - above: '16'
      condition: numeric_state
      entity_id: weather.sion
      value_template: '{{state.attributes.temperature}}'
    - condition: state
      entity_id: input_boolean.bad_weather
      state: 'off'
    - condition: state
      entity_id: input_boolean.storessejour_lockedman
      state: 'off'
    - above: '23'
      condition: numeric_state
      entity_id: sensor.aeon_dsb05_multisensor_temp
    - condition: state
      entity_id: input_boolean.storessejour_lockedauto
      state: 'off'
  action:
  - data: {}
    entity_id: script.1587918592085
    service: script.turn_on
- id: '1586617051879'
  alias: EliotropeBlindsUp
  description: ''
  trigger:
  - below: '25'
    entity_id: sun.sun
    platform: numeric_state
    value_template: '{{ state.attributes.elevation }}'
  - above: '260'
    entity_id: sun.sun
    platform: numeric_state
    value_template: '{{ state.attributes.azimuth }}'
  condition: []
  action:
  - data: {}
    entity_id: script.1587831569362
    service: script.turn_on

Hi, sorry, maybe I just didn’t understood the point. First I have to paste your code in the binary_sensor section of my configuration file, right? Because I have this error:

Invalid config for [binary_sensor.template]: offset 00:{{ states('input_number.lolux_delay_off') }}:00 should be format 'HH:MM' or 'HH:MM:SS' for dictionary value @ data['sensors']['bs_lolux']['delay_off']. Got "00:{{ states('input_number.lolux_delay_off') }}:00"
offset 00:{{ states('input_number.lolux_delay_on') }}:00 should be format 'HH:MM' or 'HH:MM:SS' for dictionary value @ data['sensors']['bs_lolux']['delay_on']. Got "00:{{ states('input_number.lolux_delay_on') }}:00". (See ?, line ?). 

Okay, Lets start from the beginning : -

  1. Have you created the input_number.lolux_delay_off and input_number.lolux_delay_on ?
  2. Did you do it in the GUI (Helpers) or Via yaml ?
  3. What values did you enter into these numbers ?
  4. When you go to ‘Developer Tools’ - ‘Template’, Clear the box and paste in : -
"00:{{ states('input_number.lolux_delay_on') | int }}:00"

hint : - it should look like : - “00:5:00” (Note: the ‘minute’ does not need the leading zero so this is fine and assumes that the input number contained a 5. I use values for minutes ranging from 1 to 480, all work fine )
What do you get ?

  1. How did you create the ‘binary_sensor.bs_lolux’ ? Was it via the GUI or via yaml ?
  2. I assume all your config is just in configutation.yaml ?
  3. Can you paste back a copy of the sensor as it appears in your yaml ?
  4. Can you paste back a copy of the input_number’s as they appear in your yaml ?

thanks for your feedback, will update my code and give you return asap

Well it’s not feedback yet :smiley:
Obviously we need to find out what went wrong didn’t go right and fix it.
This is a normal part of any development.
Hopefully at the end of this you will have learnt how to check your assumption (and more importantly, the assumptions of others (me, in this case)).
I can tell you now that the template editor will become your best friend.

:+1:
We are not there yet, but we will get there

Hi Mutt, sorry, I was trying to update my code and I still had not time trying your suggestion. I do not know how it can works in Developpers Tools…Template and not in my automation (not supported) It’s not elegant imbrication of if then else, I know, but logic is working. Could you maybe see what’s wrong?

entity_id: input_boolean.blinds_clearance
service_template: >
{% if states('sensor.avg_pvpower') | float > 350 and
  state_attr('sun.sun','azimuth') | int > 95 and state_attr('sun.sun','azimuth')
  | int < 270 and
  states('sensor.sion_temperature') | float > 18.0 and
  states('sensor.aeon_dsb05_multisensor_temp') | float > 23.0 and
  states('sensor.sion_wind_speed') | float < 25.0 %}
  input_boolean.turn_on
{% elif states('sensor.sion_wind_speed') | float > 35.0 %}
  input_boolean.turn_off
{%- else -%}
  {% if state_attr('sun.sun','azimuth') | int < 220 %}
     input_boolean.turn_on
  {%- else -%}
     input_boolean.turn_off
  {%- endif -%}
{%- endif -%}

You will need to explain what you mean by this as using the templating tool is essential if you want to do any template work yourself or you need to diagnose problems you find.

  1. Confirm you are using Advanced Mode ? (from your user button on the left side bar)
  2. You know where Developers Tools are ? (the ‘hammer’ icon on your left side bar)