Rain Detector and Node Red control

Dears,

I have and ESP32 to manage the wind speed with an anemometer and a rain detector.

The Rain detector is based on the following system:

https://github.com/hugokernel/esphome-rain-detector

But the code in ESPHome is not working well for me and I want to control the rain detector with node red.

Then, I have only the resistance measure from ESP32 and a switch to activate the resistance measure, because if the panel sensor is wet ant the voltage is active continuously the sensor degrades rapidly by oxidation.

Then I need a flow with the following specifications:

  • A general loop each 5 seconds.
  • For each loop:
    • Activate the switch.
    • Read 5 times the resistance and average the results.
    • Turn off the switch.
    • if the resistance is equal to the max resistance it means that sensor is dry, Alarm_raining = OFF. Then, I need to continue reading new five resistance values to average and have new result. Each loop needs to continue each 5 seconds.
    • If the resistance is lower than the max resistance, it means that is raining and I need to change the loop time to 30 seconds and Alarm_raining = ON.

In order to improve the systems can be interesting to know if the sensor is drying o if the sensor is weting, but then I need to know the actual average reading and the latest average value to compare.

I’m newbie about node red and I don’t know how to perform loops with variable time between readings.

Without the average readings this is the first approach using the node “configurable interval” installed:

But the rest of my proposal to average the readings I don’t know how to perform, any help for this please?.

The total code is:

[{"id":"12777efadb3c2970","type":"inject","z":"aa5ee1809b09809f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"1","topic":"","payload":"","payloadType":"date","x":150,"y":480,"wires":[["be3f7410cf69a195"]]},{"id":"726a132f8f8e2722","type":"debug","z":"aa5ee1809b09809f","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":640,"y":600,"wires":[]},{"id":"be3f7410cf69a195","type":"function","z":"aa5ee1809b09809f","name":"InitialSeconds","func":"msg.payload = msg.payload / 1000.0;\nreturn msg;","outputs":1,"noerr":0,"x":320,"y":480,"wires":[["c526925f2f36c672"]]},{"id":"c526925f2f36c672","type":"change","z":"aa5ee1809b09809f","name":"SetFlowInitial","rules":[{"t":"set","p":"dbgtimer","pt":"flow","to":"payload","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"enabled","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":480,"wires":[["161afa8ff3bb7541"]]},{"id":"161afa8ff3bb7541","type":"configurable interval","z":"aa5ee1809b09809f","name":"configurable interval","interval":"5","onstart":false,"do_enable":true,"msg":"ping","showstatus":true,"unit":"seconds","statusformat":"YYYY-MM-D HH:mm:ss","x":720,"y":480,"wires":[["9436e25edfc2798a"]]},{"id":"693b135635d313aa","type":"api-current-state","z":"aa5ee1809b09809f","name":"","server":"f6976039.c880a","version":3,"outputs":1,"halt_if":"","halt_if_type":"num","halt_if_compare":"lte","entity_id":"sensor.rain_detector_resistencia","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":360,"y":620,"wires":[["682649701f20b085","726a132f8f8e2722"]]},{"id":"9436e25edfc2798a","type":"api-call-service","z":"aa5ee1809b09809f","name":"Switch Bias Voltage ON","server":"f6976039.c880a","version":5,"debugenabled":false,"domain":"switch","service":"turn_on","areaId":[],"deviceId":[],"entityId":["switch.rain_detector_resistance_bias"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":970,"y":480,"wires":[["2ed6d6eb2ea0360b"]]},{"id":"2ed6d6eb2ea0360b","type":"delay","z":"aa5ee1809b09809f","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":1180,"y":480,"wires":[["693b135635d313aa"]]},{"id":"80b67dbd7412f945","type":"api-call-service","z":"aa5ee1809b09809f","name":"Switch Bias Voltage OFF","server":"f6976039.c880a","version":5,"debugenabled":false,"domain":"switch","service":"turn_off","areaId":[],"deviceId":[],"entityId":["switch.rain_detector_resistance_bias"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1110,"y":740,"wires":[[]]},{"id":"682649701f20b085","type":"switch","z":"aa5ee1809b09809f","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"50000","vt":"num"},{"t":"lte","v":"50000","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":410,"y":740,"wires":[["ccf2d7d27d8c57c7"],["e039d315e8977256"]]},{"id":"83bff2e1d8431b1b","type":"change","z":"aa5ee1809b09809f","name":"_30s","rules":[{"t":"set","p":"payload","pt":"msg","to":"interval","tot":"str"},{"t":"set","p":"interval_value","pt":"msg","to":"30","tot":"str"},{"t":"set","p":"interval_unit","pt":"msg","to":"seconds","tot":"str"},{"t":"set","p":"interval_activeInstantly","pt":"msg","to":"false","tot":"bool"},{"t":"set","p":"interval_fireInstantly","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":890,"y":780,"wires":[["80b67dbd7412f945","161afa8ff3bb7541"]]},{"id":"1c5a4fad0d04607d","type":"change","z":"aa5ee1809b09809f","name":"_5s","rules":[{"t":"set","p":"payload","pt":"msg","to":"interval","tot":"str"},{"t":"set","p":"interval_value","pt":"msg","to":"5","tot":"str"},{"t":"set","p":"interval_unit","pt":"msg","to":"seconds","tot":"str"},{"t":"set","p":"interval_activeInstantly","pt":"msg","to":"false","tot":"bool"},{"t":"set","p":"interval_fireInstantly","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":890,"y":720,"wires":[["80b67dbd7412f945","161afa8ff3bb7541"]]},{"id":"e039d315e8977256","type":"change","z":"aa5ee1809b09809f","name":"on","rules":[{"t":"set","p":"payload","pt":"msg","to":"on","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":780,"wires":[["7c50ccf2b65edf32"]]},{"id":"ccf2d7d27d8c57c7","type":"change","z":"aa5ee1809b09809f","name":"off","rules":[{"t":"set","p":"payload","pt":"msg","to":"off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":720,"wires":[["e2a537f4f2f2ff49"]]},{"id":"e2a537f4f2f2ff49","type":"api-call-service","z":"aa5ee1809b09809f","name":"Rain Alarm OFF","server":"f6976039.c880a","version":5,"debugenabled":true,"domain":"input_boolean","service":"turn_off","areaId":[],"deviceId":[],"entityId":["input_boolean.10_alarma_lluvia"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":740,"y":720,"wires":[["1c5a4fad0d04607d"]]},{"id":"7c50ccf2b65edf32","type":"api-call-service","z":"aa5ee1809b09809f","name":"Rain Alarm ON","server":"f6976039.c880a","version":5,"debugenabled":true,"domain":"input_boolean","service":"turn_on","areaId":[],"deviceId":[],"entityId":["input_boolean.10_alarma_lluvia"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":740,"y":780,"wires":[["83bff2e1d8431b1b"]]},{"id":"f6976039.c880a","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30"}]

Thanks in advance.

Jose

I’ve had not so good results running loops from NR to esphome. My loop times were shorter though. Anyway this should be able to be done inside esphome. You can set up a dynamic loop using a number and a binary template sensor.

binary_sensor:

  - platform: template
    name: "Loop template"
    id: loop_template
    lambda: |-
      if (id(enable_loop)) {
        return true;
      } else {
        return false;
      }
    on_state:
      - while:
          condition:
            binary_sensor.is_on: loop_template
          then:
            - if:
                condition:
                  binary_sensor.is_on: is_raining
                then:
                  - number.set:
                      id: loop_duration
                      value: 30000
                  - switch.turn_on: sensor_power
                  - delay: 5500ms ##set long enough for 5 readings
                  - switch.turn_off: sensor_power
                else:
                  - number.set:
                      id: loop_duration
                      value: 5000
                  - switch.turn_on: sensor_power
                  - delay: 5500ms
                  - switch.turn_off: sensor_power
            - delay: !lambda 'return id(loop_duration).state;'
            

    
number:

 - platform: template
   step: 1
   min_value: 5000
   max_value: 30000
   id: loop_duration
   optimistic: true
   
switch:

  - platform: template
    id: enable_loop
    optimistic: true
    name: "Enable sensor loop"

dear Mikefila,

Thanks. Do you have a rain detector like mine?, If yes can you share the entire esphome yaml file?.

Thanks,

I don’t, I do use esphome for a watering system that triggers every few minutes. I use similar code to run an infinite loop.