Here’s an alternate flow to control the bath fan based on the new derivative integration that bastero mentioned. I’ve been testing it by just logging the start and stop times, and it seems to work as expected.
For the derivative sensor I used a time window of 00:00:00 and a time unit of ‘minutes’. In theory it should only trigger when the derivative value rises or drops below zero, but I found that it sometimes would bounce to a value of positive 1, so I set the switch to turn on when the value is >=2.
[{"id":"fcb3c47492902266","type":"tab","label":"Bath Fan Control","disabled":false,"info":"","env":[]},{"id":"2ad8bfddd3944e76","type":"server-state-changed","z":"fcb3c47492902266","name":"Master Bath Humidity","server":"8be923df.d66f9","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.master_bath_humidity_testa","entityidfiltertype":"exact","outputinitially":false,"state_type":"num","haltifstate":"","halt_if_type":"num","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":true,"ignorePrevStateUnknown":true,"ignorePrevStateUnavailable":true,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"outputProperties":[{"property":"humidity","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":160,"y":100,"wires":[["975a3edb4b829a46"]]},{"id":"975a3edb4b829a46","type":"switch","z":"fcb3c47492902266","name":"+ or -","property":"humidity","propertyType":"msg","rules":[{"t":"gte","v":"2","vt":"num"},{"t":"lte","v":"0","vt":"num"}],"checkall":"false","repair":false,"outputs":2,"x":330,"y":100,"wires":[["5f1f208fe0d412e2"],["e23d2b9b09892744"]]},{"id":"5f1f208fe0d412e2","type":"function","z":"fcb3c47492902266","name":"log message Fan on","func":"var Time = new Date();\nmsg.payload=\"Fan ON \" + msg.humidity + \"%\";\n\nflow.set(\"var_master_bath_fan_start_time\", Time);\n \nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":100,"wires":[["640cce28efe28e3b"]]},{"id":"62f095e952acf380","type":"function","z":"fcb3c47492902266","name":"log message Fan off","func":"var Time = new Date();\nvar stop_time = flow.get(\"var_master_bath_fan_start_time\");\nvar duration =(Time - stop_time)/1000;\nduration=Math.ceil(duration*100)/100;\nduration=Math.ceil(duration/60);\nmsg.payload=\"Fan OFF \" + msg.humidity + \"% \" + duration + \" min\";\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":280,"wires":[["640cce28efe28e3b"]]},{"id":"640cce28efe28e3b","type":"api-call-service","z":"fcb3c47492902266","name":"","server":"8be923df.d66f9","version":5,"debugenabled":false,"domain":"logbook","service":"log","areaId":[],"deviceId":[],"entityId":[],"data":"{\"name\":\"Master Bath Humidity Sensor TEST\",\"message\":\"{{payload}}\",\"entity_id\":\"sensor.master_bath_humidity_sensor_humidity\",\"domain\":\"sensor\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":970,"y":160,"wires":[[]]},{"id":"e23d2b9b09892744","type":"api-current-state","z":"fcb3c47492902266","name":"Get Fan Status","server":"8be923df.d66f9","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"switch.shelly_master_bath_fan","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":520,"y":160,"wires":[["982ed164b76de56b"]]},{"id":"982ed164b76de56b","type":"switch","z":"fcb3c47492902266","name":"Fan On or Off?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":520,"y":220,"wires":[["62f095e952acf380"],[]]},{"id":"24047622b15b3d4a","type":"comment","z":"fcb3c47492902266","name":"Turn Fan switch ON Here","info":"","x":810,"y":120,"wires":[]},{"id":"3fdb0c9275dbd467","type":"comment","z":"fcb3c47492902266","name":"Turn Fan switch OFF Here","info":"","x":770,"y":220,"wires":[]},{"id":"8be923df.d66f9","type":"server","name":"homeassistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"","connectionDelay":false,"cacheJson":false,"heartbeat":false,"heartbeatInterval":""}]