Script to check "last seen" zigbee devices?

You use the trigger at two set times (07:00 and 16:00). Is it possible to perform this check every 5 hours?

Could you show me the complete automation?
Thanks

alias: Zigbee Device Missing Alert
trigger:
  - platform: time
    at: '07:00'
  - platform: time
    at: '16:00'
condition:
  - condition: template
    value_template: |
      {% set ns = namespace(break = false) %} {% for state in states -%}
        {%- if state.attributes.last_seen %}
          {%- if (as_timestamp(now()) - as_timestamp(state.attributes.last_seen) > (60 * 60 * 20) ) and ns.break == false %}
            {%- set ns.break = true %}
            true
          {%- endif -%}
        {%- endif -%}
      {%- endfor %} 
action:
  - service: notify.my_telegram_direct
    data:
      message: >
        Some Zigbee devices haven't been seen lately... {% for state in states
        -%}
          {%- if (state.attributes.last_seen and not (state.name | regex_search('available|linkquality|power_on_behavior|state'))) %}
            {%- if (as_timestamp(now()) - as_timestamp(state.attributes.last_seen) > (60 * 60 * 20) ) %}
              {{ ((as_timestamp(now()) - as_timestamp(state.attributes.last_seen)) / (3600)) | round(1) }} hours ago for {{"*"}}{{ state.name }}{{"*"}}
              
            {%- endif -%}
          {%- endif -%}
        {%- endfor %}
mode: single

1 Like

for every 5 hours:

trigger:
  - platform: time_pattern
    hours: /5
1 Like

I am trying this:

- alias: ALERT Zigbee Offline Devices
  id: zigbee_device_missing_alert
  trigger:
    - platform: time
      at: "07:00"
    - platform: time
      at: "16:00"
  condition:
    - condition: template
      value_template: > 
        {% set ns = namespace(break = false) %}
        {% for state in states -%}
          {%- if state.attributes.last_seen %}
            {%- if (as_timestamp(now()) - as_timestamp(state.attributes.last_seen) > (5 * 60 * 60) ) and ns.break == false %}
              {%- set ns.break = true %}
              true
            {%- endif -%}
          {%- endif -%}
        {%- endfor %} 
  action:
    - service: notify.mobile_app_inanu
      data:
        message: >
          Alcuni dispositivi Zigbee potrebbero essere OFFLINE:
          {% for state in states -%}
            {%- if state.attributes.last_seen %}
              {%- if (as_timestamp(now()) - as_timestamp(state.attributes.last_seen) > (5 * 60 * 60) ) %}
                {{ ((as_timestamp(now()) - as_timestamp(state.attributes.last_seen)) / (3600)) | round(1) }} Ore fa - {{ state.name }}
              {%- endif -%}
            {%- endif -%}
          {%- endfor %}

But how can I do to display only the name of the entity in the notification message?
Now I see “Sirena battery low” and “Led Kitchen …” (I think the notification is cut with … from ios).

Can I display only “Siren” “Kitchen Led” (device name only) in the notification?

The above is a snippet from a post higher up. You need to add unique words in the regex search to filter out the duplicates

{%- if (state.attributes.last_seen and not (state.name | regex_search('linkquality'))) %}

Can you give me an example to only display the device name?

I have made some attempts but no more notifications arrive

This is perfect for me, but I can’t correctly line up the text in the notification…

            {%- macro GetDroppedZigbee() -%}
            {% for state in states.sensor -%}
            {%- if ("linkquality" in state.name and state_attr(state.entity_id, "last_seen") != None and (as_timestamp(now()) - as_timestamp(state_attr(state.entity_id, "last_seen")) > (5 * 60 * 60))) -%}
            {{ state.name | regex_replace(find=' linkquality', replace='', ignorecase=False) }} - Da {{ ((as_timestamp(now()) - as_timestamp(state.attributes.last_seen)) / (3600)) | round(0) }} Ore {{- '\n' -}}
            {%- endif -%}
            {%- endfor %}
            {%- endmacro -%}
            {{ GetDroppedZigbee() }}

It currently looks like this:

Immagine

But it should look like this:

With the @deadly667 method instead the notification arrives correctly aligned

Not sure…try just adding another blank line between these two lines.

Not working…

Unfortunately I still have problems with your template, it would be perfect to be able to adapt this part of the template to your version

   {%- if ("linkquality" in state.name and state_attr(state.entity_id, "last_seen") != None and (as_timestamp(now()) - as_timestamp(state_attr(state.entity_id, "last_seen")) > (24 * 60 * 60))) -%}
              {{ state.name | regex_replace(find=' linkquality', replace='', ignorecase=False) }} - {{ relative_time(strptime(state_attr(state.entity_id, "last_seen"), '%Y-%m-%dT%H:%M:%S%z')) }} ago {{- '\n' -}}
              {%- endif -%}

Currently I see “siren.battery”, but I would like to see only “siren” (device name, not entity name)

If you want the friendly name then you should be able to change state.name to state.attributes.friendly_name

But this must be either wrong or highly coincidental: all sensors updated at the same time.

I am experiencing the same: I get the same value for all sensors (last seen) which in my case is incorrect. It looks like some variable is stored somewhere that messes things up. Any ideas?

My YAML:

{{ ((as_timestamp(now()) - as_timestamp(states.sensor.rookmelder_1e_node_status.last_updated)) / (3600)) | round(1) }} uur
{{ ((as_timestamp(now()) - as_timestamp(states.sensor.rookmelder_bg_temperature.last_updated)) / (3600)) | round(1) }} uur 

Just an update since there are now sensors for the last seen values

- alias: Zigbee Device Missing Alert
  id: zigbee_device_missing_alert
  trigger:
  - platform: time
    at: '10:00'
  - platform: time
    at: '20:00'
  condition:
  - condition: template
    value_template: '{% set ns = namespace(break = false) %} 
      {% for state in (expand(states.sensor,states.binary_sensor,  states.light, states.switch) |selectattr(''entity_id'', ''search'', ''last_seen'')) -%} 
        {%- if (as_timestamp(now()) - as_timestamp(state.state) > (60 * 60 * 8)) and ns.break == false %} 
          {%- set ns.break = true %} 
          true 
        {%- endif -%} 
      {%- endfor %}'
  action:
  - service: notify.mobile_app_pixel_4a
    data:
      message: "Some Zigbee devices haven't been seen lately... {% for state in (expand(states.sensor,states.binary_sensor,
        states.light, states.switch) |selectattr('entity_id', 'search', 'last_seen')  -%} {%- if (as_timestamp(now()) - as_timestamp(state.state) > (60 * 60 * 8)
        ) %} {{ ((as_timestamp(now()) - as_timestamp(state.state)) / (3600)) | round(1)
        }} hours ago for {{ state.name }} {{'\r\n'}} {%- endif -%} {%- endfor %}"

3 Likes
  action:
    - service: notify.mobile_app_my_mobile
      data:
        message: "Some Zigbee devices haven't been seen lately... {% for state in (expand(states.sensor,states.binary_sensor,
          states.light, states.switch) |selectattr('entity_id', 'search', 'last_seen')  -%} {%- if (as_timestamp(now()) - as_timestamp(state.state) > (60 * 60 * 8)
          ) %} {{ ((as_timestamp(now()) - as_timestamp(state.state)) / (3600)) | round(1)
          }} hours ago for {{ state.name }} {{'\r\n'}} {%- endif -%} {%- endfor %}"

Unfortunately the action-part gives me a:

template value should be a string for dictionary value @ data[0]['data'

although passing the template editor. Can you please support?

Well, found the culprit. As for the action-part → data, a closing bracket is missing.

The following works:

  action:
  - service: notify.mobile_app_my_mobile
    data:
      message: "Some Zigbee devices haven't been seen lately... {% for state in (expand(states.sensor,states.binary_sensor, 
        states.light, states.switch) |selectattr('entity_id', 'search', 'last_seen')) -%} {%- if (as_timestamp(now()) - as_timestamp(state.state) > (60 * 60 * 8)
        ) %} {{ ((as_timestamp(now()) - as_timestamp(state.state)) / (3600)) | round(1)
        }} hours ago for {{ state.name }} {{'\r\n'}} {%- endif -%} {%- endfor %}"

Note the added closing bracket after:

|selectattr(‘entity_id’, ‘search’, ‘last_seen’)) -%}
-------------------------------------------------------x------