Bulk Interview devices in zigbee2mqtt

Is there any way to bulk interview all my zigbee devices, without having to click interview on every single on of them?
Some kind of loop that goes through one by one of the devices?

This is the MQTT topic used to interview a device.

zigbee2mqtt/bridge/request/device/interview

The script needs to iterate through all MQTT devices and publish an appropriate payload (see linked documentation) to the topic.

The challenge is to create a template that identifies all MQTT-based devices and reports them in a list. The script’s repeat for_each will loop through that list of devices.

This template will produce that list but if you have any MQTT-based devices that were not created by Zigbee2MQTT, they will also be included in the list. Zigbee2MQTT has no knowledge of such devices so they need to be excluded from the list.

{{ integration_entities('mqtt')
  | map('device_id')
  | reject('eq', none)
  | unique
  | map('device_name')
  | list }}

I haven’t found a way (yet) to identify only the MQTT-based devices created by Zigbee2MQTT. It should be possible because, if you look at the Device page of a device created by Zigbee2MQTT, it will show:

Connected via Zigbee2MQTT Bridge

The challenge is to find that information and use it in a template.

I found how to select only the devices that are created by Zigbee2MQTT.

Your coordinator will appear as a device in Home Assistant with the name Zigbee2MQTT Bridge. That’s the default name so if you have changed it then you’ll also need to change the name in the following template.

{% set ns = namespace(devices=[]) %}
{% for d in integration_entities('mqtt') | map('device_id') | reject('eq', none) | unique
    if is_device_attr(d, 'via_device_id', device_id('Zigbee2MQTT Bridge'))-%}
  {% set ns.devices = ns.devices + [device_name(d)] %}
{% endfor -%}
{{ ns.devices }}

The template produces a list containing the names of all devices based on Zigbee2MQTT. You can test the template in the Template Editor to confirm it produces the correct results.

I was going to provide you with a script that loops through the list of devices and sends an interview request to each one. However, it made me wonder why do you want to interview all of your devices? The devices that are battery-powered typically do not respond to an interview request because they are in sleep-mode to conserve their battery.

You have to wake up a sleeping device before it will respond to an interview request. For example, if it’s a remote-control button you have to physically press it, to wake it up, then immediately send the interview request. Only devices that are powered by the mains (not a battery) are always awake and will accept an interview request.

1 Like

I tested and confirmed that the following script works on my system.

It interviews all Zigbee2MQTT devices that are not battery-powered and are not a Zigbee2MQTT Group.

alias: Interview Zigbee2MQTT Devices
sequence:
  - variables:
      z2m_devices: >
        {% set ns = namespace(devices=[]) %}
        {% for d in integration_entities('mqtt') | map('device_id') | reject('eq', none) | unique
            if is_device_attr(d, 'via_device_id', device_id('Zigbee2MQTT Bridge'))
            and 'battery' not in device_entities(d) | join('')
            and device_attr(d, 'model') != 'Group' -%}
          {% set ns.devices = ns.devices + [device_name(d)] %}
        {% endfor -%}
        {{ ns.devices }}
  - repeat:
      for_each: "{{ z2m_devices }}"
      sequence:
        - variables:
            msg:
              id: "{{ repeat.item }}"
        - action: mqtt.publish
          data:
            topic: zigbee2mqtt/bridge/request/device/interview
            payload: "{{ msg | to_json }}"
        - delay:
            seconds: 5