Another Sensor-to-Switch Thread

Problem:
How do I get my temperature setpoints to activate my switch? (thermostat operation) I have a feeling it’s something stupid to connect that last dot. I’ve just been staring at this for so long, it’s time to bounce it off of someone else’s head.

What am I trying to do?
Short: Write a flow to operate a switch based on temperature setpoint.
Long: Learn this whole IoT ‘thing’, and automate a greenhouse. Specifically, extract temperature and humidity data into a useful format, so I can automate the control of switches for fans, pumps, and other devices, and log all data, with the long-term goal of automating everything one would want to automate in a greenhouse.

What have I tried?
Seems like everything, but obviously not! LOL More background below. Sorry for the book, but I hope I was concise enough up-front that you only have to read down to the graphics.

What is the result of what I've tried?
I have a flow that picks up ALL of the data from a DHT11 (DHT3) with a parsed output. I have flows that successfully control wifi switches... finally. I seem to get a clean message from the sensor to a point at which the switch (SON1) would connect. Once I connect the switch, I get a “failure to parse JSON” error from the mqtt_subscribe node, and the switch does not respond. AAARRRRGHH!

Here's the flow that seems to get the operation all the way to the end:
image
image

When I connect the switch:
image

MQTT_subscription node:
image

Change node:
image

Software versions and configuration:
Raspbian 10 Buster
Mosquitto 1.6.8
Node-Red 1.0.5
Tasmota 8.2.0 ((tasmota.bin) Sonoff switches)
Tasmota 8.3.0 ((tasmota-sensors.bin (DHT3 only)

Hardware:
(1) Raspberry Pi 4 B, 4GB (Raspi)
(1) Sonoff 4CHAN Pro R2 (SON4CHAN)
(6) Sonoff Basic WiFi Switch (SON1, SON2, SON3,…)
(1) ESP8266MOD (NodeMCU – Amica) with DHT11 (DHT3)

Background:
Not a programmer, but trying to learn.
Not a Linux guy, but trying to learn.
I bought a handful of ESP8266 modules and DHT11 sensors. I managed to program them via Arduino IDE, and even got a few to publish data to adafruit io feeds. At this point, I’d never heard of MQTT. I was just excited that I got that far, having no programming or IoT experience. Just for fun, the public feed for my greenhouse temperature is still located here: https://io.adafruit.com/mighkel/feeds/dht1-overlay.dht1-temp

I stumbled on to Rui’s tutorials at RandomNerd Labs, and decided I wanted my own MQTT server. Following along, I was able to get my Raspi up and running with Mosquitto and Node-Red. I picked up a few Sonoff devices and an FTDI programmer, soldered on some headers, and managed to get all of the devices loaded up with a working Tasmota firmware. I also loaded one of the ESP8266/DHT11 packages with Tasmota, and it is reading temp/humidity/dewpoint.

After several days of not being able to connect the dots from Sonoff switches to Node-Red, I finally stumbled on to the right solution (server authentication) and can now control switches via Node-Red.

A few more days of trying to get the raw sensor data parsed into a useable msg format, I finally stumbled onto the “parsed JSON object” output setting on the mqtt sub node. A little (ok, a lot) more reading, and I got a little further including change node to move the message into a number object that I can use… in theory.

I'll try to write all of this up... haha, ok, since it's pretty much written up here, I'll start a thread in Projects topic to chronicle the whole greenhouse project from a total-newb point of view. :wink:

You need to set the topic to: cmnd/SON/power with a payload of ON/OFF, thus add a set msg.topic to....

People can help if you also paste the flow here (with some dummy data instead of your mqtt nodes), so that we can what is happening.

And a small tip, set the debug nodes output to complete msg object

To simplify your flow:

The change node looks like this (using JSONata), it will output ON if it higher than 16:

Example flow

[{"id":"8aca5766.617388","type":"inject","z":"5b57bfa8.a5f29","name":"tele/DHT3/SENSOR - 18 degrees","topic":"tele/DHT3/SENSOR","payload":"{\"DHT11\":{\"temperature\":18.5}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":490,"y":120,"wires":[["1d7d61c6.b04b9e"]]},{"id":"a1da0430.1d7c38","type":"debug","z":"5b57bfa8.a5f29","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":914,"y":144,"wires":[]},{"id":"1d7d61c6.b04b9e","type":"change","z":"5b57bfa8.a5f29","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"cmnd/SON/POWER","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"(payload.DHT11.temperature >16) ? \"ON\" : \"OFF\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":728,"y":144,"wires":[["a1da0430.1d7c38"]]},{"id":"cd18a642.b7f31","type":"inject","z":"5b57bfa8.a5f29","name":"tele/DHT3/SENSOR - 15 degrees","topic":"tele/DHT3/SENSOR","payload":"{\"DHT11\":{\"temperature\":15}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":490,"y":192,"wires":[["1d7d61c6.b04b9e"]]}]

For the json errors, you might want to check first if you receive a number then pass.

@bakman2 Thanks! And sorry, I meant to include the code, but got side-tracked.

So, in my 'round-a-bout' flow, simply plugging in "set msg.topic..." per your instructions made it all work correctly. Many thanks again for that!
Here's what the working flow looks like now:

[{"id":"64179d70.37f584","type":"tab","label":"Temp-to-Switch Lite","disabled":false,"info":""},{"id":"a6146e16.0e3c5","type":"mqtt in","z":"64179d70.37f584","name":"","topic":"tele/DHT3/SENSOR/#","qos":"2","datatype":"json","broker":"741d5b6a.d93184","x":120,"y":100,"wires":[["43b33431.c1f09c","3e60a5f9.880eaa"]]},{"id":"3e60a5f9.880eaa","type":"change","z":"64179d70.37f584","name":"Convert Temp","rules":[{"t":"set","p":"topic","pt":"msg","to":"cmnd/SON1/POWER","tot":"str"},{"t":"move","p":"payload.DHT11.Temperature","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":100,"wires":[["6f62f27b.16231c","18f30ac6.df62c5"]]},{"id":"f52d114.21704f","type":"debug","z":"64179d70.37f584","name":"Test OFF","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":800,"y":60,"wires":[]},{"id":"6f62f27b.16231c","type":"switch","z":"64179d70.37f584","name":"Setpoints","property":"payload","propertyType":"msg","rules":[{"t":"lte","v":"20","vt":"num"},{"t":"gt","v":"20","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":520,"y":100,"wires":[["f049f521.038298"],["6c10efe7.0b3d3"]]},{"id":"f049f521.038298","type":"change","z":"64179d70.37f584","name":"OFF","rules":[{"t":"set","p":"payload","pt":"msg","to":"OFF","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":80,"wires":[["f52d114.21704f","cb52fc6e.77601"]]},{"id":"6c10efe7.0b3d3","type":"change","z":"64179d70.37f584","name":"ON","rules":[{"t":"set","p":"payload","pt":"msg","to":"ON","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":120,"wires":[["90633418.e45e98","cb52fc6e.77601"]]},{"id":"90633418.e45e98","type":"debug","z":"64179d70.37f584","name":"Test ON","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":800,"y":160,"wires":[]},{"id":"cb52fc6e.77601","type":"mqtt out","z":"64179d70.37f584","name":"SON1","topic":"","qos":"","retain":"","broker":"741d5b6a.d93184","x":830,"y":100,"wires":[]},{"id":"43b33431.c1f09c","type":"debug","z":"64179d70.37f584","name":"Parsed Sensor Data","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":360,"y":160,"wires":[]},{"id":"18f30ac6.df62c5","type":"debug","z":"64179d70.37f584","name":"Converted Temp","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":540,"y":40,"wires":[]},{"id":"741d5b6a.d93184","type":"mqtt-broker","z":"","name":"YOUR_BROKER","broker":"111.111.111.111","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

I had mixed results playing with the flow you posted though. Having plugged in my sub & pub nodes, I got it turn on the switch at the setpoint, but couldn't get it to turn the switch off. I'll have to spend some time dinking with it until I understand what's going on there. It appears to be a much more efficient way than the flow I wrote to do it, if one can just work the code right. I have a bit to learn about json syntax though!

Since I'm excited about finally getting something to work, here's a flow that works with a Sonoff 4Chan R2 relay to ramp up/down fan speeds per various temperature ranges. I know it's not the most elegant (and probably not most effective) way to do it, but it was fun to go to a next step.

[{"id":"c08d4bd5.6e59b8","type":"tab","label":"Temp-to-Switch","disabled":false,"info":""},{"id":"add06c5c.a4478","type":"mqtt in","z":"c08d4bd5.6e59b8","name":"","topic":"tele/DHT3/SENSOR/#","qos":"2","datatype":"json","broker":"741d5b6a.d93184","x":120,"y":180,"wires":[["57a102dd.0a077c","735c8365.001bcc"]]},{"id":"57a102dd.0a077c","type":"change","z":"c08d4bd5.6e59b8","name":"Convert Temp","rules":[{"t":"set","p":"topic","pt":"msg","to":"cmnd/SON4CHAN","tot":"str"},{"t":"move","p":"payload.DHT11.Temperature","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":180,"wires":[["17c37f58.1c0b81"]]},{"id":"dbedf9ee.cea3f8","type":"debug","z":"c08d4bd5.6e59b8","name":"Temperature F","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":680,"y":140,"wires":[]},{"id":"17c37f58.1c0b81","type":"unit-converter","z":"c08d4bd5.6e59b8","category":"temperature","inputUnit":"C","outputUnit":"F","inputField":"payload","outputField":"payload","inputFieldType":"msg","outputFieldType":"msg","name":"C-to-F","x":510,"y":180,"wires":[["dbedf9ee.cea3f8","7b7d119a.a6d92"]]},{"id":"7b7d119a.a6d92","type":"switch","z":"c08d4bd5.6e59b8","name":"Setpoints","property":"payload","propertyType":"msg","rules":[{"t":"btwn","v":"0","vt":"num","v2":"50","v2t":"num"},{"t":"btwn","v":"50.01","vt":"num","v2":"60","v2t":"num"},{"t":"btwn","v":"60.01","vt":"num","v2":"70","v2t":"num"},{"t":"gt","v":"70.1","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":540,"y":300,"wires":[["6075b92f.2490f8","8df024b0.693298","a24a92db.8c643","c4065f02.c184f"],["9145984b.e92e48","8df024b0.693298","a24a92db.8c643","48f00e4b.0177a"],["6075b92f.2490f8","9145984b.e92e48","a24a92db.8c643","db4b657.dc84598"],["9145984b.e92e48","6075b92f.2490f8","8df024b0.693298","526521be.3a00f"]]},{"id":"9145984b.e92e48","type":"change","z":"c08d4bd5.6e59b8","name":"RELAY1 OFF","rules":[{"t":"set","p":"topic","pt":"msg","to":"cmnd/SON4CHAN/POWER1","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"OFF","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1100,"y":160,"wires":[["d9b5ae9c.1fc91"]]},{"id":"5c2e530b.b731dc","type":"change","z":"c08d4bd5.6e59b8","name":"RELAY1 ON","rules":[{"t":"set","p":"topic","pt":"msg","to":"cmnd/SON4CHAN/POWER1","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"ON","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1090,"y":200,"wires":[["d9b5ae9c.1fc91"]]},{"id":"d9b5ae9c.1fc91","type":"mqtt out","z":"c08d4bd5.6e59b8","name":"SON4CHAN","topic":"","qos":"2","retain":"true","broker":"741d5b6a.d93184","x":1390,"y":340,"wires":[]},{"id":"735c8365.001bcc","type":"debug","z":"c08d4bd5.6e59b8","name":"Parsed Sensor Data","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":360,"y":140,"wires":[]},{"id":"d29fcb00.8269c8","type":"inject","z":"c08d4bd5.6e59b8","name":"","topic":"cmnd/SON4CHAN/","payload":"55","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":280,"wires":[["7b7d119a.a6d92"]]},{"id":"b227547a.28c448","type":"inject","z":"c08d4bd5.6e59b8","name":"","topic":"cmnd/SON4CHAN/","payload":"65","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":320,"wires":[["7b7d119a.a6d92"]]},{"id":"58c10966.0a8608","type":"inject","z":"c08d4bd5.6e59b8","name":"","topic":"cmnd/SON4CHAN/","payload":"45","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":240,"wires":[["7b7d119a.a6d92"]]},{"id":"6075b92f.2490f8","type":"change","z":"c08d4bd5.6e59b8","name":"RELAY2 OFF","rules":[{"t":"set","p":"topic","pt":"msg","to":"cmnd/SON4CHAN/POWER2","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"OFF","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1100,"y":260,"wires":[["d9b5ae9c.1fc91"]]},{"id":"ba7e61cc.47da8","type":"change","z":"c08d4bd5.6e59b8","name":"RELAY2 ON","rules":[{"t":"set","p":"topic","pt":"msg","to":"cmnd/SON4CHAN/POWER2","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"ON","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1090,"y":300,"wires":[["d9b5ae9c.1fc91"]]},{"id":"8df024b0.693298","type":"change","z":"c08d4bd5.6e59b8","name":"RELAY3 OFF","rules":[{"t":"set","p":"topic","pt":"msg","to":"cmnd/SON4CHAN/POWER3","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"OFF","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1100,"y":360,"wires":[["d9b5ae9c.1fc91"]]},{"id":"422d86ec.058348","type":"change","z":"c08d4bd5.6e59b8","name":"RELAY3 ON","rules":[{"t":"set","p":"topic","pt":"msg","to":"cmnd/SON4CHAN/POWER3","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"ON","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1090,"y":400,"wires":[["d9b5ae9c.1fc91"]]},{"id":"a24a92db.8c643","type":"change","z":"c08d4bd5.6e59b8","name":"RELAY4 OFF","rules":[{"t":"set","p":"topic","pt":"msg","to":"cmnd/SON4CHAN/POWER4","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"OFF","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1100,"y":460,"wires":[["d9b5ae9c.1fc91"]]},{"id":"37cfb3b0.19df4c","type":"change","z":"c08d4bd5.6e59b8","name":"RELAY4 ON","rules":[{"t":"set","p":"topic","pt":"msg","to":"cmnd/SON4CHAN/POWER4","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"ON","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1090,"y":500,"wires":[["d9b5ae9c.1fc91"]]},{"id":"dadb3be.2f88cc8","type":"inject","z":"c08d4bd5.6e59b8","name":"","topic":"cmnd/SON4CHAN/","payload":"75","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":360,"wires":[["7b7d119a.a6d92"]]},{"id":"526521be.3a00f","type":"delay","z":"c08d4bd5.6e59b8","name":"delay 0.5s","pauseType":"delay","timeout":".5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":930,"y":500,"wires":[["37cfb3b0.19df4c"]]},{"id":"db4b657.dc84598","type":"delay","z":"c08d4bd5.6e59b8","name":"delay 0.5s","pauseType":"delay","timeout":".5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":930,"y":400,"wires":[["422d86ec.058348"]]},{"id":"48f00e4b.0177a","type":"delay","z":"c08d4bd5.6e59b8","name":"delay 0.5s","pauseType":"delay","timeout":".5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":930,"y":300,"wires":[["ba7e61cc.47da8"]]},{"id":"c4065f02.c184f","type":"delay","z":"c08d4bd5.6e59b8","name":"delay 0.5s","pauseType":"delay","timeout":".5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":930,"y":200,"wires":[["5c2e530b.b731dc"]]},{"id":"741d5b6a.d93184","type":"mqtt-broker","z":"","name":"YOUR_BROKER","broker":"111.111.111.111","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Please export your flow again, read this post and edit your post.

Sorry about that. I edited it and followed directions, but it still doesn't look right. How do you get it on one line like your example above?

It is key that you export your flow again (as it is already incorrectly formatted now), then edit your post and click this icon and past the code between the backticks.

three back tics, then your code gos onthe next line then on the next line three more back tics.

I edited it for you.

Thanks for the help and the patience guys!
I went ahead and re-exported the flows for both posts, and fixed post #3.

1 Like

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