Hey, I know I could do this with the RBE/filter node, but it's in some code, and I'd like to keep it in the function node.
Is there a way for this code to check for JSON objects. It does check for numbers/strings etc. But JSON objects seems to get through the code even if it's identical.
This is the code in the function node.
// block unless value changes
var new_input = msg.payload;
var last_output = context.get("last_output");
var master = global.get("master_office_lights");
if (master == "off"){
if (typeof last_output == 'undefined') {
context.set("last_output", new_input);
return msg; // forward initial value
// * return null to ignore the initial value * //
}
// otherwise last_output is defined, so did it change?
if (new_input == last_output) {
return null;
}
else {
context.set("last_output", new_input);
return msg;
}
}
if (master =="on"){
context.set("last_output", 'cleared')
}
Here is a flow that shows an object it gets through the function node, but is stopped by the RBE node.
[{"id":"e221f25fc838887b","type":"function","z":"8c249630.076968","name":"RBE & Gate","func":"// block unless value changes\n\nvar new_input = msg.payload;\nvar last_output = context.get(\"last_output\");\nvar master = global.get(\"master_office_lights\");\n\nif (master == \"off\"){\n if (typeof last_output == 'undefined') {\n context.set(\"last_output\", new_input);\n return msg; // forward initial value\n // * return null to ignore the initial value * //\n }\n \n // otherwise last_output is defined, so did it change?\n if (new_input == last_output) {\n return null;\n }\n else {\n context.set(\"last_output\", new_input);\n return msg;\n }\n}\n\nif (master ==\"on\"){\n context.set(\"last_output\", 'cleared')\n }","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":670,"y":1600,"wires":[["6c25fc06b3647bd6"]]},{"id":"6c25fc06b3647bd6","type":"debug","z":"8c249630.076968","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":850,"y":1600,"wires":[]},{"id":"cf463ab38a1e0c24","type":"inject","z":"8c249630.076968","name":"","props":[{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":310,"y":1560,"wires":[["1f77d8bf169d02dd"]]},{"id":"538f59be7e060990","type":"inject","z":"8c249630.076968","name":"","props":[{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":310,"y":1600,"wires":[["e25aa489742d569d"]]},{"id":"91e9fdaa2d1b1d30","type":"inject","z":"8c249630.076968","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"-13","payloadType":"num","x":310,"y":1660,"wires":[["e221f25fc838887b","b67a2e571f2eff2c"]]},{"id":"1f77d8bf169d02dd","type":"function","z":"8c249630.076968","name":"","func":"return {payload:{data:{'brightness_pct':60,'color_temp':275}}}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":1560,"wires":[["e221f25fc838887b","b67a2e571f2eff2c"]]},{"id":"e25aa489742d569d","type":"function","z":"8c249630.076968","name":"","func":"return {payload:{\"service\": \"turn_off\" }}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":1600,"wires":[["e221f25fc838887b","b67a2e571f2eff2c"]]},{"id":"b67a2e571f2eff2c","type":"rbe","z":"8c249630.076968","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":false,"property":"payload","topi":"topic","x":675,"y":1660,"wires":[["3850a39194311ea6"]],"l":false},{"id":"3850a39194311ea6","type":"debug","z":"8c249630.076968","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":850,"y":1660,"wires":[]}]