Offline detection for Z2M devices with last_seen

Thanks for your reply. All inputs except of 3 are ok - but that’s the reason for that blueprint. I’m sure there is a new requirement for code with unknown input. The code needs a default set like this “default=0” - but i can’t find the right place. The error message directed to “as_timestamp”. Any thoughts about that? Thanks

Ok - really strange! I changed nothing, but after a few days it just works.

Thanks anyway!

Many thanks for the code.

I have slightly adapted the code by adding two functions: “Minutes not seen” and “Included Sensors”.

Regards!

blueprint:
  name: Offline detection for Z2M devices with last_seen MOD v1
  description:
    Regularly test all sensors with 'last_seen' in name and 'timestamp'
    device_class ('last seen' Z2M sensors) to detect offline and if so execute an
    action.
  domain: automation
  input:
    hours:
      name: Hours not seen
      description: Sensors not seen this amount of time are assumed to be offline.
      default: 0
      selector:
        number:
          min: 0.0
          max: 168.0
          unit_of_measurement: h
          mode: slider
          step: 1.0
    minutes:
      name: Minutes not seen
      description: Sensors not seen this amount of minutes are assumed to be offline.
      default: 30
      selector:
        number:
          min: 0.0
          max: 59.0
          unit_of_measurement: min
          mode: slider
          step: 1.0
    time:
      name: Time to test on
      description: Test is run at configured time
      default: "10:00:00"
      selector:
        time: {}
    day:
      name: Weekday to test on
      description:
        "Test is run at configured time either everyday (0) or on a given
        weekday (1: Monday ... 7: Sunday)"
      default: 0
      selector:
        number:
          min: 0.0
          max: 7.0
          mode: slider
          step: 1.0
    exclude:
      name: Excluded Sensors
      description: "'last seen' sensors (from devices that you want to exclude)
        to exclude from detection. Only entities with 'last seen' in name and 'timestamp'
        in device_class are supported, devices must be expanded!"
      default:
        entity_id: []
      selector:
        target:
          entity:
            - domain:
                - sensor
    include:
      name: Included Sensors
      description: "'last seen' sensors (from devices that you want to include)
        to include in detection. Only entities with 'last seen' in name and 'timestamp'
        in device_class are supported, devices must be expanded!"
      default:
        entity_id: []
      selector:
        target:
          entity:
            - domain:
                - sensor
    actions:
      name: Actions
      description:
        Notifications or similar to be run. {{sensors}} is replaced with
        the names of sensors being offline.
      selector:
        action: {}
variables:
  day: !input day
  hours: !input hours
  minutes: !input minutes
  exclude: !input exclude
  include: !input include
  sensors:
    "{% set result = namespace(sensors=[]) %} {% for state in states.sensor
    | rejectattr('attributes.device_class', 'undefined') | selectattr('attributes.device_class',
    '==', 'timestamp') %}
    {% if 'last_seen' in state.entity_id and (include.entity_id == [] or state.entity_id in include.entity_id)
    and not state.entity_id in exclude.entity_id
    and (states(state.entity_id) == 'unavailable' or ((as_timestamp(now())
    - as_timestamp(states(state.entity_id))) > ((hours | int) * 3600 + (minutes | int) * 60))) %}
    {% set result.sensors = result.sensors + [state.name | regex_replace(find=' last
    seen', replace='') ~ ' (' ~ relative_time(strptime(states(state.entity_id),
    '%Y-%m-%dT%H:%M:%S%z', 'unavailable')) ~ ')'] %}
    {% endif %} {% endfor %} {{ result.sensors | join(', ') }}"
trigger:
  - platform: time
    at: !input time
condition:
  - "{{ sensors != '' and (day | int == 0 or day | int == now().isoweekday()) }}"
action:
  - choose: []
    default: !input actions
mode: single

3 Likes

Hello, I am out of ideas, please (and sorry if I post to wrong site). When I use this bueprint, I recieve this message: “Last seen (unawailable) Last seen (unawailable)”. Alld evices are awailable, so where does this mesage come from ? why it is generated?

1 Like

Offline detection for Z2M devices with LQI

2 Likes

This is great, thanks. Would it be possible to add Notification Channels for Android devices?

I am using the following code for the template sensor with the following changes:

  • default added (the 0 in as_timestamp(states(state.entity_id),0)) to fix the no default was specified error message
  • correct check to remove Last seen in the name (for my sensors it’s Last seen in stead of last seen)
  • added the domain of the entity_id
{% set result = namespace(sensors=[]) %}
{% for state in states.sensor | rejectattr('attributes.device_class', 'undefined') | selectattr('attributes.device_class', '==', 'timestamp') %}
    {% if 'last_seen' in state.entity_id and (states(state.entity_id) == 'unavailable' or ((as_timestamp(now()) - as_timestamp(states(state.entity_id),0)) > ((24 | int) * 60 * 60))) %}
        {% set result.sensors = result.sensors + [state.name | regex_replace(find=' Last seen', replace='') ~ ' (' ~config_entry_attr(config_entry_id(state.entity_id), 'domain') ~ ' | ' ~ relative_time(strptime(states(state.entity_id), '%Y-%m-%dT%H:%M:%S%z', 'unavailable')) ~ ')'] %}
    {% endif %}
{% endfor %}
{{ result.sensors | join('\n') | truncate(254, True) }}

Which results in the following list for entities which are in this example all unavailable:

Light 1 (zwave_js | unavailable)
Switch 2 (mqtt | unavailable)
Sensor 3 (mqtt | unavailable)

This can be used in the blueprint as well.

6 Likes

hello, stop working for me, I received this error:

Registratore: homeassistant.components.automation.offline_detection_for_z2m_devices_with_last_seen
Fonte: helpers/script.py:2032
Integrazione: Automazione (documentazione, problemi)
Prima occorrenza: 15:50:31 (2 occorrenze)
Ultimo accesso: 15:50:31

Offline detection for Z2M devices with last_seen: Choose at step 1: default: Error executing script. Invalid data for call_service at pos 2: required key not provided @ data[‘message’]
Offline detection for Z2M devices with last_seen: Error executing script. Invalid data for choose at pos 1: required key not provided @ data[‘message’]

Would you say using LQI is a better method of detecting offline devices?

LQI I guess better. Bot for me not works. Not sure why… Copied code as is. No errors.
But automation gave empty list…

I got the same empty list issue until I enabled Last Seen in Diagnostic for every ZB devices.

Settings → Devices & services, then select the Devices tab, look for your ZB device, select it. If under Diagnostic last seen is gray out like in the image below, you will need to enable it. Click “last seen”, then the gear icon, then enable.