Skip to content

Instantly share code, notes, and snippets.

@iceteabottle
Last active April 2, 2025 15:49
Show Gist options
  • Save iceteabottle/fe27ccad244774e44bcae651dec8c5b1 to your computer and use it in GitHub Desktop.
Save iceteabottle/fe27ccad244774e44bcae651dec8c5b1 to your computer and use it in GitHub Desktop.
z2m state sync #home-assistant

z2m state sync

This is a blueprint for a Home Assistant automation that triggers a sync of the state of a Zigbee device connected to Zigbee2MQTT. This is useful when you control a zigbee device directly via touchlink and want to keep the state in sync with Home Assistant.

Use case: Touchlink

You're using Home Assistant with Zigbee2MQTT and you have a Zigbee device that you control directly via touchlink (e.g. a remote control). E.g. you have an TRETAKT smart plug and a TRADFRI on/off switch. You don't need to create an automation to control the device via Home Assistant, because you can connect the smart plug and the switch via touchlink. This can be extremely useful for devices which supports dimming, because you can dim the device directly via the remote control. The only downside is that the state of the device in Home Assistant is not in sync with the real state of the device. This blueprint solves this problem by triggering a sync of the state of the device in Home Assistant.

Tip

You may not even need this blueprint if you use the Zigbee2MQTT binding feature. You can just bind OnOff and LevelCtrl to the coordicator. In the image below you can see a Livarno Lux Ceiling Panel. I control the LED panel with an unsupported rotary switch (S57018 from The Light Group AS) which is directly connected to the LED panel via touchlink. To get the state synced back I only needed to add this binding via Zigbee2MQTT. No automation needed!

binding via z2m

Limitations

Zigbee2MQTT

This blueprint requires that you're using Zigbee2MQTT. It's not possible to use this blueprint with other Zigbee integrations like ZHA.

Trigger actions

Choose trigger actions which should trigger the state sync. Use e.g. a switch with on/off/brightness change actions. The state sync will be triggered when the action is executed.

Installation

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

blueprint:
name: z2m State Sync
description: Syncs selected properties of a Zigbee device based on user-defined triggers. See the [readme](https://gist.github.com/iceteabottle/fe27ccad244774e44bcae651dec8c5b1#file-readme-md) for more information.
domain: automation
input:
triggers:
name: Automation Triggers
description: Select the triggers that should initiate the sync.
selector:
trigger:
default: []
target_device:
name: Target device to sync
description: The target Zigbee device which should be synced.
selector:
device:
integration: mqtt
properties:
name: Properties to Sync
description: Select the properties to sync (state, brightness, color, etc.).
selector:
select:
options:
- state
- brightness
- color
- color_temp
- effect
multiple: true
custom_value: true
default: [state]
trigger: !input triggers
variables:
target_device: !input target_device
selected_properties: !input properties
friendly_name: "{{ device_attr(target_device, 'name') | replace('_', ' ') }}"
action:
- service: mqtt.publish
data:
topic: >
zigbee2mqtt/{{ friendly_name }}/get
payload: >-
{
{%- for prop in selected_properties %}
"{{ prop }}": "{}"{% if not loop.last %},{% endif %}
{%- endfor %}
}
qos: 0
retain: false
mode: single
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment