Want to run if/then actions based upon MQTT humidity level sensor

I've just set up my first (tasmota based) hygrometer, and it's working great.

When I query the sensor I get this:
{"StatusSNS":{"Time":"2021-08-27T21:55:38","AM2301":{"Temperature":24.1,"Humidity":52.6,"DewPoint":13.8},"TempUnit":"C"}}

and here's the path when I convert to JSON:
payload.StatusSNS.AM2301.Humidity

I want to run a certain task when the humidity is reported higher than 50%, but I can't figure out how to use a switch to address only the humidity value

Hello,

What have you tried? How did you configure your Switch ?
Maybe you didnt set the value 50 to be a number ?

image

Test Flow

[{"id":"9cad5d82e8f4f06c","type":"inject","z":"4895ea10b4ee9ead","name":"higher than 50","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"StatusSNS\":{\"Time\":\"2021-08-27T21:55:38\",\"AM2301\":{\"Temperature\":24.1,\"Humidity\":52.6,\"DewPoint\":13.8},\"TempUnit\":\"C\"}}","payloadType":"json","x":300,"y":1020,"wires":[["9db8307ae1db6e80"]]},{"id":"9db8307ae1db6e80","type":"switch","z":"4895ea10b4ee9ead","name":"","property":"payload.StatusSNS.AM2301.Humidity","propertyType":"msg","rules":[{"t":"gt","v":"50","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":500,"y":1060,"wires":[["6e08e45ce4973f8b"]]},{"id":"6e08e45ce4973f8b","type":"debug","z":"4895ea10b4ee9ead","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":1060,"wires":[]},{"id":"cd1faba15e13c336","type":"inject","z":"4895ea10b4ee9ead","name":"less than 50","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"StatusSNS\":{\"Time\":\"2021-08-27T21:55:38\",\"AM2301\":{\"Temperature\":24.1,\"Humidity\":32.6,\"DewPoint\":13.8},\"TempUnit\":\"C\"}}","payloadType":"json","x":310,"y":1100,"wires":[["9db8307ae1db6e80"]]}]

There is a very good introductory video playlist on youtube made by the developers of Node-red
check it out here

thank you so much for your reply! after posting, I went back to the drawing board and solved it.

I used a change node to delete the unnecessary payloads and move the pertinent payload to msg.payload
I then used a switch node to re-enter a test loop for values >50 and process for values <=50
Next another change node to replace the msg.payload contents with "OFF"
And lastly, an MQTT out node to set the topic and attach the payload.

Also, I didn't seem to need to set to number as nodered happily parsed a double for me.

I'm sure there's a more elegant way to do this, but that's what I love about nodered... there's something for everybody including the vulgar and profane.

cheers

2 Likes

BLAST! I'm not out of the woods yet.

standard periodic telemetry updates from the sensor are triggering my change node before desired trigger time has elapsed.

my goal is for the bathroom fan to run for at least 10min any time it's manually engaged, and then query the humidity every 2.5min after that. the problem that I'm seeing is that any time the sensors publishes its data, whether automatically or via some other request, its turning off the fan.

I'm sure there's some way to grab a timestamp from the initial "wait 10min" trigger, and challenge executions based on that?

here's a visual:

and here's the flow:

[{"id":"1d522e8c.dab4f9","type":"tab","label":"Fan and Light Timers","disabled":false,"info":""},{"id":"ca74f378.77eb08","type":"mqtt in","z":"1d522e8c.dab4f9","name":"","topic":"stat/utility/bathfan/RESULT","qos":"0","datatype":"auto","broker":"c76d5418.e16138","x":210,"y":180,"wires":[["2ea164a7.5e41fc"]]},{"id":"91f04c4e.3585b","type":"debug","z":"1d522e8c.dab4f9","name":"DEBUG TURN OFF","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1030,"y":240,"wires":[]},{"id":"2ea164a7.5e41fc","type":"switch","z":"1d522e8c.dab4f9","name":"IF ON","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"ON","vt":"str"},{"t":"neq","v":"ON","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":390,"y":180,"wires":[["3ab1fe61.c3492a","546c95cf.83e964"],[]]},{"id":"3ab1fe61.c3492a","type":"trigger","z":"1d522e8c.dab4f9","name":"WAIT 45MIN","op1":"","op2":"","op1type":"nul","op2type":"pay","duration":"45","extend":false,"overrideDelay":false,"units":"min","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":530,"y":180,"wires":[["e8c473ed.40eb5"]]},{"id":"e8c473ed.40eb5","type":"change","z":"1d522e8c.dab4f9","name":"CHANGE TO OFF","rules":[{"t":"set","p":"payload","pt":"msg","to":"OFF","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":830,"y":180,"wires":[["91f04c4e.3585b","10f7c310.311295"]]},{"id":"10f7c310.311295","type":"mqtt out","z":"1d522e8c.dab4f9","name":"","topic":"cmnd/utility/bathfan/POWER","qos":"0","retain":"","broker":"c76d5418.e16138","x":1060,"y":180,"wires":[]},{"id":"7113f564.b3eaac","type":"mqtt in","z":"1d522e8c.dab4f9","name":"","topic":"stat/utility/mbathfan/RESULT","qos":"0","datatype":"auto","broker":"c76d5418.e16138","x":200,"y":300,"wires":[["7ce6f847.e70468"]]},{"id":"7ce6f847.e70468","type":"switch","z":"1d522e8c.dab4f9","name":"IF ON","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"ON","vt":"str"},{"t":"neq","v":"ON","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":390,"y":300,"wires":[["c742b691.fc6b88","546c95cf.83e964"],[]]},{"id":"c742b691.fc6b88","type":"trigger","z":"1d522e8c.dab4f9","name":"WAIT 10MIN","op1":"","op2":"","op1type":"nul","op2type":"pay","duration":"10","extend":false,"overrideDelay":false,"units":"min","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":530,"y":300,"wires":[["d5a27873.5c6d58"]]},{"id":"5fed3067.31aeb","type":"change","z":"1d522e8c.dab4f9","name":"CHANGE TO OFF","rules":[{"t":"set","p":"payload","pt":"msg","to":"OFF","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":830,"y":300,"wires":[["e66f9783.39936","91f04c4e.3585b"]]},{"id":"e66f9783.39936","type":"mqtt out","z":"1d522e8c.dab4f9","name":"","topic":"cmnd/utility/mbathfan/POWER","qos":"0","retain":"","broker":"c76d5418.e16138","x":1070,"y":300,"wires":[]},{"id":"b6e2aad5.50bf38","type":"comment","z":"1d522e8c.dab4f9","name":"UPSTAIRS BATHROOM AUTO OFF TIMER","info":"","x":260,"y":140,"wires":[]},{"id":"d13fac57.cf8ca8","type":"comment","z":"1d522e8c.dab4f9","name":"MASTER BATHROOM AUTO OFF TIMER","info":"","x":260,"y":260,"wires":[]},{"id":"a230a8a1.863688","type":"mqtt in","z":"1d522e8c.dab4f9","name":"","topic":"stat/lighting/mastercloset/RESULT","qos":"0","datatype":"auto","broker":"c76d5418.e16138","x":180,"y":600,"wires":[["1655960f.133e02"]]},{"id":"63d6ed1.9487094","type":"debug","z":"1d522e8c.dab4f9","name":"DEBUG TURN OFF","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1030,"y":560,"wires":[]},{"id":"1655960f.133e02","type":"switch","z":"1d522e8c.dab4f9","name":"IF ON","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"ON","vt":"str"},{"t":"neq","v":"ON","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":390,"y":600,"wires":[["3de7de3.8a2c422","96f5e6ad.f133b8"],[]]},{"id":"3de7de3.8a2c422","type":"trigger","z":"1d522e8c.dab4f9","name":"WAIT 5MIN","op1":"","op2":"","op1type":"nul","op2type":"pay","duration":"5","extend":false,"overrideDelay":false,"units":"min","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":530,"y":600,"wires":[["a0e661c1.761cf"]]},{"id":"a0e661c1.761cf","type":"change","z":"1d522e8c.dab4f9","name":"CHANGE TO OFF","rules":[{"t":"set","p":"payload","pt":"msg","to":"OFF","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":830,"y":600,"wires":[["63d6ed1.9487094","5911993a.0dac1"]]},{"id":"5911993a.0dac1","type":"mqtt out","z":"1d522e8c.dab4f9","name":"","topic":"cmnd/lighting/mastercloset/POWER","qos":"0","retain":"","broker":"c76d5418.e16138","x":1090,"y":600,"wires":[]},{"id":"8ba0f324.2069e","type":"comment","z":"1d522e8c.dab4f9","name":"MASTER CLOSET AUTO OFF TIMER","info":"","x":250,"y":560,"wires":[]},{"id":"546c95cf.83e964","type":"debug","z":"1d522e8c.dab4f9","name":"DEBUG IF ON","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":540,"y":240,"wires":[]},{"id":"96f5e6ad.f133b8","type":"debug","z":"1d522e8c.dab4f9","name":"DEBUG IF ON","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":540,"y":660,"wires":[]},{"id":"dec85c20.8e4bb8","type":"mqtt out","z":"1d522e8c.dab4f9","name":"","topic":"cmnd/sensors/mbathsensor/status","qos":"","retain":"","broker":"c76d5418.e16138","x":820,"y":1000,"wires":[]},{"id":"1931c779.795f89","type":"inject","z":"1d522e8c.dab4f9","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":330,"y":1000,"wires":[["1f708777.4443c1"]]},{"id":"1f708777.4443c1","type":"change","z":"1d522e8c.dab4f9","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"10","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":1000,"wires":[["dec85c20.8e4bb8","bbccbad9.314c2"]]},{"id":"bbccbad9.314c2","type":"debug","z":"1d522e8c.dab4f9","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":740,"y":1080,"wires":[]},{"id":"d5a27873.5c6d58","type":"trigger","z":"1d522e8c.dab4f9","name":"CHECK EVERY 2.5MIN","op1":"","op2":"","op1type":"pay","op2type":"pay","duration":"2.5","extend":false,"overrideDelay":false,"units":"min","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":570,"y":360,"wires":[["31476e4d.fa5172"]]},{"id":"323444d6.c2a984","type":"mqtt in","z":"1d522e8c.dab4f9","name":"RECEIVE HUMIDITY","topic":"+/sensors/mbathsensor/+","qos":"0","datatype":"auto","broker":"c76d5418.e16138","x":230,"y":420,"wires":[["416aeaf1.83ef1c"]]},{"id":"416aeaf1.83ef1c","type":"json","z":"1d522e8c.dab4f9","name":"PARSE","property":"payload","action":"","pretty":true,"x":420,"y":420,"wires":[["4778b3b9.3a639c"]]},{"id":"4778b3b9.3a639c","type":"change","z":"1d522e8c.dab4f9","name":"CROP HUMIDITY","rules":[{"t":"delete","p":"payload.StatusSNS.AM2301.Temperature","pt":"msg"},{"t":"delete","p":"payload.StatusSNS.AM2301.DewPoint","pt":"msg"},{"t":"move","p":"payload.StatusSNS.AM2301.Humidity","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":610,"y":420,"wires":[["7b10834a.806ac4"]]},{"id":"7b10834a.806ac4","type":"switch","z":"1d522e8c.dab4f9","name":"DELTA 55","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"55","vt":"str"},{"t":"lte","v":"55","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":800,"y":420,"wires":[["d5a27873.5c6d58"],["5fed3067.31aeb"]]},{"id":"727ed7f6.d43c18","type":"mqtt out","z":"1d522e8c.dab4f9","name":"EXECUTE QUERY","topic":"cmnd/sensors/mbathsensor/status","qos":"","retain":"","broker":"c76d5418.e16138","x":1030,"y":360,"wires":[]},{"id":"31476e4d.fa5172","type":"change","z":"1d522e8c.dab4f9","name":"LOAD QUERY","rules":[{"t":"set","p":"payload","pt":"msg","to":"10","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":820,"y":360,"wires":[["727ed7f6.d43c18","b5973542.208768"]]},{"id":"c942160a.067408","type":"comment","z":"1d522e8c.dab4f9","name":"HUMIDITY CHECK","info":"","x":190,"y":380,"wires":[]},{"id":"b5973542.208768","type":"debug","z":"1d522e8c.dab4f9","name":"DEBUG QUERY","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1020,"y":420,"wires":[]},{"id":"cdef13fb.5a8a28","type":"comment","z":"1d522e8c.dab4f9","name":"TRIGGER HUMIDITY CHECK","info":"","x":600,"y":960,"wires":[]},{"id":"c76d5418.e16138","type":"mqtt-broker","name":"MOSQUITTO","broker":"192.168.5.196","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","willTopic":"","willQos":"0","willRetain":"false","willPayload":""}]

possibly you can use a Trigger node
(change to wait 10min when done testing)

[{"id":"9cad5d82e8f4f06c","type":"inject","z":"4895ea10b4ee9ead","name":"higher than 50","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"StatusSNS\":{\"Time\":\"2021-08-27T21:55:38\",\"AM2301\":{\"Temperature\":24.1,\"Humidity\":52.6,\"DewPoint\":13.8},\"TempUnit\":\"C\"}}","payloadType":"json","x":300,"y":1020,"wires":[["9db8307ae1db6e80"]]},{"id":"9db8307ae1db6e80","type":"switch","z":"4895ea10b4ee9ead","name":"","property":"payload.StatusSNS.AM2301.Humidity","propertyType":"msg","rules":[{"t":"gt","v":"50","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":500,"y":1060,"wires":[["a84576be2ac0de4d"]]},{"id":"6e08e45ce4973f8b","type":"debug","z":"4895ea10b4ee9ead","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":890,"y":1060,"wires":[]},{"id":"cd1faba15e13c336","type":"inject","z":"4895ea10b4ee9ead","name":"less than 50","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"StatusSNS\":{\"Time\":\"2021-08-27T21:55:38\",\"AM2301\":{\"Temperature\":24.1,\"Humidity\":32.6,\"DewPoint\":13.8},\"TempUnit\":\"C\"}}","payloadType":"json","x":310,"y":1100,"wires":[["9db8307ae1db6e80"]]},{"id":"a84576be2ac0de4d","type":"trigger","z":"4895ea10b4ee9ead","name":"","op1":"ON","op2":"OFF","op1type":"str","op2type":"str","duration":"10","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":670,"y":1060,"wires":[["6e08e45ce4973f8b"]]}]

ps. surround you code between three backtics so it doesnt take so much space.

you have to query the device every few minutes? .. it cannot be set on the sensor to send it every few seconds ?

but then it will turn the fan off no sooner than 10min after the set point has been reached, and should the humidity begin to rise after the trigger was induced, the fan would still turn off leaving a soggy bathroom.

what's ideal is:
1)fan is engaged
2)fan runs for 10min regardless of any input (other than physical switch)
3)after 10min mark:
4)flow begins to query relative humidity every 150 seconds
5)fan turns off when set point is reached

the problem that I'm encountering is that normal telemetry occurs within step 2, causing the fan to turn off before running a full 10 minutes, which is the requisite amount of time to freshen the air after toilet use.

it's not uncommon that my wife and I will shower within 10-20 minutes of each other, and inserting the trigger delay at your suggested point will allow the fan to turn off while humidity is potentially above the set point.

I don't have to query every few minutes, but the 80CFM fan isn't likely to remove humidity at a fast pace so no need for rapid refresh. I can send faster from the sensor, but automatic sendings from the sensor are what's disturbing my flow already

no, because a new msg to turn the fan ON would arrive again by then and re-trigger (extend) it
because of the checked "extend delay if new message arrives" option.

ps. Try to seperate the flow logic of requesting the status of your sensors, from the logic that actually turns the fan on or off.

not if the fan was currently running through the 10 minute trigger (say 5min left) after the set point had been met. a user would then have to power off and power on the fan again to restart the timer, which could only be done if they expected the possible outcome.

so i think this is the part i missed
you want the Fan logic to start monitoring the humidity and make decisions to turn on/off only when you press a button ? what exactly is "engaged" in this case ?

why ? :wink:
if the fan is on (has power) and the humidity is higher than the setpoint (being checked every minute or so - maybe with an inject node set to mqtt request new data)
the logic with the trigger node will turn it on (with a minimum time of 10min)

How do you "manually engage" the fan?
Does your Tasmota device recognise the PulseTime1 command?

PulseTime1 700 will cause the device to stay on for 600 seconds and then turn off.
If you send an ON command during this time the countdown starts again.

Now your flow can be much simpler. All you need to do is request the humidity, check if it's high and send ON

[{"id":"1d522e8c.dab4f9","type":"tab","label":"Fan and Light Timers","disabled":false,"info":""},{"id":"dec85c20.8e4bb8","type":"mqtt out","z":"1d522e8c.dab4f9","name":"","topic":"cmnd/sensors/mbathsensor/status","qos":"","retain":"","broker":"c76d5418.e16138","x":750,"y":780,"wires":[]},{"id":"1931c779.795f89","type":"inject","z":"1d522e8c.dab4f9","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":260,"y":780,"wires":[["1f708777.4443c1"]]},{"id":"1f708777.4443c1","type":"change","z":"1d522e8c.dab4f9","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"10","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":780,"wires":[["dec85c20.8e4bb8","bbccbad9.314c2"]]},{"id":"bbccbad9.314c2","type":"debug","z":"1d522e8c.dab4f9","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":860,"wires":[]},{"id":"cdef13fb.5a8a28","type":"comment","z":"1d522e8c.dab4f9","name":"TRIGGER HUMIDITY CHECK","info":"","x":530,"y":740,"wires":[]},{"id":"d857341a61145d89","type":"inject","z":"1d522e8c.dab4f9","name":"Every minute","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"60","crontab":"","once":false,"onceDelay":0.1,"topic":"cmnd/sensors/mbathsensor/status","payload":"10","payloadType":"str","x":140,"y":40,"wires":[["ac1def57cb17a923"]]},{"id":"ac1def57cb17a923","type":"mqtt out","z":"1d522e8c.dab4f9","name":"Request Status","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"c76d5418.e16138","x":340,"y":40,"wires":[]},{"id":"2ddfc0e8fcb2027c","type":"mqtt in","z":"1d522e8c.dab4f9","name":"RECEIVE HUMIDITY","topic":"+/sensors/mbathsensor/+","qos":"0","datatype":"json","broker":"c76d5418.e16138","nl":false,"rap":false,"x":140,"y":100,"wires":[["f404bf413c135ff6"]]},{"id":"f404bf413c135ff6","type":"switch","z":"1d522e8c.dab4f9","name":"DELTA 55","property":"payload.StatusSNS.AM2301.Humidity","propertyType":"msg","rules":[{"t":"gt","v":"55","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":320,"y":100,"wires":[["dd7a1b187c4ba292"]]},{"id":"dd7a1b187c4ba292","type":"change","z":"1d522e8c.dab4f9","name":"Humid! ON","rules":[{"t":"set","p":"payload","pt":"msg","to":"ON","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":100,"wires":[["7391de5a77292aef"]]},{"id":"7391de5a77292aef","type":"mqtt out","z":"1d522e8c.dab4f9","name":"","topic":"cmnd/utility/mbathfan/POWER","qos":"0","retain":"","broker":"c76d5418.e16138","x":770,"y":100,"wires":[]},{"id":"c76d5418.e16138","type":"mqtt-broker","name":"MOSQUITTO","broker":"192.168.5.196","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","willTopic":"","willQos":"0","willRetain":"false","willPayload":""}]

One advantage of this approach is that if Node-Red goes down, the fan will still turn itself off.
Possibly an advantage, possibly a disadvantage: the flow will also turn the fan on without your intervention if the humidity rises.

the ventilation in the bathrooms are used for two reasons:

  1. when we bathe, to control the humidity
  2. when we poop, it frequently smells terrible

a single activation of the fan should run for 10 minutes, no matter what, without regard to humidity.
after 10 minutes, the fan should then begin to monitor humidity and turn off when the set point of 50% is reached. here's a super crude representation of what I'm talking about (if the 10minutes elapses after the set point is reached as opposed to before testing the set point:

@jbudd

the fan is turned on by human interaction with the switch (also running tasmota)

I do not wish for the fan to wait for rising humidity to turn on, as lowering the humidity without pre-running the fan causes longer run times.

the goal is for a human to turn on the fan manually, and let the logic decide when to turn it off

so all you need is an additional mqtt enabled poop sensor and you are all set :wink:

We also need to check for the manual engaging of the fan along with the humidity setpoint
maybe in a Function node ?

[{"id":"9cad5d82e8f4f06c","type":"inject","z":"4895ea10b4ee9ead","name":"hum. higher than 50","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"StatusSNS\":{\"Time\":\"2021-08-27T21:55:38\",\"AM2301\":{\"Temperature\":24.1,\"Humidity\":52.6,\"DewPoint\":13.8},\"TempUnit\":\"C\"}}","payloadType":"json","x":310,"y":1020,"wires":[["5e4e27e08238a867"]]},{"id":"6e08e45ce4973f8b","type":"debug","z":"4895ea10b4ee9ead","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":930,"y":1080,"wires":[]},{"id":"cd1faba15e13c336","type":"inject","z":"4895ea10b4ee9ead","name":"hum. less than 50","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"StatusSNS\":{\"Time\":\"2021-08-27T21:55:38\",\"AM2301\":{\"Temperature\":24.1,\"Humidity\":32.6,\"DewPoint\":13.8},\"TempUnit\":\"C\"}}","payloadType":"json","x":310,"y":1080,"wires":[["5e4e27e08238a867"]]},{"id":"a84576be2ac0de4d","type":"trigger","z":"4895ea10b4ee9ead","name":"","op1":"ON","op2":"OFF","op1type":"str","op2type":"str","duration":"10","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":710,"y":1080,"wires":[["6e08e45ce4973f8b"]]},{"id":"c02aab1f39499049","type":"inject","z":"4895ea10b4ee9ead","name":"engage ON","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":330,"y":1140,"wires":[["5e4e27e08238a867"]]},{"id":"5e4e27e08238a867","type":"function","z":"4895ea10b4ee9ead","name":"","func":"if (msg.payload?.StatusSNS?.AM2301?.Humidity > 50 || msg.payload === 'on') {\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":530,"y":1080,"wires":[["a84576be2ac0de4d"]]}]

I stepped away from my desk, I will look at that.

How to determine whether fan was triggered manually versus by automation?

How to determine whether fan was triggered manually versus by automation?

Don't you already do that in your flow by testing if stat/utility/mbathfan/RESULT == "ON"?
It's not really clear if the button you press is the same device as that which controls the fan. If they are different, they should use different MQTT topics.

[{"id":"d857341a61145d89","type":"inject","z":"1d522e8c.dab4f9","name":"Every minute","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"60","crontab":"","once":false,"onceDelay":0.1,"topic":"cmnd/sensors/mbathsensor/status","payload":"10","payloadType":"str","x":160,"y":200,"wires":[["ac1def57cb17a923"]]},{"id":"ac1def57cb17a923","type":"mqtt out","z":"1d522e8c.dab4f9","name":"Request humidity","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"c76d5418.e16138","x":790,"y":200,"wires":[]},{"id":"2ddfc0e8fcb2027c","type":"mqtt in","z":"1d522e8c.dab4f9","name":"RECEIVE HUMIDITY","topic":"+/sensors/mbathsensor/+","qos":"0","datatype":"json","broker":"c76d5418.e16138","nl":false,"rap":false,"x":160,"y":140,"wires":[["15e1ce0683258bf7"]]},{"id":"79d3ff1aa0e41073","type":"mqtt in","z":"1d522e8c.dab4f9","name":"Switch was pressed","topic":"/whatever/switch","qos":"2","datatype":"auto","broker":"77bfb9d8.845088","nl":false,"rap":true,"rh":0,"x":150,"y":80,"wires":[["d812195e3938b384"]]},{"id":"d812195e3938b384","type":"change","z":"1d522e8c.dab4f9","name":"ON","rules":[{"t":"set","p":"payload","pt":"msg","to":"ON","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":80,"wires":[["33d219c9ecbb0e1b","7ccbaeb99c2f6ee3"]]},{"id":"33d219c9ecbb0e1b","type":"mqtt out","z":"1d522e8c.dab4f9","name":"","topic":"cmnd/utility/mbathfan/POWER","qos":"0","retain":"","broker":"c76d5418.e16138","x":830,"y":100,"wires":[]},{"id":"7ccbaeb99c2f6ee3","type":"change","z":"1d522e8c.dab4f9","name":"","rules":[{"t":"set","p":"switchtime","pt":"flow","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":590,"y":60,"wires":[[]]},{"id":"15e1ce0683258bf7","type":"function","z":"1d522e8c.dab4f9","name":"> 10 min && <= 55% : OFF","func":"var timediff = new Date() - flow.get('switchtime') ;\nif (timediff > 600000  && msg.payload.StatusSNS.AM2301.Humidity <= 55)\n{\n    msg.payload = \"OFF\";\n    return msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":140,"wires":[["33d219c9ecbb0e1b"]]},{"id":"c76d5418.e16138","type":"mqtt-broker","name":"MOSQUITTO","broker":"192.168.5.196","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","willTopic":"","willQos":"0","willRetain":"false","willPayload":""},{"id":"77bfb9d8.845088","type":"mqtt-broker","name":"","broker":"10.0.0.43","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

NB - untested, expect errors!

That topic will publish whether the switch was turned on manually or via automation.

The fan is just a 120v fan operated by the topic in my flow, which is a tasmota based smart switch.

Are you suggesting I decouple the switch from the relay (same device) and use the switch to trigger an automation back to the relay?

I’m confused!

I'm confused too. You said that you want to turn the fan on by hand and have Node-Red decide when to turn it off. You said that you don't want it turned on by the humidity sensor.
So I have no idea in what circumstances it gets turned on automatically.

No, don't decouple the switch from the relay. You could do that but I don't think it's a good idea.

Sorry, I don't think I can offer any more help on this.

Haha we’ve confused each other ad infinitum.

I think my answer will be creating a trigger which won’t allow the fan to turn off (except by switch) sooner than 10min after it receives an “ON” state. This way it won’t matter if the telemetry reports low humidity.

I’m sure I can figure it out!

To make it more clear would it possible to do a small diagram of the device connectivity along with the mqtt topics each device listens to ?

hopefully this improper use of nodered helps illustrate the configuration of hardware: