If condition with multiple payloads

hello. i had a flow to get temp and humidity data from mqtt and send it Firebase real time database,
i sent temp data and humidity data to firebase succesfully. and i want to use temp and humidity data for room is "relaxing" conditions or "stressful". For example, if temp above 35 and humidity above 70, room is "stressful". Else "relaxing".

my code is like that;

var temp = msg.payload.TEMP;
var hum = msg.payload.HUM;

if(temp>35 && hum>70)
{
       msg.payload = "Stressful";
}
else()
{
       msg.payload ="relaxing";
}

msg.topic="room condition";
return msg;

i saw the JSON in debug screen
msg.topic : "room condition", msg.payload :"relaxing"
when i try to add this to FireBase there is 2 errors like that,

"SyntaxError: Unexpected token m in JSON at position 0"

why that cause. What is the solution?

Without knowing how you tried to add someting - it is difficult to help. As your payload is not an object - I do not understand at all.

Why are you storing relaxing/stressful in the database?

Surely it's better to calculate that when you retrieve and use the data.

That way you can change the point at which it becomes stressful without worrying about existing records.

1 Like

thank you for reply.this first object from humidity sensor;

object
topic: "HUM"
payload: "62"
qos: 0
retain: false
_topic: "room/251221001/hum"
_msgid: "db3f95ca60113978"

this second object from temperature sensor;

object
topic: "TEMP"
payload: "35"
qos: 0
retain: false
_topic: "room/251221001/temp"
_msgid: "db3f95ca60113978"

i want to use both in function node. and want to get this result with use if temp belove 35 and humidity belove 70;

object
topic: "room condition"
payload: "relaxed"
qos: 0
retain: false
_topic: "room/251221001/hum"
_msgid: "db3f95ca60113978"

thank you for reply. it such an example that i wrote,there are 10 different contitions at orginal node. i also send sensor and condition data to MySQL for logging. after that use values from MySQL for how many times relaxed or stressed in a day or week.

What is the structure of the database table[s] where you store temperature and humidity?
Please show us your INSERT and SELECT statements.

As the temp and humdity are in separate messages you first need to combine them into one message, otherwise only one of them at a time is available in the function node (which only receives one message at a time). See this article in the cookbook for an example of how to join messages into one object.

thank you for reply. i already use join node. wait until 2 messages arrive and after function node run. i can define new topic, payload comes from if condition. debug show an object include topic, payload, msgid. but when i connected firebase, this message appeared;

"SyntaxError: Unexpected token m in JSON at position 0"

Your payload is not an object. You need to combine them not as topic and payload, you need a payload object with properties and perhaps as JSON string - which means you have to convert the object into a JSON string.

What do you see coming out of the join node and show us how it is configured

So do you mean that the problem is nothing to do with the if condition testing, but is purely a matter of writing the value to firebase?

excactly, debug is ok. but firebase not.

doesn't care that error, it is cause from different node :

"Error: Data truncated for column 'sicaklik' at row 1"

i think firebase not recognise object.

Those two errors are coming from different nodes. Which nodes are they coming from? If you click the node id it should take you to the relevant node, unless it is a config node.

Also which firebase node are you using and how have you configured it?


it's coming from firebase node. i use same firebase configuration at other sensor nodes for connect firebase successfully.

thank you for everyone. i found the problem. The problem is Firebase node that i used. I try another firebase node. it work successfully.

In order to help others who find this please tell us which node did not work for you and which node did.

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