Hello everyone pls could anyone help me cause I'm really stuck I have an esp8266 paired with dht11 it measured temperature in the room and send to a raspberry pi 3 through the mqtt broker my main problem is I want to creat a fonction in node red to on or off the air conditioner through a topic for example if it >= 30 it gonna send on in the topic.
I tried alot but cause I luck experience I didn't made it
The items needed to assist you is perhaps the content of the message to your function, then an understanding of what your function should do to provide the proper output(s).
If you do not have programming experience, It is possible that you can use an existing node(s) to do the logic, and therefore you could avoid making a function at all.
Hi Sheen - welcome to the forum.
What you are trying to do, should be fairly straightforward using Node-RED.
It sounds as if you have some parts working (e.g. capture temperature and sending it to Node-RED via MQTT). Is it possible for you to post your flow and to explain how the AC is controlled?
You might want to read how to share code on the forum.
[
{
"id": "c190fea3f98b97dc",
"type": "mqtt in",
"z": "a0d54b312f5d36d2",
"name": "Temperature",
"topic": "Room/temperature",
"qos": "2",
"datatype": "auto",
"broker": "5f6fbb28779bee8b",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 210,
"y": 400,
"wires": [
[
"def95c09b376f824",
"0e34ae5eadcf7d15",
"08ce2264729fdabf"
]
]
},
{
"id": "eec6b8bb687b16e0",
"type": "mqtt in",
"z": "a0d54b312f5d36d2",
"name": "Humidity",
"topic": "Room/humidity1",
"qos": "2",
"datatype": "auto",
"broker": "5f6fbb28779bee8b",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 200,
"y": 460,
"wires": [
[
"9b3d02c87cdc90d0"
]
]
},
{
"id": "def95c09b376f824",
"type": "ui_gauge",
"z": "a0d54b312f5d36d2",
"name": "temperature",
"group": "3c09afbebee86489",
"order": 0,
"width": 0,
"height": 0,
"gtype": "gage",
"title": "gauge",
"label": "°C",
"format": "{{value}}",
"min": 0,
"max": "100",
"colors": [
"#00b500",
"#e6e600",
"#ca3838"
],
"seg1": "26",
"seg2": "36",
"x": 550,
"y": 540,
"wires": []
},
{
"id": "9b3d02c87cdc90d0",
"type": "ui_gauge",
"z": "a0d54b312f5d36d2",
"name": "Humidity",
"group": "8008752501712c7a",
"order": 0,
"width": 0,
"height": 0,
"gtype": "gage",
"title": "gauge",
"label": "%",
"format": "{{value}}",
"min": 0,
"max": "100",
"colors": [
"#00b500",
"#e6e600",
"#ca3838"
],
"seg1": "60",
"seg2": "80",
"x": 540,
"y": 600,
"wires": []
},
{
"id": "21e9afc73aa2757e",
"type": "mqtt out",
"z": "a0d54b312f5d36d2",
"name": "lamp",
"topic": "Room/lamp",
"qos": "2",
"retain": "",
"respTopic": "",
"contentType": "",
"userProps": "",
"correl": "",
"expiry": "",
"broker": "5f6fbb28779bee8b",
"x": 1130,
"y": 280,
"wires": []
},
{
"id": "08ce2264729fdabf",
"type": "switch",
"z": "a0d54b312f5d36d2",
"name": "",
"property": "Room/temperature",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "17",
"vt": "num"
},
{
"t": "neq",
"v": "17",
"vt": "num"
}
],
"checkall": "true",
"repair": false,
"outputs": 2,
"x": 570,
"y": 420,
"wires": [
[
"a68ef52a3dd22e48"
],
[
"6daf8ab1263077d2"
]
]
},
{
"id": "a68ef52a3dd22e48",
"type": "trigger",
"z": "a0d54b312f5d36d2",
"name": "",
"op1": "on",
"op2": "",
"op1type": "str",
"op2type": "str",
"duration": "250",
"extend": false,
"overrideDelay": false,
"units": "ms",
"reset": "",
"bytopic": "all",
"topic": "topic",
"outputs": 1,
"x": 880,
"y": 360,
"wires": [
[
"21e9afc73aa2757e"
]
]
},
{
"id": "6daf8ab1263077d2",
"type": "trigger",
"z": "a0d54b312f5d36d2",
"name": "",
"op1": "off",
"op2": "0",
"op1type": "str",
"op2type": "str",
"duration": "250",
"extend": false,
"overrideDelay": false,
"units": "ms",
"reset": "",
"bytopic": "all",
"topic": "topic",
"outputs": 1,
"x": 880,
"y": 400,
"wires": [
[
"21e9afc73aa2757e"
]
]
},
{
"id": "f0da0c7c4c3dafa7",
"type": "debug",
"z": "a0d54b312f5d36d2",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1310,
"y": 460,
"wires": []
},
{
"id": "0e34ae5eadcf7d15",
"type": "function",
"z": "a0d54b312f5d36d2",
"name": "",
"func": "if (msg.payload >= 17){\n msg.payload = \"on\";\n}\nelse if (msg.payload <= 10){\n msg.payload = \"off\";\n}\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 640,
"y": 240,
"wires": [
[
"21e9afc73aa2757e",
"f0da0c7c4c3dafa7"
]
]
},
{
"id": "5f6fbb28779bee8b",
"type": "mqtt-broker",
"name": "",
"broker": "test.mosquitto.org",
"port": "1883",
"clientid": "",
"autoConnect": true,
"usetls": false,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willPayload": "",
"willMsg": {},
"sessionExpiry": ""
},
{
"id": "3c09afbebee86489",
"type": "ui_group",
"name": "temperature",
"tab": "eb133ec6a220a53e",
"order": 1,
"disp": true,
"width": "6",
"collapse": false
},
{
"id": "8008752501712c7a",
"type": "ui_group",
"name": "Humidity",
"tab": "eb133ec6a220a53e",
"order": 2,
"disp": true,
"width": "6",
"collapse": false
},
{
"id": "eb133ec6a220a53e",
"type": "ui_tab",
"name": "demo",
"icon": "dashboard",
"disabled": false,
"hidden": false
}
]
Thanks for ur attention I want from the nodered to take the data from mqtt protocol and with a fonction node ( for example if the temperature above 25 it should to turn on ) I tried alot of solutions but it didn't work for me cause I'm not that good in programming
I suggest you place a Debug node on the output of the MQTT-In node and post the results so we can see what the message payload looks like.
When I inject 60 the msg.payload show us on as I do in the fonction but when it came to work automatic I replace the relay with an led so I can try it without using a relay when I injected with a number it work fine but when it get the data from the sensor it just blinking
You need to find out what the incoming payload looks like as I suspect it could be something like...
msg.payload.temperature or msg.payload.humidity (rather than just msg.payload).
How have you programmed the ESP8266?
What language have you used?
Have you got control over how the MQTT message is formatted?
Looking at your flow, you have two paths that seem to be doing the same thing (that might be the reason the LED is blinking).
I suggest you disconnect the wire to the Switch node and see if the LED stops blinking.
Don't forget to Deploy the flow after you make any changes.
[
{
"id": "638956695a36d835",
"type": "tab",
"label": "Flow 2",
"disabled": false,
"info": "",
"env": []
},
{
"id": "e32f7a47c54dbbc8",
"type": "mqtt out",
"z": "638956695a36d835",
"name": "lamp",
"topic": "Room/lamp",
"qos": "2",
"retain": "",
"respTopic": "",
"contentType": "",
"userProps": "",
"correl": "",
"expiry": "",
"broker": "5f6fbb28779bee8b",
"x": 1170,
"y": 380,
"wires": []
},
{
"id": "eeb6600077f84f9c",
"type": "debug",
"z": "638956695a36d835",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 1110,
"y": 240,
"wires": []
},
{
"id": "a2dc4c11eb76e946",
"type": "trigger",
"z": "638956695a36d835",
"name": "",
"op1": "on",
"op2": "",
"op1type": "str",
"op2type": "nul",
"duration": "250",
"extend": false,
"overrideDelay": false,
"units": "ms",
"reset": "",
"bytopic": "all",
"topic": "topic",
"outputs": 1,
"x": 580,
"y": 440,
"wires": [
[
"765bf762495a0e80",
"cae701e15d303d62"
]
]
},
{
"id": "69dde581d1d21036",
"type": "switch",
"z": "638956695a36d835",
"name": "",
"property": "Room/lamp",
"propertyType": "msg",
"rules": [
{
"t": "lt",
"v": "15",
"vt": "num"
},
{
"t": "gt",
"v": "15",
"vt": "num"
}
],
"checkall": "true",
"repair": false,
"outputs": 2,
"x": 370,
"y": 360,
"wires": [
[
"a2dc4c11eb76e946"
],
[
"c20b07cdae459a50"
]
]
},
{
"id": "c20b07cdae459a50",
"type": "trigger",
"z": "638956695a36d835",
"name": "",
"op1": "off",
"op2": "",
"op1type": "str",
"op2type": "nul",
"duration": "250",
"extend": false,
"overrideDelay": false,
"units": "ms",
"reset": "",
"bytopic": "all",
"topic": "topic",
"outputs": 1,
"x": 580,
"y": 520,
"wires": [
[
"c9e829692ebe1f64",
"a4e28bf2ddf091f8"
]
]
},
{
"id": "765bf762495a0e80",
"type": "debug",
"z": "638956695a36d835",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 1130,
"y": 580,
"wires": []
},
{
"id": "c9e829692ebe1f64",
"type": "debug",
"z": "638956695a36d835",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 1070,
"y": 700,
"wires": []
},
{
"id": "cae701e15d303d62",
"type": "function",
"z": "638956695a36d835",
"name": "",
"func": "if (msg.topic===\"Room/lamp\"){\n msg.payload = \"on\";\n}\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 800,
"y": 440,
"wires": [
[
"e32f7a47c54dbbc8",
"765bf762495a0e80"
]
]
},
{
"id": "a4e28bf2ddf091f8",
"type": "function",
"z": "638956695a36d835",
"name": "",
"func": "if (msg.topic===\"Room/lamp\"){\n msg.payload = \"off\";\n}\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 760,
"y": 500,
"wires": [
[
"e32f7a47c54dbbc8"
]
]
},
{
"id": "6805627abf000c28",
"type": "mqtt in",
"z": "638956695a36d835",
"name": "",
"topic": "Room/temperature",
"qos": "2",
"datatype": "auto",
"broker": "5f6fbb28779bee8b",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 110,
"y": 360,
"wires": [
[
"69dde581d1d21036",
"eeb6600077f84f9c"
]
]
},
{
"id": "5f6fbb28779bee8b",
"type": "mqtt-broker",
"name": "",
"broker": "test.mosquitto.org",
"port": "1883",
"clientid": "",
"autoConnect": true,
"usetls": false,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willPayload": "",
"willMsg": {},
"sessionExpiry": ""
}
I made this and it works fine when I manual inject it with a number less or equal 15 like I set in the switch node and the topic receive the on cause if it <= 15 it gonna turn on
But when I plugged the shema with the mqtt temperature it doesn't do it work no on or off
The manual injection of a number should work as the logic in your flow looks fine.
You need to show us what the Debug node is showing (as I think that is where your problem is located).
Once we know what the message payload looks like we can modify your Switch node.
OK - I can see your msg.payload is a string.
e.g. " 20.64"
You need to convert that to an integer with parseInt() in a function node (so your Switch node will then work correctly).
You also need to adjust the Property in your Switch node to msg.payload
(as below).
[
{
"id": "c190fea3f98b97dc",
"type": "mqtt in",
"z": "a0d54b312f5d36d2",
"name": "Temperature",
"topic": "Room/temperature",
"qos": "2",
"datatype": "auto",
"broker": "5f6fbb28779bee8b",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 170,
"y": 460,
"wires": [
[
"def95c09b376f824"
]
]
},
{
"id": "eec6b8bb687b16e0",
"type": "mqtt in",
"z": "a0d54b312f5d36d2",
"name": "Humidity",
"topic": "Room/humidity1",
"qos": "2",
"datatype": "auto",
"broker": "5f6fbb28779bee8b",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 160,
"y": 540,
"wires": [
[
"9b3d02c87cdc90d0"
]
]
},
{
"id": "def95c09b376f824",
"type": "ui_gauge",
"z": "a0d54b312f5d36d2",
"name": "temperature",
"group": "3c09afbebee86489",
"order": 0,
"width": 0,
"height": 0,
"gtype": "gage",
"title": "gauge",
"label": "°C",
"format": "{{value}}",
"min": 0,
"max": "100",
"colors": [
"#00b500",
"#e6e600",
"#ca3838"
],
"seg1": "26",
"seg2": "36",
"x": 530,
"y": 500,
"wires": []
},
{
"id": "9b3d02c87cdc90d0",
"type": "ui_gauge",
"z": "a0d54b312f5d36d2",
"name": "Humidity",
"group": "8008752501712c7a",
"order": 0,
"width": 0,
"height": 0,
"gtype": "gage",
"title": "gauge",
"label": "%",
"format": "{{value}}",
"min": 0,
"max": "100",
"colors": [
"#00b500",
"#e6e600",
"#ca3838"
],
"seg1": "60",
"seg2": "80",
"x": 520,
"y": 560,
"wires": []
},
{
"id": "28126aa0241dcab6",
"type": "mqtt out",
"z": "a0d54b312f5d36d2",
"name": "lamp",
"topic": "Room/lamp",
"qos": "2",
"retain": "",
"respTopic": "",
"contentType": "",
"userProps": "",
"correl": "",
"expiry": "",
"broker": "5f6fbb28779bee8b",
"x": 1250,
"y": 380,
"wires": []
},
{
"id": "9442f29319be7b7b",
"type": "debug",
"z": "a0d54b312f5d36d2",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1130,
"y": 480,
"wires": []
},
{
"id": "07fc59ed4fb76861",
"type": "switch",
"z": "a0d54b312f5d36d2",
"name": ">=30",
"property": "Room/lamp",
"propertyType": "msg",
"rules": [
{
"t": "gte",
"v": "30",
"vt": "num"
}
],
"checkall": "true",
"repair": false,
"outputs": 1,
"x": 590,
"y": 360,
"wires": [
[
"2f50a23caf374951"
]
]
},
{
"id": "ab725b388ba81200",
"type": "debug",
"z": "a0d54b312f5d36d2",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1130,
"y": 280,
"wires": []
},
{
"id": "236baa36ce447bc9",
"type": "mqtt in",
"z": "a0d54b312f5d36d2",
"name": "",
"topic": "Room/temperature",
"qos": "2",
"datatype": "auto",
"broker": "5f6fbb28779bee8b",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 190,
"y": 380,
"wires": [
[
"71a1fa9121f614ec",
"a4c6e888c48e38d9",
"84b9231282920bd7"
]
]
},
{
"id": "2f50a23caf374951",
"type": "trigger",
"z": "a0d54b312f5d36d2",
"name": "",
"op1": "on",
"op2": "",
"op1type": "str",
"op2type": "nul",
"duration": "250",
"extend": false,
"overrideDelay": false,
"units": "ms",
"reset": "",
"bytopic": "all",
"topic": "topic",
"outputs": 1,
"x": 820,
"y": 360,
"wires": [
[
"28126aa0241dcab6",
"ab725b388ba81200"
]
]
},
{
"id": "3f1a1f81b2190d66",
"type": "trigger",
"z": "a0d54b312f5d36d2",
"name": "",
"op1": "off",
"op2": "",
"op1type": "str",
"op2type": "nul",
"duration": "250",
"extend": false,
"overrideDelay": false,
"units": "ms",
"reset": "",
"bytopic": "all",
"topic": "topic",
"outputs": 1,
"x": 820,
"y": 400,
"wires": [
[
"28126aa0241dcab6",
"9442f29319be7b7b"
]
]
},
{
"id": "78272e988e235602",
"type": "switch",
"z": "a0d54b312f5d36d2",
"name": "<=25",
"property": "Room/lamp",
"propertyType": "msg",
"rules": [
{
"t": "lte",
"v": "25",
"vt": "num"
}
],
"checkall": "true",
"repair": false,
"outputs": 1,
"x": 590,
"y": 400,
"wires": [
[
"3f1a1f81b2190d66"
]
]
},
{
"id": "bc1c9d1284158e95",
"type": "inject",
"z": "a0d54b312f5d36d2",
"name": "50",
"props": [
{
"p": "payload"
},
{
"p": "Room/lamp",
"v": "50",
"vt": "num"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 150,
"y": 200,
"wires": [
[
"07fc59ed4fb76861"
]
]
},
{
"id": "04fb816623e4b669",
"type": "inject",
"z": "a0d54b312f5d36d2",
"name": "30",
"props": [
{
"p": "payload"
},
{
"p": "Room/lamp",
"v": "20",
"vt": "num"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 150,
"y": 260,
"wires": [
[
"78272e988e235602"
]
]
},
{
"id": "71a1fa9121f614ec",
"type": "debug",
"z": "a0d54b312f5d36d2",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 470,
"y": 180,
"wires": []
},
{
"id": "a4c6e888c48e38d9",
"type": "function",
"z": "a0d54b312f5d36d2",
"name": "",
"func": "msg.payload = preseInt(msg.payload);\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 420,
"y": 360,
"wires": [
[
"07fc59ed4fb76861",
"61ca1aba53b133be"
]
]
},
{
"id": "84b9231282920bd7",
"type": "function",
"z": "a0d54b312f5d36d2",
"name": "",
"func": "msg.payload = preseInt(msg.payload);\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 420,
"y": 400,
"wires": [
[
"78272e988e235602"
]
]
},
{
"id": "61ca1aba53b133be",
"type": "debug",
"z": "a0d54b312f5d36d2",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 770,
"y": 160,
"wires": []
},
{
"id": "5f6fbb28779bee8b",
"type": "mqtt-broker",
"name": "",
"broker": "test.mosquitto.org",
"port": "1883",
"clientid": "",
"autoConnect": true,
"usetls": false,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willPayload": "",
"willMsg": {},
"sessionExpiry": ""
},
{
"id": "3c09afbebee86489",
"type": "ui_group",
"name": "temperature",
"tab": "eb133ec6a220a53e",
"order": 1,
"disp": true,
"width": "6",
"collapse": false
},
{
"id": "8008752501712c7a",
"type": "ui_group",
"name": "Humidity",
"tab": "eb133ec6a220a53e",
"order": 2,
"disp": true,
"width": "6",
"collapse": false
},
{
"id": "eb133ec6a220a53e",
"type": "ui_tab",
"name": "demo",
"icon": "dashboard",
"disabled": false,
"hidden": false
}
]
So basically when add the fonction mode gonna change it to number but nothing change
You need to alter the Property in the Switch node so it checks msg.payload.
Your flow seems to be working on my computer.
The switch property should I put the topic of the lamp or the mqtt temperature and I'm so sorry for destructing you
msg.payload
as that's where the temperature is being held.