github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/doc/content/en/docs/plugins/zibee2mqtt.md (about)

     1  
     2  ---
     3  title: "Zigbee2mqtt"
     4  linkTitle: "zigbee2mqtt"
     5  date: 2021-10-20
     6  description: >
     7    
     8  ---
     9  
    10  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.
    11  
    12  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.
    13  
    14  The properties of the `message` object include:
    15  
    16  1. `payload`: The value of the message, represented as a dictionary (map) where the keys are strings and the values can be of any type.
    17  2. `topic`: The MQTT message topic, indicating the source or destination of the message.
    18  3. `qos`: The Quality of Service level of the MQTT message.
    19  4. `duplicate`: A flag indicating whether the message is a duplicate.
    20  5. `storage`: A `Storage` object that provides access to a data store for caching and retrieving arbitrary values.
    21  6. `error`: A string containing information about an error if one occurred during message processing.
    22  7. `success`: A boolean value indicating the successful completion of an operation or message processing.
    23  8. `new_state`: An `StateParams` object representing the new state of an actor after an operation is performed.
    24  
    25  Here's an example of using the `zigbee2mqttEvent` handler:
    26  
    27  ```javascript
    28  function zigbee2mqttEvent(message) {
    29      console.log("Received MQTT message:");
    30      console.log("Payload:", message.payload);
    31      console.log("Topic:", message.topic);
    32      console.log("QoS:", message.qos);
    33      console.log("Duplicate:", message.duplicate);
    34  
    35      if (message.error) {
    36          console.error("Error:", message.error);
    37      } else if (message.success) {
    38          console.log("Operation successful!");
    39          console.log("New state:", message.new_state);
    40      }
    41  
    42      // Accessing the storage
    43      const value = message.storage.getByName("key");
    44      console.log("Value from storage:", value);
    45  }
    46  ```
    47  
    48  The `zigbee2mqttEvent` handler can be used to process incoming MQTT messages and perform additional operations based on the received data.
    49  
    50  Here's an example in CoffeeScript:
    51  
    52  ```coffeescript
    53  zigbee2mqttEvent =(message)->
    54    # Print '---mqtt new event from plug---'
    55    if !message || message.topic.includes('/set')
    56      return
    57    payload = unmarshal message.payload
    58    attrs =
    59      'consumption': payload.consumption
    60      'linkquality': payload.linkquality
    61      'power': payload.power
    62      'state': payload.state
    63      'temperature': payload.temperature
    64      'voltage': payload.voltage
    65    EntitySetState ENTITY_ID,
    66      'new_state': payload.state
    67      'attribute_values': attrs
    68  ```
    69  
    70  ```coffeescript
    71  zigbee2mqttEvent =(message)->
    72    # Print '---mqtt new event from button---'
    73    if !message
    74      return
    75    payload = unmarshal message.payload
    76    attrs =
    77      'battery': payload.battery
    78      'linkquality': payload.linkquality
    79      'voltage': payload.voltage
    80    state = ''
    81    if payload.action
    82      attrs.action = payload.action
    83      state = payload.action + "_action"
    84    if payload.click
    85      attrs.click = payload.click
    86      attrs.action = ""
    87      state = payload.click + "_click"
    88    EntitySetState ENTITY_ID,
    89      'new_state': state.toUpperCase()
    90      'attribute_values': attrs
    91  ```