SHARE
TWEET

Untitled

a guest Jul 20th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const mqtt = require('mqtt');
  2. const client = mqtt.connect('mqtt://gladys');
  3.  
  4. client.on('connect', function() {
  5.   client.subscribe('zigbee2mqtt/#', function(err) {
  6.     if (!err) {
  7.       // client.publish('zigbee2mqtt/bridge/config/permit_join', 'false');
  8.       // client.publish('zigbee2mqtt/bridge/networkmap', 'graphviz');
  9.     }
  10.   });
  11. });
  12.  
  13. const PREFIX = 'zigbee2mqtt/';
  14. const devices = {
  15.   living: {
  16.     addr: PREFIX + '0x0017880103d934ec',
  17.     state: null,
  18.   },
  19.   bedroom: {
  20.     addr: PREFIX + '0x001788010390c83d',
  21.     state: null,
  22.   },
  23.   button: {
  24.     addr: PREFIX + '0x00158d00030ab83e',
  25.   },
  26. };
  27.  
  28. client.on('message', function(topic, message) {
  29.   console.log(topic, message.toString());
  30.  
  31.   if (message.indexOf('{') === 0) {
  32.     const data = JSON.parse(message);
  33.     switch (topic) {
  34.       case devices.living.addr:
  35.         devices.living.state = data.state;
  36.         break;
  37.       case devices.bedroom.addr:
  38.         devices.bedroom.state = data.state;
  39.         break;
  40.       case devices.button.addr:
  41.         const message = { state: devices.living.state === 'ON' ? 'OFF' : 'ON' };
  42.         client.publish(devices.living.addr + '/set', JSON.stringify(message));
  43.         client.publish(devices.bedroom.addr + '/set', JSON.stringify(message));
  44.     }
  45.   }
  46. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top