Template if a sensor ist below x for y minutes

I need a condition template that checks if a sensor is below a certain value (e.g 68) for 30 minutes.

Is this possible to do?

In regular ‘automations gui’ this is built-in but I guess you want something else
Try this thread
Time range condition template - Configuration - Home Assistant Community (home-assistant.io)

Oh lordy. Don’'t read that topic. It’s old and full of dead ends.

Because numeric state conditions don’t support for: you could create a binary template sensor that is on when your sensor is below 68 and use a state condition on that, as the state condition does support for:

template:
  - binary_sensor:
      - name: "Less than 68"
        state: "{{ states('sensor.your_sensor_here)|float(0) < 68 }}"

Then the condition is:

condition:
  - condition: state
    entity_id: binary_sensor.less_than_68
    to: on
    for:
      minutes: 30

I don’t know why numeric_state conditions don’t support for: when numeric_state triggers, state triggers and state conditions all do. I guess it will be added one day. Until then this is the best work-around.

2 Likes

Oh lordy. Don’'t read that topic. It’s old and full of dead ends.
Then why not terminate this thread? Or is that a no-go on communities?

I forgot it even existed until you just linked to it. I wasn’t putting you down for linking to it. I just wanted to save davinci some pain because re-reading it brought back how even more clueless I once was.

I’ve asked for topics older than 1 to 2 years to be locked automatically. The form admins aren’t for it.

Didn’t take it that way…no worries. In quite a few cases I just try to refer to things that I know of as I have no time to write out the whole thing… people should also learn-by-doing :slight_smile:
And…well… I also understand that one does not want to start removing things although maybe ‘dead’ (e.g. 2+ yrs unchanged) might be a opportunity to clean it out too as there are many things in there that refer to functionality which has already changed a lot, moved, embedded, etc.

Yeah there is a lot of “no longer current information” on this forum and youtube because of how much home assistant has changed over the years.

Locking the topics doesn’t delete them, just makes it impossible to add new posts to them.

I am actually also thinking if it is not a ‘must’ to delete things from GDPR perspective… I of course never read the fine print of this community but even that… well, maybe someone else will pick that up as I for sure will not spend time on that :slight_smile: )

Hi
I have the almost the same request, so I made the below binary sensor. (the washing machine to be on for more than 30 minutes)

- binary_sensor:
  - name: "Water Washing Machine on above 30 minutes"
    state: "{{ states('sensor.power_37')|float(0) > 30 }}"

So I made the following automation

## Water Laundry Washing Machine **********************************************************************
- id: Water Washing Machine new
  alias: Water Washing Machine new
  trigger:
    - platform: numeric_state
      entity_id: sensor.power_37
      below: 6
      for:
        minutes: 3
        seconds: 0
      above: 2
  condition:
    - condition: state
      entity_id: binary_sensor.water_washing_machine_on_above_30_minutes
      to: on
      for:
        minutes: 25
    - condition: numeric_state
      entity_id: sensor.power_39
      below: '6'
     
  action:
    - service: switch.turn_on
      entity_id: switch.sonoff_100090981b
    - delay:
        minutes: 0
        seconds: 30
    - service: switch.turn_off
      entity_id: switch.sonoff_100090981b

but it is not working… The automation although it pass the configuration it is not available at all. If I delete the first condition (below) it is available and working as expected…

   - condition: state
      entity_id: binary_sensor.water_washing_machine_on_above_30_minutes
      to: on
      for:
        minutes: 25

Can you see what is wrong?

Thanks, the problem is when I add the particular condition in the configuration yaml the automation turns to gray and I can not use the troubleshooting options at all. It says that the automation is not available

Your indentation is off by one space:

Should be:

   - condition: state
     entity_id: binary_sensor.water_washing_machine_on_above_30_minutes
     to: on
     for:
       minutes: 25
1 Like

I am struggling with this automation, 3 days. Still the problem remains. I get the following unfortunately

.

So it can not pass the configuration check in dev tools.

When I add the for: below to: it accepts it, it passes the config check but the automation turns gray/unavailable!

image

Very annoying :frowning:
Thanks for your time and help in any case

Quote the state:

to: "on"

"on" is a string, on is a boolean value. States are always strings.

and your indentation is till not correct.

I tried the last solution with quote and still getting the same result. I deleted the for: minutes:25 and still the automation is not available/gray. Only if I completely remove this condition is working. The most annoying is that it is accepted from the file editor, and it passes the config check. This is happening for the first time in these 3 years I am running HA.

Show the whole condition block.

I just tried to write the conditions in the automation editor with the following result

  condition:
  - condition: state
    entity_id: binary_sensor.water_washing_machine_on_above_30_minutes
    state: 'on'
    for:
      hours: 0
      minutes: 30
      seconds: 0

  - condition: numeric_state
    entity_id: sensor.power_39
    below: '6'

if you compare it with the original automation I had (6 post above) the difference is

  condition:
  - condition: state
vs
  condition:
    - condition: state

Most of my automations using the second option which apparently was rejected for some reason.

So the final automation is the following (and I hope it will work - I will test in the next laundry day)

## Water Laundry Washing Machine **********************************************************************
- id: Water Washing Machine new
  alias: Water Washing Machine new
  trigger:
    - platform: numeric_state
      entity_id: sensor.power_37
      below: 6
      for:
        minutes: 3
        seconds: 0
      above: 2
  condition:
  - condition: state
    entity_id: binary_sensor.water_washing_machine_on_above_30_minutes
    state: 'on'
    for:
      hours: 0
      minutes: 30
      seconds: 0

  - condition: numeric_state
    entity_id: sensor.power_39
    below: '6'
     
  action:
    - service: switch.turn_on
      entity_id: switch.sonoff_100090981b
    - delay:
        minutes: 0
        seconds: 30
    - service: switch.turn_off
      entity_id: switch.sonoff_100090981b

edit: now I am looking the correct automation I just can not understand why this is valid. The indentation is looking completely wrong
however it is accpeted
image

This is valid:

  condition:
    - condition: state
      entity_id: binary_sensor.water_washing_machine_on_above_30_minutes
      state: 'on'
      for:
        hours: 0
        minutes: 30
        seconds: 0

    - condition: numeric_state
      entity_id: sensor.power_39
      below: '6'

So is this;

  condition:
  - condition: state
    entity_id: binary_sensor.water_washing_machine_on_above_30_minutes
    state: 'on'
    for:
      hours: 0
      minutes: 30
      seconds: 0

  - condition: numeric_state
    entity_id: sensor.power_39
    below: '6'

This is not:

  condition:
    - condition: state
      entity_id: binary_sensor.water_washing_machine_on_above_30_minutes
      state: 'on'
      for:
        hours: 0
        minutes: 30
        seconds: 0

  - condition: numeric_state
    entity_id: sensor.power_39
    below: '6'

You must be consistent with your indentation in each block. However your problem was that you were using the GUI editor for for: it must have hours, minutes and seconds specified, even if they are 0. You are only allowed to skip the 0 options if using yaml.

1 Like

I didn’t know that, and I could never assumed it.
Thanks

I’ve made a solution using template without automations. The washing machine has three states: On, Off and Idle.

sensor:
  - platform: template
    sensors:
      washing_machine:
        friendly_name: "Washing Machine"
        icon_template: >-
          {% if states('sensor.washing-machine') in ['Idle', 'On'] %}
          mdi:washing-machine
          {% else %}
          mdi:washing-machine-off
          {% endif %}
        value_template: >-
          {% if states('sensor.smartplug_power')|float >= 5 %}
            On
          {% elif states('sensor.washing_machine') == 'On' %}
            Idle
          {% elif states('sensor.washing_machine') == 'Off' %}
            Off
          {% elif as_timestamp(now()) - as_timestamp(states.sensor.washing_machine.last_changed) >= 30 %}
            Off
          {% else %}
            Idle
          {% endif %}

This will turn the switch On if the power consumption raises above 5W, and set it Off after 30 seconds below 5 Watts.

1 Like