[SOLVED] Detect when sensors go offline

Hello,
I’m trying to detect when some sensors go offline to send an alert.

alias: Devices go offline
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.door_1_open
      - binary_sensor.door_2_open
    to: 'unavailable'
condition: []
action:
  - service: rest_command.telegram_notify_offline
    data:
      device: "{{ trigger.to_state.name }}"
      offline: true
mode: single

Any ideas?

Assuming your rest command works, try this trigger:

trigger:
  - platform: state
    entity_id:
      - binary_sensor.door_1_open
      - binary_sensor.door_2_open
    to: 
      - unavailable
      - unknown
alias: Devices go offline
trigger:
  - platform: state
    entity_id:
      - binary_sensor.door_1_open
      - binary_sensor.door_2_open
    to:
      - unavailable
      - unknown
condition: []
action:
  - service: rest_command.telegram_notify_offline
    data:
      device: "{{ trigger.to_state.name }}"
      offline: "{{ trigger.to_state.state }}"
mode: single

Doesn’t work.
I’m using same entity_id of another script that check when a door is open/closed so I’m sure the ids are corrects.

What does the automaton trace show?

Are there errors in your system log that relate to this automaton?

In /config/logs I didn’t find nothing related this automation.
In general I use “Developer tools” and I change states to check the trigger but in this case I donno how to set “unavailable” state.

Ok, with state set to “unknown” or “unavailable”, the automation work but if I remove the battery from device, nothing happens.
Maybe there is another status to add to “to:”?

it may take a little time for HA to realise the state has changed. How long have you waited for?

1 Like

I wait over a minute.

This is the current code

alias: Devices go offline
trigger:
  - platform: state
    entity_id:
      - binary_sensor.door_1_open
      - binary_sensor.door_2_open
condition: []
action:
  - service: rest_command.telegram_notify_offline
    data:
      device: "{{ trigger.to_state.name }}"
      offline: "{{ trigger.to_state.state }}"
mode: single

In theory, if the state change, the automation should start.
If I remove the battery, nothing happens.
If I insert the battery, nothing happens.
If I open “Developer tools → States” and I change “binary_sensor.door_1_open” state to “antani”, the automation start and I see on my remote server “The new state is antani”.

Are these Zigbee sensors? If so, it may be several hours before they show up as unavailable. The default for ZHA is 6 hours.

2 Likes

yes, SONOFF Zigbee SNZB-04

It’s a feature of Zigbee - the coordinator does not poll battery-powered sensors, it waits for them to check in (this is to conserve battery).

If nothing is happening (i.e. the door is not opening or closing) the only thing the sensor might need to report is battery level - typically it would do that every few hours, depending on the manufacturer. The coordinator waits until all possible check-ins have clearly been missed before marking the device as unavailable.

1 Like

oh! thanks for the clarification.
btw I think there is some other problems because now if I look the monitor state (/config/devices/device/), seems stuck into “open”.
I try to delete and re-configure… umpf

I think this device is broken…

HA has been stuck on this screen for 2 minutes

If you removed the sensor to take out the battery (as I have to do with mine) the last known state would have been “open”. It will stay that way until the coordinator decides it has lost contact and updates it to “unavailable”.

1 Like

Yes, I know but I’ve removed the sensor (deleted), rebooted HA and re-added the sensor and HA is stuck in “configuring”.
I decided to reset the automations (deleted all) and the sensors.
Then rebooted the HAOS.
Added the SNZB-04, checked if the state is working into device info page. All ok.
Then re-created the automations.
Now the “the door is open/closed” alert is working without problem.
I removed the battery and now I’ll wait to check if in some hours the alert is sent.
Sorry but I’m new in HA and this kind of “game”.
Tnx guys for all clarifications.

Try this:-

You will need the ZHA Toolkit for it to work:-

The example above just logs it, but you could change that to send you a notification.

To detect unavailable devices generally, you could try running Watchman periodically.

I confirm after 6 hours the device become UNAVAILABLE.
Thank you all for support.