Read all zigbee2mqtt device attributes and model IDs via template

hi,

i have an template, what prints alls existing mqtt entity Ids with additional infos.

in the past i got results for model ID with attribute model names in brackets with this script?

now i get back without model id, but with the exact same script?


i had documented the used template in my excel, so im sure it was exact the same…gues i had made an zigbee2mqtt update in meantime…

any chance to include the model id again? i would appreaceate to have it in seperate columns… in the past i had to extraxt it from model name in excel… but now i would be happy to have it anyhow…

here the template

{{'Device;Domain;Entity ID;Manufacturer;Model;Area;Integration;ID'}}
{% for state in states -%}
  {%- if device_attr(state.entity_id, 'identifiers') -%}
    {%- set integrations = device_attr(state.entity_id, 'identifiers') | first | list -%}
    {%- set integration = integrations[0] -%}
    {%- set unique_id = integrations[1] -%}
  {%- else -%}
    {%- set integration = '-' -%}
    {%- set unique_id = '-' -%}
  {%- endif -%}
  {%- if device_attr(state.entity_id, 'manufacturer') -%}
    {%- set manufacturer = device_attr(state.entity_id, 'manufacturer') -%}
  {%- else -%}
    {%- set manufacturer = '-' -%}
  {%- endif -%}
  {%- if device_attr(state.entity_id, 'model') -%}
    {%- set model = device_attr(state.entity_id, 'model') -%}
  {%- else -%}
    {%- set model = '-' -%}
  {%- endif -%}
  {%- if device_attr(state.entity_id, 'area_id') -%}
    {%- set area = device_attr(state.entity_id, 'area_id') -%}
  {%- else -%}
    {%- set area = '-' -%}
  {%- endif -%}
  {%- if 'zigbee2mqtt' in unique_id -%}
    {{state.name}};{{state.domain}};{{state.entity_id}};{{manufacturer}};{{model}};{{area_name(area)}};{{integration}};{{unique_id}}
  {% else -%}{#<=ACHTUNG: hier KEIN MINUS! sonst komplette RückgabeErgebnis als eine zeile!#}
  {%- endif -%}
{% endfor %}

and one another problem with that template
the devices are listed with 2 blanks in front? do i have an error in my script, can´t get rid of that too…

thanx
br
Frank

This version reports model_id in its own column. Example:
Smart light bulb;P3Z;

Device;Domain;Entity ID;Manufacturer;Model;Model ID;Area;Integration;ID
{% for s in states -%}
{%- set identifiers = (device_attr(s.entity_id, 'identifiers') | default([], true)) | first | default([], true) -%}
{%- set integration = identifiers[0] | default('-', true) -%}
{%- set unique_id = identifiers[1] | default('-', true) -%}
{%- set manufacturer = device_attr(s.entity_id, 'manufacturer') | default('-', true) -%}
{%- set model = device_attr(s.entity_id, 'model') | default('-', true) -%}
{%- set model_id = device_attr(s.entity_id, 'model_id') | default('-', true) -%}
{%- set area = device_attr(s.entity_id, 'area_id') | default('-', true) -%}
{%- if 'zigbee2mqtt' in unique_id -%}
{{s.name}};{{s.domain}};{{s.entity_id}};{{manufacturer}};{{model}};{{model_id}};{{area_name(area)}};{{integration}};{{unique_id}}
{% else -%}{#<=ACHTUNG: hier KEIN MINUS! sonst komplette RückgabeErgebnis als eine zeile!#}
{%- endif -%}
{% endfor %}

This version reports model_id in parenthesis within the model column. Example:
Smart light bulb (P3Z);

Device;Domain;Entity ID;Manufacturer;Model;Area;Integration;ID
{% for s in states -%}
{%- set identifiers = (device_attr(s.entity_id, 'identifiers') | default([], true)) | first | default([], true) -%}
{%- set integration = identifiers[0] | default('-', true) -%}
{%- set unique_id = identifiers[1] | default('-', true) -%}
{%- set manufacturer = device_attr(s.entity_id, 'manufacturer') | default('-', true) -%}
{%- set model = device_attr(s.entity_id, 'model') | default('-', true) -%}
{%- set model_id = device_attr(s.entity_id, 'model_id') | default('-', true) -%}
{%- set area = device_attr(s.entity_id, 'area_id') | default('-', true) -%}
{%- if 'zigbee2mqtt' in unique_id -%}
{{s.name}};{{s.domain}};{{s.entity_id}};{{manufacturer}};{{ model }}{{ ' (' ~ model_id ~ ')' if model_id != '-' else '' }};{{area_name(area)}};{{integration}};{{unique_id}}
{% else -%}{#<=ACHTUNG: hier KEIN MINUS! sonst komplette RückgabeErgebnis als eine zeile!#}
{%- endif -%}
{% endfor %}

Neither version produces two leading blank characters.


NOTE

There is a way to select all entities created by Zigbee2MQTT. So instead of iterating through all entities in the State Machine, which is what this does:

{% for s in states -%}

You select only the entities created via the MQTT integration and then only if it’s a member of the device named Zigbee2MQTT Bridge.

{% for s in integration_entities('mqtt') | expand 
  if is_device_attr(s.entity_id, 'via_device_id', device_id('Zigbee2MQTT Bridge')) -%}

The resulting template looks like this:

Device;Domain;Entity ID;Manufacturer;Model;Model ID;Area;Integration;ID
{% for s in integration_entities('mqtt') | expand if is_device_attr(s.entity_id, 'via_device_id', device_id('Zigbee2MQTT Bridge')) -%}
{%- set identifiers = (device_attr(s.entity_id, 'identifiers') | default([], true)) | first | default([], true) -%}
{%- set integration = identifiers[0] | default('-', true) -%}
{%- set unique_id = identifiers[1] | default('-', true) -%}
{%- set manufacturer = device_attr(s.entity_id, 'manufacturer') | default('-', true) -%}
{%- set model = device_attr(s.entity_id, 'model') | default('-', true) -%}
{%- set model_id = device_attr(s.entity_id, 'model_id') | default('-', true) -%}
{%- set area = device_attr(s.entity_id, 'area_id') | default('-', true) -%}
{{s.name}};{{s.domain}};{{s.entity_id}};{{manufacturer}};{{model}};{{model_id}};{{area_name(area)}};{{integration}};{{unique_id}}
{% endfor %}

Aside from being slightly more efficient (it iterates through a much shorter list of entities) it also excludes the entities that belong to the Zigbee2MQTT Bridge itself. In other words, these entities will not be reported:

binary_sensor.zigbee2mqtt_bridge_connection_state
select.zigbee2mqtt_bridge_log_level
switch.zigbee2mqtt_bridge_permit_join
button.zigbee2mqtt_bridge_restart
sensor.zigbee2mqtt_bridge_version
update.zigbee2mqtt_update

However, if you want them to be reported then you should continue to use:

{% for s in states -%}
2 Likes

cool, thanx @123 :+1::grin:

1 Like