Detecting a status multiple times before an action is taken

I have homeassistant monitoring lots of devices, and it is detecting state changes, but i’m getting some false positives. Most of the sensors are binary sensors.

I want to be able to detect that an entity has entered a failed state 3 times in a row before an automation triggers a notification.

All the actions seem to need a state change to trigger an automation , so i can detect the first state change and increment a counter , but as the state does not chnage after that i can’t i cant detect no chnage even if still in a failure state. Any tips on how to accomplish this. I did looking forcing the entity back in to an unknown state but this does not look possible.

Describe exactly what happens and how your entity fails and maybe someone can suggest a good way to trap it.

So a few examples an ftp connection does not work or a ping test fails or dhcp server fails to respond, whatever the error my sensors are something like this

  • binary_sensor:
    command: ‘response=$(curl -s -u http://:61208/api/3/processlist/name | grep “"dhcpd"” | grep -v grep | wc -l ); test “$response” -gt 0&& echo “0” || echo “1”’
    name: “ dhcp”
    scan_interval: 60
    device_class: problem
    payload_on: 1
    payload_off: 0

The response from the test is 0 or 1, 0 means the test worked and 1 means it failed. I need to over 3 test cycles get a 1 (failed eadh time) to ensure false positives are rules out. so when a failure occurs the values started from working will be
0 (working)
1 (failed)
1 (failed)
1 (failed)
with no state changes on the last 2 tests.

Also and this comes latger if say the 1 tests fails, then the 2nd or 3rd works, the counter needs to be reset and the false positive ignored.
If after 3 fails in a row i need a notification (which i can do)

I test that my NAS is running by pinging it. I know the frequency of the pings (30 sec by default) so I have an automation that triggers if the binary sensor remains off for 5 min - ie 10 pings - and sends a notification.

I tried that the issue i’m facing is homeassistant seems need a state change to trigger.
so good to bad is a change (triggers)
bad to bad is no change ( fails to trigger)
bad to bad is no change (fails to trigger)

I think I’ve misunderstood the question… Why do you need a state change, when what you’re trying to detect is a lack of change? The binary sensor will retain it’s value.

This works for me:

trigger:
  - platform: state
    entity_id:
      - binary_sensor.192_168_1_141
    to: "off"
    for:
      hours: 0
      minutes: 5
      seconds: 0

There’s some help with including code in posts here:

How to format your code in forum posts

Edit: Don’t forget that people can read your posts for a few hours after you detele them (click on the red pencil top right). :laughing:

Another edit: Welcome to the forum, by the way. You sound as if you know what you’re doing, but there’s lot’s of good stuff here:

The Home Assistant Cookbook - Index

Thanks everyone i think the issue was the scan times on the sensor and the wait for times in the automation i have adjusted them to allow 6 scans and it works now. Sorry simple question i tried to over complicate

1 Like