How to get OpenHAB to send and receive Node.js requests

Over the past week I’ve been setting up OpenHAB on my Raspberry Pi which connects to my custom Arduiro sensors which run MySensors.org.

So far it’s all been going great, especially because of all the things I’ve read on this forum.

Now I’ve got a new issue.

I’ve got some smart lights, Connected by TCP, and I’ve got the fabulous Node.js based api library for controlling them https://github.com/stockmopar/connectedbytcp

I’ve got it all installed and I’ve figured out how to control it and it’s great so far.

The problem I have now is that I have no way of knowing where to start on how to get OpenHAB to call the Node.js.

I don’t know if this may help, but I do have an MQTT, mosquitto service running for passing messages between OpenHAB and my Arduinos

There are several approaches but they all depend on the Node.js side, not the OH side. You need to either set up Node.js to publish/subscribe the data (e.g. Send/receive MQTT messages) or expose an API that OH can call.

Since Node.js was created to serve up web pages it seems pretty natural to me to create a REST API on the Node.js side that OH will call using the HTTP Binding.

Thanks for your tips, they got me going in the right direction.

Since then I’ve accomplished a lot. I forked stockmopar’s api which controls my “Connected By TCP lights”, here is a link to my fork https://github.com/SingleDadLiving/connectedbytcp

I added code that I could call from my OpenHAB, for example node RoomControl.js -a on -r 1 will turn on all the lights in my living room

I then figured out how to call it from openhab
rule cTCP_ROOMS_1 when Item SwitchRoom_1 received command then val String results = "" if (SwitchRoom_1.state == ON) { results = executeCommandLine("node ../conf/scripts/node/RoomControl.js -a off -r 1 ", 5000) }else { results = executeCommandLine("node ../conf/scripts/node/RoomControl.js -a on -r 1 ", 5000) } logInfo("SwitchRoom_1", results) end

I did run into some permission and location of files issues, but using this forum I was able to figure it out :slight_smile:

Now I’ve got one more challenge and I was hoping someone could point me in the right direction again.

I can’t rely on persistence alone to know the state of my lights because they could have been switched manually. So I need a way to query the state of the light and then update the OpenHAB gui.

So far I’ve written the node code required to communicate with the light/room and get the state. node RoomControl.js -a state -r 1 the preceeding code works and returns ON or OFF depending on the state.

So how do I use items, rules and scripts to check the state? is there an onload function or something similar?

Use the System started trigger for a rule and you can have OH query your Node.js for the current state there. When you update your Items use MyItem.postUpdate(newState) which will just change its state and not command it.

thanks for the help. I’ve now got everything working perfectly :slight_smile:

Hi Micah, I also have to connected Lights and i would really appreciate it if you could share in detail how you got your lights to work with openhab

hey micah,

i am not shure if i understood your topic right but maybe you can help and explain: using the OH rule you trigger a node script to run ? and from the other side, so trigger a OH rule from node side, you use mqtt/api ? or do you know a simply way to trigger a OH rule from a node script?

cheers, tobi