Who can help me improve my node (homekit)

Hi,

since a long time I want to add homekit to my home automation, but the node I have written is so buggy that half the time it doesn't work. If you look at the debug it's also obvious why.

So, I use the node-red-contrib-homekit-bridged to unlock my door.
Whats's the logic action I need?

  1. I push on the lock Icon on my Iphone
  2. My homekit node zijdeur sends out LockTargetState : 0
  3. This needs to be acknowledged by LockCurrentState : 0
    At this time my door is open, because the pulse is enough for my door This Pulse is transfered by modbus but that's not important here.
    After 4 seconds my door automatically lock's back again, so this I also wan't to see on my Iphone. Therefore...
  4. After 4 seconds I generate a LockTargetState: 1
  5. Again, this needs to be confirmed by a LockCurrentState: 1

So actually, 4 program steps are needed.
Also, if possible, on initialization the door needs to be locked, so LockCurrentState: 1 at startup.

But I'm not happy with what I have made. I just can't get away with this 'node' language. I'm very familiar in Structured Text.

It's the 'circled' piece that needs tobe improved, is as less blocks as possible.

This is my program

[{"id":"3a68d0f5.9e89b","type":"change","z":"93b1b9fe.021678","name":"Acknowledge Target State","rules":[{"t":"move","p":"payload.LockTargetState","pt":"msg","to":"payload.LockCurrentState","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":207.01752281188965,"y":289.3334641456604,"wires":[["cc94e4b9.a72198"]]},{"id":"cc94e4b9.a72198","type":"delay","z":"93b1b9fe.021678","name":"","pauseType":"delay","timeout":"500","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":410.85077476501465,"y":289.00000524520874,"wires":[["e2858010.1649f","91dc72cd.cbc03","d8346625.ed7288"]]},{"id":"e2858010.1649f","type":"change","z":"93b1b9fe.021678","name":"Set Lock State","rules":[{"t":"set","p":"payload.LockTargetState","pt":"msg","to":"1","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":622.017370223999,"y":356.33338594436646,"wires":[["f3637270.24692"]]},{"id":"f3637270.24692","type":"delay","z":"93b1b9fe.021678","name":"","pauseType":"delay","timeout":"4","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":794.8507061004639,"y":356.00001192092896,"wires":[["91dc72cd.cbc03","42fbfcf0.0ff784"]]},{"id":"b8278997.c80a48","type":"function","z":"93b1b9fe.021678","name":"Deur openen","func":"var x = msg.payload.LockTargetState;\nif (x== \"0\") {\n var msg_o = {payload : 1}\n} \nreturn msg_o;\n\n","outputs":1,"noerr":0,"x":752.8508262634277,"y":164.99999809265137,"wires":[["9e11f920.4ff558"]]},{"id":"91dc72cd.cbc03","type":"homekit-service","z":"93b1b9fe.021678","bridge":"41183ea0.85f1a","name":"Zijdeur","serviceName":"LockMechanism","manufacturer":"Default Manufacturer","model":"Default Model","serialNo":"Default Serial Number","characteristicProperties":"{}","x":348.0694236755371,"y":170.04864501953125,"wires":[["3a68d0f5.9e89b","b8278997.c80a48","c275987b.93deb8"]]},{"id":"d8346625.ed7288","type":"debug","z":"93b1b9fe.021678","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":582.4167041778564,"y":515.5035419464111,"wires":[]},{"id":"10b19e45.79daa2","type":"inject","z":"93b1b9fe.021678","name":"","topic":"","payload":"{\"LockTargetState\":\"1\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":157.40625762939453,"y":41.97223091125488,"wires":[["91dc72cd.cbc03"]]},{"id":"c275987b.93deb8","type":"debug","z":"93b1b9fe.021678","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":483.4131832122803,"y":77.4930648803711,"wires":[]},{"id":"118e6da0.881352","type":"inject","z":"93b1b9fe.021678","name":"","topic":"","payload":"{\"LockTargetState\":\"0\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":155.35069274902344,"y":89.01041412353516,"wires":[["91dc72cd.cbc03"]]},{"id":"42fbfcf0.0ff784","type":"debug","z":"93b1b9fe.021678","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1034.4028072357178,"y":408.50000762939453,"wires":[]},{"id":"41183ea0.85f1a","type":"homekit-bridge","z":"","bridgeName":"NodeRed","pinCode":"111-11-111","port":"","manufacturer":"Default Manufacturer","model":"Default Model","serialNo":"Default Serial Number"}]

Thank you to who has time for this.

Br

Nick

Please follow:

to post your flow.

There are multiple ways to do this.

First use a switch node to only act on LockCurrentState:0, with a change node you can fire both LockCurrentState : 0 and LockTargetState: 1 at the same time with a msg.delay into a delay node:

Or with a function node, using node.send

Both in example:

[{"id":"b6332160.ea8508","type":"delay","z":"986f0e33.08175","name":"","pauseType":"delayv","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":565,"y":286,"wires":[["e45d6456.515fd8"]],"l":false},{"id":"c7913aa7.9d9a98","type":"change","z":"986f0e33.08175","name":"LockCurrentState","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.LockCurrentState","pt":"msg","to":"0","tot":"num"},{"t":"set","p":"delay","pt":"msg","to":"500","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":477,"y":242,"wires":[["b6332160.ea8508"]],"l":false},{"id":"1fbe1c94.1b3f9b","type":"switch","z":"986f0e33.08175","name":"from homekit","property":"payload.LockTargetState","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":356,"y":286,"wires":[["c7913aa7.9d9a98","f71b8c2.63fc5f","e078bf04.673998"]]},{"id":"f71b8c2.63fc5f","type":"change","z":"986f0e33.08175","name":"LockTargetState","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.LockTargetState","pt":"msg","to":"1","tot":"num"},{"t":"set","p":"delay","pt":"msg","to":"4500","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":477,"y":286,"wires":[["b6332160.ea8508"]],"l":false},{"id":"9a26952b.b833d8","type":"inject","z":"986f0e33.08175","name":"","topic":"","payload":"{\"LockTargetState\":0}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":286,"wires":[["1fbe1c94.1b3f9b"]]},{"id":"e45d6456.515fd8","type":"debug","z":"986f0e33.08175","name":"to homekit node","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":696,"y":286,"wires":[]},{"id":"5be30c44.3127ec","type":"delay","z":"986f0e33.08175","name":"","pauseType":"delayv","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":367,"y":418,"wires":[["226131df.bc1c9e"]],"l":false},{"id":"d21155bf.7465a8","type":"inject","z":"986f0e33.08175","name":"","topic":"","payload":"{\"LockTargetState\":0}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":418,"wires":[["9dc84255.4705e8"]]},{"id":"226131df.bc1c9e","type":"debug","z":"986f0e33.08175","name":"to homekit node","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":498,"y":418,"wires":[]},{"id":"9dc84255.4705e8","type":"function","z":"986f0e33.08175","name":"","func":"i = msg.payload.LockTargetState\nif(i===0){\n    node.send({delay:400,payload:{ LockCurrentState: 0}})\n    node.send({delay:4500,payload:{ LockTargetState: 1}})\n    node.send({delay:5000,payload:{ LockCurrentState: 1}})\n}\n","outputs":1,"noerr":0,"x":301,"y":418,"wires":[["5be30c44.3127ec"]],"l":false},{"id":"e078bf04.673998","type":"change","z":"986f0e33.08175","name":"LockTargetState","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.LockCurrentState","pt":"msg","to":"1","tot":"num"},{"t":"set","p":"delay","pt":"msg","to":"5500","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":477,"y":330,"wires":[["b6332160.ea8508"]],"l":false}]

Hi bakman2,

your function node seems great. I think that will do the trick just fine, thanks.
I was not able to test it in reality, I can't add any device anymore. The settings of the homekit node became a little bit more advanced, not sure if I undestand them all correctly.

Thanks again, I'll come back to this.