Openhab and node-red

ahhh…now it looks loke this:

can go further…

ok…i´m on the way to get it to work ( i think so ).
Status:

  • added node-red homekit-bridge in homekit
  • the windowCovering is added via node-red homekit-bridge
    but…
    the item has the state “opening…” and the “wheel is turning”
    IMG_F2E4CB252FB2-2

any ideas so far?

UPDATE:
it worked know. Problem was to set the openhab2-out node “Topic” to “ItemCommand”.
But the “opening” and “Wheelturning” - Problem still exists.

Try using the blinds example of the tutorial, see if you have problems.
image

1 Like

It also links the two debug checks, so you see the messages going to and from homekit

Check the two functions if you need to change something.
image

1 Like

This is what I use to adapt the opening and closing of the garage door.
I do not have a sensor that tells me if it’s open or closed. I only have a relay that opens and only itself closes.
I modified the function to make it work with HomeKit.
Look at the Homekit specifications on the web, find the command strings.

[{"id":"3cc8ec66.cd7894","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"a3a81a5d.ce7a68","type":"openhab2-out","z":"3cc8ec66.cd7894","name":"Cancelletto","controller":"93cadc2.e96f72","itemname":"Pul_canceletto","topic":"ItemCommand","payload":"","x":930,"y":68,"wires":[[]],"inputLabels":["msg.payload"]},{"id":"416db6c8.65e038","type":"function","z":"3cc8ec66.cd7894","name":"Openhab to HomeKit","func":"//\n\nif (msg.payload == \"ON\") { \n    // Apertura\n    msg.payload = {\n        \"PositionState\": 0,\n        \"CurrentPosition\":100, \n        \"TargetPosition\" :100\n    };\n} else {\n    // Chiusura\n    msg.payload = {\n        \"PositionState\": 1,\n        \"CurrentPosition\":0, \n        \"TargetPosition\" :0\n    };\n}\nreturn msg;\n","outputs":1,"noerr":0,"x":299,"y":67,"wires":[["82705cd3.1482"]]},{"id":"aa56e691.7c2008","type":"openhab2-in","z":"3cc8ec66.cd7894","name":"Cancelletto","controller":"93cadc2.e96f72","itemname":"Pul_canceletto","x":99,"y":67,"wires":[["416db6c8.65e038"],[]],"outputLabels":["msg.payload","msg.payload"]},{"id":"82705cd3.1482","type":"homekit-service","z":"3cc8ec66.cd7894","bridge":"555238e4.b364d8","name":"Cancelletto","serviceName":"Door","manufacturer":"Hayley","model":"Outlet","serialNo":"1","characteristicProperties":"{}","x":509,"y":67,"wires":[["f76c7bbe.9e3f18"]]},{"id":"f76c7bbe.9e3f18","type":"function","z":"3cc8ec66.cd7894","name":"HomeKit to OpenHAB","func":"if (msg.hap.context !== undefined )\n{\nif(msg.payload.TargetPosition === 0){\n    msg.payload = \"OFF\";\n}\nelse {\n    if(msg.payload.TargetPosition === 100){\n    msg.payload = \"ON\";\n}\n}\nreturn msg;\n}\n","outputs":1,"noerr":0,"x":713,"y":68,"wires":[["a3a81a5d.ce7a68"]]},{"id":"93cadc2.e96f72","type":"openhab2-controller","z":"","name":"Openhab","protocol":"http","host":"localhost","port":"8080","path":"","username":"","password":""},{"id":"555238e4.b364d8","type":"homekit-bridge","z":"","bridgeName":"Casa","pinCode":"550-00-820","port":"","manufacturer":"Gozilla","model":"Pi3","serialNo":"Rev.1"}]

image

1 Like

ok - now it works. Thanks a lot for bringing me into this!

The problem was, that my openhab2 - item was a “rollershutter” and this one has complete other object-types than used within the functions ( or as dimmers ). And the “delay”-node was missing.
so nothing happens at the first time.

@crxporter if i want to forgo the delay-node, i have to adapt both functions, right?

i think the magic happens within the functions. so i have to learn much more about that.

I’m glad it works.
You’ll see that you feel comfortable

Test with the voice commands to check if the commands are not inverted.
The delay I do not remember exactly has what it takes, if you see mine is set to 5 seconds while the original was 20 seconds.
Change the value and see what changes.
Have fun

Edit:
in my Openweb protocol it also questions the position in%
Voice messages also work with the command in%.
I do not know yours

1 Like

Actually- you should have no problem just deleting the delay node and running a wire (node red connection line) in its place. You’ll immediately see on your phone open/closed stated instead of “opening…” for the short time.

That’s all the delay does. It’s 12 seconds for me of showing “opening…” or “closing…” in homekit. Completely unnecessary but a neat little feature that was easy to add.

1 Like

Ok…:+1:

Now i added all my 19 rollershutters and it seems to work well.
Spezial thanks to @crxporter and @gozilla01 for their immidiate support.

Please let that post open for concerned questions :wink:

:+1:

@crxporter Hi Garrett,
what is this part in your function for:
var delay = {payload:0};
Could you please explain the need of this variable to me?

That line creates a variable in my nodered functions that will be sent to the delay timer. I use it in both of my curtain functions.

The first is:

var input = msg.payload;
var delay = {payload:0};
if(input < 101){
    msg.payload = {
        "TargetPosition": input
    };
    delay.payload = {
        "CurrentPosition": input
    };
    return [msg,delay];
}

In this one the line you asked about just creates a dummy variable. Later on I update the variable with the input (which is from openhab). Then I finally have return[msg,delay]; which sends “msg” right to homekit and “delay” to my delay timer.

For the other function we have:

var t = context.get('t')||0;
var delay = {payload:0}
if(msg.payload.TargetPosition){
    t = msg.payload.TargetPosition;
    context.set('t',t);
    msg.payload=t
}
if(msg.payload.TargetPosition === 0){
    t = msg.payload.TargetPosition;
    context.set('t',t);
    msg.payload=t
}
if (msg.hap.context !== undefined){
    delay.payload = {
        "CurrentPosition": t
    };
    return [msg,delay]
}

There’s more going on here. It’s looking for “target position” messages from homekit. I could probably make the code a little simpler but this works so I haven’t bothered! Anyway, the delay does the same thing. Create the dummy variable, update it with whatever position is being sent to openhab, and send that position back through my delay timer to later update the homekit “current position”.

HomeKit window/curtain looks for two messages: target position and current position. If the most recent target is different from the most recent current - that’s when your homekit will show either “opening” or “closing”.

Anyway, long answer and way too much information - the line you asked for creates the dummy variable placeholder that I send to my delay timer.

I’m actually looking now and I see that the delay variable is probably unnecessary. I’ll be thinking about it today… I noticed it’s always going to be exactly the same as the msg - so why couldn’t we just send the msg to the delay timer?

I’m out of town though and can’t test it till I get home this weekend. Try it and let me know!

i´m completly new in node. so i have to learn a lot to understand, whats going on here. Do you know some tutorials where i could learn to understand the functions?

The nodered docs are actually quite good. I picked up a lot of my function programming there (especially the context and global variables).

Outside of that the “function” node is simply a javascript function.

You’re probably past “getting started” so I’d go right to “user guide” here:

https://nodered.org/docs/

1 Like

Hi,
just want to thank you guys; I loved node-red so much - i migrated everything to it. It’s really good. And it’s fun.

My latest Fun-Part was using the RedBot to have a chatbot with telegram implementet to switch things:

The little Invert-Function is missing in the openhab-node-red standard (ON <-> OFF; UP<->DOWN). But nothing complicated there:
if(msg.payload.state==‘ON’){
msg.payload=‘OFF’
}
else{
msg.payload=‘ON’
}
return msg;

Some advice to the redbot: Only deploy Full. There is a Bug in partial deploys.

1 Like

yes i love the redbot-nodered-openhab comination. I have quite an expansive chatbot by now. Especially beeing able to use rivescript makes it alot better than other chatbot nodes. I have flow to controll all my switchable items with natural names and one alias per item. But i use the MQTT2 publish trigger for connecting it to openhab.


The link nodes go to the Telegram receiver/sender nodes but all the magic is in the function node:

Each line in the array is for one Item and its aliases. This in combination with iOS text replacement makes a really nice way to trigger scenes as I have a three later acronym saved for each scene.
I havent encountered any bugs with a partial deploy.

Looks great.
Does this mean you can write Telegram messages (e.g. from cell phone) to OH to trigger something?

Yes that is possible either from nodered if you are already using it for your automation or if you are using the built in possibilities like rules DSL or Jsr233 you could have a look here:

Johannes

Thanks for sharing Johannes.
I will play around with it and might come back with questions.