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

**URL:** <https://discourse.nodered.org/t/how-to-make-condition-with-msg-payload-empty/19396>\
**Category:** General\
**Created:** [20 December 2019 14:43 UTC](https://discourse.nodered.org/t/how-to-make-condition-with-msg-payload-empty/19396 "2019-12-20T14:43:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yortz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/yortz/32/9480_2.png) [@Yortz](https://discourse.nodered.org/u/Yortz)\
**Post date:** [20 December 2019 14:43 UTC](https://discourse.nodered.org/t/how-to-make-condition-with-msg-payload-empty/19396/1 "2019-12-20T14:43:59Z")

</div>

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:)  
 ![ภาพ](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/5aa8654a3fd3dd2cebb7decc0caa06fd19c891e1.png)

```auto
[{"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"}]

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [20 December 2019 14:56 UTC](https://discourse.nodered.org/t/how-to-make-condition-with-msg-payload-empty/19396/2 "2019-12-20T14:56:15Z")

</div>

Try

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

```

---

<div class="post-metadata">

**Author:** ![Yortz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/yortz/32/9480_2.png) [@Yortz](https://discourse.nodered.org/u/Yortz)\
**Post date:** [20 December 2019 15:28 UTC](https://discourse.nodered.org/t/how-to-make-condition-with-msg-payload-empty/19396/3 "2019-12-20T15:28:29Z")

</div>

I test undefined and result as picture

 ![ภาพ](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/bf50eace9a2399b4f05083d8370c514217c5159e.png)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [20 December 2019 15:46 UTC](https://discourse.nodered.org/t/how-to-make-condition-with-msg-payload-empty/19396/4 "2019-12-20T15:46:04Z")

</div>

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](https://stackoverflow.com/questions/44277505/what-is-the-best-procedure-to-check-an-array-is-empty-or-not-in-jquery)

---

<div class="post-metadata">

**Author:** ![Yortz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/yortz/32/9480_2.png) [@Yortz](https://discourse.nodered.org/u/Yortz)\
**Post date:** [20 December 2019 16:59 UTC](https://discourse.nodered.org/t/how-to-make-condition-with-msg-payload-empty/19396/5 "2019-12-20T16:59:23Z")

</div>

This work for me. By check the length of array  
if nothing inside length = 0  
something inside length \>= 1  
correct?  
 ![ภาพ](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/a/a04f1665446ff659cb8ddf86b9981c0a69bac30f.png)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [20 December 2019 17:15 UTC](https://discourse.nodered.org/t/how-to-make-condition-with-msg-payload-empty/19396/6 "2019-12-20T17:15:00Z")

</div>

That looks fine and if it works, it works 😁

---

<div class="post-metadata">

**Author:** ![Pongpun364](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pongpun364/32/32388_2.png) [@Pongpun364](https://discourse.nodered.org/u/Pongpun364)\
**Post date:** [22 November 2020 08:18 UTC](https://discourse.nodered.org/t/how-to-make-condition-with-msg-payload-empty/19396/7 "2020-11-22T08:18:56Z")

</div>

Thank god... I found this stuff..

---

<div class="post-metadata">

**Author:** ![Nikolay](https://avatars.discourse-cdn.com/v4/letter/n/a698b9/32.png) [@Nikolay](https://discourse.nodered.org/u/Nikolay)\
**Post date:** [1 April 2021 12:55 UTC](https://discourse.nodered.org/t/how-to-make-condition-with-msg-payload-empty/19396/8 "2021-04-01T12:55:32Z")

</div>

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;
