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.
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
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.
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?