# Conditional iptus for data from MQTT to inserst into a new database table

**URL:** <https://discourse.nodered.org/t/conditional-iptus-for-data-from-mqtt-to-inserst-into-a-new-database-table/71766>\
**Category:** General\
**Tags:** database, function-node, mqtt\
**Created:** [6 December 2022 14:10 UTC](https://discourse.nodered.org/t/conditional-iptus-for-data-from-mqtt-to-inserst-into-a-new-database-table/71766 "2022-12-06T14:10:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deh89](https://avatars.discourse-cdn.com/v4/letter/d/0ea827/32.png) [@Deh89](https://discourse.nodered.org/u/Deh89)\
**Post date:** [6 December 2022 14:10 UTC](https://discourse.nodered.org/t/conditional-iptus-for-data-from-mqtt-to-inserst-into-a-new-database-table/71766/1 "2022-12-06T14:10:20Z")

</div>

Hello community members,  
I'm not familiar with Node Red and javascript but I decided to start this challenge.  
I want to collect data from a MQTT and insert the data into a new posgresql database.  
This data has this structure below.

{  
"value":87829,  
"timestamp":1670331643594.0977,  
"localTime":"2022-12-06T13:00:43.594Z",  
"metadata":{  
"type":"num",  
"deviceId":"760FX\_CF60\_KBS5\_TILT\_SHIFT",  
"aliasName":"nContadorCiclos",  
"key":"nContadorCiclos",  
"variable":"nContadorCiclos",  
"id":"f3025051-a228-4a89-9a74-fbed4259deb7",  
"variableName":"nContadorCiclos",  
"task":"T\_ROB1",  
"module":"DynModel\_Validation\_760FX"  
}  
}

Basically with this node, I want to get the values from the data input for **value** , **LocalTime** , **variableName** and **deviceId** and return to another data called **demoprod760fx**. Later this new data will be inserted into a new database table.  
With the same MQTT topic I will get different but similar data where objects have same name so I want to use a function node to indentify and select only what I need and avoind to get wrong values. That's why I'm considering the payload verification for taks and module because they exist only in this data type.

I started this project using another example but seems it's not correct.

Please, see below what I'm doing below with this function node.

if (msg.payload.timestamp  
&& msg.payload.value  
&& msg.payload.metadata  
&& msg.payload.metadata.task  
&& msg.payload.metadata.module  
&& msg.payload.metadata.deviceId === '760FX\_CF60\_KBS5\_TILT\_SHIFT',  
&& msg.payload.metadata.variableName === 'nContadorCiclos' ) {  
return {  
demoprod760fx: {  
device\_id: msg.payload.metadata.deviceId,  
variable\_name: msg.payload.metadata.variableName,  
timestamp: msg.payload.localTime,  
value: (typeof msg.payload.value === 'object') ? JSON.stringify(msg.payload.value): msg.payload.value,  
}  
}  
}

return null;  
return msg;

The most important for my project is to define the condition to get data only if the deviceId is 760FX\_CF60\_KBS5\_TILT\_SHIFT and the variableName is nContadorCiclos. The other verifications is just to ensure that I'm collecting the right value from the right data type but it's secondary and probably reduntancy.

I will appreciate if you guys can help me to set the code for this node.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [6 December 2022 14:50 UTC](https://discourse.nodered.org/t/conditional-iptus-for-data-from-mqtt-to-inserst-into-a-new-database-table/71766/2 "2022-12-06T14:50:15Z")

</div>

If you remove the comma after **...TILT\_SHIFT'** then your function seems to work.

The message it creates looks like this  
 ![Untitled 3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f8436aaed55a1ce9bcc2c74427d81e4b7063a.jpeg)  
It's slightly unusual because it doesn't have msg.payload (so a debug node showing msg.payload gives "undefined"), but if that's what you need it's no problem.

You don't really need the **return null** line. You could delete this and **return msg**. If the data does not pass your validation checks the function would just end without emitting a message.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [4 February 2023 14:51 UTC](https://discourse.nodered.org/t/conditional-iptus-for-data-from-mqtt-to-inserst-into-a-new-database-table/71766/3 "2023-02-04T14:51:02Z")

</div>

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