How replace two nodes of "changes" with one node of "function"?

I want to replace two nodes of "changes" with one node of "function". But, I do something wrong.
Could you help me, please!
Idea is enable / disable the dashboard switch by "function" node.

my code of "function" node.

if (msg.payload === "On") { 
    msg.payload = {'enabled':"true"};
    return [ msg ];
} 
else if (msg.payload === "Off"){   
       msg.payload = {'enabled':"false"};
       return [ msg ];
}
else {                          
       return [ null ];
}

my flow

[{"id":"c22901ee.cddb8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"4c18cc5b.2438a4","type":"ui_switch","z":"c22901ee.cddb8","name":"","label":"switch","tooltip":"","group":"298a6214.c57fde","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":550,"y":300,"wires":[[]]},{"id":"b2723bce.8e2718","type":"change","z":"c22901ee.cddb8","name":"Enable","rules":[{"t":"set","p":"enabled","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":200,"wires":[[]]},{"id":"4dc147c7.dfb608","type":"change","z":"c22901ee.cddb8","name":"Disable","rules":[{"t":"set","p":"enabled","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":260,"wires":[[]]},{"id":"90d39172.e8eef","type":"inject","z":"c22901ee.cddb8","name":"Enable","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"On","payloadType":"str","x":150,"y":280,"wires":[["b2723bce.8e2718","a781deac.dd63d"]]},{"id":"dea5ceb5.fced3","type":"inject","z":"c22901ee.cddb8","name":"Disable","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Off","payloadType":"str","x":150,"y":340,"wires":[["4dc147c7.dfb608","a781deac.dd63d"]]},{"id":"533e2cb3.14e894","type":"debug","z":"c22901ee.cddb8","name":"222222","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":520,"y":400,"wires":[]},{"id":"a781deac.dd63d","type":"function","z":"c22901ee.cddb8","name":"","func":"\n\nif (msg.payload === \"On\") { \n    msg.payload = {'enabled':\"true\"};\n    return [ msg ];\n} \nelse if (msg.payload === \"Off\"){   \n       msg.payload = {'enabled':\"false\"};\n       return [ msg ];\n}\nelse {                          \n       return [ null ];\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":360,"wires":[["533e2cb3.14e894","4c18cc5b.2438a4"]]},{"id":"298a6214.c57fde","type":"ui_group","name":"A1","tab":"cf795f11.f1c7b","order":1,"disp":true,"width":"6","collapse":false},{"id":"cf795f11.f1c7b","type":"ui_tab","name":"TEST","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

1

You are senting msg.payload.enabled to "true" .
You want to set msg.enabled to true .
msg.enabled = true;.

you can also do it in one change node e.g.

[{"id":"1823c52e.f89b4b","type":"debug","z":"e412bf07.04765","name":"222222","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":540,"y":240,"wires":[]},{"id":"d36f3af0.99b8d","type":"change","z":"e412bf07.04765","name":"Enable","rules":[{"t":"set","p":"enabled","pt":"msg","to":"payload = \"On\" ? true : (payload = \"Off\" ? false : null)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":200,"wires":[["1823c52e.f89b4b","156f6518.6c213b"]]},{"id":"156f6518.6c213b","type":"ui_switch","z":"e412bf07.04765","name":"","label":"switch","tooltip":"","group":"ee77c8fe.154dc","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":550,"y":300,"wires":[[]]},{"id":"acf054a3.aef528","type":"inject","z":"e412bf07.04765","name":"Enable","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"On","payloadType":"str","x":150,"y":280,"wires":[["d36f3af0.99b8d"]]},{"id":"18167e30.256102","type":"inject","z":"e412bf07.04765","name":"Disable","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Off","payloadType":"str","x":160,"y":360,"wires":[["d36f3af0.99b8d"]]},{"id":"ee77c8fe.154dc","type":"ui_group","name":"A1","tab":"86d61473.cb198","order":1,"disp":true,"width":"6","collapse":false},{"id":"86d61473.cb198","type":"ui_tab","name":"TEST","icon":"dashboard","order":1,"disabled":false,"hidden":false}]
1 Like

I didn't get it!
Do you mean that?


if (msg.payload === "On") { 
    msg.enabled = true;
    return [ msg ];
} 
else if (msg.payload === "Off"){   
       msg.enabled = false;
       return [ msg ];
}
else {                          
       return [ null ];
}

That doesn't work too. Or I do again something wrong?

I need to implement this in node of "function", after I want to add more code ...

return msg;

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.