How to track if it rained in the last 6 hours?

My end goal is to create automation that disables my automatic Lawn Mower in case there was any rain over the past 6 hours.

To track when it rains I’m using the OpenWeatherMap integration. But since I’m using the free tier - I can only use the Hourly API which returns weather forecast for the next hour only (instead for the whole day).

How do I make this work?

I was thinking of using the sensor.openweathermap_forecast_precipitation history data to check if it was raining over the past 6 hours:

As can be seen from the screenshot - the history is there.
But I’m not sure how to translate this into actual history stats.
Could you help me define the yaml please?

  - platform: history_stats
    name: rain_over_past_6_hours
    entity_id: sensor.openweathermap_forecast_precipitation
    state: <WHAT-DO-I-PUT-HERE?>
    type: time
    start: <WHAT-DO-I-PUT-HERE-TO-MAKE-IT-6-HOURS-FROM-NOW?>
    end: "{{ now() }}"

Additionally, after I create the history stats - how can I define a simple boolean switch that says whether it has rained or not depending on the history stats?

Hi, is this information accurate enough?
To have a better result you could get a rain sensor.

Because of the way History stats sensors work you should create a binary sensor first then use that in the History Stats…

template:
  - binary_sensor:
      - name: "OWM Precipitation"
        state: "{{ states('sensor.openweathermap_forecast_precipitation') | float(0) > 0 }}"
        availability: "{{ has_value('sensor.openweathermap_forecast_precipitation') }}"
sensor:
  - platform: history_stats
    name: rain_over_past_6_hours
    entity_id: binary_sensor.owm_precipitation
    state: 'on'
    type: time
    start: "{{ now() - timedelta(hours=6) }}"
    end: "{{now()}}"

With this setup you can use a State condition in your automation like the following:

...
condition:
  - condition: state
    entity: sensor.rain_over_past_6_hours
    state: '0.0'
...
2 Likes

Yeah, it’s a very decent approach.
The forecasting itself may not be supe accurate but the current weather is - or in other words OpenWeatherMap knows very well if it’s raining at the moment or not. So if we store that history and put it in a switch it’s actually pretty trustworthy. I don’t think I need a rain sensor because other people already installed them and this is where OpenWeatherMap takes its data from

Works like a charm! Thank you!

So this is my final configuration that used to work fine for the past 22 days. However, I noticed that it doesn’t work anymore - the history stats shows positive value when not raining and vice-versa - 0 value when it starts raining. I’m trying to wrap my head around why this is the case.
I recently updated HASSIO so it might be related, although not really sure how.

template:
  - binary_sensor:
      - name: "Is it raining"
        state: "{{ not is_state('sensor.openweathermap_precipitation_kind', 'None')}}"
        availability: "{{ has_value('sensor.openweathermap_precipitation_kind') }}"
sensor
  - platform: history_stats
    name: rain_over_past_6_hours
    entity_id: binary_sensor.is_it_raining
    state: 'on'
    type: time
    start: "{{ now() - timedelta(hours=6) }}"
    end: "{{now()}}"



From the graphics it really looks like the history stats works in an inverted way - when it has been raining for the past 6 hours - the value is 0 and vice-versa - when there hasn’t been any rain the value is above 0. But why is that? This started happening since last HASSIO update and I already checked the graphics for the previous days - the history stats works as expected there and is on-par with the binary sensor.
Any suggestions?

@mdzhigarov I would like to do the same. Did you get it working again?

I haven’t been able to recreate Marin’s issue, my sensors are still working as expected.

Sensor Configurations
template:
  - binary_sensor:
      - name: Precipitation
        unique_id: precipitation_00001
        state: "{{ not is_state('sensor.openweathermap_precipitation_kind', 'None') }}"
        availability: "{{ has_value('sensor.openweathermap_precipitation_kind') }}"

      - name: Rained in Last 6 Hours
        unique_id: rained_in_six_hours_0001
        state: "{{ states('sensor.precipitation_duration_past_6_hrs')|float > 0  }}"
        availability: "{{ has_value('sensor.precipitation_duration_past_6_hrs') }}"

sensor:
  - platform: history_stats
    name: "Precipitation duration past 6 hrs"
    entity_id: binary_sensor.precipitation
    state: "on"
    type: time
    start: "{{ now() - timedelta(hours=6) }}"
    end: "{{ now() }}"
1 Like