How can send notification when device is not connected (Solved)

Hello, I am trying to create an automation to detect offline devices, either due to lack of electricity, network connection or malfunction.

I have created this automation according to the documentation. The Shelly 3 device is offline but it does not notify me when it is unavailable every 30 seconds. If I try to run the automation, I receive the notification via WhatsApp, but the automation does not detect that the device is offline.

# Cheking shelly hort 3*
  - id: '1737925037649'
    alias: Shelly Error
    description: ''
  triggers:
  - trigger: state
    entity_id: switch.hort_3
    to: "unavailable"
    for: "00:00:30"
  actions:
  - action: notify.whatsapp
    metadata: {}
    data:
      message: Shelly 3 Error
  mode: single

I have then modified this automation according to a similar post on this same forum. But the result is the same as above, if I run automation the message arrives but it does not detect that the device is offline


# Cheking shelly hort 3*
  - id: '1737925037649'
    alias: Shelly Error
    mode: parallel
  triggers:
  - platform: state
    entity_id: switch.hort_3
    to: "unavailable"
    for: 
     minutes: 3
  actions:
  - action: notify.whatsapp
    metadata: {}
    data:
      message: Shelly 3 Error
  

Does anyone know where I can fix the problem so that it doesn’t detect the device as offline? I think the problem is that the automation does not detect that the device is not online and that is why I do not receive the WhatsApp, since if I run the automation I receive a WhatsApp with the message

Thanks in advance

How are you testing the trigger and determining that it does not work?

A state trigger will only fire when the state changes. If your device is already unavailable at the time the automation is (re)loaded, it will never fire until the device becomes not unavailable and then goes unavailable again.

Thanks Mayhem. Ahh ok if not change the state never knows if device is offline…

Do you know what can i use for that ?

I mean, it will work, under normal conditions. It just will not fire during testing, unless the state changes from/to unavailable. You can test for that using the Developer Tools panel where you can manually override the state to something else, then back to unavailable and see if the trigger fires after X amount of time.

You can also add another trigger that fires immediately when the automation is (re)loaded and then add the state check as a condition for that trigger. But then it will also run for example when you restart the HASS server, or whenever you edit and save a different automation, as that will cause all automations to be reloaded. May or may nor make a difference, or may be annoying.

1 Like

Ok i understand. i think that not is the best way for do that. I will search other manners to receive notifications if device is offline.

Thanks,

i found the solution.

I create a script for check the state of device :

check_device_1:
  alias: "Comprobar extractor aire"
  sequence:
    - condition: state
      entity_id: switch.extractor_aire_switch_0   # Cambié device_tracker a switch
      state: "unavailable"
    - service: notify.whatsapp
      data:
         message: "¡Atenció! El dispositiu {{ state_attr('switch.extractor_aire_switch_0', 'friendly_name') }} no està disponible."

Then i created and automation for execute the script every 3 minutes


- id: '1651516706516'
  alias: "Comprobar dispositius fora de línea"
  trigger:
  - platform: time_pattern
    minutes: '/3'  # Cada 3 minuts
  action:
    - service: script.check_device_1
      # Add more services for additional device
  mode: single

Works perfectly for my needs.