Zigbee2mqtt
The “Zigbee2mqtt” plugin is part of the system and provides integration between Zigbee devices and an MQTT broker. This plugin allows you to control and manage Zigbee devices through MQTT messages.
The “Zigbee2mqtt” plugin implements a JavaScript handler called zigbee2mqttEvent
, which doesn’t take any parameters. Inside the handler, there is an accessible message
object that represents the information of the received MQTT message.
The properties of the message
object include:
payload
: The value of the message, represented as a dictionary (map) where the keys are strings and the values can be of any type.topic
: The MQTT message topic, indicating the source or destination of the message.qos
: The Quality of Service level of the MQTT message.duplicate
: A flag indicating whether the message is a duplicate.storage
: AStorage
object that provides access to a data store for caching and retrieving arbitrary values.error
: A string containing information about an error if one occurred during message processing.success
: A boolean value indicating the successful completion of an operation or message processing.new_state
: AnStateParams
object representing the new state of an actor after an operation is performed.
Here’s an example of using the zigbee2mqttEvent
handler:
function zigbee2mqttEvent(message) {
console.log("Received MQTT message:");
console.log("Payload:", message.payload);
console.log("Topic:", message.topic);
console.log("QoS:", message.qos);
console.log("Duplicate:", message.duplicate);
if (message.error) {
console.error("Error:", message.error);
} else if (message.success) {
console.log("Operation successful!");
console.log("New state:", message.new_state);
}
// Accessing the storage
const value = message.storage.getByName("key");
console.log("Value from storage:", value);
}
The zigbee2mqttEvent
handler can be used to process incoming MQTT messages and perform additional operations based on the received data.
Here’s an example in CoffeeScript:
zigbee2mqttEvent =(message)->
# Print '---mqtt new event from plug---'
if !message || message.topic.includes('/set')
return
payload = unmarshal message.payload
attrs =
'consumption': payload.consumption
'linkquality': payload.linkquality
'power': payload.power
'state': payload.state
'temperature': payload.temperature
'voltage': payload.voltage
EntitySetState ENTITY_ID,
'new_state': payload.state
'attribute_values': attrs
zigbee2mqttEvent =(message)->
# Print '---mqtt new event from button---'
if !message
return
payload = unmarshal message.payload
attrs =
'battery': payload.battery
'linkquality': payload.linkquality
'voltage': payload.voltage
state = ''
if payload.action
attrs.action = payload.action
state = payload.action + "_action"
if payload.click
attrs.click = payload.click
attrs.action = ""
state = payload.click + "_click"
EntitySetState ENTITY_ID,
'new_state': state.toUpperCase()
'attribute_values': attrs
Last modified February 5, 2024: Merge pull request #270 from e154/master (7108cb6)