Smart Sprinkler Automation (skip watering if there is rain in next 48 hours - 2 days)

Like this:

        value_template: >-
          {% set p0 = state_attr('weather.openweathermap', 'forecast[0].precipitation') == None %}
          {% set p1 = state_attr('weather.openweathermap', 'forecast[1].precipitation') == None %}
          {{ 'false' if p0 and p1 else 'true' }}

or like this:

        value_template: >-
          {% set p0 = state_attr('weather.openweathermap', 'forecast[0].precipitation') == None %}
          {% set p1 = state_attr('weather.openweathermap', 'forecast[1].precipitation') == None %}
          {{ not (p0 and p1) }}
2 Likes

That worked.
I used the first one
Thank you Thank you

here is my completed working setup configuration:

configuration.yaml

weather:
  - platform: openweathermap
    api_key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    latitude: 00.00000
    longitude: 000.00000
    mode: freedaily

switches.yaml

# EspEasy and MQTT (created a switch device to monitor gpio status)
  - platform: mqtt
    name: "Walkway Sprinkler"
    command_topic: "/WalkwaySprinkler/gpio/14"
    state_topic: "/WalkwaySprinkler/Sprinkler/Switch"
    qos: 1
    payload_on: "0"
    payload_off: "1"
    retain: true

sensors.yaml

# Using openweathermap forecast for today and tomorrow ([0] and [1])
rainy_day:
        friendly_name: "Rainy in next 48 Hours"
        value_template: >-
          {% set p0 = state_attr('weather.openweathermap', 'forecast[0].precipitation') == None %}
          {% set p1 = state_attr('weather.openweathermap', 'forecast[1].precipitation') == None %}
          {{ 'false' if p0 and p1 else 'true' }}

automation.yaml

- id: '1560827530261'
  alias: Switch Walkway Sprinkler
  trigger:
# Start watering at 6AM
  - at: 06:00:00
    platform: time
  condition:
  - condition: state
    entity_id: sensor.rainy_day
    state: 'false'
  - condition: time
# only on below weekdays
    weekday:
    - mon
    - wed
    - fri
  action:
  - data:
      entity_id: switch.walkway_sprinkler
    service: switch.turn_on
# Water for 15 Minutes
  - delay: 00:15:00
  - data:
      entity_id: switch.walkway_sprinkler
    service: switch.turn_off
1 Like

Glad to hear that the latest version of the template works correctly for you.

To make it easier for others seeking a similar solution, please mark my previous post (the one containing the two functional versions of the template) as the ‘Solution’ (only the author of a topic can mark a post with this tag). After you do that, a link to the ‘Solution’ post will automatically appear under your first post.

marked it :slight_smile:

1 Like

Trying to implement your logic. When I run the following in Template Editor
p0 is {{ states.weather.openweathermap.attributes.forecast[0].precipitation }}
p1 is {{ states.weather.openweathermap.attributes.forecast[1].precipitation }}

result is
p0 is None
p1 is 0.6

When I run this in Template Editor from your sensor.yam
{% set p0 = state_attr(‘weather.openweathermap’, ‘forecast[0].precipitation’) == None %}
{% set p1 = state_attr(‘weather.openweathermap’, ‘forecast[1].precipitation’) == None %}
{{ ‘false’ if p0 and p1 else ‘true’ }}
the result is
false

Shouldn’t I be getting true since I am showing 0.6 for p1 in the first result? I am not clear what the 0.6 represents?

when there will be no rain (precipitation) then the result is None and in my logic when there is no rain today or tomorrow then it will send false, otherwise you get true.
precipitation result represents the amount of rain.
e.g.
Light Rain: A rain rate of 0.10 inches per hour or less
Rain: A rain rate of 0.11 to 0.30 inches per hour
Heavy Rain: A rain rate of 0.31 inches per hour or greater.

I just updated my logic to below and it’s working for me

{% set p0 = state_attr('weather.openweathermap', 'forecast')[0].precipitation != None %}
{% set p1 = state_attr('weather.openweathermap', 'forecast')[1].precipitation != None %}
{{ 'true' if p0 or p1 else 'false' }}

so if today or tomorrow precipitation result exists you will get true and the last line is checking if there is a true for today OR tomorrow.
there are many different paths you can take like instead of using precipitation you can use condition

{% set p0 = state_attr('weather.openweathermap', 'forecast')[0].condition == "rainy" %}
{% set p1 = state_attr('weather.openweathermap', 'forecast')[1].condition == "rainy" %}
{{ 'true' if p0 or p1 else 'false' }}

hope this help you

2 Likes

@arvage is there a possibility to activate the trigger at 6AM the next day?
Then I could be sure, that the day before was no rain too.

Thanks for this, just implemented! Stupid question but so it displays nicely in Lovelace, I’m assuming we can just change true/false to Yes/No?

Yes you can customize it the way you like

1 Like

Hey Armin. I’ve just moved OWM over to the integration under 0.115.2 and I’ve lost sensor.openweathermap_forecast_precipitation and sensor.openweathermap_forecast_temperature_low. Have you see that too?

I haven’t upgraded to 0.115.2 yet. I’ll do it today and will let you know.

Just checked and I have the same problem! we need to report the issue.

Issue reported.

Thanks, appreciate that.

Thank you to @arvage and @123 for this post. I’m new to HA (and Python, YAML, Jinja and the template language) so this post helped me a lot in getting my watering system going. I ran into a few problems and I thought I’d share the details in case it helps any other newbies.

The first problem is that (for me) forecast precipitation is always null so the only useful forecast value is condition, I’m using the next 4 days with this code:

  - platform: template
    sensors:
      rainy_day:
        friendly_name: "Rain in next 48 Hours"
        value_template: >-
          {% set p0 = state_attr('weather.openweathermap', 'forecast')[0].condition == 'rainy' %}
          {% set p1 = state_attr('weather.openweathermap', 'forecast')[1].condition == 'rainy' %}
          {% set p2 = state_attr('weather.openweathermap', 'forecast')[2].condition == 'rainy' %}
          {% set p3 = state_attr('weather.openweathermap', 'forecast')[3].condition == 'rainy' %}
          {{ 'Yes' if p0 or p1 or p2 or p3 else 'No' }}

image

forecast precipitation is always null unless there will rain.

I see there is now a PR to have this fixed? https://github.com/home-assistant/core/pull/39839

I put the pull request and they were working on It. Not sure if it’s been applied to the HA yet. I’m getting null on precipitation which is correct for when there is no rain so maybe they did. I have to check.

No, not yet. You can see this by looking at the temperature low forecast, which also and still is “unknown” (same bug, same fix)

This looks rather promising… How can i import the .yaml filesin my home assistant? I tried importing the blue prints but it’s giving me errors.

I’ve got a setup like this where i want to automate the watering, between May and Oktober with a groundwater pump on a smart switch.