[newbie] How to make this function?

I'd like to display the voltage measured by a sonoff pow r2 displayed in a graph.
I have captured the msg.payload in json format and made two input nodes with it, so you can see
how the data is made up. I just want a function which takes out the voltage to make a graph with it in
the gui.
Here is my flow:

`[{"id":"75df3d17.fc49ec","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"e318257b.48a138","type":"inject","z":"75df3d17.fc49ec","name":"testdataSENSOR","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"tele/sonoff2/SENSOR","payload":"{\"Time\":\"2020-08-22T19:16:33\",\"ENERGY\":{\"TotalStartTime\":\"2020-08-20T16:12:14\",\"Total\":0.003,\"Yesterday\":0,\"Today\":0,\"Period\":0,\"Power\":0,\"ApparentPower\":0,\"ReactivePower\":0,\"Factor\":0,\"Voltage\":230,\"Current\":0}}","payloadType":"json","x":222.1666717529297,"y":203.16665649414062,"wires":[["c4021a8e.ae7d68"]]},{"id":"eac79629.fc0f6","type":"inject","z":"75df3d17.fc49ec","name":"testdataSTATE","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"tele/sonoff2/STATE","payload":"{\"Time\":\"2020-08-22T19:16:33\",\"ENERGY\":{\"TotalStartTime\":\"2020-08-20T16:12:14\",\"Total\":0.003,\"Yesterday\":0,\"Today\":0,\"Period\":0,\"Power\":0,\"ApparentPower\":0,\"ReactivePower\":0,\"Factor\":0,\"Voltage\":230,\"Current\":0}}","payloadType":"json","x":220,"y":140,"wires":[["c4021a8e.ae7d68"]]},{"id":"c4021a8e.ae7d68","type":"debug","z":"75df3d17.fc49ec","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":180,"wires":[]},{"id":"75836fb9.c2c8c","type":"ui_chart","z":"75df3d17.fc49ec","name":"Voltage sonoff2","group":"cd3ce492.ee804","order":4,"width":0,"height":0,"label":"Voltage sonoff2","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"0","ymax":"250","removeOlder":"24","removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"outputs":1,"x":640,"y":300,"wires":[[]]},{"id":"dba3ade3.57e96","type":"mqtt in","z":"75df3d17.fc49ec","name":"","topic":"tele/sonoff2/#","qos":"0","datatype":"auto","broker":"f56d1a7c.e35768","x":190,"y":60,"wires":[["b836be04.6530d8"]]},{"id":"b836be04.6530d8","type":"json","z":"75df3d17.fc49ec","name":"","property":"payload","action":"","pretty":false,"x":390,"y":60,"wires":[["c4021a8e.ae7d68"]]},{"id":"d928f648.1f4df8","type":"function","z":"75df3d17.fc49ec","name":"","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":440,"y":300,"wires":[["75836fb9.c2c8c"]]},{"id":"cd3ce492.ee804","type":"ui_group","z":"","name":"sonoff2","tab":"d2e4816a.0ecad","order":2,"disp":true,"width":"6","collapse":false},{"id":"f56d1a7c.e35768","type":"mqtt-broker","z":"","name":"localhost:1883","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"d2e4816a.0ecad","type":"ui_tab","z":"","name":"sonoff","icon":"dashboard","order":8,"disabled":false,"hidden":false}]`

could anybody here help me with this function?

kind regards,
Hugo

Is that the format of the actual information? If you look at the debug node the path to the value is - payload.ENERGY.Voltage, that should get you the value you are looking for or am I missing something here?

1 Like

your flow with a modification

[{"id":"56f28088.90e27","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"32235739.b7c98","type":"inject","z":"56f28088.90e27","name":"testdataSENSOR","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"tele/sonoff2/SENSOR","payload":"{\"Time\":\"2020-08-22T19:16:33\",\"ENERGY\":{\"TotalStartTime\":\"2020-08-20T16:12:14\",\"Total\":0.003,\"Yesterday\":0,\"Today\":0,\"Period\":0,\"Power\":0,\"ApparentPower\":0,\"ReactivePower\":0,\"Factor\":0,\"Voltage\":230,\"Current\":0}}","payloadType":"json","x":222.1666717529297,"y":203.16665649414062,"wires":[["5c23158e.e5c9a4","93603809.3d0108"]]},{"id":"ea80d8ba.e1a68","type":"inject","z":"56f28088.90e27","name":"testdataSTATE","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"tele/sonoff2/STATE","payload":"{\"Time\":\"2020-08-22T19:16:33\",\"ENERGY\":{\"TotalStartTime\":\"2020-08-20T16:12:14\",\"Total\":0.003,\"Yesterday\":0,\"Today\":0,\"Period\":0,\"Power\":0,\"ApparentPower\":0,\"ReactivePower\":0,\"Factor\":0,\"Voltage\":230,\"Current\":0}}","payloadType":"json","x":220,"y":140,"wires":[["5c23158e.e5c9a4"]]},{"id":"5c23158e.e5c9a4","type":"debug","z":"56f28088.90e27","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":180,"wires":[]},{"id":"8fa657d9.bf2358","type":"ui_chart","z":"56f28088.90e27","name":"Voltage sonoff2","group":"9cc3eb1f.df2b08","order":4,"width":0,"height":0,"label":"Voltage sonoff2","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"0","ymax":"250","removeOlder":"24","removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"outputs":1,"x":640,"y":300,"wires":[[]]},{"id":"8d28f9d1.aa6178","type":"mqtt in","z":"56f28088.90e27","name":"","topic":"tele/sonoff2/#","qos":"0","datatype":"auto","broker":"871e6f6.0ad161","x":190,"y":60,"wires":[["3628d9cd.d1d256"]]},{"id":"3628d9cd.d1d256","type":"json","z":"56f28088.90e27","name":"","property":"payload","action":"","pretty":false,"x":390,"y":60,"wires":[["5c23158e.e5c9a4"]]},{"id":"93603809.3d0108","type":"function","z":"56f28088.90e27","name":"","func":"let z = msg.payload.ENERGY.Voltage;\nnode.status({fill:\"blue\",shape:\"dot\",text:z});\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":440,"y":300,"wires":[["8fa657d9.bf2358"]]},{"id":"9cc3eb1f.df2b08","type":"ui_group","z":"","name":"sonoff2","tab":"bc22e353.23bdf","order":2,"disp":true,"width":"6","collapse":false},{"id":"871e6f6.0ad161","type":"mqtt-broker","z":"","name":"localhost:1883","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"bc22e353.23bdf","type":"ui_tab","z":"","name":"sonoff","icon":"dashboard","order":8,"disabled":false,"hidden":false}]

by the way, when posting flows use the backticks around your flow to make it easier to import,

2 Likes

Dear Gerry,
Thank you for trying to help me.

Is that the format of the actual information?

No, this is the format after it went through a json-node. You probably ask why I don't make inject nodes with the actual information, the reason is I don't succeed in doing that because I don't know what format to choose in the inject node.(except for the case of json)

I have however copied the two types of messages here below:
This one belongs to tele/sonoff2/STATE

`{"Time":"2020-08-22T20:54:48","Uptime":"0T04:02:16","UptimeSec":14536,"Heap":25,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"MqttCount":52,"POWER":"ON","Wifi":{"AP":1,"SSId":"bbox2","BSSId":"E0:B9:E5:F1:65:17","Channel":11,"RSSI":58,"Signal":-71,"LinkCount":1,"Downtime":"0T00:00:03"}}`

and
This one belongs to tele/sonoff2/SENSOR


{"Time":"2020-08-22T20:56:03","ENERGY":{"TotalStartTime":"2020-08-20T16:12:14","Total":0.003,"Yesterday":0.000,"Today":0.000,"Period":0,"Power":0,"ApparentPower":0,"ReactivePower":0,"Factor":0.00,"Voltage":232,"Current":0.000}}

I have modified your flow so you can see where exactly the function should act
I guess I also should change the topic in the mqtt-node? But to what exactly?

[{"id":"216e6aeb.b6cb36","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"5f497394.0f9ff4","type":"inject","z":"216e6aeb.b6cb36","name":"testdataSENSOR","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"tele/sonoff2/SENSOR","payload":"{\"Time\":\"2020-08-22T19:16:33\",\"ENERGY\":{\"TotalStartTime\":\"2020-08-20T16:12:14\",\"Total\":0.003,\"Yesterday\":0,\"Today\":0,\"Period\":0,\"Power\":0,\"ApparentPower\":0,\"ReactivePower\":0,\"Factor\":0,\"Voltage\":230,\"Current\":0}}","payloadType":"json","x":222.1666717529297,"y":203.16665649414062,"wires":[["662943f.f51ccbc"]]},{"id":"503179f6.277078","type":"inject","z":"216e6aeb.b6cb36","name":"testdataSTATE","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"tele/sonoff2/STATE","payload":"{\"Time\":\"2020-08-22T19:16:33\",\"ENERGY\":{\"TotalStartTime\":\"2020-08-20T16:12:14\",\"Total\":0.003,\"Yesterday\":0,\"Today\":0,\"Period\":0,\"Power\":0,\"ApparentPower\":0,\"ReactivePower\":0,\"Factor\":0,\"Voltage\":230,\"Current\":0}}","payloadType":"json","x":220,"y":140,"wires":[["662943f.f51ccbc"]]},{"id":"662943f.f51ccbc","type":"debug","z":"216e6aeb.b6cb36","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":180,"wires":[]},{"id":"b0971b12.0d28c","type":"ui_chart","z":"216e6aeb.b6cb36","name":"Voltage sonoff2","group":"56858d37.44f834","order":4,"width":0,"height":0,"label":"Voltage sonoff2","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"0","ymax":"250","removeOlder":"24","removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"outputs":1,"x":640,"y":300,"wires":[[]]},{"id":"8a8538ff.033bc","type":"mqtt in","z":"216e6aeb.b6cb36","name":"","topic":"tele/sonoff2/#","qos":"0","datatype":"auto","broker":"bd855bd4.a4715","x":190,"y":60,"wires":[["75055c91.d445e4"]]},{"id":"75055c91.d445e4","type":"json","z":"216e6aeb.b6cb36","name":"","property":"payload","action":"","pretty":false,"x":390,"y":60,"wires":[["662943f.f51ccbc","64c3d226.8a5cec"]]},{"id":"64c3d226.8a5cec","type":"function","z":"216e6aeb.b6cb36","name":"","func":"let z = msg.payload.ENERGY.Voltage;\nnode.status({fill:\"blue\",shape:\"dot\",text:z});\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":440,"y":300,"wires":[["b0971b12.0d28c"]]},{"id":"56858d37.44f834","type":"ui_group","z":"","name":"sonoff2","tab":"432fd9b.0515ca8","order":2,"disp":true,"width":"6","collapse":false},{"id":"bd855bd4.a4715","type":"mqtt-broker","z":"","name":"localhost:1883","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"432fd9b.0515ca8","type":"ui_tab","z":"","name":"sonoff","icon":"dashboard","order":8,"disabled":false,"hidden":false}]

kind regards,
hugo
p.s. Maybe the conversion to json isn't necessary after all?

I had to wander off and probably won't be at a computer for a couple of days. At this point my best advice would be to try different things and see what works. As far as topics go anything works choose something that makes sense to you. Best wishes
Sent from phone

Hi Hugo,

You can configure the output of your MQTT to be a parsed JSON object without the need of the JSON node after mqtt. Like this :

[{"id":"c6bf4616.f2d7c8","type":"inject","z":"fa9ab452.58e15","name":"testdataSENSOR","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"tele/sonoff2/SENSOR","payload":"{\"Time\":\"2020-08-22T19:16:33\",\"ENERGY\":{\"TotalStartTime\":\"2020-08-20T16:12:14\",\"Total\":0.003,\"Yesterday\":0,\"Today\":0,\"Period\":0,\"Power\":0,\"ApparentPower\":0,\"ReactivePower\":0,\"Factor\":0,\"Voltage\":230,\"Current\":0}}","payloadType":"json","x":222.1666717529297,"y":203.16665649414062,"wires":[["ec456f14.99d2d8"]]},{"id":"acc445ea.cbbc2","type":"inject","z":"fa9ab452.58e15","name":"testdataSTATE","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"tele/sonoff2/STATE","payload":"{\"Time\":\"2020-08-22T19:16:33\",\"ENERGY\":{\"TotalStartTime\":\"2020-08-20T16:12:14\",\"Total\":0.003,\"Yesterday\":0,\"Today\":0,\"Period\":0,\"Power\":0,\"ApparentPower\":0,\"ReactivePower\":0,\"Factor\":0,\"Voltage\":230,\"Current\":0}}","payloadType":"json","x":220,"y":140,"wires":[["ec456f14.99d2d8"]]},{"id":"ec456f14.99d2d8","type":"debug","z":"fa9ab452.58e15","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":610,"y":140,"wires":[]},{"id":"32d5e0d9.8c5ca8","type":"ui_chart","z":"fa9ab452.58e15","name":"Voltage sonoff2","group":"597adaa0.e95d74","order":4,"width":0,"height":0,"label":"Voltage sonoff2","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"0","ymax":"250","removeOlder":"24","removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"outputs":1,"x":640,"y":300,"wires":[[]]},{"id":"c172a58d.43b818","type":"mqtt in","z":"fa9ab452.58e15","name":"","topic":"tele/sonoff2/#","qos":"0","datatype":"json","broker":"c8c2ac9f.488858","x":190,"y":60,"wires":[["ec456f14.99d2d8"]]},{"id":"aeb61a65.0078b8","type":"function","z":"fa9ab452.58e15","name":"","func":"let z = msg.payload.ENERGY.Voltage;\nnode.status({fill:\"blue\",shape:\"dot\",text:z});\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":440,"y":300,"wires":[["32d5e0d9.8c5ca8"]]},{"id":"597adaa0.e95d74","type":"ui_group","z":"","name":"sonoff2","tab":"1f2f2bfc.237244","order":2,"disp":true,"width":"6","collapse":false},{"id":"c8c2ac9f.488858","type":"mqtt-broker","z":"","name":"localhost:1883","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"1f2f2bfc.237244","type":"ui_tab","z":"","name":"sonoff","icon":"dashboard","order":8,"disabled":false,"hidden":false}]

Can you send us a screenshot of the full expanded debug message coming from MQTT. Basically you set you debug node to 'complete msg'

[EDIT] What you did in function seems correct but i didnt undestand the true structure of data from SENSOR.
If what you posted from SENSOR is in payload, then what you wrote in function is correct .. just push that data to msg.payload like this :

let z = msg.payload.ENERGY.Voltage;    // could be msg.ENERGY.Voltage
node.status({fill:"blue",shape:"dot",text:z});
msg.payload = z;  //   make new payload with voltage
return msg;
1 Like

Hi UnborN,
Thanks for answering.

You can configure the output of your MQTT to be a parsed JSON object without the need of the JSON node after mqtt. Like this :

[{"id":"c6bf4616.f2d7c8","type":"inject","z":"fa9ab452.58e15","name":"testdataSENSOR","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"tele/sonoff2/SENSOR","payload":"{\"Time\":\"2020-08-22T19:16:33\",\"ENERGY\":{\"TotalStartTime\":\"2020-08-20T16:12:14\",\"Total\":0.003,\"Yesterday\":0,\"Today\":0,\"Period\":0,\"Power\":0,\"ApparentPower\":0,\"ReactivePower\":0,\"Factor\":0,\"Voltage\":230,\"Current\":0}}","payloadType":"json","x":222.1666717529297,"y":203.16665649414062,"wires":[["ec456f14.99d2d8"]]},{"id":"acc445ea.cbbc2","type":"inject","z":"fa9ab452.58e15","name":"testdataSTATE","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"tele/sonoff2/STATE","payload":"{\"Time\":\"2020-08-22T19:16:33\",\"ENERGY\":{\"TotalStartTime\":\"2020-08-20T16:12:14\",\"Total\":0.003,\"Yesterday\":0,\"Today\":0,\"Period\":0,\"Power\":0,\"ApparentPower\":0,\"ReactivePower\":0,\"Factor\":0,\"Voltage\":230,\"Current\":0}}","payloadType":"json","x":220,"y":140,"wires":[["ec456f14.99d2d8"]]},{"id":"ec456f14.99d2d8","type":"debug","z":"fa9ab452.58e15","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":610,"y":140,"wires":[]},{"id":"32d5e0d9.8c5ca8","type":"ui_chart","z":"fa9ab452.58e15","name":"Voltage sonoff2","group":"597adaa0.e95d74","order":4,"width":0,"height":0,"label":"Voltage sonoff2","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"0","ymax":"250","removeOlder":"24","removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"outputs":1,"x":640,"y":300,"wires":[[]]},{"id":"c172a58d.43b818","type":"mqtt in","z":"fa9ab452.58e15","name":"","topic":"tele/sonoff2/#","qos":"0","datatype":"json","broker":"c8c2ac9f.488858","x":190,"y":60,"wires":[["ec456f14.99d2d8"]]},{"id":"aeb61a65.0078b8","type":"function","z":"fa9ab452.58e15","name":"","func":"let z = msg.payload.ENERGY.Voltage;\nnode.status({fill:\"blue\",shape:\"dot\",text:z});\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":440,"y":300,"wires":[["32d5e0d9.8c5ca8"]]},{"id":"597adaa0.e95d74","type":"ui_group","z":"","name":"sonoff2","tab":"1f2f2bfc.237244","order":2,"disp":true,"width":"6","collapse":false},{"id":"c8c2ac9f.488858","type":"mqtt-broker","z":"","name":"localhost:1883","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"1f2f2bfc.237244","type":"ui_tab","z":"","name":"sonoff","icon":"dashboard","order":8,"disabled":false,"hidden":false}]

I don't get this, could it be you copy-pasted the wrong flow?

However I got it working with this flow:

[{"id":"70c06a15.6b2114","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"b6760797.38765","type":"ui_chart","z":"70c06a15.6b2114","name":"Voltage sonoff2","group":"4593adf4.79530c","order":4,"width":0,"height":0,"label":"Voltage sonoff2","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"0","ymax":"250","removeOlder":"24","removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"outputs":1,"x":760,"y":140,"wires":[[]]},{"id":"f4ab477a.ad8158","type":"mqtt in","z":"70c06a15.6b2114","name":"","topic":"tele/sonoff2/SENSOR","qos":"0","datatype":"auto","broker":"ce2134ef.d631","x":220,"y":60,"wires":[["5cfdce75.cc202"]]},{"id":"5cfdce75.cc202","type":"json","z":"70c06a15.6b2114","name":"","property":"payload","action":"","pretty":false,"x":410,"y":60,"wires":[["aa17d2ca.ec75c"]]},{"id":"aa17d2ca.ec75c","type":"function","z":"70c06a15.6b2114","name":"","func":"let z = msg.payload.ENERGY.Voltage;    // could be msg.ENERGY.Voltage\nnode.status({fill:\"blue\",shape:\"dot\",text:z});\nmsg.payload = z;  //   make new payload with voltage\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":540,"y":60,"wires":[["b6760797.38765","a7ff3f43.6df7a8"]]},{"id":"a7ff3f43.6df7a8","type":"debug","z":"70c06a15.6b2114","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":730,"y":60,"wires":[]},{"id":"4593adf4.79530c","type":"ui_group","z":"","name":"sonoff2","tab":"82f310a2.14ce4","order":2,"disp":true,"width":"6","collapse":false},{"id":"ce2134ef.d631","type":"mqtt-broker","z":"","name":"localhost:1883","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"82f310a2.14ce4","type":"ui_tab","z":"","name":"sonoff","icon":"dashboard","order":8,"disabled":false,"hidden":false}]

I figured out I have set the topic of the "mqtt-in node" to "tele/sonoff2/SENSOR" i.e. leaving in the # as I original did caused the other topic "tele/sonoff2/STATE" to get also in the flow. Probably very logical with hindsight...
The other thing I checked was that I have to use the json-node for your function to work.
Maybe it is possible to leave out the json-node but then the function should be adapted, however I don't know how.
A last question:
Could you explain what the statement:

node.status({fill:"blue",shape:"dot",text:z});

is doing exactly? It seems necessary to make the function work.

kind regards,
Hugo

Hi @hugocoolens

You can leave the JSON node out if you change the MQTT In to "a parsed JSON object" (as suggested ealier by @UnborN

image

It is NOT required (but it is a nice touch)

This node.status({fill:"blue",shape:"dot",text:z}); statement simply puts a nice info label below your function with a blue dot. example...
image

TBH, you dont really need a function node either - you are simply copying a payload object property to the payload - you could just use a change node...

1 Like

Thanks a lot for teaching me all this interesting stuff.

kind regards,
Hugo

2 Likes

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