Activate switch based on humidity

I'm at the beginning of my Node-RED journey.

I have successfully installed a 4 gang smart switch (Deta switch running Tasmota) - device A - that's now controllable via Google Assistant (Nora in Node-RED). One of the switches controls a fan.

I have another device (device B) also running Tasmota measuring temp & humidity in the same room. I now want to use the humidity as an indicator to turn on the fan given humidity rises when you shower :slight_smile:

Device A is MQTT enabled so here's what I'd like to do:

  • Sample the humidity of device B based on the default telemetry interval of Tasmota
  • If humidity of device B > threshold turn on Fan connected to device A via MQTT and run the fan for a given time (as opposed to measuring drop humidity)

Trouble I now have is accessing humidity of device B. The MQTT in node provides a string containing

{"Time":"2020-06-13T05:39:40","HTU21":{"Temperature":18.5,"Humidity":53.8,"DewPoint":9.0},"TempUnit":"C"}

and I need to access the humidity. Do I need to convert this string to a Json object to able to extract this? Can I use a switch node?

What's the easiest way to get this done?

I am guessing it will be visible as msg.Humidity

Eventually as msg.HTU21.Humidity

The mqtt node can output json as well, check the output option.

To be pedantic it converts the JSON (string) coming in via mqtt from JSON to a javascript object (which isn't JSON).
If you don't select the parsed json option then it outputs JSON (ie the string), if you select the option then it outputs a javascript object instead, which is what you generally want.

@tfmeier Thomas, a little helpful hint

A simple way to determine the path to Humidity would be to add an inject connected to a debug node. In the inject select {} JSON and add that data. Deploy and press the inject button.

In the debug output you will see:
Screen Shot 2020-06-13 at 4.48.19 AM
click the triangle to open it up and you will see:
Screen Shot 2020-06-13 at 4.48.30 AM
click the triangle next to 'HTU21 and you get:
Screen Shot 2020-06-13 at 4.48.40 AM
When you move your cursor over 'Humidity' three icons will show on the right side:
Screen Shot 2020-06-13 at 4.48.54 AM
and the one >_ ("Copy path") will copy the path to that itme in the object. In this case it is payload.HTU21.Humidity

You could also have added a debug node to the mqtt-out node and done the same thing - if the mqtt node was set to return an object.

Thanks. Didn't know you can find out the path like that

This and many other really useful tips can be gleaned by reading the docs.

I also recommend watching this playlist: Node-RED Essentials. It is by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in less than an hour. A small investment for a lot of gain.

3 Likes

Thanks for your rich input. There's so much to learn from this forum but also Node-RED as a tool to process data from various devices/sensors around the house.

Have gotten this to work by setting the output of the MQTT In node to 'a parsed Json object' into a switch node to access the humidity property and either turning the fan on (MQTT Out) of off (MQTT Out) based on a threshold.

Solves me one challenge I had with my wife as I always forget to switch on the fan after a shower :slight_smile:

[{"id":"ce06233a.542c5","type":"comment","z":"69ade805.b33b18","name":"Logic to manage ensuite fan","info":"# Logic used to turn on/off the heater and fan in the ensuite\n\n## Fan\n - Controlled via humidity\n - Default state = off\n - If humidity > 'x%' this means someone's taking a shower which requires the fan to be turned on\n - One humidity reaches x% the fan runs for a given time 't' before switching off (this is in contrast to running until the humidity has dropped to 'y%')","x":140,"y":358,"wires":[]},{"id":"97c56f56.c0003","type":"switch","z":"69ade805.b33b18","name":"Check humidity level","property":"payload.HTU21.Humidity","propertyType":"msg","rules":[{"t":"gte","v":"65","vt":"num"},{"t":"lte","v":"60","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":400,"y":399,"wires":[["4bdaa54b.92895c"],["e5fd3f4d.990d9"]]},{"id":"208d2050.510a","type":"debug","z":"69ade805.b33b18","name":"Debug Off","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":630,"y":479,"wires":[]},{"id":"5866b95.8240048","type":"mqtt out","z":"69ade805.b33b18","name":"","topic":"cmnd/tasmota-CFC67A/POWER4","qos":"","retain":"","broker":"354b0974.633fa6","x":700,"y":359,"wires":[]},{"id":"4bdaa54b.92895c","type":"change","z":"69ade805.b33b18","name":"Turn on the fan","rules":[{"t":"set","p":"payload","pt":"msg","to":"ON","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":359,"wires":[["5866b95.8240048","467ab7c5.3b8738"]]},{"id":"376a91c3.8df0ae","type":"mqtt in","z":"69ade805.b33b18","name":"","topic":"tele/tasmota_0061EA/SENSOR","qos":"0","datatype":"json","broker":"354b0974.633fa6","x":150,"y":399,"wires":[["97c56f56.c0003"]]},{"id":"e5fd3f4d.990d9","type":"change","z":"69ade805.b33b18","name":"Turn off the fan","rules":[{"t":"set","p":"payload","pt":"msg","to":"OFF","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":439,"wires":[["a25cafa0.6b9f6","208d2050.510a"]]},{"id":"a25cafa0.6b9f6","type":"mqtt out","z":"69ade805.b33b18","name":"","topic":"cmnd/tasmota-CFC67A/POWER4","qos":"","retain":"","broker":"354b0974.633fa6","x":700,"y":439,"wires":[]},{"id":"467ab7c5.3b8738","type":"debug","z":"69ade805.b33b18","name":"Debug On","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":630,"y":319,"wires":[]},{"id":"354b0974.633fa6","type":"mqtt-broker","z":"","name":"AWS_Broker","broker":"a1mpax8uhmvady-ats.iot.us-east-1.amazonaws.com","port":"8883","tls":"9425efda.027e4","clientid":"","usetls":true,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"9425efda.027e4","type":"tls-config","z":"","name":"","cert":"","key":"","ca":"","certname":"42c033985e-certificate.pem.crt","keyname":"42c033985e-private.pem.key","caname":"AmazonRootCA1.pem","servername":"","verifyservercert":true}]

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