mdeweerd / zha-toolkit

🧰 Zigbee Home Assistant Toolkit - service for "rare" Zigbee operations using ZHA on Home Assistant
GNU General Public License v3.0
201 stars 31 forks source link

Add example on how to set last seen attribute #223

Open regevbr opened 8 months ago

regevbr commented 8 months ago

I created the following HA automation to set the last seen attribute on all ZHA devices, similar to how zigbee2mqtt does. Think it will be informative to people as I couldn't find any other solution published anywhere.

@mdeweerd can you please add it to the docs where relevant?

alias: zha last seen attribute
description: set the last seen attribute on all entities of zha end devices
trigger:
  - platform: time_pattern
    seconds: "5"
condition: []
action:
  - service: zha_toolkit.zha_devices
    response_variable: dev_data
    data:
      command_data: last_seen,device_type,entities
  - repeat:
      for_each: "{{ dev_data.devices }}"
      sequence:
        - condition: template
          value_template: "{{ repeat.item.device_type == 'EndDevice' }}"
        - variables:
            last_seen: "{{ repeat.item.last_seen }}"
        - repeat:
            for_each: "{{ repeat.item.entities }}"
            sequence:
              - service: zha_toolkit.ha_set_state
                data:
                  attr_val: "{{ last_seen }}"
                  state_id: "{{ repeat.item.entity_id }}"
                  state_attr: last_seen
mode: single
mdeweerd commented 8 months ago

I'll add it somewhere, probably to scripts - I need to think of a good way to add it to the readme as well (probably just 'Related examples:' in the proper section).

Any example will help, and this example is surely closer to what users expect.

There are examples that are related: https://github.com/mdeweerd/zha-toolkit/blob/main/examples/script_use_zha_devices_response.yaml https://github.com/mdeweerd/zha-toolkit/blob/main/examples/script_use_zha_devices.yaml