DS18B20 Temperature Driving Wemo Switch

I’ve created an app that is taking the temperature from three DS18B20 sensors.

I want to use the temperature from one of these sensors to drive a Wemo plug.

My idea is that the temperature is <22 the Wemo plug turns on.

If the temperature is 23> the the plug turns off.

Does anyone know the best way to approach this?

Have you created a simple Wemo flow so that you can manually turn the switch on and off?

I would do that first.

Then how are you getting the sensor values? Direct on a Pi or something like an ESP8266?

If direct to the Pi, you can consume directly. If on an external microprocessor platform, you might want to look at using MQTT as an intermediary.

what does the wemo do if the temperature is between 22&23?

Take a look at flows.nodered.org where there are flows for the thermometer and controlling a wemo that you can import into Node-RED and study

Hiya,

The temp is being driven direct by the DS18B20.

Yes I have simple switch separate to the team flowwhoch turns theswit h on when I inject an actionable I know the switch is working.

No, I'm afraid it really isn't. That is just a temperature sensor, it has no intelligence. What is it connected to? That makes a difference about how you will take the data from it and pass it to Node-RED so that you can create a flow that changes the switch status.

Correct :slight_smile:
I'm using the sensor-ds18b20 node.

I'm trying to get the function node to give me the switching capability, however my programming sucks.... This is how far I've got so far....

[{“id”:“88d60841.fe09a8”,“type”:“function”,“z”:“2bbdde75.4c3d22”,“name”:“Temp Switch”,“func”:“if (msg.payload) number = 22.9999;\nreturn msg.payload ;‘on’;\nif (msg.payload) number = 23.000;\nreturn off;”,“outputs”:“2”,“noerr”:0,“x”:483.0663070678711,“y”:171.8724822998047,“wires”:[[],[“ad832b46.ab7b58”]]},{“id”:“6a4bc351.1ae7cc”,“type”:“inject”,“z”:“2bbdde75.4c3d22”,“name”:"",“topic”:"",“payload”:"",“payloadType”:“date”,“repeat”:“1”,“crontab”:"",“once”:false,“x”:55,“y”:278.8209114074707,“wires”:[[“c5ce5445.fd8928”,“26a3c3c3.d8674c”,“ad7b657.8c5b598”]]},{“id”:“302ba024.8414”,“type”:“debug”,“z”:“2bbdde75.4c3d22”,“name”:“Head Temperature”,“active”:true,“console”:“false”,“complete”:“payload”,“x”:498.3384094238281,“y”:20.17958354949951,“wires”:[]},{“id”:“59e6bb15.abe9a4”,“type”:“debug”,“z”:“2bbdde75.4c3d22”,“name”:“Condenser Temperature”,“active”:true,“console”:“false”,“complete”:“payload”,“x”:542.1951141357422,“y”:224.11061763763428,“wires”:[]},{“id”:“b856b0ef.1d183”,“type”:“debug”,“z”:“2bbdde75.4c3d22”,“name”:“Parrot Temperature”,“active”:true,“console”:“false”,“complete”:“payload”,“x”:533.8943710327148,“y”:409.8540668487549,“wires”:[]},{“id”:“c5ce5445.fd8928”,“type”:“sensor-ds18b20”,“z”:“2bbdde75.4c3d22”,“name”:“Head Temperature”,“sensorid”:“28-0417032fa4ff”,“timer”:“1”,“repeat”:false,“x”:252.3289031982422,“y”:79.84571647644043,“wires”:[[“302ba024.8414”,“2d1455b6.d6265a”,“edbc5381.84158”,“88d60841.fe09a8”]]},{“id”:“26a3c3c3.d8674c”,“type”:“sensor-ds18b20”,“z”:“2bbdde75.4c3d22”,“name”:“Condenser Temperature”,“sensorid”:“28-0517025602ff”,“timer”:“1”,“repeat”:false,“x”:286.446044921875,“y”:277.4435920715332,“wires”:[[“59e6bb15.abe9a4”,“73bfe4d6.f7c2cc”,“f8759e9a.e1bf”]]},{“id”:“ad7b657.8c5b598”,“type”:“sensor-ds18b20”,“z”:“2bbdde75.4c3d22”,“name”:“Parrot Temperature”,“sensorid”:“28-0517020dd1ff”,“timer”:“1”,“repeat”:false,“x”:270.7387237548828,“y”:465.9509506225586,“wires”:[[“b856b0ef.1d183”,“2e019082.cef81”,“e0da7d51.0f864”]]},{“id”:“2d1455b6.d6265a”,“type”:“ui_chart”,“z”:“2bbdde75.4c3d22”,“name”:“Head Temperature”,“group”:“de1b7ef0.5f4a”,“order”:2,“width”:0,“height”:0,“label”:“Head Temperature”,“chartType”:“line”,“legend”:“false”,“xformat”:“HH:mm:ss”,“interpolate”:“linear”,“nodata”:"",“dot”:false,“ymin”:"",“ymax”:"",“removeOlder”:1,“removeOlderPoints”:"",“removeOlderUnit”:“3600”,“cutout”:0,“useOneColor”:false,“colors”:["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],“x”:500.0970916748047,“y”:123.76178359985352,“wires”:[[],[]]},{“id”:“73bfe4d6.f7c2cc”,“type”:“ui_gauge”,“z”:“2bbdde75.4c3d22”,“name”:“Condenser Temperature”,“group”:“9025e422.27f758”,“order”:0,“width”:0,“height”:0,“gtype”:“gage”,“title”:“Gauge”,“label”:“units”,“format”:"{{value}}",“min”:0,“max”:“30”,“colors”:["#00ffff","#ffff00","#ff0000"],“seg1”:“10”,“seg2”:“20”,“x”:544.1988525390625,“y”:277.54280853271484,“wires”:[]},{“id”:“f8759e9a.e1bf”,“type”:“ui_chart”,“z”:“2bbdde75.4c3d22”,“name”:“Condenser Temperature”,“group”:“9025e422.27f758”,“order”:0,“width”:0,“height”:0,“label”:“Condenser Temperature”,“chartType”:“line”,“legend”:“false”,“xformat”:“HH:mm:ss”,“interpolate”:“linear”,“nodata”:"",“dot”:false,“ymin”:"",“ymax”:"",“removeOlder”:1,“removeOlderPoints”:"",“removeOlderUnit”:“3600”,“cutout”:0,“useOneColor”:false,“colors”:["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],“x”:544.4769897460938,“y”:328.4635772705078,“wires”:[[],[]]},{“id”:“2e019082.cef81”,“type”:“ui_gauge”,“z”:“2bbdde75.4c3d22”,“name”:“Parrot Temperature”,“group”:“8008d62a.5f8d88”,“order”:0,“width”:0,“height”:0,“gtype”:“gage”,“title”:“Gauge”,“label”:“units”,“format”:"{{value}}",“min”:0,“max”:“30”,“colors”:["#00ffff","#00ff00","#ff0000"],“seg1”:“10”,“seg2”:“20”,“x”:534.7219467163086,“y”:464.90016746520996,“wires”:[]},{“id”:“e0da7d51.0f864”,“type”:“ui_chart”,“z”:“2bbdde75.4c3d22”,“name”:“Parrot Temperature”,“group”:“8008d62a.5f8d88”,“order”:0,“width”:0,“height”:0,“label”:“Parrot Temperature”,“chartType”:“line”,“legend”:“false”,“xformat”:“HH:mm:ss”,“interpolate”:“linear”,“nodata”:"",“dot”:false,“ymin”:"",“ymax”:"",“removeOlder”:1,“removeOlderPoints”:"",“removeOlderUnit”:“3600”,“cutout”:0,“useOneColor”:false,“colors”:["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],“x”:532.7218780517578,“y”:515.6596508026123,“wires”:[[],[]]},{“id”:“edbc5381.84158”,“type”:“ui_gauge”,“z”:“2bbdde75.4c3d22”,“name”:“Head Temperature”,“group”:“de1b7ef0.5f4a”,“order”:1,“width”:0,“height”:0,“gtype”:“gage”,“title”:“Gauge”,“label”:“units”,“format”:"{{value}}",“min”:0,“max”:“100”,“colors”:["#00ffff","#00ff00","#ff0000"],“seg1”:“88”,“seg2”:“92”,“x”:500.0549087524414,“y”:71.61585235595703,“wires”:[]},{“id”:“ad832b46.ab7b58”,“type”:“wemo out”,“z”:“2bbdde75.4c3d22”,“name”:"",“device”:“d9dc8b49.b60538”,“label”:“Wemo Switch”,“x”:1823.982566833496,“y”:390.965838432312,“wires”:[]},{“id”:“5193838a.7578ec”,“type”:“inject”,“z”:“2bbdde75.4c3d22”,“name”:"",“topic”:"",“payload”:“on”,“payloadType”:“str”,“repeat”:"",“crontab”:"",“once”:false,“x”:322.02071380615234,“y”:584.9146347045898,“wires”:[[“ad832b46.ab7b58”]]},{“id”:“6f83d568.5d229c”,“type”:“inject”,“z”:“2bbdde75.4c3d22”,“name”:"",“topic”:"",“payload”:“off”,“payloadType”:“str”,“repeat”:"",“crontab”:"",“once”:false,“x”:317.3670959472656,“y”:658.6856870651245,“wires”:[[“ad832b46.ab7b58”]]},{“id”:“de1b7ef0.5f4a”,“type”:“ui_group”,“z”:"",“name”:“Head Temperature”,“tab”:“6a3d4b78.615284”,“disp”:true,“width”:“6”},{“id”:“9025e422.27f758”,“type”:“ui_group”,“z”:"",“name”:“Condenser Temperature”,“tab”:“6a3d4b78.615284”,“disp”:true,“width”:“6”},{“id”:“8008d62a.5f8d88”,“type”:“ui_group”,“z”:"",“name”:“Parrot Temperature”,“tab”:“6a3d4b78.615284”,“disp”:true,“width”:“6”},{“id”:“d9dc8b49.b60538”,“type”:“wemo-dev”,“z”:"",“device”:“221713K1100986”,“name”:“Wemo Switch”},{“id”:“6a3d4b78.615284”,“type”:“ui_tab”,“z”:"",“name”:“Home”,“icon”:“dashboard”}]

The function you posted isn’t written in javascript.
If you wish to do this in a function I would start by reading one of the many javascript tutorials online(i still read the w3schools pages when i get stuck.

if (hour < 18) {
msg.payload = “Good day”;
}

The second thing is when you return in node-red you need to return the entire msg object.
Take a look at the documentation on the website about writing functions

Thanks, still stuck as my coding knowledge is lacking and dining it hard to locate examples which match my requirements so.fimdimg it difficult to test…

Just run this through an online editor…

if (number < 22.99) {
msg.payload = “on”;
}
if (number > 22.98) {
msg.payload = “off”;
}

Hopefully this will work… I’m trying with baby steps :+1:

What do you expect to happen if number = 22.985 ?

Between 0->22.98 = On
Over 22.99 = Off

Maybe I will.chamge to three decamal places!

alternatively you could make your logic cover all numbers…
less than
greater than or equal to …

Well I’m still struggling to get this to work, now receiving errors in my JavaScript

[{“id”:“6a4bc351.1ae7cc”,“type”:“inject”,“z”:“2bbdde75.4c3d22”,“name”:"",“topic”:"",“payload”:"",“payloadType”:“date”,“repeat”:“1”,“crontab”:"",“once”:false,“x”:193.44049072265625,“y”:281.14111328125,“wires”:[[“c5ce5445.fd8928”,“26a3c3c3.d8674c”,“ad7b657.8c5b598”]]},{“id”:“302ba024.8414”,“type”:“debug”,“z”:“2bbdde75.4c3d22”,“name”:“Head Temperature”,“active”:true,“console”:“false”,“complete”:“payload”,“x”:622.2900390625,“y”:20,“wires”:[]},{“id”:“59e6bb15.abe9a4”,“type”:“debug”,“z”:“2bbdde75.4c3d22”,“name”:“Condenser Temperature”,“active”:true,“console”:“false”,“complete”:“payload”,“x”:666.1467437744141,“y”:223.93103408813477,“wires”:[]},{“id”:“b856b0ef.1d183”,“type”:“debug”,“z”:“2bbdde75.4c3d22”,“name”:“Parrot Temperature”,“active”:true,“console”:“false”,“complete”:“payload”,“x”:657.8460006713867,“y”:409.67448329925537,“wires”:[]},{“id”:“c5ce5445.fd8928”,“type”:“sensor-ds18b20”,“z”:“2bbdde75.4c3d22”,“name”:“Head Temperature”,“sensorid”:“28-0417032fa4ff”,“timer”:“1”,“repeat”:false,“x”:376.28053283691406,“y”:79.66613292694092,“wires”:[[“302ba024.8414”,“2d1455b6.d6265a”,“edbc5381.84158”,“88d60841.fe09a8”]]},{“id”:“26a3c3c3.d8674c”,“type”:“sensor-ds18b20”,“z”:“2bbdde75.4c3d22”,“name”:“Condenser Temperature”,“sensorid”:“28-0517025602ff”,“timer”:“1”,“repeat”:false,“x”:410.3976745605469,“y”:277.2640085220337,“wires”:[[“59e6bb15.abe9a4”,“73bfe4d6.f7c2cc”,“f8759e9a.e1bf”]]},{“id”:“ad7b657.8c5b598”,“type”:“sensor-ds18b20”,“z”:“2bbdde75.4c3d22”,“name”:“Parrot Temperature”,“sensorid”:“28-0517020dd1ff”,“timer”:“1”,“repeat”:false,“x”:394.6903533935547,“y”:465.7713670730591,“wires”:[[“b856b0ef.1d183”,“2e019082.cef81”,“e0da7d51.0f864”]]},{“id”:“2d1455b6.d6265a”,“type”:“ui_chart”,“z”:“2bbdde75.4c3d22”,“name”:“Head Temperature”,“group”:“de1b7ef0.5f4a”,“order”:2,“width”:0,“height”:0,“label”:“Head Temperature”,“chartType”:“line”,“legend”:“false”,“xformat”:“HH:mm:ss”,“interpolate”:“linear”,“nodata”:"",“dot”:false,“ymin”:"",“ymax”:"",“removeOlder”:1,“removeOlderPoints”:"",“removeOlderUnit”:“3600”,“cutout”:0,“useOneColor”:false,“colors”:["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],“x”:624.0487213134766,“y”:123.582200050354,“wires”:[[],[]]},{“id”:“73bfe4d6.f7c2cc”,“type”:“ui_gauge”,“z”:“2bbdde75.4c3d22”,“name”:“Condenser Temperature”,“group”:“9025e422.27f758”,“order”:0,“width”:0,“height”:0,“gtype”:“gage”,“title”:“Gauge”,“label”:“units”,“format”:"{{value}}",“min”:0,“max”:“30”,“colors”:["#00ffff","#ffff00","#ff0000"],“seg1”:“10”,“seg2”:“20”,“x”:668.1504821777344,“y”:277.36322498321533,“wires”:[]},{“id”:“f8759e9a.e1bf”,“type”:“ui_chart”,“z”:“2bbdde75.4c3d22”,“name”:“Condenser Temperature”,“group”:“9025e422.27f758”,“order”:0,“width”:0,“height”:0,“label”:“Condenser Temperature”,“chartType”:“line”,“legend”:“false”,“xformat”:“HH:mm:ss”,“interpolate”:“linear”,“nodata”:"",“dot”:false,“ymin”:"",“ymax”:"",“removeOlder”:1,“removeOlderPoints”:"",“removeOlderUnit”:“3600”,“cutout”:0,“useOneColor”:false,“colors”:["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],“x”:668.4286193847656,“y”:328.2839937210083,“wires”:[[],[]]},{“id”:“2e019082.cef81”,“type”:“ui_gauge”,“z”:“2bbdde75.4c3d22”,“name”:“Parrot Temperature”,“group”:“8008d62a.5f8d88”,“order”:0,“width”:0,“height”:0,“gtype”:“gage”,“title”:“Gauge”,“label”:“units”,“format”:"{{value}}",“min”:0,“max”:“30”,“colors”:["#00ffff","#00ff00","#ff0000"],“seg1”:“10”,“seg2”:“20”,“x”:658.6735763549805,“y”:464.72058391571045,“wires”:[]},{“id”:“e0da7d51.0f864”,“type”:“ui_chart”,“z”:“2bbdde75.4c3d22”,“name”:“Parrot Temperature”,“group”:“8008d62a.5f8d88”,“order”:0,“width”:0,“height”:0,“label”:“Parrot Temperature”,“chartType”:“line”,“legend”:“false”,“xformat”:“HH:mm:ss”,“interpolate”:“linear”,“nodata”:"",“dot”:false,“ymin”:"",“ymax”:"",“removeOlder”:1,“removeOlderPoints”:"",“removeOlderUnit”:“3600”,“cutout”:0,“useOneColor”:false,“colors”:["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],“x”:656.6735076904297,“y”:515.4800672531128,“wires”:[[],[]]},{“id”:“edbc5381.84158”,“type”:“ui_gauge”,“z”:“2bbdde75.4c3d22”,“name”:“Head Temperature”,“group”:“de1b7ef0.5f4a”,“order”:1,“width”:0,“height”:0,“gtype”:“gage”,“title”:“Gauge”,“label”:“units”,“format”:"{{value}}",“min”:0,“max”:“100”,“colors”:["#00ffff","#00ff00","#ff0000"],“seg1”:“88”,“seg2”:“92”,“x”:624.0065383911133,“y”:71.43626880645752,“wires”:[]},{“id”:“ad832b46.ab7b58”,“type”:“wemo out”,“z”:“2bbdde75.4c3d22”,“name”:"",“device”:“d9dc8b49.b60538”,“label”:“Wemo Switch”,“x”:1947.934196472168,“y”:390.7862548828125,“wires”:[]},{“id”:“5193838a.7578ec”,“type”:“inject”,“z”:“2bbdde75.4c3d22”,“name”:"",“topic”:"",“payload”:“on”,“payloadType”:“str”,“repeat”:"",“crontab”:"",“once”:false,“x”:445.9723434448242,“y”:584.7350511550903,“wires”:[[“ad832b46.ab7b58”]]},{“id”:“6f83d568.5d229c”,“type”:“inject”,“z”:“2bbdde75.4c3d22”,“name”:"",“topic”:"",“payload”:“off”,“payloadType”:“str”,“repeat”:"",“crontab”:"",“once”:false,“x”:441.3187255859375,“y”:658.506103515625,“wires”:[[“ad832b46.ab7b58”]]},{“id”:“88d60841.fe09a8”,“type”:“function”,“z”:“2bbdde75.4c3d22”,“name”:“Temp Switch”,“func”:“if (number < 22) {\nmsg.payload = “on”;\n}\nif (number > 22) {\nmsg.payload = “off”;\n}”,“outputs”:“2”,“noerr”:0,“x”:607.017936706543,“y”:171.69289875030518,“wires”:[[],[“ad832b46.ab7b58”]]},{“id”:“de1b7ef0.5f4a”,“type”:“ui_group”,“z”:"",“name”:“Head Temperature”,“tab”:“6a3d4b78.615284”,“disp”:true,“width”:“6”},{“id”:“9025e422.27f758”,“type”:“ui_group”,“z”:"",“name”:“Condenser Temperature”,“tab”:“6a3d4b78.615284”,“disp”:true,“width”:“6”},{“id”:“8008d62a.5f8d88”,“type”:“ui_group”,“z”:"",“name”:“Parrot Temperature”,“tab”:“6a3d4b78.615284”,“disp”:true,“width”:“6”},{“id”:“d9dc8b49.b60538”,“type”:“wemo-dev”,“z”:"",“device”:“221713K1100986”,“name”:“Wemo Switch”},{“id”:“6a3d4b78.615284”,“type”:“ui_tab”,“z”:"",“name”:“Home”,“icon”:“dashboard”}]

The error says 'number is not defined' and points you at the Temp Switch function. Looking at its code, you have:

Where does number come from? I guess it is being passed in from the previous node as msg.payload - so you should use that rather than number.

Also, your Function doesn't return anything, so no message will be sent to the next node.

if (msg.payload <  22) {
   msg.payload = “on”;
} else if (msg.payload > 22) {
   msg.payload = “off”;
}

As Mark said previously, you also need to decide which of those if statements to change so it includes the value 22 itself - so either <= 22 or >= 22

Nick

Sorry guys I'm lost, coding is just going above my head and confusing me more. Everyone a touch the thing I'm getting more errors. Maybe simple for everyone else but I'm going around in circles! If I could see where it was going wrong but it's just not making sense to me.

Hi,

did you try the code I suggested? I admit it is missing a return statement at the end - so for completeness:

if (msg.payload <  22) {
   msg.payload = “on”;
} else if (msg.payload > 22) {
   msg.payload = “off”;
}
return msg;

Thank you! It worked…

I am having a issue though, the code seems to turn the WeMo switch on and the when the temperature gets to 25 it turns off as programmed however when the temp drops below 24 it won’t turn on.

It’s almost like it isn’t refreshing. I’m also noticing that quite often the dashboard I have running as well will freeze? Any suggestions?