TheAgentK / tuya-mqtt

Nodejs-Script to combine tuyaapi and openhab via mqtt
MIT License
173 stars 80 forks source link

Constant off states after start #11

Closed lucs7 closed 5 years ago

lucs7 commented 5 years ago

Great project! I tried the wrapper in a small linux test environment and it seems to work and detect device switches correctly. Unfortunately right after the first correct state, i get a constant stream of 'off' state messages from the state topic.

Ubuntu 18.04 Mosquito in a docker 1.5 nodejs v8.10.0

I am using a TECKIN WLAN Smart socket.

Steps to reproduce the behavior:

  1. Start local freshly mosquitto (deleted mosquitto.db)
  2. Starting tuya-mqtt.js in degug
  3. Log shows connect to mqqt broker
  4. Connect to broker with mqqt.fx client (broker status shows two clients connected)
  5. subscribe to device state topic (tuya/socket/022000606001946fedd5/9b278320e6e41c83/192.168.180.104/state) the device id and localkey were tested with the tuyaapi work switching the device
  6. send 'on' paload to device command topic
  7. devices witches on
  8. state topic switches to 'on'
  9. a second later state topic switches to 'off' while the device is still on and repeates sending this indefinitely

I would expect the wrapper to send the current status. Maybe I'm understanding mqqt wrong and this is a retained message? Any idea how to solve this? I would attach the debug output, but I don#t have a lot of experience in nodejs and don#t know how to pipe the node console outputin a file.

thanks for any help

samlangdon commented 5 years ago

Hi @lucs7 - I had the same issue and just figured out a fix. It seems the dps property (1) that contains the state is only sent by the device when the state changes. The device is constantly chirping away with various other updates and for each update, the tuya-mqtt.js script tries to get the state and publish it. When it's not there, the state bmap reports OFF. So I think we need a little logic to check if the status is set before publishing it.

@TheAgentK - (thanks very much for your excellent work by the way - great script) - I've made a tweak that resolves the issue for me. Not sure what the etiquette is on sending pull requests, so I've just pasted my suggested fix for tuya-mqtt.js below. I'm only using sockets, so not sure how this affects lightbulbs and whether the existing lightbulb specific IF does the same thing.

TuyaDevice.onAll('data', function (data) {
    try {
        debugTuya('Data from device ' + this.type + ' :', data);
        var status = data.dps['1'];
        if (this.type == "lightbulb" && status == undefined) {
            status = true;
        }
        if(status !== undefined){
            debugTuya('Status is defined as ' + status);
            publishStatus(this, bmap(status));
        }
        publishDPS(this, data.dps);
    } catch (e) {
        debugError(e);
    }
});
TheAgentK commented 5 years ago

Now i set the status only if it realy exists. If dps[1] does not exist, then nothing happens.

samlangdon commented 5 years ago

No i set the status only if it realy exists. If dps[1] does not exist, then nothing happens.

I've pulled the latest code to test/sync with your repo & it works for me - thanks for updating so quickly @TheAgentK. @lucs7 - I'm sure this will do the job for you too.

TheAgentK commented 5 years ago

@samlangdon nice to hear my changes solved the problem.