PatchworkBoy / homebridge-edomoticz

Domoticz Homebridge-Plugin
Other
118 stars 43 forks source link

Error causing Homebridge to keep restarting #129

Closed rjblake closed 6 years ago

rjblake commented 6 years ago

All I have from the logs is:

SyntaxError: Unexpected token C in JSON at position 0 at JSON.parse (<anonymous>) at MqttClient.<anonymous> (/usr/lib/node_modules/homebridge-edomoticz/lib/mqtt.js:57:24) at emitThree (events.js:136:13) at MqttClient.emit (events.js:217:7) at MqttClient._handlePublish (/usr/lib/node_modules/homebridge-edomoticz/node_modules/mqtt/lib/client.js:934:12) at MqttClient._handlePacket (/usr/lib/node_modules/homebridge-edomoticz/node_modules/mqtt/lib/client.js:305:12) at work (/usr/lib/node_modules/homebridge-edomoticz/node_modules/mqtt/lib/client.js:261:12) at Writable.writable._write (/usr/lib/node_modules/homebridge-edomoticz/node_modules/mqtt/lib/client.js:271:5) at doWrite (/usr/lib/node_modules/homebridge-edomoticz/node_modules/readable-stream/lib/_stream_writable.js:406:64) at writeOrBuffer (/usr/lib/node_modules/homebridge-edomoticz/node_modules/readable-stream/lib/_stream_writable.js:395:5)

This is running on Ubuntu and NodeJS 8.9.3. I have checked my config.json on JSONLint and it is correct. If I change the "mqtt": 1 to a 0 it runs without issue.

Lorccan commented 6 years ago

Do you have more than one room defined in config.json? I had a similar problem that I fixed by defining a single room in Domoticz for all devices that I wanted to expose to Homebridge (and then using that index). The alternative - and a way of testing without modifying the Domoticz config - is to define a single room with index 0 (which will expose all Domoticz devices).

rjblake commented 6 years ago

No - I have a single room (IDX9) with the devices. I also deleted all devices and added back a single one to see if that was the issue, but problem still is there

PatchworkBoy commented 6 years ago

You need to manually subscribe to domoticz’s mqtt stream using an mqtt reader and find the specific mqtt message causing the error, and the device sending that message. Your Domoticz installation is sending a piece of data in an unexpected format...

You could also manually run homebridge with debugging on, which should show more info.

DEBUG=* homebridge -D

jannnfe commented 6 years ago

@PatchworkBoy Can you please describe how to fix this issue in detail. I am about 5 hours of trying to fix it! :( I deleted all my Domoticz Devices, deleted all Homebridge configurations but nothing helps and reinstall Homebridge and homebridge-edomoticz. Deleted accessories and persist folder. Nothing helps

PatchworkBoy commented 6 years ago

Already told you... run homebridge with the debug command. Wait for it to crash. Copy and paste the tail end of the output.

A json object via MQTT begins and ends with { and }. Something is sending a message beginning with C instead of {. Only way to find out what is by seeing the MQTT messages.

jannnfe commented 6 years ago

Thanks for your reply! Im getting this:

[2018-2-13 20:41:41] [Domoticz Bridge] Successfully connected to MQTT broker.
undefined:1
Connected
^

SyntaxError: Unexpected token C in JSON at position 0
    at JSON.parse (<anonymous>)
    at MqttClient.<anonymous> (/usr/local/lib/node_modules/homebridge-edomoticz/lib/mqtt.js:57:24)
    at emitThree (events.js:135:13)
    at MqttClient.emit (events.js:216:7)
    at MqttClient._handlePublish (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/mqtt/lib/client.js:940:12)
    at MqttClient._handlePacket (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/mqtt/lib/client.js:305:12)
    at work (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/mqtt/lib/client.js:261:12)
    at Writable.writable._write (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/mqtt/lib/client.js:271:5)
    at doWrite (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/readable-stream/lib/_stream_writable.js:418:64)
    at writeOrBuffer (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/readable-stream/lib/_stream_writable.js:407:5)

Do I understand the right thing that the "Connected" message by a device is caused the error? But I do not know which device that should be, because my MQTT Broker show no "Connected"

EDIT: Now its:

[2018-2-13 21:10:53] [Domoticz Bridge] Successfully connected to MQTT broker.
undefined:1
Connection Lost
^

SyntaxError: Unexpected token C in JSON at position 0
PatchworkBoy commented 6 years ago

Which MQTT Broker are you using...? I’ve never seen one configured to send "connected" when it’s connected...

jannnfe commented 6 years ago

I'am using mosquitto. I think it's just so weird, because yesterday everything was fine and today I suddenly get this mistake. I'm really baffled and hope that you can somehow help me further. Sorry I bother you so much with my mistake.

When i try:

mosquitto_sub -v -t 'domoticz/out'

I get:

domoticz/out Connection Lost

It must be related to that because the error says above "Connection Lost" and "domoticz/out" send exactly that

PatchworkBoy commented 6 years ago

Therein lies your problem. Your broker can’t connect to Domoticz and therefore nor can homebridge.

Have you definitely enabled the MQTT Client Hardware in Domoticz, and configured it as per the attached?

mqtt

Does the Domoticz log show anything relating to MQTT connections?

jannnfe commented 6 years ago

Yes I have MQTT Client Gateway enabled in Domoticz and I think its working because when i toggle a light i get a MQTT message:

# mosquitto_sub -v -t '#'
ThermostatSensor/status Connected
tele/Aquariumlicht/LWT Online
tele/Garagentor/LWT Online
tele/Plattenspieler/LWT Online
tele/Doppellampe/LWT Online
tele/Thermostat/LWT Online
tele/Türöffner/LWT Online
tele/Aquariumtemperatur/LWT Online
domoticz/out Connection Lost
domoticz/out {
   "Battery" : 255,
   "RSSI" : 12,
   "description" : "",
   "dtype" : "Lighting Limitless/Applamp",
   "id" : "00001BCE",
   "idx" : 11,
   "name" : "Schreibtisch",
   "nvalue" : 0,
   "stype" : "RGBW",
   "svalue1" : "100",
   "switchType" : "Dimmer",
   "unit" : 1
}
jannnfe commented 6 years ago

Finally I fixed it. I had to delete my MQTT Gateway in Domoticz, then execute the following commands on my MQTT Broker:

sudo service mosquitto stop
sudo rm /var/lib/mosquitto/mosquitto.db
sudo service mosquitto start

And now again set the MQTT Gateway in domoticz. Now it works again. Funny I would like to know what that was. Thanks for your help I hope the error is no longer! :)

PatchworkBoy commented 6 years ago

It’s working intermittently. Occasionally it’s losing the connection - this shouldn’t be happening.

You need to open a post over on the Domoticz forum to work out why Mosquitto is losing it’s connection with your system...

In the meantime we can add a handler for it to stop homebridge dying and just write an error to the error log.

Replace homebridge-edomoticz/lib/mqtt.js with this: https://gist.github.com/PatchworkBoy/530b9e5128904d9edac3c028fd261cab

...and restart homebridge. That should hopefully stop it from hard-crashing on you and let it recover whenever it comes back up. Test it and let me know if it works... if so I’ll add it into the next npm commit.

EDIT: glad you’ve got it resolved... but replace file as per above anyway just in case it goes again.

jannnfe commented 6 years ago

I have now also found the reason why "Connected" or "Connection Lost" was found in the MQTT topic "domoticz/out". If you switch on the Domoticz Controller with the ESPEasy firmware, it sends exactly one of the two status messages when it is connected and when the connection is interrupted. Just a pity, because I would like to use this feature in combination with eDomoticz. Do you have an idea how to get this to work?

PatchworkBoy commented 6 years ago

The gist above should disregard any malformed MQTT messages and log them as an error without halting homebridge.

jannnfe commented 6 years ago

Problem with ESPEasy fixed in newest Version: https://github.com/letscontrolit/ESPEasy/issues/869