Test of a sensor

#1

I would like to test if a sensor is working, my schematic I have is the following

what I intend is through a test function if the sensor 3 is working and showing a message saying ok, and when it is off to say no ok, the function I did was this:

var test=msg.payload;
if (test=== "OK"){
    return;
    
}else{
    return (" Sensor not working ");
}
return msg;

the msg.payload (is a number) the function output is as follows:

"Function tried to send a message of type string"

How can I solve the error?

0 Likes

#2

Best thing to do is to read a bit about basics first.
https://nodered.org/docs/user-guide/messages

1 Like

#3

As the error says you have returned a string ("sensor not working"). You must always return a message (or nothing). Perhaps you want to put the error string into msg.payload and then return msg. I also see that if it is OK you return nothing, which means no message will be sent, which I suspect is not what you want.

0 Likes

#4

I make this for testing the sensor 1 when it is at zero value and sensor 3 when it is at zero value:

if(msg.payload[0]===0){
    msg.payload = " Sensor 1 not ok";
}
else if(msg.payload[1]===0){
    msg.payload = " Sensor 2 is not ok";
}
else if(msg.payload[2]===0){
    msg.payload = " Sensor 3 is not ok";
}
else{
    msg.payload = " Sensors are ok";
}
return msg;

and return msg is :

sensor3 : msg.payload : string[16]
" Sensor 1 not ok"

don't show the return msg " Sensor 3 not ok" of sensor 3 when it is at zero value and is the sensor 3 is show the return message of sensor 1 !! I

0 Likes

#5

If it is returning sensor 1 not ok then that means that msg.payload[0] is 0. If that is not what is happening then show us what is in the message going in by putting a debug node to show it and expanding it in the debug pane to show all the contents.

[Edit] Note that if msg.payload[0] is zero then it does not test any further, so it will not test the other elements of the array. If you are trying to test all of them then what do you want to happen when more than one of them is zero?

0 Likes

#6

yes the msg.payload[0] is correct, I want is to run the Join array and detect the position of the array that is at zero and show the sensors that are at zero and in this case with sensor 1 and sensor 3 at zero only shows that the 1 is not ok

 08/02/2019, 16:38:33node: Output of state of sensor
    sensor3 : msg : Object
    object
    topic: "sensor3"
    payload: " Sensor 1 not ok"
    _msgid: "f86dc19a.905e5"

0 Likes

#7

Export the test flow by selecting the nodes and slicking Ctrl-E which will allow you to export to the clipboard, select Compact mode. Then you can paste it here for us to see exactly what you are doing. See this for how to format it so the forum formatting does not mess it up.

0 Likes

#8

the node of state function

[{"id":"2778a415.504f8c","type":"function","z":"ebbf8292.d6fd4","name":"State of Sensor","func":"if(msg.payload[0]===0){\n msg.payload = " Sensor 1 not ok";\n}\nif(msg.payload[1]===0){\n msg.payload = " Sensor 2 is not ok";\n}\nif(msg.payload[2]===0){\n msg.payload = " Sensor 3 is not ok";\n}\n\nreturn msg;","outputs":2,"noerr":0,"x":672.1001281738281,"y":203.00000286102295,"wires":[["1fec7822.d11a68","e740535a.cb834"],["27b816cd.b0bf6a"]]}]

the node of debug

[{"id":"27b816cd.b0bf6a","type":"debug","z":"ebbf8292.d6fd4","name":"Output of state of sensor3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":985.8000640869141,"y":313.00000381469727,"wires":[]}]

sensor 1
[{"id":"1be92342.c9d3fd","type":"inject","z":"ebbf8292.d6fd4","name":"","topic":"sensor1","payload":"0","payloadType":"num","repeat":"10","crontab":"","once":false,"onceDelay":0.1,"x":274.3000030517578,"y":65.00000476837158,"wires":[["9a3bc2da.a4121"]]}]

sensor 2
[{"id":"a4dcbec2.bbf95","type":"inject","z":"ebbf8292.d6fd4","name":"","topic":"sensor2","payload":"25","payloadType":"num","repeat":"10","crontab":"","once":false,"onceDelay":0.1,"x":291.1000061035156,"y":181.00000286102295,"wires":[["9a3bc2da.a4121"]]}]

sensor 3
[{"id":"634894b0.fdecec","type":"inject","z":"ebbf8292.d6fd4","name":"","topic":"sensor3","payload":"0","payloadType":"num","repeat":"10","crontab":"","once":false,"onceDelay":0.1,"x":284.1000061035156,"y":288.00000286102295,"wires":[["9a3bc2da.a4121"]]}]

0 Likes

#9

Select the set of nodes including the inject nodes so we can try it out. Then export it.

0 Likes

#10

I assume from the function that you are joining the messages together into an array. The problem with that is, how do you know which element is which sensor? You cannot guarantee the order they will arrive at the node. Instead you can specify joining to a key/value pair. Make sure each message coming in has the topic set to indicate where it comes from then the payload will include them all. Try it and look in a debug node to see what is there.

0 Likes

#11

The program show this:
[
{
"id": "8ee0a7b9.5a9af8",
"type": "debug",
"z": "ebbf8292.d6fd4",
"name": "Output of function",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"x": 740.1001243591309,
"y": 26.200007438659668,
"wires": []
},
{
"id": "2778a415.504f8c",
"type": "function",
"z": "ebbf8292.d6fd4",
"name": "State of Sensor",
"func": "if(msg.topic[0]===0){\n msg.topic = " Sensor 1 not ok";\n}\nif(msg.topic[1]===0){\n msg.topic = " Sensor 2 is not ok";\n}\nif(msg.topic[2]===0){\n msg.topic = " Sensor 3 is not ok";\n}\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 672.1001281738281,
"y": 203.00000286102295,
"wires": [
[
"1fec7822.d11a68",
"e740535a.cb834"
]
]
},
{
"id": "9a3bc2da.a4121",
"type": "join",
"z": "ebbf8292.d6fd4",
"name": "",
"mode": "custom",
"build": "object",
"property": "payload",
"propertyType": "msg",
"key": "topic",
"joiner": "\n",
"joinerType": "str",
"accumulate": false,
"timeout": "",
"count": "3",
"reduceRight": false,
"reduceExp": "",
"reduceInit": "",
"reduceInitType": "",
"reduceFixup": "",
"x": 503.1000061035156,
"y": 192.1999969482422,
"wires": [
[
"2778a415.504f8c",
"f6ef318b.063df"
]
]
},
{
"id": "1be92342.c9d3fd",
"type": "inject",
"z": "ebbf8292.d6fd4",
"name": "",
"topic": "sensor1",
"payload": "0",
"payloadType": "num",
"repeat": "10",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 274.3000030517578,
"y": 65.00000476837158,
"wires": [
[
"9a3bc2da.a4121"
]
]
},
{
"id": "a4dcbec2.bbf95",
"type": "inject",
"z": "ebbf8292.d6fd4",
"name": "",
"topic": "sensor2",
"payload": "25",
"payloadType": "num",
"repeat": "10",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 291.1000061035156,
"y": 181.00000286102295,
"wires": [
[
"9a3bc2da.a4121"
]
]
},
{
"id": "634894b0.fdecec",
"type": "inject",
"z": "ebbf8292.d6fd4",
"name": "",
"topic": "sensor3",
"payload": "0",
"payloadType": "num",
"repeat": "10",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 284.1000061035156,
"y": 288.00000286102295,
"wires": [
[
"9a3bc2da.a4121"
]
]
},
{
"id": "f6ef318b.063df",
"type": "debug",
"z": "ebbf8292.d6fd4",
"name": "Output of join",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"x": 588.8999938964844,
"y": 337.40000343322754,
"wires": []
},
{
"id": "e740535a.cb834",
"type": "debug",
"z": "ebbf8292.d6fd4",
"name": "Output of state of sensor",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"x": 939.1000633239746,
"y": 314.00000381469727,
"wires": []
},
{
"id": "1fec7822.d11a68",
"type": "function",
"z": "ebbf8292.d6fd4",
"name": "Function to send the data to database",
"func": "var newmsg=new Date().toString();\nmsg.topic= "INSERT INTO testebase VALUES ('"+new Date().toString()+"','"+msg.topic["sensor1"]+"','"+msg.topic["sensor2"]+"','"+msg.topic["sensor3"]+"')";\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 982.1001205444336,
"y": 203.20000076293945,
"wires": [
[
"8ee0a7b9.5a9af8",
"b5426519.0a4f88"
]
]
}
]

0 Likes

#12

Change the Output of join debug node to display the complete msg ogbect and you should discover what is going on.

0 Likes

#13

the output of join is this
[
{
"id": "f6ef318b.063df",
"type": "debug",
"z": "ebbf8292.d6fd4",
"name": "Output of join",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"x": 588.8999938964844,
"y": 337.40000343322754,
"wires": []
}
]

0 Likes

#14

that looks like and export the debug node - what I want you to look at is the debug sidebar. the output will be something like

2/8/2019, 3:42:00 PM  node: 71c70200.2c2588
msg : Object
object
 payload: object
    mains: 1
 _msgid: "d5d6da92.41c2f8"
0 Likes

#15

after the function of test the node the debug is

09/02/2019, 00:30:26[node: Output of function](http://192.168.1.10:1880/#)INSERT INTO testebase VALUES ('Sat Feb 09 2019 00:30:26 GMT+0000 (GMT)','undefined','undefined','undefined') : msg : Object

object

topic: "INSERT INTO testebase VALUES ('Sat Feb 09 2019 00:30:26 GMT+0000 (GMT)','undefined','undefined','undefined')"

payload: object

sensor1: 0

sensor2: 25

sensor3: 0

_msgid: "b88f150b.256c78"
0 Likes

#16

Your function node that generates the query is doing this:

msg.topic= "INSERT INTO testebase VALUES ('"+new Date().toString()+"','"+msg.topic["sensor1"]+"','"+msg.topic["sensor2"]+"','"+msg.topic["sensor3"]+")";

It is trying to pull the sensor values out of msg.topic. but the are not under msg.topic, they are under msg.payload

So where you use msg.topic["sensor1"] you should be doing msg.payload["sensor1"]

This is why the Debug node is so important in understanding the structure and content of the full message object you are passing around.

0 Likes

#17

Thanks one more time for helping me. It's working.

0 Likes