# Activate switch based on humidity

**URL:** <https://discourse.nodered.org/t/activate-switch-based-on-humidity/28263>\
**Category:** General\
**Created:** [13 June 2020 04:44 UTC](https://discourse.nodered.org/t/activate-switch-based-on-humidity/28263 "2020-06-13T04:44:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![tfmeier](https://avatars.discourse-cdn.com/v4/letter/t/a9a28c/32.png) [@tfmeier](https://discourse.nodered.org/u/tfmeier)\
**Post date:** [13 June 2020 04:44 UTC](https://discourse.nodered.org/t/activate-switch-based-on-humidity/28263/1 "2020-06-13T04:44:10Z")

</div>

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 🙂

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?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [13 June 2020 05:21 UTC](https://discourse.nodered.org/t/activate-switch-based-on-humidity/28263/2 "2020-06-13T05:21:02Z")

</div>

I am guessing it will be visible as `msg.Humidity`

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [13 June 2020 05:43 UTC](https://discourse.nodered.org/t/activate-switch-based-on-humidity/28263/3 "2020-06-13T05:43:41Z")

</div>

Eventually as `msg.HTU21.Humidity`

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [13 June 2020 06:09 UTC](https://discourse.nodered.org/t/activate-switch-based-on-humidity/28263/4 "2020-06-13T06:09:01Z")

</div>

> [@tfmeier](#):
>
> Do I need to convert this string to a Json object to able to extract this?

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 June 2020 07:53 UTC](https://discourse.nodered.org/t/activate-switch-based-on-humidity/28263/5 "2020-06-13T07:53:45Z")

</div>

> [@bakman2](#):
>
> The mqtt node can output json as well,

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.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [13 June 2020 08:56 UTC](https://discourse.nodered.org/t/activate-switch-based-on-humidity/28263/6 "2020-06-13T08:56:12Z")

</div>

@tfmeier Thomas, a little helpful hint

> [@tfmeier](#):
>
> {"Time":"2020-06-13T05:39:40","HTU21":{"Temperature":18.5,"Humidity":53.8,"DewPoint":9.0},"TempUnit":"C"}

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/6/4638ebc60add2c1c123b06c74f1663c1a228ee00.png)  
click the triangle to open it up and you will see:  
 ![Screen Shot 2020-06-13 at 4.48.30 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/2/32ca26f7cc4a20808d7e29fae5d36e3726738970.png)  
click the triangle next to 'HTU21 and you get:  
 ![Screen Shot 2020-06-13 at 4.48.40 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/6/56dfe691b3bd8c815f08d14e8c02c559e9453d71.png)  
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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/6/d67cce1425ca30c7ffbb0877ac962b07d497337e.png)  
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.

---

<div class="post-metadata">

**Author:** ![tfmeier](https://avatars.discourse-cdn.com/v4/letter/t/a9a28c/32.png) [@tfmeier](https://discourse.nodered.org/u/tfmeier)\
**Post date:** [14 June 2020 06:40 UTC](https://discourse.nodered.org/t/activate-switch-based-on-humidity/28263/7 "2020-06-14T06:40:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [14 June 2020 09:21 UTC](https://discourse.nodered.org/t/activate-switch-based-on-humidity/28263/8 "2020-06-14T09:21:54Z")

</div>

> [@tfmeier](#):
>
> 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](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). 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.

---

<div class="post-metadata">

**Author:** ![tfmeier](https://avatars.discourse-cdn.com/v4/letter/t/a9a28c/32.png) [@tfmeier](https://discourse.nodered.org/u/tfmeier)\
**Post date:** [14 June 2020 11:18 UTC](https://discourse.nodered.org/t/activate-switch-based-on-humidity/28263/9 "2020-06-14T11:18:41Z")

</div>

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 🙂

`[{"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}]`

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [28 June 2020 11:18 UTC](https://discourse.nodered.org/t/activate-switch-based-on-humidity/28263/10 "2020-06-28T11:18:49Z")

</div>

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