MacWyznawca / homebridge-mqtt-switch-tasmota

Plugin to HomeBridge optimized to work with firmware Sonoff-Tasmota, MQTT.
MIT License
57 stars 18 forks source link

Homebridge wont detect status change #12

Closed mrcsmxms closed 6 years ago

mrcsmxms commented 6 years ago

Hello. i just moved from HAP-NodeJS to Homebridge and now i'm strugglin' with this Plugin. i can control the MQTT-Tasmota-Devices in HomeKit but when i trigger the Sonoff Touch manually, HomeKit wont detect that.

here's my old accessory from HAP, which worked completely:

var Accessory = require('../').Accessory;
var Service = require('../').Service;
var Characteristic = require('../').Characteristic;
var uuid = require('../').uuid;
var mqtt = require('mqtt');
var MQTT_IP = 'localhost' //change this if your MQTT broker is different
var mqttMSG = false;

var name = "Badezimmer"; //accessory name
var sonoffUUID = "hap-nodejs:accessories:sonof3fstand"; //change this to your preferences
var sonoffUsername = "1A:CB:3C:4D:5E:FF";
var MQTT_NAME = 'bathroom' //MQTT topic that was set on the Sonoff firmware

var options = {
  port: 1883,
  host: MQTT_IP,
//  username: 'pi', enable only if you have authentication on your MQTT broker
//  password: 'raspberry', enable only if you have authentication on your MQTT broker
  clientId: MQTT_NAME+'HAP'
};
var sonoffTopic = 'cmnd/'+MQTT_NAME+'/power';
var client = mqtt.connect(options);

client.on('message', function(topic, message) {
//  console.log(message.toString());
  message = message.toString();
  mqttMSG = true;
  if (message.includes('ON')){
    sonoffObject.powerOn = true;
  }
  else{
    sonoffObject.powerOn = false;
  }
  sonoff
    .getService(Service.Outlet)
    .setCharacteristic(Characteristic.On,sonoffObject.powerOn);
});

client.on('connect', function () {
  client.subscribe('stat/'+MQTT_NAME+'/POWER')
});

var sonoffObject = {
  powerOn: false,
  setPowerOn: function(on) {
    sonoffObject.powerOn = on;
    if (on) {
      client.publish(sonoffTopic, 'on');
    } else {
      client.publish(sonoffTopic, 'off');
    }
  },
  identify: function() {
    console.log(name + " Identified!");
  }
}

var sonoff = exports.accessory = new Accessory(name, uuid.generate(sonoffUUID));

sonoff.username = sonoffUsername;
sonoff.pincode = "031-45-154";

// listen for the "identify" event for this Accessory
sonoff.on('identify', function(paired, callback) {
  sonoffObject.identify();
  callback();
});

sonoff
  .addService(Service.Outlet, name)
  .getCharacteristic(Characteristic.On)
  .on('set', function(value, callback) {
    if(mqttMSG){
      mqttMSG = false;
      callback();
    }
    else {
      sonoffObject.setPowerOn(value);
      callback();
    }
  });

sonoff
  .getService(Service.Outlet)
  .getCharacteristic(Characteristic.On)
  .on('get', function(callback) {
    client.publish(sonoffTopic,'')
    callback(undefined, sonoffObject.powerOn);
  });

and this is my code from the config.json in HomeBridge:

{
                "accessory": "mqtt-switch-tasmota",
                "switchType": "outlet",
                "name": "bathroom",
                "url": "mqtt://homebridge.fritz.box",
                "username": "DVES_USER",
                "password": "DVES_PASS",
                "topics": {
                        "statusGet": "stat/bathroom/RESULT",
                        "statusSet": "cmnd/bathroom/POWER",
                        "stateGet": "tele/bathroom/STATE"
                },
                "onValue": "ON",
                "offValue": "OFF",
                "activityTopic": "tele/bathroom/LWT",
                "activityParameter": "Online",
                "startCmd": "cmnd/bathroom/TelePeriod",
                "startParameter": "60",
                "manufacturer": "iTead",
                "model": "Sonoff Touch",
                "serialNumberMAC": "1234"
        }

when i trigger manually at the switch these are the MQTT-Commands:

for on:

stat/bathroom/RESULT
{"POWER":"ON"}

stat/bathroom/POWER
ON

and for off:

stat/bathroom/RESULT
{"POWER":"OFF"}

stat/bathroom/POWER
OFF

and when i trigger with HomeKit these are the results:

for on:

cmd/bathroom/POWER
ON

stat/bathroom/RESULT
{"POWER":"ON"}

stat/bathroom/POWER
ON

for off:

cmd/bathroom/POWER
OFF

stat/bathroom/RESULT
{"POWER":"OFF"}

stat/bathroom/POWER
OFF

sorry for the long post but i can't figure out why it doesn't work like it should. can anyone help me with that one?

mrcsmxms commented 6 years ago

a reboot solved it. sometimes it's just that simple

jeylites commented 6 years ago

@mrcsmxms

Hi, I noticed your MQTT url, you have it as characters as appose to numbers Eg: ip address. How did you do that for it to talk to MQTT broker?

mrcsmxms commented 6 years ago

@jeylites

i used the mDNS. i have a FRITZ!Box which allows me to name all network clients. i named the raspberry "homebridge" so i can use the mDNS "homebridge.fritz.box". even if the IP changes the mDNS will still work.

i hope that answers your question

jeylites commented 6 years ago

@mrcsmxms This is a great tip to solve the frequent ip change my raspberry Pi goes through. So much so I gave it a static ip but this is even cooler. I don’t think my stock network provider router supports it however, I did some googling that lead me to this link mDNS RPI

Will implement it over the weekend. Thank you!

mrcsmxms commented 6 years ago

@jeylites You‘re welcome ;)