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…
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?
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 ...
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.
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”)
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.
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
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.
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 continuouslyoff 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 ...`
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.