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

     1  
     2  ---
     3  title: "MQTT"
     4  linkTitle: "mqtt"
     5  date: 2021-10-20
     6  description: >
     7    
     8  ---
     9  
    10  The "mqtt" plugin is an extended version of the "sensor" plugin that provides MQTT protocol functionality. Here are some settings available for the "mqtt" plugin:
    11  
    12  1. `subscribe_topic`: The MQTT topic to subscribe to for receiving data.
    13  2. `mqtt_login`: The login for authentication when connecting to the MQTT broker.
    14  3. `mqtt_pass`: The password for authentication when connecting to the MQTT broker.
    15  
    16  These settings allow you to specify the connection parameters for the MQTT broker and define the topic to subscribe to for receiving data. Thus, the "mqtt" plugin enables integration with MQTT messages, allowing you to receive data from devices using this protocol and use them in automation systems or other components.
    17  
    18  The "sensor" plugin also implements a JavaScript handler called `entityAction`. This handler is designed to process actions related to "entity" type devices based on the "sensor" plugin.
    19  
    20  Here's an example implementation of the `entityAction` handler:
    21  
    22  ```javascript
    23  entityAction = (entityId, actionName, args) => {
    24    // Action handling code
    25  };
    26  ```
    27  
    28  It also implements a JavaScript handler called `mqttEvent`. This handler is designed to process actions related to "entity" type devices based on the "sensor" plugin.
    29  
    30  Here's an example implementation of the `mqttEvent` handler:
    31  
    32  ```javascript
    33  function mqttEvent(message) {
    34    // Action handling code
    35  };
    36  ```
    37  
    38  Example usage of the `mqttEvent` handler:
    39  ```coffeescript
    40  mqttEvent =(message)->
    41    #print '---mqtt new event from plug---'
    42    if !message || message.topic.includes('/set')
    43      return
    44    payload = unmarshal message.payload
    45    attrs =
    46      'consumption': payload.consumption
    47      'linkquality': payload.linkquality
    48      'power': payload.power
    49      'state': payload.state
    50      'temperature': payload.temperature
    51      'voltage': payload.voltage
    52    EntitySetState ENTITY_ID,
    53      'new_state': payload.state
    54      'attribute_values': attrs
    55  ```