Automation with "state" trigger: how to toggle if the state holds for some time AFTER STARTUP

Let’s assume that there is a binary_sensor.
For example, it is ON when Internet access is available:

binary_sensor:
  - platform: ping
    host: google.com
    count: 2
    scan_interval: 60
    name: net_is_available_google

  - platform: ping
    host: ya.ru
    count: 2
    scan_interval: 60
    name: net_is_available_yandex

  - platform: template
    sensors:
      net_is_available_internet:
        value_template: "{{ is_state('binary_sensor.net_is_available_google','on') or
                            is_state('binary_sensor.net_is_available_yandex','on') }}"
        device_class: connectivity

We may check if the state of the sensor is OFF.
Automation #1:

  - alias: 'net: Internet unavailable'
    description: ''
    trigger:
      - platform: state
        entity_id: binary_sensor.net_is_available_internet
        to: 'off'
    action:
      ...
    mode: single

Also we may check if the state is OFF for some period (for example - 10 minutes).
Automation #2:

  - alias: 'net: Internet unavailable for 10 minutes'
    description: ''
    trigger:
      - platform: state
        entity_id: binary_sensor.net_is_available_internet
        to: 'off'
        for: '00:10:00'
    action:
      ...
    mode: single

Also we may check if the state is OFF on HA startup.
Automation #3:

  - alias: 'net: Internet unavailable on startup'
    description: ''
    trigger:
      - platform: homeassistant
        event: start
    condition:
      - condition: template
        value_template: "{{states('binary_sensor.net_is_available_internet') == 'off'}}"
    action:
      ...
    mode: single

But - how can we check if the state is OFF even after 10 minutes after startup?
Unfortunately, Automation #2 is not toggled in my setup…

trigger:
  platform: template
  value_template: "{{ as_timestamp(now()) - as_timestamp(states('sensor.uptime')) > 10*60 }}"

Requires:

Thanks a lot for the reply!
As I understood, this automation will check every second if the template condition is TRUE.
Does this automation require lot of CPU time?

The template will be evaluated every minute, not every second.

So this is a normal practice? I mean, this will not make any harm to other tasks?
Sorry, if I am asking stupid question.

Also, if I need to perform this check only ONCE (i.e. exactly at 00:10:00) shall I specify it like this:

trigger:
  platform: template
  value_template: "{{ as_timestamp(now()) - as_timestamp(states('sensor.uptime')) > 10*60 and 
                      as_timestamp(now()) - as_timestamp(states('sensor.uptime')) < 11*60}}"

Automation’s action checks if the binary_sensor is off ten minutes after startup.

  - alias: 'net: Internet unavailable 10 minutes after startup'
    trigger:
    - platform: homeassistant
      event: start
    action:
    - delay: '00:10:00'
    - condition: template
      value_template: "{{states('binary_sensor.net_is_available_internet') == 'off'}}"
      ... do something if it is still off ...

I really need to learn more about script syntax, thank you, Taras.
Will the automation work ONCE?
i.e. once after 10 minutes.

The automation has only one trigger and that’s startup. It will not automatically trigger again until the next startup (so only one execution per operating session).

After it triggers, it waits 10 minutes and then checks the binary_sensor’s state.

Very clear explanation!

Could you also help me with this:

I think your solution is at least more elegant, but for educational purpose I also would like to know about that method too - will it work ? (i.e. if I specify this range “600…660”)

That’s tom_I’s suggestion and you should ask him.

Sure it will work but it’s pointless additional config that adds nothing. It’s still going to check every minute.

I assume you are trying to limit the number of triggers the template makes but that is not how this works. Templates update when the entities they contain update. The uptime sensor updates once on start up, sensor.time updates every minute, whether you are testing for a time range or not.

Go with 123’s solution. It only checks once 10 minutes after start up.

Technically this checks if internet is available after 10 minutes. Not if it’s been unavailable for 10 minutes since startup. Depends on what the OP is trying to achieve.

Pretty sure that’s what he asked for:

The statement ‘even after’ can be interpreted in both ways. Like ‘for’ or ‘after’. :thinking:

I think the Example is what’s needed but hey, just saying. :wink:

All the examples so far have been 10 minutes after start up and no objection has been made.

Let me explain my goals.
There are some binary sensors which I want to monitor:

  1. On startup:
  • if the sensor’s state is OFF -> log a warning message.
    Maybe there is no need to worry; let’s wait a little, may be the state will be changed to ON.
  - alias: 'xxxx'
    description: ''
    trigger:
      - platform: homeassistant
        event: start
    condition:
      - condition: template
        value_template: "{{states('binary_sensor.some_sensor') == 'off'}}"
    action:
      service: notify.xxxxx
      ...log a warning message...
    mode: single
  1. After XXX minutes (let it be 10 minutes) after startup:
  • if the sensor’s state is still OFF -> log a critical message.
    It happens ONCE at T=Tstart+10.
    If this happens, I need to fix this “bad” state.
  - alias: 'xxxxx'
    trigger:
    - platform: homeassistant
      event: start
    action:
      - delay: '00:10:00'
      - condition: template
        value_template: "{{states('binary_sensor.some_sensor') == 'off'}}"
      - service: notify.xxxxx
        ...log a critical message...
    mode: single
  1. After startup:
  • if the sensor’s state was ON and is changed to OFF -> log a warning message.
    Maybe there is no need to worry; let’s wait a little, may be the state will be restored to ON.
  - alias: 'xxxxxx'
    description: ''
    trigger:
      - platform: state
        entity_id: binary_sensor.some_sensor
        to: 'off'
    action:
      service: notify.xxxxx
      ...log a warning message...
    mode: single
  1. After startup:
  • if the sensor’s state is OFF for XXX minutes (i.e. keeping the same state) -> log a critical message.
    If this happens, I need to fix this “bad” state.
  - alias: 'xxxxxx'
    description: ''
    trigger:
      - platform: state
        entity_id: binary_sensor.some_sensor
        to: 'off'
        for: '00:10:00'
    action:
      service: notify.xxxxx
      ...log a critical message...
    mode: single

By “logging a message” I mean sending a message to a particular Telegram channel:

  • there are channels named “climate”, “network”, “life360” etc - these channels are for “normal” & warning messages for climate components, network componets, …;
  • there is a channel named “important” - for critical messages from all components.
    I separated messages between channels just to provide some order & for easy analysis.
    May be there is a better practice, but so far I am using this method.

Please, confirm that this solution (made with your help) will work.

Reading this, I was thinking of the Alert component. Have you ever considered it? And if so, why isn’t this useful?

I assumed the OP wanted to ‘spot check’ at the 10-minute mark but, you’re right, the requirement could be interpreted as a confirmation that, at the 10-minute mark, the binary_sensor has been continuously off since startup. I believe one can achieve that using a State Condition with a for option.

  - alias: 'net: Internet unavailable 10 minutes after startup'
    trigger:
    - platform: homeassistant
      event: start
    action:
    - delay: '00:10:00'
    - condition: state
      entity_id: binary_sensor.net_is_available_internet
      state: 'off'
      for: '00:10:00'

    ... do something if it is still off ...`
1 Like

Frankly, the fourth automation you posted should be sufficient. It triggers whenever the binary_sensor has been off for 10 continuous minutes. That can happen during the first 10 minutes after startup or at any other time afterwards.