Start lawn sprinkler if yesterday and today is no rain

Hi,

I am planning to start my lawn sprinkler between 5am and 6am if yesterday was no rain and today is also no rain predicted. My idea was to use the sensor dark_sky_precip_intensity. But I don’t know how to get the amount of rain from yesterday or today. In DarkSky with “mode: Daily” I still get only see the hourly forecast.
Anybody who can support me with this?

forecast takes a list of days you want to track. Each day you add here will be a new sensor with all of the monitored conditions. Same with hourly.

So if you add precip_probability and precip_intensitiy, you’ll get 2 sensors for each forecast day (or hourly) you added.

Getting future predictions is easy, it’s built into the sensor! But, I don’t think they have the time machine part integrated. https://darksky.net/dev/docs#time-machine-request I think that would be pretty useful to have (allow users to put in negative days for the forecast to get X days ago reports).

Because of no past history, I’d create an input_boolean which turns on if it rains. This boolean will turn off at the 5am automation that checks if the sprinkler should turn on.

sensor:
  - platform: darksky
    api_key: YOUR_API_KEY
    forecast:
      - 0
      - 1
    hourly_forecast:
      - 0
      - 1
    monitored_conditions:
      - precip_intensity
      - precip_type
      - precip_probability

Feel free to add more days/hours and monitored conditions for other things.

precip_intensity is the current precipitation in mm/hr. We will check this periodically to turn on the input_boolean if it is raining.

input_boolean:
  rained_yesterday:
    name: Rainfall yesterday
    icon: mdi:weather-pouring

Now create an automation that turns that on the boolean if it rains.

- alias: rain_meter
  trigger:
    # Run every 1h 15 minutes. 
    platform: time_pattern
    hours: "/1"
    minutes: "15"
conditions:
    # No need to do anything if this is already on
  - condition: state
    entity_id: input_boolean.rain_meter
    state: 'off'
    # If there was no accumulation, nothing to do.
  - condition: template
    value_template: "{{ not is_state('sensor.dark_sky_precip_accumulation_0h', 'unknown') }}"
    # Only care about rain...not snow
    # This may be redundant with the above...but shouldn't hurt, right? 
    # Valid values are 'snow', 'rain', 'sleet'. 
  - condition: template
    value_template: "{{ is_state('sensor.dark_sky_precip_0h', 'rain') }}"
  - condition: numeric_state
    entity_id: 'sensor.dark_sky_precip_intensity_0h'
    # Check https://water.usgs.gov/edu/activity-howmuchrain-metric.html to see how much rain
    # we consider enough to call it 'raining'. 
    # Anything above 0.5mm/hr is 'moderate rain'. 
    above: 0.5
 action:
   # It rained today. Turn this on.
   service: input_boolean.turn_on
   entity_id: input_boolean.rain_meter

OK, now that boolean should turn on if it rains more than 0.5mm/hr any given hour. You can tweak this algorithm to turn on in any method. It is decoupled from the next part so that you can upgrade the weather sensor or algorithms and still leave the next automation the same. (Though, I say that and realize the next automation will be checking the future weather for rain…)

- alias: Sprinkler Time
  name: Turn on sprinkler if no rain.
  trigger: 
    platform: time
    # Check at 530 am
    at: "05:30:00"
  condition:
    # If it's going to rain today, there's nothing to do. Just leave the
    # input_boolean.rain_meter on since today already counts as rain. 
    platform: template
    # Check the 1d for the current day's forecast. The prcip_intensity uses
    # should use the current day's precip_probability to determine a value...
    # but I'm not exactly sure. Might just have to use the precip_probability...
    # Basically, if today's forecast says there will be moderate rain, don't do anything. 
    # Definately tweak this condition and test it out. Check this state in developer tools on
    # a day when it's supposed to rain and see what this value reports. 
    value_template: "{{ states('sensor.dark_sky_precip_intensity_1d') | float > 0.5 }}"
  action:
    # It's easier to let a script do the sequence for us since we can pass in a variable that
    # will have it's scope valid for the entire script. 
    # If we'd have to try to get fancy with service_templates and conditions. 
    service: script.turn_on_sprinklers_if_no_rain
    data_template: 
      # Pass in True or False if it rained yesterday.
      rain_yesterday: "{{ is_state('input_boolean.rain_meter', 'on') }}"
script:
  turn_on_sprinklers_if_no_rain:
    sequence: 
      # In ALL conditions, turn off this boolean. Even if it's off, doesn't matter. 
      # The state of it is preserved in {{ rain_yesterday }} data field. 
      - service: input_boolean.turn_off
        entity_id: input_boolean.rain_meter
      - condition: template
        # If this condition evaluates to false, this script will exit. 
        # So if there was no rain yesterday, we want to continue. 
        value_template: "{{ not rain_yesterday}}"
        # If we made it here, it didn't rain yesterday. This script doesn't check the 
        # current day weather, we leave that up to the automation. 
        # But you could just as easily add it here instead.
        # TODO: Update this service call to actually turn on your sprinkerls. 
      - service: sprinkler.turn_on
        entity_id: sprinklers.money_maker

Future Enhancements:

  1. You could set another input boolean for if it’s going to rain today. This would decouple your entire automation from the weather sensor and have it tied to these 2 input booleans. I like doing this so that I can update/enhance just the weather part while leaving the sprinkler automation/script alone. But it’s not too important.

  2. Could find a better way to get past weather. There might be one that gives previous day’s weather. In this case, you could remove the input booleans and simply query the one sensor for yesterday and today’s weather.

1 Like

Thanks!

I was thinking about checking forecast today and tomorrow. And if there is less than 1mm rain, then my sprinkler should start tomorrow at 5am for one hour. This should be possible with DarkSky or OpenWeatherMap too.

Yes, that works too. Rather than look backwards, you could look forwards (even easier!!).

Every morning, check today and tomorrows forecast. If both show no rain, turn on the boolean.

If the boolean is on, the sprinklers start.

input_boolean:
  sprinklers_scheduled:
    name: Sprinklers scheduled to run
    icon: mdi:weather-sunny
- alias: Sprinkler Time
  name: Schedule sprinklers for tomorrow
  trigger: 
    platform: time
    # This has to run AFTER the scheduled sprinkler time, else it will turn it on for today. 
    at: "07:00:00"
  conditions:
    # If today's forecast shows no rain, continue
    - condition: numeric_state
      entity_id: 'sensor.dark_sky_precip_intensity_0d'
      above: 0.5
    # If tomorrow's forecast shows no rain, continue
    - condition: numeric_state
      entity_id: 'sensor.dark_sky_precip_intensity_1d'
      above: 0.5
  action:
    service: input_boolean.turn_on
    entity_id: input_boolean.sprinklers_scheduled
    

Now we simply turn sprinklers on if that boolean is set.

- alias: Sprinkler time
  name: Turn on sprinklers
  trigger:
    platform: time
    at: "05:30:00"
  conditions:
    condition: state
    entity_id: input_boolean.sprinkler_time
    state: 'on'
  action:
    # Turn off the schedule so it has to be rescheduled.
    - service: input_boolean.turn_off
      entity_id: input_boolean.sprinkler_time
    # Start the sprinklers. 
    - service: sprinkler.turn_on
      entity_id: sprinkler.make_it_rain

Just pick your favorite weather applications to determine if/how much rain. For dark sky, precip_accumulation only works with snow, so we can’t tell how much it rained. All we can see is how much it might rain in terms of mm/hr.

I would create an input_datetime to store the sprinkler’s scheduled on time, then you can trigger based on this time rather than hard coding 530 am and 7am. Also would let you pick the on-time in lovelace with a nice datetime picker.

1 Like

that look really nice, thank you!

Only one more question:
If today (monday) and tomorrow (tuesday) is no rain the boolean will be set. Thats fine. But if it runs on the next day and forecast is sunny for today (tuesday) and day after (wednesday) rain, will the boolean reset? Because if it does, it won’t start the sprinkler… but as far as I understand, it won’t change…

Yep, that’s why I check the weather AFTER starting the sprinklers.

So if monday and tuesday have no rain (this was check at 7 am), boolean will turn on.

Tuesday morning at 530 am, sprinklers will run.

7 am tuesday, it will check tuesday weather and wednesday weather. If still clear, it will turn it back on. If not, it will turn it off.

1 Like

Perfect, thanks!

If I add - delay: 00:15:00 at action it will sprinkle for 15 minutes, right?

Not sure how your sprinkler integration works.

Yeah, it could be something like

- service: sprinkler.turn_on
  entity_id: sprinkler.make_it_rain
- delay: "00:15:00"
- service: sprinkler.turn_off
  entity_id: sprinkler.make_it_rain
1 Like

I configured all but I have some issues… Maybe someone can help?

Logs:
Invalid config for [automation]: [name] is an invalid option for [automation]. Check: automation->name. (See /config/configuration.yaml, line 13).
Invalid config for [automation]: [name] is an invalid option for [automation]. Check: automation->name. (See /config/configuration.yaml, line 13).

automations.yaml

###############
# Gartenpumpe #
###############
- alias: 'SprinklerTime'
  name: Schedule sprinklers for tomorrow
  trigger: 
    platform: time
    # This has to run AFTER the scheduled sprinkler time, else it will turn it on for today. 
    at: "07:00:00"
  conditions:
    # If today's forecast shows no rain, continue
    - condition: numeric_state
      entity_id: 'sensor.dark_sky_precip_intensity_0d'
      above: 0.5
    # If tomorrow's forecast shows no rain, continue
    - condition: numeric_state
      entity_id: 'sensor.dark_sky_precip_intensity_1d'
      above: 0.5
  action:
    service: input_boolean.turn_on
    entity_id: input_boolean.sprinklers_scheduled


# Beregnung einschalten, wenn Boolean gesetzt ist

- alias: 'Sprinkler time'
  name: Beregnung aktivieren
  trigger:
    platform: time
    at: "05:00:00"
  conditions:
    condition: state
    entity_id: input_boolean.sprinkler_time
    state: 'on'
  action:
    # Turn off the schedule so it has to be rescheduled.
    - service: input_boolean.turn_off
      entity_id: input_boolean.sprinkler_time
    # Start the sprinklers. 
    - service: sprinkler.turn_on
      entity_id: switch.gartenpumpe
    # For 15 Minutes
    - delay: "00:15:00"
    - service: sprinkler.turn_off
      entity_id: switch.gartenpumpe

configuration.yaml


# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Uncomment this if you are using SSL/TLS, running in Docker container, etc.
# http:
#   base_url: example.duckdns.org:8123

# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

# Sprinkler
# Wenn Boolean auf ON, dann Beregnung starten
input_boolean:
  sprinklers_scheduled:
    name: Sprinklers scheduled to run
    icon: mdi:weather-sunny

I noticed the same thing. Just remove the name from SprinklerTime automation.

Yeah, @ryanm7780 is right. For some reason I put a name field in there. But that doesn’t exist for automations. The ‘alias’ is the name.

Another issue/typo I noticed: conditions: -> condition:

Thanks!

1 Like

One other thing…in the SprinklerTime automation, wouldn’t you want to negate that expression for the forecast? This is how it’s spelled out now:

# If tomorrow's forecast shows no rain, continue
    - condition: numeric_state
      entity_id: 'sensor.dark_sky_precip_intensity_1d'
      above: 0.5

But isn’t that saying that to continue execution (to run the sprinklers) the predicted rain intensity for tomorrow must be greater than 0.5. Unless I am misunderstanding how that sensor works, I’d think you’d want something more like this:

# If tomorrow's forecast shows no rain, continue
    - condition: numeric_state
      entity_id: 'sensor.dark_sky_precip_intensity_1d'
      below: 0.5

I want to activate the switch (of my plug) to start the sprinklers tomorrow morning if today and tomorrow is less rain than 0.5mm

Think you are right, both lines should have below to activate the switch/sprinklers…

Makes sense to me, then. I tweaked the automation to look like this, since I decided to forego handing it off to a script and just do it all in once place:

- alias: Sprinkler Time
  trigger: 
    platform: time
    # Check at 530 am
    at: "05:30:00"
  condition:
    # If today's forecast shows no rain, continue
    - condition: numeric_state
      entity_id: 'sensor.dark_sky_precip_intensity_0d'
      below: 0.5
    # If tomorrow's forecast shows no rain, continue
    - condition: numeric_state
      entity_id: 'sensor.dark_sky_precip_intensity_1d'
      below: 0.5
    # If it didn't rain yesterday
    - condition: state
      entity_id: input_boolean.rained_yesterday
      state: 'off'
  action:
    - service: scene.turn_on
      entity_id: scene.regular_watering
    - service: notify.telegram_main
      data_template:
        message: "The sprinklers are running because it apparently didn't rain yesterday and rain isn't predicted today or tomorrow."
1 Like

Mine still doesn’t work…

How i create a service?
I have “sprinkler.turn_on” but it seems that it runs into an issue as you can see in my screenshots…
And if I look in developer menu - services, I can’t find the service.

Full code:

  action:
    # Turn off the schedule so it has to be rescheduled.
    - service: input_boolean.turn_off
      entity_id: input_boolean.sprinkler_time
    # Start the sprinklers. 
    - service: sprinkler.turn_on
      entity_id: switch.gartenpumpe
    # For 15 Minutes
    - delay: "00:15:00"
    - service: sprinkler.turn_off
      entity_id: switch.gartenpumpe


I just 100% made up the service ‘sprinkler.turn_on’. I don’t have any sprinklers so wasn’t sure what the syntax would even be!

You’ll have to replace those services with however you currently turn them on/off.

  action:
    # Turn off the schedule so it has to be rescheduled.
    - service: input_boolean.turn_off
      entity_id: input_boolean.sprinkler_time
    # Start the sprinklers. 
    - service: ...
    # For 15 Minutes
    - delay: "00:15:00"
    # Stop the sprinklers
    - service: ...

It looks like it might just be “switch.turn_on” instead of “sprinkler.turn_on”, but I don’t know what the switch name is for you.

My switch is called “switch.gartenpumpe”, so I have to write “switch.gartenpumpe.turn_on”?

- service: switch.turn_on
  entity_id: switch.gartenpumpe

Then another for switch.turn_off

1 Like

I’ve found another thing which is wrong I think…

If it’s less rain than 0.5 I set the boolean to on:

  action:
    service: input_boolean.turn_on
    entity_id: input_boolean.sprinklers_scheduled

Then I start the sprinkler but here if have to change the entity from sprinkler_time to sprinkler_scheduled, right?

# Start sprinkler if Boolean is on

- alias: 'Startzeit Beregnung'
  trigger:
    platform: time
    at: "05:00:00"
  condition:
    condition: state
    entity_id: input_boolean.sprinkler_scheduled # instead of sprinkler_time
    state: 'on'