Device online/offline alert automation

Hi all
I’ve got an automation that triggers a script to notify me when devices are offline and again when they’re back online.

I’m trying to limit the alerts per device/entity so that it only alerts when a device has been offline for over 5 minutes which I have been able to do in the trigger. However, I still get an immediate alert when the device is online.

How can I make it so that it will trigger the “device online” part only if the previous state was “unavailable” for 5 minutes?

Any thoughts?

Simplest would be to add a helper that triggers after you get the offline notify. Use that as a condition for sending online.
So device X goes offline and after 5 minutes you trigger the notify and set helper X_offline as true.
After it goes online: in the condition you just check if X_offline is true and if its online, sendthe online message and set helper X_offline as false.

OP, please show your automation

Here’s the yaml for the automation

alias: Device Online/Offline Alert
description: ""
trigger:
  - alias: When entities go offline
    platform: state
    entity_id:
      - switch.nice_socket_01
      - switch.power_socket_01
      - switch.power_socket_02
      - switch.kitchenap
      - light.bedroom_light
      - sensor.aqara_cube
      - binary_sensor.aqara_motion_sensor_occupancy
      - sensor.aqara_switch_action
      - sensor.bedroom_aqara_sensor_temperature
      - sensor.bathroom_aqara_sensor_temperature
      - sensor.livingroom_aqara_sensor_temperature
      - switch.zsocket_01
      - binary_sensor.front_door_aqara_sensor_contact
      - switch.living_area_aqara_switch_left
      - light.stairs_aqara_switch
      - light.hallway_switch_center
      - sensor.hallway_aqara_wireless_switch_action
      - switch.zsocket_01
    to: unavailable
    id: Devices Offline
    for:
      hours: 0
      minutes: 5
      seconds: 0
  - alias: When Zigbee2MQTT Bridge gets disconnected
    type: not_connected
    platform: device
    device_id: 42fe7de51c4f0b1c10f721444cf8c34e
    entity_id: 945c77990f996fba5ff40da68e80f17f
    domain: binary_sensor
    id: Zigbee2MQTT Disconnected
    for:
      hours: 0
      minutes: 2
      seconds: 0
  - alias: When entities are online
    platform: state
    entity_id:
      - switch.nice_socket_01
      - switch.power_socket_01
      - switch.power_socket_02
      - switch.kitchenap
      - light.bedroom_light
      - sensor.aqara_cube
      - binary_sensor.aqara_motion_sensor_occupancy
      - sensor.aqara_switch_action
      - sensor.bedroom_aqara_sensor_temperature
      - sensor.bathroom_aqara_sensor_temperature
      - sensor.livingroom_aqara_sensor_temperature
      - switch.zsocket_01
      - binary_sensor.front_door_aqara_sensor_contact
      - switch.living_area_aqara_switch_left
      - light.stairs_aqara_switch
      - light.hallway_switch_center
      - sensor.hallway_aqara_wireless_switch_action
      - switch.zsocket_01
    from: unavailable
    id: Devices Online
    for:
      hours: 0
      minutes: 5
      seconds: 0
  - alias: When Zigbee2MQTT Bridge is connected
    type: connected
    platform: device
    device_id: 42fe7de51c4f0b1c10f721444cf8c34e
    entity_id: 945c77990f996fba5ff40da68e80f17f
    domain: binary_sensor
    id: Zigbee2MQTT Connected
    for:
      hours: 0
      minutes: 5
      seconds: 0
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Devices Offline
              - Zigbee2MQTT Disconnected
        sequence:
          - service: script.mqtt_alert
            data:
              message: >-
                🔴 Device {{ state_attr(trigger.entity_id, 'friendly_name') }}
                is offline
            alias: Offline - Send MQTT Alert
      - conditions:
          - condition: trigger
            id:
              - Devices Online
              - Zigbee2MQTT Connected
        sequence:
          - service: script.mqtt_alert
            data:
              message: >-
                ✅ Device {{ state_attr(trigger.entity_id, 'friendly_name') }} is
                online
            alias: Online - Send MQTT Alert
mode: queued
max: 10

Thank you. I’ll give this a go and see how it goes.

I would appreciate help with the logic of the condition in the automation if you can help?

The automation has 4 triggers, one for Wifi and other devices and another for Zigbee2MQTT and its linked devices.

The action taken is based on the trigger id. See the yaml code above.

Switching the helper on/off is simple enough as it’s just another step in the sequence.

Would this ever be triggered? It’s making my brain hurt!

alias: Device Online/Offline Alert
description: ""
trigger:
  - alias: When entities go offline
    platform: state
    entity_id:
      - switch.nice_socket_01
      - switch.power_socket_01
      - switch.power_socket_02
      - switch.kitchenap
      - light.bedroom_light
      - sensor.aqara_cube
      - binary_sensor.aqara_motion_sensor_occupancy
      - sensor.aqara_switch_action
      - sensor.bedroom_aqara_sensor_temperature
      - sensor.bathroom_aqara_sensor_temperature
      - sensor.livingroom_aqara_sensor_temperature
      - switch.zsocket_01
      - binary_sensor.front_door_aqara_sensor_contact
      - switch.living_area_aqara_switch_left
      - light.stairs_aqara_switch
      - light.hallway_switch_center
      - sensor.hallway_aqara_wireless_switch_action
      - switch.zsocket_01
    to: unavailable
    id: Devices Offline
    for:
      hours: 0
      minutes: 5
      seconds: 0
  - alias: When Zigbee2MQTT Bridge gets disconnected
    type: not_connected
    platform: device
    device_id: 42fe7de51c4f0b1c10f721444cf8c34e
    entity_id: 945c77990f996fba5ff40da68e80f17f
    domain: binary_sensor
    id: Zigbee2MQTT Disconnected
    for:
      hours: 0
      minutes: 2
      seconds: 0
  - alias: When entities are online
    platform: state
    entity_id:
      - switch.nice_socket_01
      - switch.power_socket_01
      - switch.power_socket_02
      - switch.kitchenap
      - light.bedroom_light
      - sensor.aqara_cube
      - binary_sensor.aqara_motion_sensor_occupancy
      - sensor.aqara_switch_action
      - sensor.bedroom_aqara_sensor_temperature
      - sensor.bathroom_aqara_sensor_temperature
      - sensor.livingroom_aqara_sensor_temperature
      - switch.zsocket_01
      - binary_sensor.front_door_aqara_sensor_contact
      - switch.living_area_aqara_switch_left
      - light.stairs_aqara_switch
      - light.hallway_switch_center
      - sensor.hallway_aqara_wireless_switch_action
      - switch.zsocket_01
    from: unavailable
    id: Devices Online
    for:
      hours: 0
      minutes: 5
      seconds: 0
  - alias: When Zigbee2MQTT Bridge is connected
    type: connected
    platform: device
    device_id: 42fe7de51c4f0b1c10f721444cf8c34e
    entity_id: 945c77990f996fba5ff40da68e80f17f
    domain: binary_sensor
    id: Zigbee2MQTT Connected
    for:
      hours: 0
      minutes: 5
      seconds: 0
condition:
  - condition: trigger
    id:
      - Devices Online
      - Zigbee2MQTT Connected
  - condition: and
    conditions:
      - condition: state
        entity_id: input_boolean.device_online_offline_helper
        state: "on"
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Devices Offline
              - Zigbee2MQTT Disconnected
        sequence:
          - service: script.mqtt_alert
            data:
              message: >-
                🔴 Device {{ state_attr(trigger.entity_id, 'friendly_name') }}
                is offline
            alias: Offline - Send MQTT Alert
          - service: input_boolean.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: input_boolean.device_online_offline_helper
      - conditions:
          - condition: trigger
            id:
              - Devices Online
              - Zigbee2MQTT Connected
        sequence:
          - service: script.mqtt_alert
            data:
              message: >-
                ✅ Device {{ state_attr(trigger.entity_id, 'friendly_name') }} is
                online
            alias: Online - Send MQTT Alert
          - service: input_boolean.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: input_boolean.device_online_offline_helper
mode: queued
max: 10

I would do it another way, but that’s me and how my system is set up.

  • I would use an alert for this.
  • I would “outsource” the entites into a group and only use the group in the different triggers.
  • I would avoid all device_ids, see here: Home Assistant Cookbook - Discussion Thread

Yep, that sums it up for the first run :rofl:

Seriously, you should take a look at alerts, they are a powerful and underestimated tool for such things. :slight_smile:

Thanks for that.

I would love to use an alert. I know you can get some that are persistent and can be dismissed/snoozed etc. I don’t know if alerts support triggering a script and passing it values. My current automation runs a script that posts an MQTT message to mqttwarn which, amongst other things, posts a message in Telegram. Do you know if that would be possible?

As regards creating groups, I wasn’t sure if it was worth creating a group. Oddly, I couldn’t find a simple way to group devices via the UI. I am hoping to be able to use the newly introduced labels at some point though once the feature is made even more powerful in the UI.

As regards device_ids, is this general advice? I double-checked my script and I’m not using any. I’m using entity_ids which is also not ideal as I have to modify the automation to add new entities.

No, alerts only offer the possibility to send out notifications, no scripts or automations. But that’s fortunately not necessary, alerts work a little different. :slight_smile:

Alerts are an entity, and so you can use it to act as a trigger for all kind of different scripts or automations.

This is just a minimal example:

input_boolean:  
  hallway_frontdoor_open_notify:
    name: Frontdoor open!
    icon: mdi:alert

template:
  - binary_sensor:
      - name: hallway_frontdoor_open_alert_active
        state: "{{ is_state('hallway_frontdoor_contact_on_off', 'on') and is_state('input_boolean.hallway_frontdoor_open_notify', 'on') }}"
        delay_on: '00:00:30'

alert:
  hallway_frontdoor_open_critical_alert_active:
    name: Frontdoor open
    entity_id: binary_sensor.hallway_frontdoor_open_alert_active
    state: 'on'
    repeat:
      - 1
    can_acknowledge: false
    skip_first: false
    title: "Critical warning"
    message: "Frontdoor is open!"
    done_message: "Frontdoor is now closed!"
    notifiers:
      - NOTIFY_critical
    data:
      tag: hallway_frontdoor_open_critical_alert_active

What you’re doing here, you have an input_boolean to disable the alert, but not by turning on or off the alert itself, but rather a template binary_sensor. That comes in handy later for display and setting purposes.

The binary_sensor itself is a nice way, to cover all fancy conditions and sensors in one more or less short way. An alert wouldn’t be able to handle complex criterias.

That brings us to the alert, here you are just reacting on the binary_sensors state. I don’t want to get in to deep here, if you have questions what does what in an alert, please ask.

As you have different possibilities to work here, one could be an automation reacting on the state of the alert. It’s easy, because you have three alert states:

State Description
idle The condition for the alert is false.
on The condition for the alert is true.
off The condition for the alert is true but it was acknowledged.

Regarding your telegram notification. HA has an integration for that, that works great. This is the part of the notifications in that alert (NOTIFY_critical):

notify:
  - name: notification_telegram
    platform: telegram
    chat_id: !secret telegram_chat_id
  - name: NOTIFY_critical
    platform: group
    services:
      - service: mobile_app_1
      - service: mobile_app_2
      - service: notification_telegram
      - service: notification_tv
        data:
          data:
            duration: 10
            fontsize: medium
            position: center
            color: grey
            transparency: 0%

I’m using telegram here as another notification channel. One sends via HA companion app a notice, one per telegram and one to the TV.

Depends on what mqttwarn additionally does, but for “simple” notifications I’d use something like the above.

I unfortunately can’t say how groups work in UI, haven’t tested it (yet). In general, groups are just a list of entities, and as the entities are identical in your automation, it just makes sense to use. If you need to change something, you wouldn’t need to change two things in one automation, but just in one place => the group. And it would make it easier to read in the automation. :slight_smile:

Yes the device_id was a general advice, it just messes up things (in my opinion). Here you use it:

  - alias: When Zigbee2MQTT Bridge gets disconnected
    type: not_connected
    platform: device
    device_id: 42fe7de51c4f0b1c10f721444cf8c34e
    entity_id: 945c77990f996fba5ff40da68e80f17f
    domain: binary_sensor
    id: Zigbee2MQTT Disconnected
    for:
      hours: 0
      minutes: 2
      seconds: 0

And for the modification strategy, see above: groups :rofl: :rofl:

Hope this helps a little, as always, if you have questions, ask! :slight_smile: