My Garden Irrigation

Added some extra code to the template switches in order to support unavailable

switch:
  - platform: template
    switches:
      zone1_valve:
        value_template: >-
          {% if is_state('switch.sonoff_name', 'on') %}
          'on'
          {% elif is_state('switch.sonoff_name', 'off') %}
          'off'
          {% else %}
          'unavailable'
          {% endif %}
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.sonoff_name
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.sonoff_name

In case you are interested :slight_smile:

edit: Or even cleaner:

switch:
  - platform: template
    switches:
      zone1_valve:
        value_template: "{{ states('switch.xxx') }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.xxx
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.xxx

props to @123

1 Like

I had a proper look at this now and I must admit I was scratching my head a bit…
Then I realised (I think) what you meant…

This will allow people to name their switches anything they like by setting them up just once here.

I was getting confused by having a sensor switch with the same name as the physical switch and wondering what the point or the benefit was!

Am I right this is just to help people in future? If so it is an even better idea and I really should include it.

But then all the cycles/zones will be affected by a single location’s weather conditions right? The goal would be to be able to use dual locations.

Exactly, i did it this way so that i didnt have to search/replace your naming throughout your code. It’s to help people use their own switch naming :slight_smile: (check another post below that, i edited the code to be shorter)

Regarding the weather adjustments, (It’s raining today here) and i noticed that the “last 3 days adj ratio” does not include the current day. Is that intentional?
I added this piece of code inside your garden_weather_calculations.yaml. so that it updates every 30 minutes the sensor data to the input number, with the current days rain amount.
Did i get it right? or did i miss something?

  - alias: Irrigation Weather - Update average weather conditions every 30 mins
    initial_state: 'on'
    trigger:
      - platform: time_pattern
        minutes: '/30'
        seconds: 00
    action:
      - service: input_number.set_value
        data_template:
          entity_id: input_number.rain_minus0
          value: "{{ states.sensor.rain_minus0.state }}"
...

Lastly, I noticed that when “Use Weather adjustment” is enabled (in my case, after the code above, setting all the timers to zero) and i request a manual cycle, the manual operation will ignore the weather adjustment and run normally. This is also intentional right? Manual cycle ignores weather adjustment

1 Like

Ah… yes. In that case I think it might be a bit harder.

I suspect almost all the weather calculations bit would need to be duplicated. It’s hard for me to say for sure, it is a long time ago I wrote this and it is a scenario I never, ever considered, but it would be very cool if you can adapt it to work!

No :slight_smile:

Rainfall has been the worst part of this whole system until the SmartWeather component arrived a few weeks ago and over the months I have changed the data source and the way it was calculated so many times to try and get a sensible result. So, I think there is an oversight now. Today’s rain is taken into account but todays rain is constantly updated in sensor.rain_minus0

      rain_minus0:
        friendly_name: Rain today
        value_template: "{{ states('sensor.smartweather_rain_today') }}"     
        unit_of_measurement: "mm"

So the input_number.rain_minus0 is actually now redundant. And I didn’t update the script.calculate_average_weather_conditions

      # Add in todays rain
      - service: input_number.set_value
        data_template:
          entity_id: input_number.rain_3days_total
          value: >
            {{ float(states.input_number.rain_3days_total.state) + 
               (float(states.sensor.rain_minus0.state)) }}  

It was different in my version

               (float(states.input_number.rain_minus0.state)) 

I will change it to your code it makes more sense than running an extra automation step :slight_smile:

Yes that is what I had too as I didn’t change it when I moved to SmartWeather.

I’m gonna say yes to this but I don’t remember specifically how I planned it back then but it certainly makes most sense to ignore weather adjustments for a manual run.

Hi,

For safety you typically use a metal box (or metal shielded) and tie that to ground so if anything comes loose inside it would short to ground and blow the fuse.

Just be careful – 240VAC can kill you and being outside and water and such it’s a danger.

You may want to plug it into a GFCI plug to get a bit more safety.

1 Like

I’ve spent some time to think about it and i cant find any easy work-around.
Unless some more experienced user could give us some easy way to duplicate the code, i think that i’d rather spend 40€ and buy an extra rpi and copy/paste the whole thing. (and there’s this home assistant remote that enables you to import all the slave’s entities into a master installation, that might work well but i haven’t looked into it yet)
Or at least write some new code that will simply set timers and turn them off if it’s raining on that specific day.
I think the workload of going through your whole - great - code and adding some _b identifier is just not worth it :slight_smile:

1 Like

I can tie the back metal plate to ground via the incoming two core and earth plug socket which goes back to a consumer unit with earth leakage circuit breakers (is that what you mean by a a GFCI plug?).

So this morning this happened:
I have the timers set to 5 minutes each, schedule enabled at 08:00 and weather adjustment enabled.
Due to weather adjustment, all timers go to 0, as it was raining yesterday.
The system fired at 08:00 i got a notification that it would water the garden and that the total time would be 0 minutes. (OK so far)

The thing is that the cycle started, zone 1 started watering and it worked for ~32 minutes in HA’s history From 08:00 to 08:32.(guessing that’s when your failsafes kicked in).
At ~13:00 I found the UI stuck at zone being watered: 1 and time remaining: idle
Looking into my logbook now, it seems that exactly at 08:00 my automation to restart HA also kicked in.

Any ideas as to where i should look at?

Here are my logbook entries (line order reversed for easier reading and i tried to clear everything non relevant up)
You’ll see that valve1 fires at 08:00 just before hassio restarted

Last 3 days rain adj. multiplyer changed to 1.0
7:30 AM
Adjusted cycle1 zone1 duration changed to 3.230769230769231
7:30 AM
Adjusted cycle1 zone1 duration changed to -2.769230769230769
7:30 AM
Adjusted cycle1 zone1 duration changed to 0.0
7:30 AM
Adjusted cycle1 zone2 duration changed to 3.230769230769231
7:30 AM
Adjusted cycle1 zone2 duration changed to -2.769230769230769
7:30 AM
Adjusted cycle1 zone2 duration changed to 0.0
7:30 AM
Adjusted cycle1 zone3 duration changed to 3.230769230769231
7:30 AM
Adjusted cycle1 zone3 duration changed to -2.769230769230769
7:30 AM
Adjusted cycle1 zone3 duration changed to 0.0
7:30 AM
Adjusted cycle1 zone4 duration changed to 9.153846153846153
7:30 AM
Adjusted cycle1 zone4 duration changed to -7.846153846153847
7:30 AM
Adjusted cycle1 zone4 duration changed to 0.0
7:30 AM
Adjusted cycle1 zone1 duration changed to 1.0769230769230769
7:30 AM
Adjusted cycle1 zone1 duration changed to -0.9230769230769231
7:30 AM
Adjusted cycle1 zone1 duration changed to 0.0
7:30 AM
Adjusted cycle1 zone2 duration changed to 0.5384615384615384
7:30 AM
Adjusted cycle1 zone2 duration changed to -0.46153846153846156
7:30 AM
Adjusted cycle1 zone2 duration changed to 0.0
7:30 AM
Adjusted cycle1 zone3 duration changed to 1.0769230769230769
7:30 AM
Adjusted cycle1 zone3 duration changed to -0.9230769230769231
7:30 AM
Adjusted cycle1 zone3 duration changed to 0.0
7:30 AM
Adjusted cycle1 zone4 duration changed to 1.0769230769230769
7:30 AM
Adjusted cycle1 zone4 duration changed to -0.9230769230769231
7:30 AM
Adjusted cycle1 zone4 duration changed to 0.0
7:30 AM
...
Wind Direction changed to N
7:55 AM
Irrigation - Run Cycle 1 has been triggered
8:00 AM
Irrigation Weather - Update average weather conditions every 30 mins has been triggered
8:00 AM
Temp today changed to 15.0
8:00 AM
Cycle 1 running turned on
8:00 AM
Irrigation - Notify about irrigation events when on holiday has been triggered
8:00 AM
Avg 2 day temp changed to 15.0
8:00 AM
Last 3 days rain adj changed to 1.9
8:00 AM
Last 3 days rain adj changed to 1.0
8:00 AM
Adjusted cycle1 zone1 duration changed to 3.4615384615384612
8:00 AM
#[spam of adjusting cycles here]
Adjusted cycle1 zone4 duration changed to 0.0
8:00 AM
Sonoff irrigation paleo CH 1 turned on
8:00 AM
zone1_valve turned on
8:00 AM
Irrigation Failsafe - Valve 1 on has been triggered
8:00 AM
Restart Hassio in the morning 1h after sunset has been triggered
8:00 AM
Home Assistant stopped
8:00 AM
Home Assistant started
8:01 AM
Battery Charging changed to False
8:01 AM
#[irrelevant spam]
Last 3 days rain adj. multiplyer changed to 1.0
8:01 AM
Zone being watered changed to None
8:01 AM
Last irrigation cycle ended changed to Sat 06 Apr at 21:09
8:01 AM
Next scheduled run time changed to Mon 08 Apr at 08:00
8:01 AM
Next scheduled run time changed to None
8:01 AM
Morning cycle changed to Running
8:01 AM
Afternoon cycle changed to Not running
8:01 AM
Zone being watered changed to 1
8:01 AM
Irrigation Failsafe - master control switch turned on
8:01 AM
Irrigation Weather Data - Collect new and cycle historic weather data turned on
8:01 AM
Irrigation Failsafe - HA start turned on
8:01 AM
[...]
Irrigation - Notify about irrigation events when on holiday turned on
8:01 AM
Irrigation Weather - Update average weather conditions every 30 mins turned on
8:01 AM
Stairts MD ON turned on
8:01 AM
Irrigation Master Control Switch turned on
8:01 AM
[...]
Irrigation Weather - Adjust durations with change in rain or temp baseline turned on
8:01 AM
[...]
Irrigation Weather - Option to use weather adjustment turned on turned on
8:01 AM
[...]
Irrigation Weather - Adjust durations with change in cycle2 baseline duration turned on
8:01 AM
[...]
Irrigation - Update the last time a cycle ended turned on
8:01 AM
[...]
Irrigation Weather - Adjust durations with change in cycle1 baseline duration turned on
8:01 AM
[...]
Irrigation - Run a cycle manually turned on
8:01 AM
[...]
Irrigation - Cancel a manual cycle turned on
8:01 AM
[...]
Irrigation - Run Cycle 2 turned on
8:01 AM
[...]
Irrigation - Set next run time when schedule enabled or time changed turned on
8:01 AM
[...]
Irrigation - Run Cycle 1 turned on
8:01 AM
[...]
Irrigation Failsafe - Valve 4 on turned on
8:01 AM
[...]
Irrigation Failsafe - Valve 5 on turned on
8:01 AM
[...]
Irrigation Failsafe - Valve 3 on turned on
8:01 AM
Irrigation Failsafe - Valve 1 on turned on
8:01 AM
[...]
Irrigation Failsafe - Valve 2 on turned on
8:01 AM
zone1_valve turned on
8:01 AM
[...]
Irrigation Failsafe - master control switch has been triggered
8:01 AM
Irrigation Failsafe - HA start has been triggered
8:01 AM
Irrigation Master Control Switch has been triggered
8:01 AM
[...]
Irrigation Failsafe - Valve 1 on has been triggered
8:01 AM
[...]
Irrigation Weather - Update average weather conditions every 30 mins has been triggered
8:30 AM
Last 3 days rain adj changed to 1.9
8:30 AM
Last 3 days rain adj changed to 1.0
8:30 AM
Last 3 days rain adj. multiplyer changed to 1.0
8:30 AM
Adjusted cycle1 zone1 duration changed to 3.4615384615384612
8:30 AM
Adjusted cycle1 zone1 duration changed to -2.5384615384615388
8:30 AM
Adjusted cycle1 zone1 duration changed to 0.0
8:30 AM
#[more spam of adjusting cycles to 0]
Sonoff irrigation paleo CH 1 turned off
8:32 AM
zone1_valve turned off
8:32 AM
Irrigation - Update the last time a cycle ended has been triggered
8:32 AM
Last irrigation cycle ended changed to Sun 07 Apr at 08:32
8:32 AM

Im also getting some connection errors to the sonoffs right after restart in the log (which is totally unrelated to your code, but might be the issue?:

websocket is not connected
8:01 AM custom_components/sonoff/__init__.py (ERROR) - message first occured at 8:01 AM and shows up 7 times
Error doing job: Task exception was never retrieved
8:01 AM custom_components/sonoff/__init__.py (ERROR)
websocket error: Connection is already closed.
8:01 AM custom_components/sonoff/__init__.py (ERROR)

Yes I think I do!

It looks like it is another result of me never having used the weather adjustments… :scream:

In irrigation_irrigate_a_zone it checks for zero duration but only the standard cycle time…

Do you want to try this? I can’t test it right now…

      #=== Don't continue if duration is zero
      - condition: template
        value_template: >
            {% if is_state('input_boolean.cycle' + cycle + '_use_weather_adjustment', 'on') %}
              {% set n = states('input_number.adjusted_cycle' + cycle + '_zone' + zone + '_duration')  | int %}
            {% else %}
              {% set n = states('input_number.cycle' + cycle + '_zone' + zone + '_duration')  | int %}
            {% endif %}
            {{ n != 0 }}

At least we know the failsafe seems to work :wink:

I’m slightly confused by this. Do you restart HA every day automatically? I’m not totally sure how things will respond to this in the irrigation system because timers are all reset to zero on a restart…

Yeah, at first I thought that something got screwed up becauss the restart happened at the same time with the irrigation cycle (the restart is not at fixed time, it’s around 1h after sunrise).

But then I noticed that zone1 started before the restart occurred. And then it seems that after booting back up, something got stuck.

By standard you mean the manual times and not the weather adjusted ones right?

I’ll be home soon and I’ll try the extra code. (i added extra starting times tpt he drop down so I can test it every 30 mins - hope that’s not messing anything up right?)

Just replicated it:


Standard timer set to 1, Adjusted timer set to 0, Schedule and adjustment enabled.
Zone 1 kicked in, ti shows as active, time remaining: idle and its a;ready been up 3 minutes.
image
At that point, if i click on the manual cycle, everything comes back to normal (dont know if it switches off the valve, i had done that manually) and resets the schedule time to 18:00. for some reason :slight_smile:

Will try with the code block you sent and report in 1h :slight_smile:

GFCI seems to be called a Residual Current Device ( RCD ) in the UK.

Earth Leakage breakers will only help if the box is metal as they sense small voltages and if it becomes dangerous they will trip. Think if you had a metal washer and a wire came loose and the chassis wasn’t grounded it could become live. A ELB should sense this.

Yes I do.

Actually I think that might cause a problem. I am pretty sure when I wrote this that I decided that as I was still learning the HA way of doing things and that I thought I was only writing it for myself that I would keep it simple and only allow for run times on the hour. I think the automation only run on the hour. I’d need to check this…

Let me know if the change I suggested helps.

And, I like your picture, any chance you can share it? :slight_smile:

There ya go :slight_smile:

Just checked HA logs through ssh at 15:30, only weather related scripts ran.
That’s cool, ill just remove the :30 times and leave the :00. (Extra :00 times should be ok right?)

Btw, im not quite sure how recorder and history components really work. Do you think there’ll be an issue if i leave everything on on the recorder and exclude all the entities from history? That would clear my history view right?

Thanks!

Yes, just make sure there is no possibility of a Cycle 1 run overlapping a Cycle 2 run. I do no checking for that either :scream:

Recorder and History: To be honest I am not 100% clear myself(!) what the difference is but I don’t think they affect anything except your ability to see what has happened.

Understood

To my understanding you need recorder enabled in order to retain values of stuff between restarts (plus other stuff)

So the automation run at 16:00, and it executed corretly, no valve went on so it seems like your code fixed it :wink:

2019-04-07 17:00:00 INFO (MainThread) [homeassistant.helpers.script] Script calculate_average_weather_conditions: Executing step call service
2019-04-07 17:00:00 INFO (MainThread) [homeassistant.helpers.script] Script irrigation_irrigate_a_zone: Running script
2019-04-07 17:00:00 INFO (MainThread) [homeassistant.helpers.script] Script irrigation_irrigate_a_zone: Test condition template: True
2019-04-07 17:00:00 INFO (MainThread) [homeassistant.helpers.script] Script irrigation_irrigate_a_zone: Test condition template: False
2019-04-07 17:00:00 INFO (MainThread) [homeassistant.helpers.script] Script irrigation_run_a_cycle: Executing step wait template
2019-04-07 17:00:00 INFO (MainThread) [homeassistant.helpers.script] Script irrigation_run_a_cycle: Executing step call service
2019-04-07 17:00:00 INFO (MainThread) [homeassistant.helpers.script] Script irrigation_irrigate_a_zone: Running script
2019-04-07 17:00:00 INFO (MainThread) [homeassistant.helpers.script] Script irrigation_irrigate_a_zone: Test condition template: True
2019-04-07 17:00:00 INFO (MainThread) [homeassistant.helpers.script] Script irrigation_irrigate_a_zone: Test condition template: False
2019-04-07 17:00:00 INFO (MainThread) [homeassistant.helpers.script] Script irrigation_run_a_cycle: Executing step wait template
2019-04-07 17:00:00 INFO (MainThread) [homeassistant.helpers.script] Script irrigation_run_a_cycle: Executing step call service
2019-04-07 17:00:00 INFO (MainThread) [homeassistant.helpers.script] Script calculate_average_weather_conditions: Executing step call service
2019-04-07 17:00:00 INFO (MainThread) [homeassistant.helpers.script] Script irrigation_irrigate_a_zone: Running script
2019-04-07 17:00:00 INFO (MainThread) [homeassistant.helpers.script] Script irrigation_irrigate_a_zone: Test condition template: True
2019-04-07 17:00:00 INFO (MainThread) [homeassistant.helpers.script] Script irrigation_irrigate_a_zone: Test condition template: False
2019-04-07 17:00:00 INFO (MainThread) [homeassistant.helpers.script] Script irrigation_run_a_cycle: Executing step wait template
2019-04-07 17:00:00 INFO (MainThread) [homeassistant.helpers.script] Script irrigation_run_a_cycle: Executing step call service
2019-04-07 17:00:00 INFO (MainThread) [homeassistant.helpers.script] Script calculate_average_weather_conditions: Executing step call service
2019-04-07 17:00:00 INFO (MainThread) [homeassistant.helpers.script] Script irrigation_run_a_cycle: Executing step call service
2019-04-07 17:00:00 INFO (MainThread) [homeassistant.helpers.script] Script calculate_average_weather_conditions: Executing step call service
2019-04-07 17:00:00 INFO (MainThread) [homeassistant.components.automation] Executing Irrigation - Notify about irrigation events when on holiday
2019-04-07 17:00:00 INFO (MainThread) [homeassistant.helpers.script] Script Irrigation - Notify about irrigation events when on holiday: Executing step call service
2019-04-07 17:00:00 INFO (MainThread) [homeassistant.helpers.script] Script notify_irrigation_end: Running script
2019-04-07 17:00:00 INFO (MainThread) [homeassistant.helpers.script] Script notify_irrigation_end: Executing step call service
2019-04-07 17:00:00 INFO (MainThread) [homeassistant.helpers.script] Script Irrigation Weather - Update average weather conditions every 30 mins: Executing step wait template
2019-04-07 17:00:00 INFO (MainThread) [homeassistant.helpers.script] Script Irrigation Weather - Update average weather conditions every 30 mins: Executing step call service
2019-04-07 17:00:00 INFO (MainThread) [homeassistant.helpers.script] Script irrigation_run_a_cycle: Executing step call service
2019-04-07 17:00:00 INFO (MainThread) [homeassistant.helpers.script] Script adjust_cycle1_durations: Running script
(then it gets into it's weather checking spree)

One thing tho, i should be able to see the script irrigation_irrigate_a_zone or script irrigation_run_a_cycle run on the logs in ssh right? I only see weather calculations again but my buffer starts at 16:00 so maybe i missed something, will check again in an hour.
It ran OK, logs are above :sunny: Looks like i missed the buffer before.

Some more thoughts on this, (duplicating the whole code for mutliple locations). I believe the hard but safest way is to create a second package folder, copy the files there and go through all the code and rename all the occurances of the entities to zone1_valve_location2 for example. That s going to work right?
Then second step for a user that would like to have more locations, would be a very quick find/replace and create instances changing _location2 to _location3, 4 etc.
Am i thinking this correctly?

I hate to keep sounding like I’m evading the question but yes, I think making a copy of the whole package and renaming everything should work. I can think of no reason why a separate copy can’t exist happily in one HA.

I would like to think that the code has been designed to be modular enough* that if you renamed all the existing entities to include a location number it would not be impossible to incorporate two locations by expanding the existing scripts to cope with that, and not have to duplicate all the code.

It would take some work though and some careful testing :wink: Duplicating everything would be tedious but possibly the safest way.

*But that could of course just be wishful thinking!