My Garden Irrigation

Thank you for the fix

Klogg wrote the program in a really great way, reading it and trying to understand it (I’m not a programmer and I started using Hassio, yaml etc. about 6 weks ago, so it’s quite hard for me) is very useful.

About that… switching the master control off switches off the schedules, it’s correct.
Master Control has initial_state: on, so at boot it should be already on and schedules should mantain their previous state.
I don’t know why this doesn’t work and they are switched off anyway.
Bypassing them apparently solves the problem, but they will not turn off if you switch off master control.

I’m trying to adapt notifications to my config.
I changed them to use my telegram bot, but I can’t make them work.
For example the irrigation start:

notify_irrigation_start:    
    sequence:
      - service: notify.telegram_bot
        data:
          message: >
            {% set cycle_map = {
              '1': 'morning',
              '2': 'afternoon'
            } %}
            {% set cycle_time = cycle_map[cycle] %}

            {% if is_state('input_boolean.cycle' + cycle + '_use_weather_adjustment', 'off') %}
              {% set total_time = states('input_number.cycle' + cycle + '_zone1_duration') | int
                                + states('input_number.cycle' + cycle + '_zone2_duration') | int
                                + states('input_number.cycle' + cycle + '_zone3_duration') | int
                                + states('input_number.cycle' + cycle + '_zone4_duration') | int
                                + states('input_number.cycle' + cycle + '_zone5_duration') | int %}

            {% else %}
              {% set total_time = states('input_number.adjusted_cycle' + cycle + '_zone1_duration') | int
                                + states('input_number.adjusted_cycle' + cycle + '_zone2_duration') | int
                                + states('input_number.adjusted_cycle' + cycle + '_zone3_duration') | int
                                + states('input_number.adjusted_cycle' + cycle + '_zone4_duration') | int
                                + states('input_number.adjusted_cycle' + cycle + '_zone5_duration') | int %}
            {% endif %}

            I thought you'd like to know that the {{cycle_time}} irrigation cycle has just started.


            I expect it will take about {{ total_time }} minutes to finish but I'll let you know when it has.


            Don't worry your garden is safe with me. I hope you're having a lovely holiday!
      - service: notify.telegram_bot
        data:
          message: TEST

I added a second TEST notify for testing.
If I just trigger the script nothing happens.
If I comment the first “- service” part and leave only my TEST message, I receive it in telegram.
As wrote before, I’m not good enough to find the problem here.

Irrigation End script act a little different:

notify_irrigation_end:    
    sequence:
      - service: notify.telegram_bot
        data:
          message: >
            {% set cycle_map = {
              '1': 'morning',
              '2': 'afternoon'
            } %}
            {% set cycle_time = cycle_map[cycle] %}

            All the {{cycle_time}} watering is done.

when triggered, I receive “All the watering is done.”

So for both the problem is in the data part, but I can’t figure it out…

Hi @klogg, I think there is a problem to set pulsetime in case someone use more than 1 sonoff 4ch…your script is ok for one sonoff 4ch.

But, For example, having 5 zones… Supposing first 4 are on the “sonoff4ch01”, and zone 5 in on “sonoff4ch02”… The script:


set_sonoff_pulsetime:
    sequence:
      - service: mqtt.publish
        data_template:
          topic: >
            sonoff4ch01/cmnd/pulsetime{{ zone }}
          payload: >
            {% set payload = states('input_number.cycle' + cycle + '_zone' + zone + '_duration')  | int %}
            {% set payload = 100 + (payload * 60) + 30 %}
            {{ payload }}

Doesn’t work cause variable passed “zone” work from 1 to 4…but >= 5 not work cause sonoff internally count the relay from 1 to 4.

So zone 5 must be converted in pulsetime1 (not pulsetime5) command… And referred to sonoff4ch02 (not sonoff4ch01).

Do you agree?

I missed this discussion as I have been on holiday but yes, @xalex75 you are right (of course). The Master Control Switch was something I added just before I put the system into use. It was my first serious HA project and I became a little paranoid about it working, or maybe I should say, failing. Anyway, yes turning off the schedule at every restart is a bit overkill I think…

And I got Telegram notifications every day when it started and finished watering :slight_smile:
Even better though I used the weather adjustments and my garden looks like I was never away - except the grass needs cutting!

Finally, @itajackass, I will need to think about this issue with two sonoff4ch (but not now, I need to unpack from my holiday!). I have one 4ch for zones 1 to 4 and 1 basic for zone 5 which seem to work ok.

This relates to the automation, not the switch itself. An automation can be switched off so that it doesn’t ever trigger.

As an aside, using initial_state: on is not always the best thing. Since v0.95 the way HA behaves at startup has been improved to make it unnecessary unless you have a specific reason to include it. I think it is considered a breaking change because it potentially changes the way HA works but really I think it is a bug fix. Anyway there are other discussions on the forum that can explain it better than I can but I will eventually be removing all initial_state: on from my config.

Thanks for the reply.
As said, I’m new to this world so I have a lot to learn.
Any tip about my telegram notification problem?
I just changed the notification service and kept your data, but I don’t get anything.
About history, rain is now populated, but temperatures keep on staying to zero.
I’m not sure if I will use them or not, I have a little garden, working schedules and notification can be enough.
Anyway, reading your code is very helpful.

I think you are right!

In fact it is completely ignoring the fact that there could be more than 4 zones! Anything over zone4 will I presume, not do anything at all as there is no such thing as pulsetime5.

I need to look into a new way to do that…
(Unless you already have :wink: )

EDIT: Actually I think it is easy. But this solution isn’t very elegant. I am not happy with it but should work until I come up with something better but it is not high priority at the moment as I have other things I need to work on and I won’t be leaving my system unattended again for a while - and my network has never caused me problems anyway!

  set_sonoff_pulsetime:
    sequence:
      - service: mqtt.publish
        data_template:
          topic: >
            {% if zone in ['1', '2', '3', '4'] %}
              {% set sonoff = 'sonoff4ch01' %}
            {% elif zone == '5' %}
              {% set sonoff = 'sonoff06' %}
              {% set zone = zone | int - 4 %}
            {% endif %}

            {{ sonoff }}/cmnd/pulsetime{{ zone }}

          payload: >
            {% set payload = states('input_number.cycle' + cycle + '_zone' + zone + '_duration')  | int %}
            {% set payload = 100 + (payload * 60) + 30 %}
            {{ payload }}

Obviously you’ll need to adjust it to suit your zone numbers and sonoff device names - which is mostly why I am not happy with this solution!

1 Like

Try changing data to data_template

notify_irrigation_start:    
    sequence:
      - service: notify.telegram_bot
        data_template:  <----- CHANGE THIS
          message: >
            {% set cycle_map = {
              '1': 'morning',
              '2': 'afternoon'
            } %}
etc.....

Klogg… Thank you again.
The solution was simple, but not enough to allow me to find it by myself.
It’s working perfectly now!

1 Like

Hi. Yes this is actually a fast solution and work great (with personal edit to suit my own zones).

A solution can be something like:

Set a global array variable (is it possible in HA?) to store your own list of sonoff and for each set the number of relays and zones associated:

Sonoff_list = array(0 => array(“name” => “sonoff1”, “relays” => 4, zones => array(1,2,3,4)), 1 => array(“name” => “sonoff2”, “relays” => 4, zones => array(5,6,7,8) ));

So in your code we can check the variable passed: zone

For example zone = 7

Then we need to loop inside the array and check where zone 7 is on it. to retrieve the name of the sonoff… In this case sonoff2

To set the correct pulsetime N we need to do: 7 MOD 4 (number of relays)… That return 3

If result of MOD IS 0, pulsetime must be the number of relays (4 in this case)… For example zone 12 (is in sonoff3 with 4 zones) 12 mod 4 = 0…0 doesn’t exist and we assume is 4.

Btw this solution is not so good and no sense for few sonoff… Your code is better :slight_smile:

Btw, @klogg is it possible to edit your code to insert a second start time (in schedule) without pain?
For example morning cycle schedule has 2 start times: start at 08:00 and 12:00 also.

I think that would be a big job.
It might be possible and a lot easier to add a third cycle but I’m not sure.

It’s been a while :slight_smile: but that was a good question and the answer was that I really didn’t know any better. However…

Since then I have learnt a fair bit and now use a few ESP8266 boards for some simple stuff using ESPHome. The irrigation system has been running so well we have decided to expand it to possibly eight zones and rather than duplicating all the sonoff/transformer side I am considering something along the lines of what you suggested.

The valve box I use is 24vAC. Is that still a possibility?

Yes, sorry there was a mistake in my previous post. I am using 24Vac switched through the relays. I also used a bridge rectifier and step-down power supply to derive 5Vdc from the 24Vac power supply to run the ESP.

Nice.
Is your setup documented anywhere?
(Pictures, circuit diagram etc.)

Only my code is documented on this forum (here) however I will try to create something regarding the hardware this weekend

1 Like

Hello everyone, I have adapted the code originally written by @klogg (thanks !!!) and customized to my own settings (ESP32 using ESPHome, up to 8 zones, removed the weather related watering adjustments). The ESP is powered with a 5v power supply, the relays are connected to a separate 24v power supply. All works great with one issue : if the relays are powered before the ESP is plugged in then all the valves are opened and water flows until the ESP32 is plugged in. If the house power goes off it should not be an issue since both would be off but i am worried that if the ESP goes bad then all the valves open…I hope someone can spot if there is a problem with my install or wiring ? Thanks in advance everyone !

The code for the ESP is as follows :

esphome:
  name: esp32_1
  platform: ESP32
  board: nodemcu-32s

wifi:
  ssid: "XXXX"
  password: "XXXXXXX"

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "XXXX"

switch:
  - platform: gpio
    name: "Zone1"
    pin: GPIO4
  - platform: gpio
    name: "Zone2"
    pin: GPIO16
  - platform: gpio
    name: "Zone3"
    pin: GPIO17
  - platform: gpio
    name: "Zone4"
    pin: GPIO18
  - platform: gpio
    name: "Zone5"
    pin: GPIO19
  - platform: gpio
    name: "Zone6"
    pin: GPIO21
  - platform: gpio
    name: "Zone7"
    pin: GPIO22
  - platform: gpio
    name: "Zone8"
    pin: GPIO23

Pictures of my wiring as well as my configs are located here.

This is exactly what I am considering doing, inspired by @sparkydave.

Over the last couple of days I have been reading up a bit but I am still at least several levels below expert in the field of ESP32s and electronics so forgive me if this is obvious, but the relay board can be wired so that the relays are either normally open or normally closed. Have you got them wired normally closed?

I have them wired in the normally open port. I tried and switch the wire to the NC port, same behavior, as soon as I plug in the 24v supply the valves all open. I also tried to switch the Plus and the Minus on the 24v power supply, no change…I am a total beginner and have no clue here :slight_smile: btw, congrats on your code again ! Very well documented and so easy to adapt. Thank you !

1 Like