PatchworkBoy / homebridge-edomoticz

Domoticz Homebridge-Plugin
Other
118 stars 43 forks source link

Lock support #36

Closed jonferreira closed 8 years ago

jonferreira commented 8 years ago

Fantastic work so far, congrats!

Any chance we can get proper "lock" support?

"SubType" : "Switch", "SwitchType" : "Door Lock", "SwitchTypeVal" : 11,

This was I could tell Siri to "lock/unlock Garage Gate" instead of "Turn On/Off Garage Gate" if that makes sense

PatchworkBoy commented 8 years ago

Need full JSON output for the device from http://ipaddress:8080/json.htm?type=devices&rid=[IDX] with the device in all it's possible states (i.e.: one JSON output with it locked, another with it unlocked).

Replace [IDX] with the idx number for the lock device as per Domoticz' Settings > Devices page.

jonferreira commented 8 years ago

Closed

{
   "ActTime" : 1471877582,
   "ServerTime" : "2016-08-22 15:53:02",
   "Sunrise" : "06:57",
   "Sunset" : "20:20",
   "result" : [
      {
         "AddjMulti" : 1.0,
         "AddjMulti2" : 1.0,
         "AddjValue" : 0.0,
         "AddjValue2" : 0.0,
         "BatteryLevel" : 255,
         "CustomImage" : 0,
         "Data" : "Closed",
         "Description" : "",
         "Favorite" : 0,
         "HardwareID" : 15,
         "HardwareName" : "Dummy",
         "HardwareType" : "Dummy (Does nothing, use for virtual switches only)",
         "HardwareTypeVal" : 15,
         "HaveDimmer" : true,
         "HaveGroupCmd" : true,
         "HaveTimeout" : false,
         "ID" : "00014108",
         "Image" : "Light",
         "InternalState" : "Closed",
         "IsSubDevice" : false,
         "LastUpdate" : "2016-08-22 13:10:16",
         "Level" : 0,
         "LevelInt" : 0,
         "MaxDimLevel" : 100,
         "Name" : "Garage Gate",
         "Notifications" : "false",
         "PlanID" : "4",
         "PlanIDs" : [ 4 ],
         "Protected" : false,
         "ShowNotifications" : true,
         "SignalLevel" : "-",
         "Status" : "Closed",
         "StrParam1" : "",
         "StrParam2" : "",
         "SubType" : "Switch",
         "SwitchType" : "Door Lock",
         "SwitchTypeVal" : 11,
         "Timers" : "false",
         "Type" : "Light/Switch",
         "TypeImg" : "door",
         "Unit" : 1,
         "Used" : 1,
         "UsedByCamera" : false,
         "XOffset" : "0",
         "YOffset" : "0",
         "idx" : "185"
      }
   ],
   "status" : "OK",
   "title" : "Devices"
}

Opened

{
   "ActTime" : 1471877623,
   "ServerTime" : "2016-08-22 15:53:43",
   "Sunrise" : "06:57",
   "Sunset" : "20:20",
   "result" : [
      {
         "AddjMulti" : 1.0,
         "AddjMulti2" : 1.0,
         "AddjValue" : 0.0,
         "AddjValue2" : 0.0,
         "BatteryLevel" : 255,
         "CustomImage" : 0,
         "Data" : "Open",
         "Description" : "",
         "Favorite" : 0,
         "HardwareID" : 15,
         "HardwareName" : "Dummy",
         "HardwareType" : "Dummy (Does nothing, use for virtual switches only)",
         "HardwareTypeVal" : 15,
         "HaveDimmer" : true,
         "HaveGroupCmd" : true,
         "HaveTimeout" : false,
         "ID" : "00014108",
         "Image" : "Light",
         "InternalState" : "Open",
         "IsSubDevice" : false,
         "LastUpdate" : "2016-08-22 15:53:38",
         "Level" : 0,
         "LevelInt" : 0,
         "MaxDimLevel" : 100,
         "Name" : "Garage Gate",
         "Notifications" : "false",
         "PlanID" : "4",
         "PlanIDs" : [ 4 ],
         "Protected" : false,
         "ShowNotifications" : true,
         "SignalLevel" : "-",
         "Status" : "Open",
         "StrParam1" : "",
         "StrParam2" : "",
         "SubType" : "Switch",
         "SwitchType" : "Door Lock",
         "SwitchTypeVal" : 11,
         "Timers" : "false",
         "Type" : "Light/Switch",
         "TypeImg" : "door",
         "Unit" : 1,
         "Used" : 1,
         "UsedByCamera" : false,
         "XOffset" : "0",
         "YOffset" : "0",
         "idx" : "185"
      }
   ],
   "status" : "OK",
   "title" : "Devices"
}
PatchworkBoy commented 8 years ago

Ta!

jonferreira commented 8 years ago

I'm happy to help anyway I can although I'm a bit lost with Apple documentation

I can see there's a Door Lock and Garage Opened "category" but no idea what characteristics each one have - https://developer.apple.com/reference/homekit/hmaccessorycategory/2315143-accessory_category_types

PatchworkBoy commented 8 years ago

Service.LockMechanism... https://github.com/KhaosT/HAP-NodeJS/blob/master/lib/gen/HomeKitTypes.js#L2131

Service.GarageDoorOpener... https://github.com/KhaosT/HAP-NodeJS/blob/master/lib/gen/HomeKitTypes.js#L1996

Note the required characteristics of the Garage Door Opener (obstruction and door position states, separate to lock states, which are optional). These aren't all provided by Domoticz so more likely I'll just set it up as a LockMechanism, which only needs current & target state.

jonferreira commented 8 years ago

yeah that's all that's "needed" I reckon

happy to test it once you have the time to look at it ;-)

PatchworkBoy commented 8 years ago

Added to the mqtt-integration branch (https://github.com/PatchworkBoy/homebridge-eDomoticz/tree/mqtt-integration)

If you need to switch to that branch from Master, see instructions on forum at https://www.domoticz.com/forum/viewtopic.php?f=36&t=10272&start=560#p93815

You can skip setting up MQTT etc and run it as the non-MQTT version by setting mqttdisable=0 in config.json

Feedback once you’ve tested it please!

jonferreira commented 8 years ago

Well I've followed instructions but did not seen any difference, still being shown as an On/Off Switch.

Maybe I missed something on the instructions to change from Master branch?

Will re-do and update in a bit

PatchworkBoy commented 8 years ago

Just head back into the folder and run sudo git pull origin again as I’ve just corrected a typo or two... then repeat the two rm - rf commands to empty accessories and persist, then restart homebridge.

(You can tell if you’re on the MQTT-integration branch by the presence of a lib/ folder within your homebridge-edomoticz folder - the non-MQTT version does NOT have this folder... also running head CHANGELOG should state v2.2 in top right above logo rather than v2.1)

jonferreira commented 8 years ago

well I'm getting a "fatal: Not a git repository (or any of the parent directories): .git" error even though I have the .git/config file as expected

PatchworkBoy commented 8 years ago

Sorry - I’ve also just changed the repo to lower case... just open the .git/config file and change eDomoticz to edomoticz in the git url (have just amended the forum post to reflect this). steps away from keyboard for an hour You should be safe now...!

jonferreira commented 8 years ago

OK now it did update as expected although something is wrong as my iPhone can no longer find a Homebridge accessory on the network even though the service appears to be running fine

PatchworkBoy commented 8 years ago

This happens every now and again with HomeKit / homebridge - did it to me the other night. Usually due to a cached sensor’s service type being changed, and home kit gets in a muddle between your devices and iCloud.

On your iOS device, head to Settings > homekit and delete your existing home. Back on your Domoticz device, empty ~/.homebridge/accessories & ~/.homebridge/persist folders (again), then restart homebridge.

It should then let you re-bind to the bridge.

If NOT, then you MAY need to change the username (mac address style) in your homebridge config.json also.

jonferreira commented 8 years ago

well it did helped, Homebridge now shows as an accessory to be "pair" with but for some reason it seems to hang when I click Add and instead of asking for the code it eventually times out and gets back to the first page. the homebridge then goes missing and I can no longer attempt to pair with it until I clear those folders... I'll change username and report back in a few minutes

jonferreira commented 8 years ago

OK it finally managed to pair successfully, hurray for that! On the other hand the doorlock is still showing as a Light Switch so clearly I've missed something here, unsure what as I could see the files being updated when I did the git pull origin and npm install

PatchworkBoy commented 8 years ago

bangs head on desk ...sorry! Found a line of code I must’ve got distracted on, as I never completed the line (line 837 of domoticz_accessory.js)...

One last sudo git pull origin and restart, and it should come online.

jonferreira commented 8 years ago

no worries :-)

I got this

pi@raspberrypi:/usr/local/lib/node_modules/homebridge-edomoticz $ sudo git pull origin
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 4 (delta 3), reused 4 (delta 3), pack-reused 0
Unpacking objects: 100% (4/4), done.
From https://github.com/PatchworkBoy/homebridge-edomoticz
   d8bd9a1..9740317  mqtt-integration -> origin/mqtt-integration
You asked to pull from the remote 'origin', but did not specify
a branch. Because this is not the default configured remote
for your current branch, you must specify a branch on the command line.```
PatchworkBoy commented 8 years ago

sudo git pull origin -b mqtt-integration (I think)...

failing that just head into homebridge-edomoticz/lib and sudo rm domoticz* then sudo wget https://raw.githubusercontent.com/PatchworkBoy/homebridge-edomoticz/mqtt-integration/lib/domoticz_accessory.js to just update that one file.

jonferreira commented 8 years ago

think the right command would be

pi@raspberrypi:/usr/local/lib/node_modules/homebridge-edomoticz $ sudo git pull origin mqtt-integration              From https://github.com/PatchworkBoy/homebridge-edomoticz
 * branch            mqtt-integration -> FETCH_HEAD
error: Untracked working tree file 'CHANGELOG' would be overwritten by merge.

still not working though so will attempt manual path

PatchworkBoy commented 8 years ago

Might be easier just to delete the entire homebridge-edomoticz folder, and from within node_modules/ run sudo git clone -b mqtt-integration https://github.com/PatchworkBoy/homebridge-edomoticz.git then head into it and run sudo npm install

PatchworkBoy commented 8 years ago

Being a clumsy dev tonight... still working on it, found more issues.

PatchworkBoy commented 8 years ago

Righty - finally... done. All tested and seems to be working correctly for me here on my virtual switch, so should theoretically work for you also seeing as yours is a virtual switch also.

Do another sudo git pull origin and restart homebridge, and the lock device should hopefully spring to life.

jonferreira commented 8 years ago

well I still haven't been able to pair with Homebridge again, think I need a refill :-(

jonferreira commented 8 years ago

OK I've managed to pair again but it's loosing connection after just a few seconds. Looking at error log I have the following which probably explains it:

Tue, 23 Aug 2016 08:41:32 GMT Accessory [Homebridge] Getting value for Characteristic "Wind Chill"
/usr/local/lib/node_modules/homebridge-edomoticz/lib/helper.js:55
   var stringval = value.toString();
                        ^

TypeError: Cannot read property 'toString' of undefined
    at Function.Helper.cleanFloat (/usr/local/lib/node_modules/homebridge-edomoticz/lib/helper.js:55:25)
    at Object.<anonymous> (/usr/local/lib/node_modules/homebridge-edomoticz/lib/domoticz_accessory.js:447:22)
    at Array.map (native)
    at Object.<anonymous> (/usr/local/lib/node_modules/homebridge-edomoticz/lib/domoticz_accessory.js:446:13)
    at Request.self.callback (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/request/request.js:198:22)
    at emitTwo (events.js:106:13)
    at Request.emit (events.js:191:7)
    at Request.<anonymous> (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/request/request.js:1082:10)
    at emitOne (events.js:101:20)
    at Request.emit (events.js:188:7)
jonferreira commented 8 years ago

on a side note I've changed

`var stringval = value.toString();``

to

var stringval = value ? value.toString() : "";

just so that I could test it and it's working great :-)

PatchworkBoy commented 8 years ago

Cool - have just dropped that change into helper.js... thumbsup

jonferreira commented 8 years ago

oh dear, another one:

Tue, 23 Aug 2016 14:20:15 GMT EventedHTTPServer [::ffff:192.168.1.113] Sending HTTP event 'keepalive' with data: {"characteristics":[]}
/usr/local/lib/node_modules/homebridge-edomoticz/lib/mqtt.js:40
        characteristic.setValue(value, null, "eDomoticz-MQTT");
                      ^

TypeError: Cannot read property 'setValue' of undefined
    at /usr/local/lib/node_modules/homebridge-edomoticz/lib/mqtt.js:40:23
    at Object.eDomoticzAccessory.handleMQTTMessage (/usr/local/lib/node_modules/homebridge-edomoticz/lib/domoticz_accessory.js:945:8)
    at MqttClient.<anonymous> (/usr/local/lib/node_modules/homebridge-edomoticz/lib/mqtt.js:39:17)
    at emitThree (events.js:116:13)
    at MqttClient.emit (events.js:194:7)
    at MqttClient._handlePublish (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/mqtt/lib/client.js:792:12)
    at MqttClient._handlePacket (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/mqtt/lib/client.js:282:12)
    at process (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/mqtt/lib/client.js:238:12)
    at Writable.writable._write (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/mqtt/lib/client.js:248:5)
    at doWrite (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/readable-stream/lib/_stream_writable.js:237:10)
    at writeOrBuffer (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/readable-stream/lib/_stream_writable.js:227:5)
    at Writable.write (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/readable-stream/lib/_stream_writable.js:194:11)
PatchworkBoy commented 8 years ago

Need more of the log really to work out what service / characteristic is failing... one of your devicetypes is returning 'null' for the characteristic instance.

jonferreira commented 8 years ago

that's the last 15 mins before the error:

Tue, 23 Aug 2016 13:46:44 GMT EventedHTTPServer [::ffff:192.168.1.113] New connection from client
Tue, 23 Aug 2016 13:46:44 GMT EventedHTTPServer [::ffff:192.168.1.113] HTTP server listening on port 36626
Tue, 23 Aug 2016 13:46:45 GMT EventedHTTPServer [::ffff:192.168.1.113] HTTP request: /pair-verify
Tue, 23 Aug 2016 13:46:45 GMT HAPServer [CC:21:3E:E4:DE:36] HAP Request: POST /pair-verify
Tue, 23 Aug 2016 13:46:45 GMT HAPServer [CC:21:3E:E4:DE:36] Pair verify step 1/2
Tue, 23 Aug 2016 13:46:45 GMT EventedHTTPServer [::ffff:192.168.1.113] HTTP Response is finished
Tue, 23 Aug 2016 13:46:45 GMT EventedHTTPServer [::ffff:192.168.1.113] HTTP request: /pair-verify
Tue, 23 Aug 2016 13:46:45 GMT HAPServer [CC:21:3E:E4:DE:36] HAP Request: POST /pair-verify
Tue, 23 Aug 2016 13:46:45 GMT HAPServer [CC:21:3E:E4:DE:36] Pair verify step 2/2
Tue, 23 Aug 2016 13:46:45 GMT HAPServer [CC:21:3E:E4:DE:36] Client 6FB62A48-246E-4A6C-9299-FF67067F576F verification complete
Tue, 23 Aug 2016 13:46:45 GMT EventedHTTPServer [::ffff:192.168.1.113] HTTP Response is finished
Tue, 23 Aug 2016 13:46:45 GMT EventedHTTPServer [::ffff:192.168.1.113] HTTP request: /accessories
Tue, 23 Aug 2016 13:46:45 GMT HAPServer [CC:21:3E:E4:DE:36] HAP Request: GET /accessories
Tue, 23 Aug 2016 13:46:45 GMT EventedHTTPServer [::ffff:192.168.1.113] HTTP Response is finished
Tue, 23 Aug 2016 13:46:47 GMT EventedHTTPServer [::ffff:192.168.1.113] HTTP request: /characteristics
Tue, 23 Aug 2016 13:46:47 GMT HAPServer [CC:21:3E:E4:DE:36] HAP Request: PUT /characteristics
Tue, 23 Aug 2016 13:46:47 GMT Accessory [Homebridge] Processing characteristic set: [{"aid":17,"iid":9,"ev":true},{"aid":17,"iid":10,"ev":true}]
Tue, 23 Aug 2016 13:46:47 GMT Accessory [Homebridge] Registering Characteristic "Lock Current State" for events
Tue, 23 Aug 2016 13:46:47 GMT Accessory [Homebridge] Registering Characteristic "Lock Target State" for events
Tue, 23 Aug 2016 13:46:47 GMT EventedHTTPServer [::ffff:192.168.1.113] HTTP Response is finished
Tue, 23 Aug 2016 13:50:15 GMT EventedHTTPServer [2001:8a0:fa4d:bc01:14c1:31a3:8694:5908] Sending HTTP event 'keepalive' with data: {"characteristics":[]}
Tue, 23 Aug 2016 13:50:15 GMT EventedHTTPServer [::ffff:192.168.1.113] Sending HTTP event 'keepalive' with data: {"characteristics":[]}
Tue, 23 Aug 2016 13:54:51 GMT EventedHTTPServer [::ffff:192.168.1.113] Client connection closed
Tue, 23 Aug 2016 13:54:51 GMT EventedHTTPServer [::ffff:192.168.1.113] HTTP connection was closed
Tue, 23 Aug 2016 13:54:51 GMT EventedHTTPServer [::ffff:192.168.1.113] HTTP server was closed
Tue, 23 Aug 2016 13:57:25 GMT EventedHTTPServer [::ffff:192.168.1.113] New connection from client
Tue, 23 Aug 2016 13:57:25 GMT EventedHTTPServer [::ffff:192.168.1.113] HTTP server listening on port 38675
Tue, 23 Aug 2016 13:57:25 GMT EventedHTTPServer [::ffff:192.168.1.113] HTTP request: /pair-verify
Tue, 23 Aug 2016 13:57:25 GMT HAPServer [CC:21:3E:E4:DE:36] HAP Request: POST /pair-verify
Tue, 23 Aug 2016 13:57:25 GMT HAPServer [CC:21:3E:E4:DE:36] Pair verify step 1/2
Tue, 23 Aug 2016 13:57:25 GMT EventedHTTPServer [::ffff:192.168.1.113] HTTP Response is finished
Tue, 23 Aug 2016 13:57:25 GMT EventedHTTPServer [::ffff:192.168.1.113] HTTP request: /pair-verify
Tue, 23 Aug 2016 13:57:25 GMT HAPServer [CC:21:3E:E4:DE:36] HAP Request: POST /pair-verify
Tue, 23 Aug 2016 13:57:25 GMT HAPServer [CC:21:3E:E4:DE:36] Pair verify step 2/2
Tue, 23 Aug 2016 13:57:25 GMT HAPServer [CC:21:3E:E4:DE:36] Client 6FB62A48-246E-4A6C-9299-FF67067F576F verification complete
Tue, 23 Aug 2016 13:57:25 GMT EventedHTTPServer [::ffff:192.168.1.113] HTTP Response is finished
Tue, 23 Aug 2016 13:57:25 GMT EventedHTTPServer [::ffff:192.168.1.113] HTTP request: /accessories
Tue, 23 Aug 2016 13:57:25 GMT HAPServer [CC:21:3E:E4:DE:36] HAP Request: GET /accessories
Tue, 23 Aug 2016 13:57:26 GMT EventedHTTPServer [::ffff:192.168.1.113] HTTP Response is finished
Tue, 23 Aug 2016 13:57:28 GMT EventedHTTPServer [::ffff:192.168.1.113] HTTP request: /characteristics
Tue, 23 Aug 2016 13:57:28 GMT HAPServer [CC:21:3E:E4:DE:36] HAP Request: PUT /characteristics
Tue, 23 Aug 2016 13:57:28 GMT Accessory [Homebridge] Processing characteristic set: [{"aid":17,"iid":9,"ev":true},{"aid":17,"iid":10,"ev":true}]
Tue, 23 Aug 2016 13:57:28 GMT Accessory [Homebridge] Registering Characteristic "Lock Current State" for events
Tue, 23 Aug 2016 13:57:28 GMT Accessory [Homebridge] Registering Characteristic "Lock Target State" for events
Tue, 23 Aug 2016 13:57:28 GMT EventedHTTPServer [::ffff:192.168.1.113] HTTP Response is finished
Tue, 23 Aug 2016 14:00:15 GMT EventedHTTPServer [2001:8a0:fa4d:bc01:14c1:31a3:8694:5908] Sending HTTP event 'keepalive' with data: {"characteristics":[]}
Tue, 23 Aug 2016 14:00:15 GMT EventedHTTPServer [::ffff:192.168.1.113] Sending HTTP event 'keepalive' with data: {"characteristics":[]}
Tue, 23 Aug 2016 14:10:15 GMT EventedHTTPServer [2001:8a0:fa4d:bc01:14c1:31a3:8694:5908] Sending HTTP event 'keepalive' with data: {"characteristics":[]}
Tue, 23 Aug 2016 14:10:15 GMT EventedHTTPServer [::ffff:192.168.1.113] Sending HTTP event 'keepalive' with data: {"characteristics":[]}
Tue, 23 Aug 2016 14:20:15 GMT EventedHTTPServer [2001:8a0:fa4d:bc01:14c1:31a3:8694:5908] Sending HTTP event 'keepalive' with data: {"characteristics":[]}
Tue, 23 Aug 2016 14:20:15 GMT EventedHTTPServer [::ffff:192.168.1.113] Sending HTTP event 'keepalive' with data: {"characteristics":[]}
/usr/local/lib/node_modules/homebridge-edomoticz/lib/mqtt.js:40
        characteristic.setValue(value, null, "eDomoticz-MQTT");
                      ^

TypeError: Cannot read property 'setValue' of undefined
    at /usr/local/lib/node_modules/homebridge-edomoticz/lib/mqtt.js:40:23
    at Object.eDomoticzAccessory.handleMQTTMessage (/usr/local/lib/node_modules/homebridge-edomoticz/lib/domoticz_accessory.js:945:8)
    at MqttClient.<anonymous> (/usr/local/lib/node_modules/homebridge-edomoticz/lib/mqtt.js:39:17)
    at emitThree (events.js:116:13)
    at MqttClient.emit (events.js:194:7)
    at MqttClient._handlePublish (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/mqtt/lib/client.js:792:12)
    at MqttClient._handlePacket (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/mqtt/lib/client.js:282:12)
    at process (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/mqtt/lib/client.js:238:12)
    at Writable.writable._write (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/mqtt/lib/client.js:248:5)
    at doWrite (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/readable-stream/lib/_stream_writable.js:237:10)
    at writeOrBuffer (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/readable-stream/lib/_stream_writable.js:227:5)
    at Writable.write (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/readable-stream/lib/_stream_writable.js:194:11)
PatchworkBoy commented 8 years ago

On close inspection, you've got an incoming mqtt message for a sensor that it can't map to a characteristic - Need the MQTT message that triggers it.

If you run homeBridge directly from CLI with DEBUG=* homebridge -D it should show the FULL logs with all incoming messages.

jonferreira commented 8 years ago

hmm so I reckon I'll need to change my autostart service to run in debug mode and just wait till it happens, there's dozens of MQTT messages every minute

PatchworkBoy commented 8 years ago

Yep - I have similar problems - 60+ sensors...

jonferreira commented 8 years ago

OK changed to

dir="/home/pi"
cmd="DEBUG=* /usr/local/bin/homebridge -D"
user="pi"

let's see what happens

jonferreira commented 8 years ago

Again but same amount of information...this won't be easy to diagnose I reckon

Tue, 23 Aug 2016 17:14:36 GMT EventedHTTPServer [::ffff:192.168.1.119] Sending HTTP event 'keepalive' with data: {"characteristics":[]}
Tue, 23 Aug 2016 17:14:36 GMT EventedHTTPServer [::ffff:192.168.1.113] Sending HTTP event 'keepalive' with data: {"characteristics":[]}
/usr/local/lib/node_modules/homebridge-edomoticz/lib/mqtt.js:40
        characteristic.setValue(value, null, "eDomoticz-MQTT");
                      ^

TypeError: Cannot read property 'setValue' of undefined
    at /usr/local/lib/node_modules/homebridge-edomoticz/lib/mqtt.js:40:23
    at Object.eDomoticzAccessory.handleMQTTMessage (/usr/local/lib/node_modules/homebridge-edomoticz/lib/domoticz_accessory.js:945:8)
    at MqttClient.<anonymous> (/usr/local/lib/node_modules/homebridge-edomoticz/lib/mqtt.js:39:17)
    at emitThree (events.js:116:13)
    at MqttClient.emit (events.js:194:7)
    at MqttClient._handlePublish (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/mqtt/lib/client.js:792:12)
    at MqttClient._handlePacket (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/mqtt/lib/client.js:282:12)
    at process (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/mqtt/lib/client.js:238:12)
    at Writable.writable._write (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/mqtt/lib/client.js:248:5)
    at doWrite (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/readable-stream/lib/_stream_writable.js:237:10)
    at writeOrBuffer (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/readable-stream/lib/_stream_writable.js:227:5)
    at Writable.write (/usr/local/lib/node_modules/homebridge-edomoticz/node_modules/readable-stream/lib/_stream_writable.js:194:11)
jonferreira commented 8 years ago

any chance you can make it log the MQTT message before being processed, therefore making a record of what message is causing this?

PatchworkBoy commented 8 years ago

copy line 45 of mqtt.js and paste at line 38... platform.log(message);

PatchworkBoy commented 8 years ago

(Personally I use node-red with an mqtt input to json parser to debug node flow for debugging mqtt issues - handy to have)

jonferreira commented 8 years ago

yeah I'm using node-red for all logic etc but did not noticed anything out of the ordinary...

I did the necessary changes, now to wait till it happens again

PatchworkBoy commented 8 years ago

Set Node-Red to write the output to a text file, injecting timestamp... then you can cross reference by time.

jonferreira commented 8 years ago

OK this is driving me nuts. First of all I have the feeling this has nothing to do with my MQTT broker cuz A) there's no mqtt message at the same exact time as the error is thrown and B) cuz it happens exactly in 10 mins interval at the same exact time the keepalive event

Wed, 24 Aug 2016 09:14:31 GMT EventedHTTPServer [::ffff:192.168.1.113] Sending HTTP event 'keepalive' with data: {"characteristics":[]}

looking at the error is the characteristic that is undefined and indeed keepalive event has no characteristics

/usr/local/lib/node_modules/homebridge-edomoticz/lib/mqtt.js:41
        characteristic.setValue(value, null, "eDomoticz-MQTT");
                      ^

TypeError: Cannot read property 'setValue' of undefined

what's bugging me is that I've edited the mqtt.js file to log both message and characteristics but I have nothing!

Mqtt.prototype.connect = function(url) {
  client = mqtt.connect(url);

  client.on('connect', function() {
    client.subscribe('domoticz/out');
  });

  client.on('message', function (topic, buffer) {
    var message = JSON.parse(buffer.toString());
    if ('undefined' !== typeof message.nvalue || 'undefined' !== typeof message.svalue1) {
      var accessory = accessories["idx_" + message.idx];
      if (!accessory) {
        return;
      }
        platform.log(accessory);
      platform.log(message);
      accessory.handleMQTTMessage(message, function(characteristic, value) {
        characteristic.setValue(value, null, "eDomoticz-MQTT");
      });

    } else {
                  platform.log('[ERR] MQTT message received, but no nvalue or svalue1 was found:');
                  platform.log(message);
          }
  });
}

Need a fresh pair of eyes!

rswilem commented 8 years ago

Wow, weird! Looks like characteristic is undefined in the callback. This means handleMQTTMessage returns an empty characteristic. But if your code gets to handleMQTTMessage it should also log the incoming message and accessory!

Try console.log instead of platform.log for a change. platform.log has a check built in. You have probably not enabled debug mode the right way. Doesn't matter. 👍🏼

jonferreira commented 8 years ago

yeah bizarre really...

my init

dir="/home/pi"
cmd="DEBUG=* /usr/local/bin/homebridge -D"
user="pi"

pretty sure that's correct

either way I've added the following to MQTT.js

Mqtt.prototype.connect = function(url) {
  client = mqtt.connect(url);

  client.on('connect', function() {
    client.subscribe('domoticz/out');
  });

  client.on('message', function (topic, buffer) {
    var message = JSON.parse(buffer.toString());
    if ('undefined' !== typeof message.nvalue || 'undefined' !== typeof message.svalue1) {
      var accessory = accessories["idx_" + message.idx];
      if (!accessory) {
        return;
      }
      //platform.log(accessory);
      //platform.log(message);
      accessory.handleMQTTMessage(message, function(characteristic, value) {
        if('undefined' !== typeof accessory) {
            characteristic.setValue(value, null, "eDomoticz-MQTT");
        } else {
            platform.log('******************************');
            platform.log('[ERR] undefined characteristic');
            platform.log(accessory);
            platform.log(message);
            platform.log(value);
            platform.log('******************************');
        }
      });

    } else {
                  platform.log('[ERR] MQTT message received, but no nvalue or svalue1 was found:');
                  platform.log(message);
          }
  });
}

this should prevent it for crashing and provide further info

rswilem commented 8 years ago

Alright, still weird. I have just committed a few changes I have made, one fixes an incorrect state for the doorlock, the other has your sanity check built in.

Debug looks valid yeah.

jonferreira commented 8 years ago

haven't got an error in the past 2 hours so clearly the sanity check is doing its thing but bizarre that I don't have any logs! so as it stands it's "fixed" but no idea what was causing it

rswilem commented 8 years ago

Yeah, weird! I normally run DEBUG=* homebridge -D from the console, and not start it through the init.d/ script. But don't know if that's the cause!

I don't think the sanity check is a bad idea either. I mean, it tries to update an undefined characteristic with an undefined value. No harm in dropping those messages. The other characteristics still get updated perfectly.

I have committed everything and should be up on MQTT branch now 👍🏼

jonferreira commented 8 years ago

thanks

PatchworkBoy commented 8 years ago

Interesting... Re: the not-here post (looking at email notifications) it was caused by a Temp & Humidity sensor, which should only have 2 characteristics, yet the MQTT msg carried 3x sValues. What does the third value show as / represent in Domoticz??

jonferreira commented 8 years ago

yeah I removed it as it end up not being related to the original problem

I have no idea what svalue3 holds though, seems to be stuck at "3" for as long as I remember and domoticz doesn't show it anywhere else (Data variable or any other as far as I can see)

PatchworkBoy commented 8 years ago

Hmmm. Duly noted! Ta!

jonferreira commented 8 years ago

OK I think I've finally tracked down what was causing the error!

******************************
[8/25/2016, 11:04:17 PM] [eDomoticz] [ERR] undefined characteristic
[8/25/2016, 11:04:17 PM] [eDomoticz] { haveDimmer: false,
  services:
   [ Service {
       displayName: undefined,
       UUID: '0000003E-0000-1000-8000-0026BB765291',
       subtype: undefined,
       iid: null,
       characteristics: [Object],
       optionalCharacteristics: [Object] },
     Service {
       displayName: 'JF Location',
       UUID: '259b474a-f1b2-4741-a64e-d8e53ae6b4dc',
       subtype: undefined,
       iid: 7,
       characteristics: [Object],
       optionalCharacteristics: [],
       _events: [Object],
       _eventsCount: 2 } ],
  log: [Function],
  server: '192.168.1.2',
  port: '8080',
  IsScene: false,
  status: 1,
  idx: '258',
  name: 'JF Location',
  eve: undefined,
  subType: 'Text',
  swType: undefined,
  swTypeVal: false,
  isSwitch: false,
  Type: 'General',
  batteryRef: 255,
  CounterToday: 1,
  onValue: 'On',
  offValue: 'Off',
  param: 'switchlight',
  access_url: 'http://192.168.1.2:8080/json.htm?',
  control_url: 'http://192.168.1.2:8080/json.htm?type=command&param=switchlight&idx=258',
  status_url: 'http://192.168.1.2:8080/json.htm?type=devices&rid=258',
  requestHeaders: {} }
[8/25/2016, 11:04:17 PM] [eDomoticz] { Battery: 255,
  RSSI: 12,
  dtype: 'General',
  id: '00082257',
  idx: 258,
  name: 'JF Location',
  nvalue: 0,
  stype: 'Text',
  svalue1: '<iframe width="365" height="300" frameborder="0" style="border:0" src="https://www.google.com/maps/embed/v1/place?key=AIzaSyDJEFKMCqoOUnG4vf1KzI6W0ZNWFV1D0rk&maptype=satellite&q=xxx" allowfullscreen></iframe>',
  unit: 1 }
[8/25/2016, 11:04:17 PM] [eDomoticz] <iframe width="365" height="300" frameborder="0" style="border:0" src="https://www.google.com/maps/embed/v1/place?key=AIzaSyDJEFKMCqoOUnG4vf1KzI6W0ZNWFV1D0rk&maptype=satellite&q=xxx" allowfullscreen></iframe>
[8/25/2016, 11:04:17 PM] [eDomoticz] ******************************

It's a dummy device that I use to start text, in this case a google maps with GPS coordenates