Saving (/w 'context') values within function nodes for future use

#1

Hi all,

I'm new to the Java coding and I have been trying to create a dimmable LED (using PWM) that is controlled through homekit.

Homekit outputs the following: ON/OFF or Brightness
The goal: If the LED is turned 'ON', instead I want it to send the last set 'Brightness'. Otherwise it will always start at 1% Brightness. The LED therefore only receives 'OFF' and 'Brightness' commands.

E.g.:
Set Brightness 80% -> LED on at 80%
Turn off -> LED off
Turn on -> ['ON' to 'Brightness 80%'] -> LED on at 80%

For this I have the following code:

output = {};
if (msg.payload.Brightness === null){
    if (msg.payload.On){
        var Brightness = flow.get("prevBrightness");
        output.payload = Brightness;
    } else {
    output.payload = false;
    }
} else { 
    var Brightness = msg.payload.Brightness;
    flow.set("prevBrightness",Brightness);
    output.payload = Brightness;
}
return output;

The full code for the flow is the following:
[{"id":"ba05e7a4.be9018","type":"rpi-gpio out","z":"4ea512c6.711b5c","name":"","pin":"12","set":"","level":"0","freq":"200","out":"pwm","x":840,"y":740,"wires":[]},{"id":"e39bb0c1.66e14","type":"inject","z":"4ea512c6.711b5c","name":"MakeDimmable","topic":"","payload":"{\"Brightness\":80}","payloadType":"json","repeat":"","crontab":"","once":true,"onceDelay":"","x":160,"y":740,"wires":[["ce6a60a0.64b24"]]},{"id":"ce6a60a0.64b24","type":"homekit-service","z":"4ea512c6.711b5c","accessory":"fa29e912.e3a088","name":"Lamp","serviceName":"Lightbulb","x":330,"y":740,"wires":[["3818978f.cc2718","73a8c686.2ec3e8"]]},{"id":"c7cb4ad4.444d2","type":"debug","z":"4ea512c6.711b5c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":830,"y":820,"wires":[]},{"id":"3818978f.cc2718","type":"function","z":"4ea512c6.711b5c","name":"Signal_conversion","func":"output = {};\nif (msg.payload.Brightness === null){\n if (msg.payload.On){\n var Brightness = flow.get(\"prevBrightness\");\n output.payload = Brightness;\n } else {\n output.payload = false;\n }\n} else { \n var Brightness = msg.payload.Brightness;\n flow.set(\"prevBrightness\",Brightness);\n output.payload = Brightness;\n}\nreturn output;","outputs":1,"noerr":0,"x":570,"y":820,"wires":[["c7cb4ad4.444d2"]]},{"id":"73a8c686.2ec3e8","type":"change","z":"4ea512c6.711b5c","name":"","rules":[{"t":"move","p":"hap.newValue","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":590,"y":740,"wires":[["ba05e7a4.be9018"]]},{"id":"fea26934.03499","type":"comment","z":"4ea512c6.711b5c","name":"Current","info":"","x":970,"y":740,"wires":[]},{"id":"913b6d97.59b4","type":"comment","z":"4ea512c6.711b5c","name":"New","info":"","x":970,"y":820,"wires":[]},{"id":"fa29e912.e3a088","type":"homekit-accessory","z":"","accessoryName":"Lamp","pinCode":"333-33-333","port":"","manufacturer":"Default Manufacturer","model":"Default Model","serialNo":"Default Serial Number","accessoryType":"1"}]

Does anyone has advice on how to get this to work?

Thank you!

#2

It is javascript not Java.

Are you sure that msg.payload.Brightness is set to null when an On/Off is received?
I would start by adding a debug node after your homekit node and set the debug to output complete message object.

My guess is that msg.payload.Brightness is undefined and therefore testing for null isn't going to work. A quick google for" javascript testing if variable is defined" should get you on your way.

#3

Thank you for your reply, it works now!

output = {};
if (typeof msg.payload.Brightness == 'undefined'){
    if (msg.payload.On){
        var Brightness = flow.get("prevBrightness");
        output.payload = Brightness;
    } else {
    output.payload = false;
    }
} else { 
    var Brightness = msg.payload.Brightness;
    flow.set("prevBrightness",Brightness);
    output.payload = Brightness;
}
return output;

I had been searching with the wrong criteria I realise. Thanks.