Homekit in nodeRED to control a color bulb attached to openHAB

Hi,

I tried to achieve this and made a function node before and after the homekit-node

The function node before the homekit-node has this:

var results = msg.payload.split(',');
if (results[2] < 1) {
   OnOff = false;
} else {
   OnOff = true;
}
msg.payload = {
    "Brightness": results[2],
    "Hue": results[0],
    "Saturation": results[1],
    "On": OnOff
        };
return msg;

What is actually should do is taking the HSB-Value we get from the color-item and split it to it’s different values and create the objects Brightness Hue and Saturation in the desired format for the homekit-node.
Also to represent the on/off-state if brightness is not zero the bulb is on.

After the homekit-node I created a function node with this content:

var h = context.get('h')||0;
var s = context.get('s')||0;
var b = context.get('b')||0;

if(msg.payload.On === false){
        h = 0;
        context.set('h',h)
        s = 0;
        context.set('s',s)
        b = 0;
        context.set('b',b)
}
if(msg.payload.Hue){
    h = msg.payload.Hue;
        context.set('h',h)
}
if(msg.payload.Saturation){
    s = msg.payload.Saturation;
        context.set('s',s)
}
if(msg.payload.Brightness){
    b = msg.payload.Brightness;
        context.set('b',b)
}
msg.payload = h+","+s+","+b;
if (msg.hap.context !== undefined )
{
return msg;
}

The homekit-node puts out the objects Hue, Saturation and Brightness individually but for sending a h,s,b-String we need all values. So I thought of the “persisting” variables in the node with this context-thing.

So when initializing the node create h, s and b with “0”

Then if homekit puts out “On: false” (so literally off) send 0,0,0.
Then when a value of Saturation, Hue or Brightness arrives set the respective variable to that value and take the other values from the previous run so we can send out a complete h,s,b-value every time to our olor-item.
At last as in the other thread mentioned to prevent loops when setting the node in between the color-item out and in check if the changes are coming from the homekit-node and only then send the payload.

Please keep in mind that I don’t really know what I am doing here with javascript but it seems to work.
If someone can optimize that or rewrite it in a better way you are very welcome.

HTH

Nico

2 Likes