My node-red + HomeKit + OpenHAB setup

Thanks Garrett for the quick reply,
I will have a look now at the link… For the moment all I want the TV item to do is switch the OpenHab TV scene on and off.
Thanks again for your help.

Shouldn’t be too much trouble to pull out the command(s) to turn it on and off. I don’t remember if it’s a single message or multiple sending to homekit.

Let me know if you can’t get it. The tv item is on my to do list for my next examples…

This is what I have so far but I’m getting this error code.

msg : string[1128]

“response error '{“statusCode”:400,“body”:”\n\n<meta http-equiv=“Content-Type” content=“text/html;charset=utf-8”/>\nError 400 Bad Request\n\n

HTTP ERROR 400

\n

Problem accessing /rest/items/Living_Room_TV. Reason:\n

 Bad Request

<a href=“http://eclipse.org/jetty”>Powered by Jetty:// 9.4.12.v20180830
\n\n\n\n",“headers”:{“connection”:“close”,“cache-control”:“must-revalidate,no-cache,no-store”,“content-type”:“text/html;charset=iso-8859-1”,“content-length”:“346”,“server”:“Jetty(9.4.12.v20180830)”},“request”:{“uri”:{“protocol”:“http:”,“slashes”:true,“auth”:“openhabian:openhabian”,“host”:“192.168.86.27:8080”,“port”:“8080”,“hostname”:“192.168.86.27”,“hash”:null,“search”:null,“query”:null,“pathname”:"/rest/items/Living_Room_TV",“path”:"/rest/items/Living_Room_TV",“href”:“http://openhabian:openhabian@192.168.86.27:8080/rest/items/Living_Room_TV"},“method”:“POST”,“headers”:{“authorization”:"Basic b3B…”
[{"id":"6c070a7.71ca174","type":"debug","z":"2534cf2f.5cfa28","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1050,"y":160,"wires":[]},{"id":"625578c4.99863","type":"homekit-service","z":"2534cf2f.5cfa28","isParent":true,"bridge":"90af8346.e9d348","parentService":"","name":"TTV","serviceName":"Television","topic":"","filter":false,"manufacturer":"Default Manufacturer","model":"Default Model","serialNo":"Default Serial Number","characteristicProperties":"","x":470,"y":160,"wires":[["b6958fbe.3d93d"]]},{"id":"71d57b31.18c80c","type":"function","z":"2534cf2f.5cfa28","name":"","func":"if (msg.payload===false){\n    msg.payload={\"Active\":0}\n//        nrSource:true\n} else {\n    msg.payload={\"Active\":1}\n//        nrSource:true\n}\nreturn msg","outputs":1,"noerr":0,"x":310,"y":160,"wires":[["625578c4.99863"]]},{"id":"b6958fbe.3d93d","type":"function","z":"2534cf2f.5cfa28","name":"","func":"switch (msg.payload.Active){\n    case 0:\n        msg1 = {payload:\"Off\"};\n        break;\n    case 1:\n        msg1 = {payload:\"On\"};\n        break;\n    default:\n        msg1 = {};\n        return;\n}\nreturn msg1;","outputs":1,"noerr":0,"x":630,"y":160,"wires":[["8690b13c.d8f6"]]},{"id":"8690b13c.d8f6","type":"openhab2-out","z":"2534cf2f.5cfa28","name":"TV","controller":"9650ccbc.234be","itemname":"Living_Room_TV","topic":"ItemCommand","payload":"","x":830,"y":160,"wires":[["6c070a7.71ca174"]]},{"id":"bed8ea6e.5a22d","type":"openhab2-in","z":"2534cf2f.5cfa28","name":"TV","controller":"9650ccbc.234be","itemname":"Living_Room_TV","x":110,"y":160,"wires":[["71d57b31.18c80c"],[]]},{"id":"90af8346.e9d348","type":"homekit-bridge","z":"","bridgeName":"Pi3B+","pinCode":"031-45-138","port":"","allowInsecureRequest":true,"manufacturer":"Rasberry Pi3 B+","model":"Pi3 B+","serialNo":"Default Serial Number","customMdnsConfig":false,"mdnsMulticast":true,"mdnsInterface":"","mdnsPort":"","mdnsIp":"","mdnsTtl":"","mdnsLoopback":true,"mdnsReuseAddr":true},{"id":"9650ccbc.234be","type":"openhab2-controller","z":"","name":"OpenHab","protocol":"http","host":"192.168.86.27","port":"8080","path":"","username":"openhabian","password":"openhabian"}]

That looks like an issue with your openhab node - looking at your functions it looks like the old one sent "ON" or "OFF" but your new one is sending "On" and "Off".

That error is with the REST api connection to OpenHab.

My recommendation is put debug nodes all over, watch what comes in/out of them, and make sure the functions you make are all exactly like the messages that need to be sent.

Hi Garrett,
I now have it working.
Im now going to have a play with the inputs to see what els I can do.
Thank you for all your help

[{"id":"625578c4.99863","type":"homekit-service","z":"2534cf2f.5cfa28","isParent":true,"bridge":"90af8346.e9d348","parentService":"","name":"TV","serviceName":"Television","topic":"","filter":false,"manufacturer":"Default Manufacturer","model":"Default Model","serialNo":"Default Serial Number","characteristicProperties":"","x":470,"y":140,"wires":[["b6958fbe.3d93d"]]},{"id":"71d57b31.18c80c","type":"function","z":"2534cf2f.5cfa28","name":"OpenHab - HomeKit","func":"if (msg.payload == \"ON\") \n{msg.payload={\"Active\":1};\n    \n} \nelse {\n    msg.payload = {\"Active\":0}\n    \n}\nreturn msg;","outputs":1,"noerr":0,"x":280,"y":140,"wires":[["625578c4.99863"]]},{"id":"b6958fbe.3d93d","type":"function","z":"2534cf2f.5cfa28","name":"HomeKit - OpenHab","func":"switch (msg.payload.Active){\n    case 0:\n        msg1 = {payload:\"OFF\"};\n        break;\n    case 1:\n        msg1 = {payload:\"ON\"};\n        break;\n    default:\n        msg1 = {};\n        return;\n}\nreturn msg1;","outputs":1,"noerr":0,"x":660,"y":140,"wires":[["a37f70e9.10f318"]]},{"id":"bed8ea6e.5a22d","type":"openhab2-in","z":"2534cf2f.5cfa28","name":"TV","controller":"9650ccbc.234be","itemname":"Living_Room_TV","x":90,"y":140,"wires":[["71d57b31.18c80c"],[]]},{"id":"a37f70e9.10f318","type":"openhab2-out","z":"2534cf2f.5cfa28","name":"TV","controller":"9650ccbc.234be","itemname":"Living_Room_TV","topic":"ItemCommand","payload":"","x":850,"y":140,"wires":[[]]},{"id":"90af8346.e9d348","type":"homekit-bridge","z":"","bridgeName":"Pi3B+","pinCode":"031-45-138","port":"","allowInsecureRequest":true,"manufacturer":"Rasberry Pi3 B+","model":"Pi3 B+","serialNo":"Default Serial Number","customMdnsConfig":false,"mdnsMulticast":true,"mdnsInterface":"","mdnsPort":"","mdnsIp":"","mdnsTtl":"","mdnsLoopback":true,"mdnsReuseAddr":true},{"id":"9650ccbc.234be","type":"openhab2-controller","z":"","name":"OpenHab","protocol":"http","host":"192.168.86.27","port":"8080","path":"","username":"openhabian","password":"openhabian"}]

Alas I spoke too soon, the lifeline association group had helped, but, we were still experiencing the occasional missed HomeKit automation event (hydroponics lighting not switching on in the morning or off at night) hence I shall now try Olivier’s other suggestion.

Edit: Argh! So as soon as I went to clear the lifeline association group and try the rules route instead I am left wondering why OpenHAB2 does not show the controller listed for the device. I wonder if I am experiencing Z-wave things lose their Lifeline Association Group or similar.

Because there is a bug in Paper UI that means it doesn’t show the associations properly. If you are using OH2.5, it should not be possible to remove the lifeline as the binding will prevent this if the controller is set to be the master.

If you have problems with this, then I’d suggest to open another discussion and provide debug logs showing the issue as this thread is not really the right place to discuss this issue I think :slight_smile:

1 Like

Did you find any reference for this? I will try your example to turn tv on and off. Would be great if additonal inputs could be selected

Check the issues on the node red plugin GitHub page. I’ve done a quick demo and know that tv on/off as well as input selection (linked services) is working fine. The big unknown right now is volume control…

I’d search for “television”

Thanks, however I couldn’t find the demo code of yours…

Ps.: Television is working flawlessly, however I have added an InputSource linked to the Television but I can’t see it there.

I personally haven’t done any examples.

Check this issue, there are some pasted flows and some text files with flows

Thanks this is the one I have found. See my edited earlier post…

I’m trying to enable / disable HomeKit scenes from OpenHAB, but I couldn’t yet figure out how to do so. Here’s already a partially functional solution that sets a switch if a scene is enabled or disabled, so OH gets to know that the scene is active or not:

What I can’t yet get to work, is how to enable or disable a scene in HomeKit through setting a switch in OH… I tried using a stateless switch but it invariably sends “0” to the HomeKit bridge node in Node-RED.

My take on this is to use a stateless programmable switch. I put up the wiki page about it almost 2 weeks ago.

You get to choose within the switch (button) which scene is triggered by that button press. You would be able to send {"ProgrammableSwitchEvent":0/1/2} into the stateless switch node to trigger that switch button.

I like to do this in OpenHAB with a “dummy switch” that has expire binding set to like 5 seconds. Then make a rule (OH or node red) that changes ON to a number 0/1/2 and send it into your homekit node.

I’m happy to answer any questions, check out that wiki page I linked above and let me know what you think.

That was the Wiki page I stumbled upon when searching for a solution. Sadly I didn’t manage to get it work on my end. I’m using a standard “proxy” On/Off Switch item in OH which I hoped to be able to propagate to HomeKit to enable and disable a given scene.

Meanwhile I’ve seen that the Home app does indeed allow to enable a scene, but not to disable it like a tap on an enabled scene in the Home app does. So I apparently can’t use the “off” state of the switch in OH to deactivate a scene in HomeKit.

I was getting an example ready for you then I stumbled upon your issue as mentioned above - homekit won’t run an automation to “disable a scene”. Best I can tell this is a homekit limitation and scenes can’t be “turned off” from anything but pressing on the scene in the home app… It makes sense though, what would a garage door or thermostat do when it was “turned off”? If the scene is to set the thermostat to 70º, what does turning the scene off set it to?

A (cumbersome) workaround would be to make “on scenes” and “off scenes” in HomeKit. It’ll get messy pretty quickly but it would work.

I’m assuming that you have various “official homekit” devices you are trying to control from OH? Otherwise you could keep the scenes within OH / node red…

Since I was already mostly through the example, I’ll go ahead and share it here:

Note: change the inject nodes for your OH item

40%20AM

This flow gives you 2 “buttons”. I’ve set them to be “arrive home” and “leave home”. As stated above you’ve got the issue where homekit won’t automate scenes turning off.

Now you can link these buttons to set any scene in homekit, I’ve chosen “ON” to be “arrive home” and “OFF” to be “leave home”:

45%20AM

And the copy-paste for you to see my code:

[{"id":"3fdc6bf0.b7263c","type":"homekit-service","z":"c3a5a440.d4673","isParent":true,"bridge":"fc1c5b0d.cd50f8","parentService":"","name":"shutterfreak","serviceName":"StatelessProgrammableSwitch","topic":"","filter":false,"manufacturer":"Default Manufacturer","model":"Default Model","serialNo":"Default Serial Number","characteristicProperties":"{}","x":790,"y":360,"wires":[[]]},{"id":"e0102731.e16d5","type":"inject","z":"c3a5a440.d4673","name":"ON from OpenHAB","topic":"","payload":"ON","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":330,"y":340,"wires":[["3350e929.8cfb7e"]]},{"id":"52408ac8.b705bc","type":"inject","z":"c3a5a440.d4673","name":"OFF from OpenHAB","topic":"","payload":"OFF","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":330,"y":380,"wires":[["3350e929.8cfb7e"]]},{"id":"3350e929.8cfb7e","type":"change","z":"c3a5a440.d4673","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"ON","fromt":"str","to":"{\"ProgrammableSwitchEvent\":0}","tot":"json"},{"t":"change","p":"payload","pt":"msg","from":"OFF","fromt":"str","to":"{\"ProgrammableSwitchEvent\":1}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":360,"wires":[["3fdc6bf0.b7263c"]]},{"id":"fc1c5b0d.cd50f8","type":"homekit-bridge","z":"","bridgeName":"Irrigation","pinCode":"123-45-321","port":"","allowInsecureRequest":false,"manufacturer":"Garage","model":"Pi HAT","serialNo":"3Aplus","customMdnsConfig":false,"mdnsMulticast":true,"mdnsInterface":"","mdnsPort":"","mdnsIp":"","mdnsTtl":"","mdnsLoopback":true,"mdnsReuseAddr":true}]
1 Like

I forgot to mention another issue you may run into with the proxy switch - for a normal switch (not stateless), homekit will ignore repeated commands since it remembers the state.

So you can’t send “on” twice in a row and expect the homekit automation to run for the second “on” command - it will be ignored because the light is already on.

The normal switches work like the OH item changed to on rules…

Stateless switches DO respond to repeated commands.

1 Like

@crxporter or others watching this thread, I have question/problem: it seems that HomeKit gets out of sync with the state of things, which then causes issues when I try and use Siri to update an item. For example, HK will think a switch is On, and it I issue a command “turn switch on”, nothing will happen. I believe this is because HK thinks it doesn’t need to take any action. If I were to say “turn switch off”, followed by “turn switch on”, it works, because the “off” command will cause HK to update on its end. If I recall from when I used the HK binding, HK does poll items, but I’m guessing that Node Red is caching the state (or maybe not?). Has anyone seen this problem before?

HomeKit doesn’t really poll anything. The important thing for this to be working properly is to have an openhab node which will receive the item updates then connect that to your homekit node (you’ll need a couple functions in between)

Generally the openhab node should only need to pass state changes into homekit but if it’s anpolling item, that shouldn’t cause problems.

If this doesn’t make sense- share a screenshot of your node red flow and I’ll try pointing you in the right direction!

I’m using basically the setup/functions that you posted at the start of this thread. They seem to generally work fine, in that I can control w/HK → OH w/o issue. The only issue is when HK seems to not have the proper status (sometimes after a restart, or perhaps they get a wrong status somehow?)

Also, if HK doesn’t poll, how does it get an initial status, or what happens after we restart Node RED? Does NR just push all of the statuses of the Items?

Another question around all of this (and maybe part of the problem) is how Dimmers work. I.e. Dimmers originally send their target status (i.e. if it’s turning off, it sends 0), but then will send updated status as they transition, so it looks something start: 100, then 0, then 50, then 0 (depending on timing). Some of this ends up looking a bit odd in HK, as if you turn off a Dimmer, it go from Off, to On (partially) and then eventually off.

Finally, re: polling. When I was using the OH/Homekit binding, I noticed that when you load the HK app on your phone, it would query OH for that statuses of the items. Is that not the case?