Irrigation scheduler

Hi everyone! I’m building a hydroponic system that I’m integrating into home automation, using ESPHome.

Specifically I used an ESP8266 to control a mosfet to which an electric pump is connected.

Up to now, I’ve managed to configure everything correctly, however I would need to create an automation to handle:

  • activation/deactivation of the pump

  • pump power (via PWM)

For example, I would like the pump to turn on every day every 30 minutes at 80% power, then turn off for another 30 minutes and then turn on again for 30 minutes. This must repeat forever when the switch is set to ON.

In fact I would also like to be able to view via UI a tab that shows

  • pump power (with adjustable slider)

  • activation time (with slider)

  • deactivation time (with slider)

  • general countdown showing for example “the pump will activate in: 00:20:14”

  • general countdown showing for example “the pump will remain active for: 00:28:11”

I’ve already done a lot of tests but not being very experienced, I still haven’t managed after almost 2 weeks to get anything usable. The code I’ve made so far is as follows:

switches:
 - platform: gpio
   name: "Electric pump"
   pins:
     number: GPIO4
     inverted: False
   id: electropump_relay

outputs:
 - platform: esp8266_pwm
   pins: GPIO4
   frequency: 500Hz
   id: pwm_output


light:
 -platform: monochromatic
   output: pwm_output
   name: "Electric pump power"

Any particular reason you are using a monochromatic light platform to control a pump? There are examples for pwm automation in the documentation. You’ll also need to look at the Time component for setting 30 min timers

No, no particular reason. Simply being a beginner, I used a code found on the net that regulated the power of a light. Now I’ll try to look at examples of PWM and the Time component

That’s perfectly fine, I was just curious if there was a specific reason or if you just weren’t sure. There really isn’t a difference except the fact it shows up as a light. Are you using HA as well?

Oh ok perfect! Thanks for telling me! yes, I’m also using Home Assistant to manage other devices in the house, however given the need to turn the pump on and off every 30 minutes, I’m not finding other easy ways to set up this automation.

I tried to look also for examples to configure the ESP8266 via yaml file (also asking ChatGpt haha) but nothing works. I’m afraid that the project I want to make is much more complicated than I imagined.

chatGPT is horrible at writing esphome config as im sure you found out. Have you looked at the esphome irrigation component? It has most if not all of the features your trying to set up manually. Setting up timers is pretty simple, as well as returning the remaining time. You can configure multiple valves/pumps and create a schedule for them. I think you’ll like this a lot more.

Yes, absolutely! ChatGpt is unusable. Anyway thanks for the tip, I study well the sprinkler controller you linked.

For the UI configuration in Lovelance, is it already included in the sprinkler code or can you link me something better?

Can you point in the direction of the generic PWM documentation you’re referring to? The only references I can find use PWM, either esp8266_pwm or ledc and use the light component. What sort of configuration/component are you suggesting he use instead?

the “light component” is just an example they use but, it returns a light entity to the front end. They likely used a Light as an example because it’s one of the most common ways to use pwm for home automation builds. You can use the Output Component by itself and not linked to something else like a Light. If you look at it, the light is using the Output Component. You can manipulate the output without it being tied to a light entity or anything else. Like it’s name implies, its an OUTPUT and you can tell it what to output a number of different ways.

Ahh I see. I thought that the output component needed to be linked to some sort of other type, like in the example. Thanks!

If you havn’t noticed, the documentation is very vague and not detailed at all. I’m not 100% sure but, having the output linked to a light or a fan or whatever, thats really just for HA because an Esphome Light entity will show up in HA as a light and the same goes for a fan. It simplifies things for the users and especially those who don’t want to dedicate the time to be professional programmers. The problem though, is there isn’t a pre-made option for a pump or a motor and other things like there is for a fan and a light. You can use a light or a fan for simplicity and then change the icon in HA but, It’s going to get automatically put in the light category for HA and any automations where for example you want to do a “turn off all lights if i’m away from home longer than 30min” you’re now going to have to remember to exclude your pump because it’s technically a light entity and an automation like that would shut your pump off when you don’t want it to be shut off.

yes it’s already configured but you still have to instruct it to include it. There are a lot of differnt options available to use and you may not want or need all them, so you have to tell in what to include.

sensor:
### SENSORS
  - platform: template
    name: "Zone Time Remaining Sensor"
    icon: mdi:progress-clock    
    unit_of_measurement: 'Min'
    accuracy_decimals: 0
    update_interval: 10s   
    lambda: |-
      if(id(lawn_sprinkler_ctrlr_status).state != "Paused")
      {
        return id(lawn_sprinkler_ctrlr).time_remaining_active_valve().value_or(0) / 60;
      }
      else
      {
        return {};
      }

That is my Time remaining sensor. I use pause, stop, and start services so for mine im telling it to return the time remaining and the valve number that is active IF it isn’t paused

If you want services exposed to HA like Stop, Start, Pause, Repeat, Divided, etc you have to include them in your services part of the yaml.

api:
  encryption:
    key: ""
  reboot_timeout: 0s
  services:
    - service: set_multiplier
      variables:
        multiplier: float
      then:
        - sprinkler.set_multiplier:
            id: lawn_sprinkler_ctrlr
            multiplier: !lambda 'return multiplier;'

    - service: start_full_cycle
      then:
        - sprinkler.start_full_cycle: lawn_sprinkler_ctrlr

    - service: start_single_valve
      variables:
        valve: int
      then:
        - sprinkler.start_single_valve:
            id: lawn_sprinkler_ctrlr
            valve_number: !lambda 'return valve;'

    - service: set_divider   
      
      then:
        - sprinkler.set_divider:
            id: lawn_sprinkler_ctrlr
            divider: 3      

    - service: next_valve
      then:
        - sprinkler.next_valve: lawn_sprinkler_ctrlr

    - service: pause
      then:
        - media_player.pause: esp32_media_barn

    - service: stop
      then:
        - media_player.stop: esp32_media_barn

    - service: previous_valve
      then:
        - sprinkler.previous_valve: lawn_sprinkler_ctrlr

    - service: shutdown
      then:
        - sprinkler.shutdown: lawn_sprinkler_ctrlr
        

    - service: pause
      then:
        - sprinkler.pause: lawn_sprinkler_ctrlr

    - service: resume
      then:
        - sprinkler.resume: lawn_sprinkler_ctrlr

    - service: resume_or_full_cycle
      then:
        - sprinkler.resume_or_start_full_cycle: lawn_sprinkler_ctrlr    

    - service: repeat_2
      then:
        - sprinkler.set_repeat:
            id: lawn_sprinkler_ctrlr
            repeat: 1  

Would you mind sharing your whole yaml? I am trying to set up sensors for variety of attributes (state, remaining time of zone, cycle, etc, but I am unable to do so.
I have defined sensor but getting variety of errors when compiling:

Compiling /data/gardenwatering8ch/.pioenvs/gardenwatering8ch/src/main.cpp.o
/config/esphome/gardenwatering8ch.yaml: In lambda function:
/config/esphome/gardenwatering8ch.yaml:186:33: error: 'class esphome::sprinkler::Sprinkler' has no member named 'state'; did you mean 'esphome::sprinkler::SprinklerState esphome::sprinkler::Sprinkler::state_'? (not accessible from this context)
  186 |       if(id(irrigation_controller).state != "Paused")
      |                                 ^~~~~
In file included from src/esphome/components/sprinkler/automation.h:5,
                 from src/esphome.h:43,
                 from src/main.cpp:3:
src/esphome/components/sprinkler/sprinkler.h:531:18: note: declared protected here
  531 |   SprinklerState state_{IDLE};
      |                  ^~~~~~
/config/esphome/gardenwatering8ch.yaml: In lambda function:
/config/esphome/gardenwatering8ch.yaml:201:33: error: 'class esphome::sprinkler::Sprinkler' has no member named 'state'; did you mean 'esphome::sprinkler::SprinklerState esphome::sprinkler::Sprinkler::state_'? (not accessible from this context)
  201 |       if(id(irrigation_controller).state != "Paused")
      |                                 ^~~~~
In file included from src/esphome/components/sprinkler/automation.h:5,
                 from src/esphome.h:43,
                 from src/main.cpp:3:
src/esphome/components/sprinkler/sprinkler.h:531:18: note: declared protected here
  531 |   SprinklerState state_{IDLE};
      |                  ^~~~~~
/config/esphome/gardenwatering8ch.yaml:201:84: error: 'class esphome::sprinkler::Sprinkler' has no member named 'cycle_time_enabled_valves'; did you mean 'total_cycle_time_enabled_valves'?
  201 |       if(id(irrigation_controller).state != "Paused")
      |                                                                                    ^                        
      |                                                                                    total_cycle_time_enabled_valves
/config/esphome/gardenwatering8ch.yaml: In lambda function:
/config/esphome/gardenwatering8ch.yaml:187:3: error: control reaches end of non-void function [-Werror=return-type]
  187 |       {
      |   ^
cc1plus: some warnings being treated as errors
*** [/data/gardenwatering8ch/.pioenvs/gardenwatering8ch/src/main.cpp.o] Error 1
========================== [FAILED] Took 3.06 seconds ==========================

Thanks for specifying that! I’m doing several tests with the guide you linked to me even if, being a beginner, I’m not getting anything good, except for a severe headache…

I tried to look for alternatives to the code and I found an integration (available in HACS) called “Simple Scheduler” which allows to schedule the activity and inactivity period of an entity, however it allows to create maximum 12 time slots …

Is there an easier way to achieve what I want to achieve?

You’re just setting up a simple switch that turns on a pump right?

Yes exactly! For the sake of simplicity, I can reduce the need for use to the simple fact that the pump turns on every 30 minutes at 80% power. This must repeat forever (every day the pump will turn on for 30 minutes, at 80%, after which it will turn off for 30 minutes and then turn on again)
That’s all.

did you even set up a sprinkler controller like the documentation explains? Just post your yaml and see why you’re getting these errors. It looks like you havn’t even created the actual sprinkler controller configuration.

ok. Post what you have so far and lets see what you still need.

The code I’ve written (actually copied) so far is what I posted in the first post. Simple, does not give errors and allows you to effectively control the power of the pump.

Reading the guide you linked to me, I tried to change the examples adapting them to my case, but every time I change something, everything turns red and nothing works anymore.

I know it doesn’t say much like that, but this type of programming is completely new to me and I would have to spend weeks if not months to be able to write the entire code I need.