How to make condition with msg.payload = [empty]

Hi. I have a question. I select data from influxdb in 3 condition
1.now()
2.now() - 1d
3.now() - 7d
My problem is when I tried to get data with [ now() - 1d ] & [ now() - 7d ] influx in node return empty msg.payload
Then I need to use function node to write condition for empty msg.payload or not empty msg.payload.
My question is when get data from influxdb that empty (msg.payload = empty) How to write condition to check that payload is empty or not empty. Thanks:)
ภาพ

[{"id":"4ef6e421.af2424","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"f3057743.301908","type":"influxdb in","z":"4ef6e421.af2424","influxdb":"8a29a247.2f9818","name":"Get now() - 7d energy value","query":"SELECT \"value\" AS \"mean_value\" FROM \"home_assistant\".\"autogen\".\"Wh\" WHERE time = now() - 7d","rawOutput":false,"precision":"","retentionPolicy":"","x":760,"y":200,"wires":[["88eca94f.40a0b","33bd9ad9.3efc0e"]]},{"id":"1cec616f.a68317","type":"inject","z":"4ef6e421.af2424","name":"Push data for calculate and send to API every 5 minutes","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":330,"y":140,"wires":[["f3057743.301908","7a187a0e.a4c354","6946f91.d9fa988","c6e7ef0b.462c58"]]},{"id":"7a187a0e.a4c354","type":"influxdb in","z":"4ef6e421.af2424","influxdb":"8a29a247.2f9818","name":"Get now() - 1d energy value","query":"SELECT \"value\" AS \"mean_value\" FROM \"home_assistant\".\"autogen\".\"Wh\" WHERE time = now() - 1d","rawOutput":false,"precision":"","retentionPolicy":"","x":760,"y":140,"wires":[["3764d95a.30a906","d274614c.25d5b"]]},{"id":"13cf691b.7a3d67","type":"function","z":"4ef6e421.af2424","name":"Calculate last 1 day energy usage value","func":"var now = parseFloat(flow.get(\"now\"))\nvar yd = parseFloat(flow.get(\"yd\"))\nvar ydwh = (now - yd)/1000;\nvar kwh = ydwh.toFixed(2);\nmsg.payload = parseFloat(kwh);\nreturn msg;","outputs":1,"noerr":0,"x":280,"y":420,"wires":[["8327ac5f.b506"]]},{"id":"3764d95a.30a906","type":"function","z":"4ef6e421.af2424","name":"Set NOW - 1d energy value to flow ","func":"if(msg.payload[0].mean_value === []){\n    msg.payload = \"unavailable\"\n}\nelse{\nflow.set(\"yd\",msg.payload[0].mean_value)\n}\nreturn msg;","outputs":1,"noerr":0,"x":1060,"y":140,"wires":[["5363d932.2dcf08"]]},{"id":"88eca94f.40a0b","type":"function","z":"4ef6e421.af2424","name":"Set NOW - 7d energy value to flow ","func":"flow.set(\"l7d\",msg.payload[0].mean_value)\nreturn msg;","outputs":1,"noerr":0,"x":1060,"y":200,"wires":[[]]},{"id":"6946f91.d9fa988","type":"influxdb in","z":"4ef6e421.af2424","influxdb":"8a29a247.2f9818","name":"Get now() energy value","query":"SELECT last(\"value\") AS \"mean_value\" FROM \"home_assistant\".\"autogen\".\"Wh\" ","rawOutput":false,"precision":"","retentionPolicy":"","x":750,"y":80,"wires":[["c317a139.e6eb28","7a5f2ab4.3a4574"]]},{"id":"c317a139.e6eb28","type":"function","z":"4ef6e421.af2424","name":"Set NOW energy value to flow ","func":"flow.set(\"now\",msg.payload[0].mean_value)\nreturn msg;","outputs":1,"noerr":0,"x":1050,"y":80,"wires":[[]]},{"id":"31b767f8.3772","type":"function","z":"4ef6e421.af2424","name":"Calculate last 7 day energy usage value","func":"var now = parseFloat(flow.get(\"now\"))\nvar l7d = parseFloat(flow.get(\"l7d\"))\nvar l7dwh = (now - l7d)/1000;\nvar kwh = l7dwh.toFixed(2);\nmsg.payload = parseFloat(kwh);\nreturn msg;","outputs":1,"noerr":0,"x":280,"y":480,"wires":[["6d575cb4.be8a5c"]]},{"id":"8327ac5f.b506","type":"function","z":"4ef6e421.af2424","name":"SET FORMAT OF ENTITIES","func":"msg.entity_id = \"ebvalue.yd\";\nvar ydkwh = msg.payload;\nmsg.payload = {\n    data: {\n        state: ydkwh,\n        attributes: {\n            unit_of_measurement: \"kWh\",\n            friendly_name: \"พลังงานไฟฟ้าที่ใช้ในช่วง 1 วันที่ผ่านมา\"\n        }\n    }\n};\nreturn msg;","outputs":1,"noerr":0,"x":580,"y":420,"wires":[["937d6097.17da1","e41c183a.ecb5b"]]},{"id":"e41c183a.ecb5b","type":"ha-api","z":"4ef6e421.af2424","name":"API TO HASSIO","server":"acd054c7.c52cd8","protocol":"http","method":"post","path":"/api/states/{{entity_id}}","data":"","dataType":"json","location":"payload","locationType":"msg","responseType":"json","x":840,"y":420,"wires":[[]]},{"id":"c6e7ef0b.462c58","type":"delay","z":"4ef6e421.af2424","name":"delay for get & calculate","pauseType":"delay","timeout":"20","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":350,"y":280,"wires":[["13cf691b.7a3d67","31b767f8.3772"]]},{"id":"6d575cb4.be8a5c","type":"function","z":"4ef6e421.af2424","name":"SET FORMAT OF ENTITIES","func":"msg.entity_id = \"ebvalue.l7d\";\nvar l7dkwh = msg.payload;\nmsg.payload = {\n    data: {\n        state: l7dkwh,\n        attributes: {\n            unit_of_measurement: \"kWh\",\n            friendly_name: \"พลังงานไฟฟ้าที่ใช้ในช่วง 7 วันที่ผ่านมา\"\n        }\n    }\n};\nreturn msg;","outputs":1,"noerr":0,"x":600,"y":480,"wires":[["90bf3876.4e0e08","dfd26355.b8706"]]},{"id":"dfd26355.b8706","type":"ha-api","z":"4ef6e421.af2424","name":"API TO HASSIO","server":"acd054c7.c52cd8","protocol":"http","method":"post","path":"/api/states/{{entity_id}}","data":"","dataType":"json","location":"payload","locationType":"msg","responseType":"json","x":840,"y":480,"wires":[[]]},{"id":"937d6097.17da1","type":"debug","z":"4ef6e421.af2424","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","x":810,"y":340,"wires":[]},{"id":"90bf3876.4e0e08","type":"debug","z":"4ef6e421.af2424","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":760,"y":560,"wires":[]},{"id":"7a5f2ab4.3a4574","type":"debug","z":"4ef6e421.af2424","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":940,"y":40,"wires":[]},{"id":"d274614c.25d5b","type":"debug","z":"4ef6e421.af2424","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":980,"y":120,"wires":[]},{"id":"33bd9ad9.3efc0e","type":"debug","z":"4ef6e421.af2424","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":980,"y":280,"wires":[]},{"id":"5363d932.2dcf08","type":"debug","z":"4ef6e421.af2424","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1320,"y":80,"wires":[]},{"id":"8a29a247.2f9818","type":"influxdb","z":"","hostname":"127.0.0.1","port":"8086","protocol":"http","database":"home_assistant","name":"","usetls":false,"tls":""},{"id":"acd054c7.c52cd8","type":"server","z":"","name":"Home Assistant"}]

Try

if (msg.payload === undefined) {...do something...}

I test undefined and result as picture

Ahh you notice, the msg.payload is an array and it is empty, that is a little different than an empty payload. Doing a quick qoogle search finds https://stackoverflow.com/questions/44277505/what-is-the-best-procedure-to-check-an-array-is-empty-or-not-in-jquery

1 Like

This work for me. By check the length of array
if nothing inside length = 0
something inside length >= 1
correct?
ภาพ

3 Likes

That looks fine and if it works, it works :grin:

1 Like

Thank god... I found this stuff..

For me this worked as as the debug shows ''null'' and the default variable ''null'' does the job. When trying to match if there is msg.payload.length I got this ""TypeError: Cannot read property 'length' of null"".

if (msg.payload=== null) {
msg.payload = 0;
}
return msg;

1 Like