Custom Component to retrieve five days of rain history from OpenWeatherMap

openweathremaphistory

A home assistant sensor that uses the OpenWeatherMap API to get the last 5 days rainfall. A call is made for each day so the scan_interval is set at 3600 seconds (1 hour) as the free tier allows only 1000 calls per day.

This information is used to calculate a factor that can be used to reduce the watering time of the Irrigation Program custom component.

A OpenWeatherMap API Key is required see the OpenWeatherMap custom component for more information.

You need an API key, which is free, but requires a registration.

Calculation

The adjustment factor is calculated based on the the cumulative rainfall for each day. For yesterday the cumulative value is today’s (day 0) rainfall + yesterday’s (day 1) rainfall.

The lowest factor of the up to five days of rainfall is return as the state of the sensor.

factor = 1 - ((cumulative rainfall - daymin)/(daymax - daymin))

If the factor is less than 0 the factor is set to 0.

Attributes

Attributes are returned for:

  • daily rainfall
  • daily cumulative rainfall

This is the first iteration of this and any feedback is appreciated.

3 Likes

Version 1.0.2 released resolving update problems and improvements to API call efficiency.

1 Like

Version 1.04 -

  • Refactored the logic into a class
  • Fixed issue with daily refresh - changed to UTC time
  • Expanded attributes to include min and max temperature
  • Unit system (metric, imperial) config option
  • Reduce refresh time to 30 minutes
  • remove cumulative rain from the attributes

Version 1.0.5

  • Added unique id
  • limited factor to two decimals

Hello,

thank you very much for the integration. Unfortunately, I cannot understand the value of the factor when I recalculate it with the precipitation that actually occurred.

I would like to use the integration to know the actual precipitation for the last 24 hrs. Is it possible to output the precipitation retrieved via the API as an attribute for the last days?

Hi,
The rainfall and min/max temperature are available as attributes. Day 0 is the last 24 hours and so on.

Dear petergridge,

thank you for your answer. :slight_smile:
Are the attributes the precipitation in mm? I thought these would be the calculated factors.

By default it is metric, but you can configure the unit system if required

1 Like

Thanks for the component. I installed it via HACS and added the configuration to configuration.yaml. How low does it take to calculate the rainfactor? Right now, it shows “unknown” for the rainfactor.
Thanks again.

Hi, What version have you installed?

If you have signed up for V3 of the API you will need to instal the beta version 1.2, if you are using the v2.5 API you need to install version 1.1.1

We are working on a more integrated solution that will be available soon.

I signed up for V3 and installed 1.2. The rainfactor value still shows “unknown”. The V3 API call via Web Browser works.

I can see, that, that openweathermaphistory did not make any calls at All.

Can I force an api call via OpenWeatherMapHistory?
Are there some Blogs?

Edit:
I needed to add the sensor to configuration.yaml before downloading via HACS. I Re-downloaded and now it works.

Anyhow, thanks for your help.

NEW Version 2.0.0 Beta

  • A totally new way to access the data!
  • Supports Jinja templates to provide you control over how you utilise the data.
  • Current Observations
  • 8 days of forecast
  • Up to 30 days of history

Hello all

Hey Peter,

Hoping you or someone in this thread can give me a little help on my issue below (maybe I have missed an update or something that has caused this to start up?).

Below is what I am experiencing and I believe any background that would be needed to help troubleshoot.

Thank you again to anyone that can shed some light on how to fix this!:

I have the below error showing at startup that is related to my rainfall sensors (almost as if the sensor isn’t populating. Can someone help me figure out how to address this problem? I think the issue is that the day_0_rain is showing as “null” rather than a numerical value.

2023-06-08 11:32:02.652 ERROR (MainThread) [homeassistant.helpers.template_entity] TemplateError('ValueError: Template error: float got invalid input 'None' when rendering template '{{ state_attr('sensor.rainfactor', 'day_0_rain') | float  + state_attr('sensor.rainfactor', 'day_1_rain') | float  }}' but no default was specified') while processing template 'Template<template=({{ state_attr('sensor.rainfactor', 'day_0_rain') | float  + state_attr('sensor.rainfactor', 'day_1_rain') | float  }}) renders=4>' for attribute '_attr_native_value' in entity 'sensor.previous_rainfall'

Below is my sensor:

      previous_rainfall:
        friendly_name: "Past 48h Rainfall"
        value_template: "{{ state_attr('sensor.rainfactor', 'day_0_rain') | float  + state_attr('sensor.rainfactor', 'day_1_rain') | float  }}"

And my daily rainfall sensor

      - name: "Daily Rainfall"
        #unit_of_measurement: "in"
        state: "{{ state_attr('sensor.rainfactor', 'day_0_rain') }}"
        state_class: measurement

Below is a screenshot of developer tools on “day_0_rain”. I don’ really understand what would’ve caused it to stop populating all of the sudden.

Thank you all again!!

Rainfactor sensor is also showing unavailable:

Additional info:

For some reason the below was commented out of my config

  - platform: openweathermaphistory
    name: "rainfactor"
    api_key: !secret owmh_api_key
    latitude: !secret owmh_latitude
    longitude: !secret owmh_longitude
    # unit_system: imperial

Also, after looking at the link below, I’m questioning if I am using the proper latest version (I usually

I believe I am up to speed now on the new component. Can someone help me determine how to handle the screen below? I don’t really understand what to put in it even after reading the github.

I don’t really understand what to put in the jinja2 template area
My goal is to have the past 48 hour cumulative rainfall and past 5 day cumulative rainfall.

Below is what I have so far:

I am trying to get to the below essentially (as in an earlier post):

5489a25dd62772fbfe449b503689448dbba132fa_2_517x255

Update: I ended up moving forward with the below in the dialog box

Here is the result in developer tools. It looks like the rainfall is probably in mm and I don’t feel like I am seeing all of the attributes I spec’d out in the initial list above.

Thank you all again for help with troubleshooting.

Hi,

You are on the right track.

Try these as the template to give you the equivalent to the rain sensor value you previously received.
Version 1 factor, verifying to an expected 10mm rainfall

{{ 
  [(10 
  - day0rain 
  - day1rain*0.5
  - day2rain*0.25
  - day3rain*0.12
  - day4rain*0.06)/10
  ,0]|max
}}

Factor utilising forecast rain and probability of precipitation

{{ 
  [(10 
  - day0rain 
  - day1rain*0.5
  - day2rain*0.25
  - forecast1rain*forecast1pop*0.5
  - forecast2rain*forecast2pop*0.25)/10
  ,0]|max
}}

Homeassistant do not recommend using attributes that have changing values on sensors as it impacts the history and statistics performance. I kept the feature to support users that relied on the functionality.

The precipitation in the attributes is in mm however if you add them as sensors and select the correct type they will be converted to imperial automatically by HA. The same applies to the temperature.

I can see you configured 7+ days into your attributes. Can you provide a screenshot of the setup, redact the API Key of course. This may be a bug

Hi, thanks a lot for this. Could you please help me to understand the results here? E.g. with the template above I’m getting this results:

day0rain: 2.4099999999999997
day1rain: 1.02
day2rain: 11.3
forecast1rain: 0
forecast2rain: 0

What does it mean?

  • rain factor: 0.4255 → what does that mean?
  • day0rain: 2.4099999999999997 → it was raining 2.4mm today?
  • forecast1rain: 0 → no rain expected today?

Thanks a lot!

Hi,

There is a explanation of the data elements here: GitHub - petergridge/openweathermaphistory: A home assistant sensor that uses the OpenWeatherMap API to get forecast, current obs and history data

Rain factor is simple trying to calculate a factor that can be used to affect a change to the watering time for my irrigation system. The calculation is essentially assuming 10mm of rain is the cut off where no watering is required. The multipliers e.g. 0.5 is assigning a reduced importance to rain from yesterday. And so on.

This is only an example and you can modify this to suit your needs.

Day0Rain is how much rain (mm) in the last 24 hrs
Forecast1rain is the rain expected tomorrow i.e. 25-48 hours in the future

I hope this helps.

This looks very interesting. I have set up a sensor.rainfactor and it gives me some value (e.g. 0.94).

I also have an automation for my irrigation which currently looks like this:

alias: Bewässerung Rasen
description: 6:00 Uhr
trigger:
  - platform: time
    at: "06:00:00"
condition:
  - condition: time
    weekday:
      - tue
action:
  - type: turn_on
    device_id: a1225731c83ba4e762e749c6da2e36cb
    entity_id: switch.hm_lc_sw4_pcb_req1391537_ch3
    domain: switch
  - delay:
      hours: 1
      minutes: 0
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: a1225731c83ba4e762e749c6da2e36cb
    entity_id: switch.hm_lc_sw4_pcb_req1391537_ch3
    domain: switch
mode: single

How can I use the rainfactor in my automation? As petergridge suggested, I would probably change the duration from
1 h
to
sensor.rainfactor * 1 h

But how can this be accomplished?

Hi,

Check out GitHub - petergridge/Irrigation-V5: Irrigation custom component for Home Assistant also available on HACS. This will allow you to set up your schedule and apply the rain adjustment, no need to maintain automations and also has a custom card.

To apply the delay to your automation

- delay:
    seconds: "{{ 3600 * states.sensor.rainfactor.state|float }}"

Assuming a one hour base watering time.

1 Like

Hi @petergridge,

thank you for this wonderful integration!
I’ve a beginners question:

In your example sensor configuration:

{{ 
  [(10 
  - day0rain 
  - day1rain*0.5
  - day2rain*0.25
  - forecast1rain*forecast1pop*0.5
  - forecast2rain*forecast2pop*0.25)/10
  ,0]|max
}}

What does ,0 and |max stand for?

All the best
Benedikt