Hoping to get a little help on getting "today rainfall", "yesterday rainfall", and "past 48h rainfall" into a sensor in my Home Assistant instance

There is a custom card that shows the data for the OpenWeatherMapHistory sensor

image

Update on the history sensor solution, the following sensor will give you 48 hours worth of data.

    sensor:
      - platform: statistics
        name: "Rain last 48 hours"
        entity_id: sensor.daily_rainfall
        state_characteristic: total
        max_age:
          hours: 48

A small change for the template sensor for this to work, add the state_class: measurement stanza.

    template:
        - trigger:
          - platform: time_pattern
            # This will update every night
            hours: 0
            minutes: 0 
          sensor:
          - name: "Daily Max Temp"
            unit_of_measurement: "°C"
            state: "{{ state_attr('sensor.rainfactor', 'day_0_max') }}"
            state_class: measurement
          - name: "Daily Min Temp"
            unit_of_measurement: "°C"
            state: "{{ state_attr('sensor.rainfactor', 'day_0_min') }}"
            state_class: measurement
          - name: "Daily Rainfall"
            unit_of_measurement: "mm"
            state: "{{ state_attr('sensor.rainfactor', 'day_0_rain') }}"
            state_class: measurement
1 Like

Thank you so much for this @petergridge , you really didn’t have to do this. I am very appreciative of it though. – I would have eventually figured it all out… maybe :slight_smile:

And do both the sensor code and template code just go in a package and they will just start working?

Last question, so if I want a historic graph, I essentially just graph the sensor “Daily Rainfall”? and I can use “Daily Rainfall” within the “HIstory Stats” integration?

Thank you so much for this!!! This is great!

Can I use hours: 24, since it is called “daily” or will that not work properly? Sorry for such a basic question.

Both snippets can go into a package, restart HA an they should start working.

These will get you started just create multiple version of the statistics with different parameters to see what the behaviour is. I haven’t had history running long enough to really see what it will give me.

This is the first time I have looked at this feature in HA. I am now thinking I should be recording hourly rainfall and temperature and simply using this feature instead of the custom component. HA keeps improving and giving new capabilities that make some of the old methods obsolete.

Try using the statistics capability on these:
- sensor.openweathermap_rain
- sensor.openweathermap_temperature
- sensor.openweathermap_uv_index
- sensor.openweathermap_pressure
- sensor.openweathermap_humidity

Ahhh, I follow you. Thank you for the response Peter. And thank you for being so helpful.

I follow you.

I understand.

Ok, can you think of what may be going on below? It’s almost as is the rain is still coming through in millimeters rather than inches where I am. The temperature looks correct and it’s in degrees F. Thank you for any assistance you can give. Everything else seems to be working properly.

My configuration is:

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

And one last question would be, how can we clear a data point of of the statistics sensors if we figure out the above?

Thank you Peter for everything you are/ have helped me with.

I just reviewed the API documentation from OpenWeather and the Imperial setting only impact the temperature not the rainfall, I will add the mm to inches conversion to the backlog.

You can try these sensors, they are looking at the normal weather data not the history collected by my control. This logic would work with any weather source that exposes the rainfall as a sensor value.

      - platform: statistics
        name: "Rain last 48 hours"
        entity_id: sensor.daily_rainfall
        state_characteristic: total
        sampling_size: 500
        max_age:
          hours: 48

      - platform: statistics
        name: "Rain last 24 hours"
        entity_id: sensor.daily_rainfall
        state_characteristic: total
        sampling_size: 500
        max_age:
          hours: 24

Thank you for your response @petergridge and thank you for being so helpful. I really look forward to using this.

After updating from HACS, will changing the below to “in” and the configuration to “imperial” now make the “day_0_rain” show in the developer states as “inches”?

  template:
        - trigger:
          - platform: time_pattern
            # This will update every night
            hours: 0
            minutes: 0 
          sensor:
          - name: "Daily Max Temp"
            unit_of_measurement: "°C"
            state: "{{ state_attr('sensor.rainfactor', 'day_0_max') }}"
          - name: "Daily Min Temp"
            unit_of_measurement: "°C"
            state: "{{ state_attr('sensor.rainfactor', 'day_0_min') }}"
          - name: "Daily Rainfall"
            unit_of_measurement: "mm"
            state: "{{ state_attr('sensor.rainfactor', 'day_0_rain') }}"

And then by using the below. I will be able to see the history of the sensor in inches. (sensor.daily_rainfall has been assigned to sensor.rainfactor, day_0_rain as shown above)

Thank you for your clarification on these. These should be the last questions.

Thank you again @petergridge , you have been so helpful!!

Hi, the latest code snippet I provided does not need to use the OPENWEATHERHIST data. This example will also use data collected directly from the Weather integration. The Weather integration exposes the rainfall as a sensor that is updated every 10 minutes. So this statistics integration example looks to use this information to determine the rainfall in the last few days. You will need to adjust the sample size to match the number of hours. There are about 150 entries per day for the weather so to go back 10 days the sample size will need to be 1500.

If you are only after rainfall I would use this solution.

Version 1.0.9 has been released. This version provides rainfall in inches when the imperial unit system is selected.

Thank you for this Peter, it is working great!

Ahh, I understand. I didn’t realize this.

So if when I setup openweather map, I chose the setting below, will your idea still work properly or do I need to change the setting below to something else (I.e.) is it still updated every 10 minutes). Thank you for being so helpful. Currently I am using your sensors and it is working fantastic.

I think it should work regardless, I would just set it up and see what the result is, can always play around with the settings and then change them back if you don’t like the result. I am still trying this out myself it takes a while to determine the best config.

On reading the API documentation the rain value is the rainfall for the hour so the cumulative value is inflated as it is repeated 6 times resulting in a very inflated sum. almost had a solution :slight_smile:

I follow, so the repeats are if you use the sensor directly in open weather? The sensors you built don’t do that, correct? And I was curious, why do you feel using the sensor that is given directly by open weather would be a better option for me rather than the solution you built? I thought yours was great! (Do you not trust yours?)

Is yours the one creating the 6x increase or the one open weather provides?

Or are you saying the 6x increase would occur if using their original sensor within the statistics feature of home Assistant?

Hi, my integration will be more accurate, I thought a native control would be better maintained, but the recorded does not lend itself to using the statistics model to get the data.

Ahhh, now I follow you completely. I thought that’s what you meant. Thank you so much Peter for being so helpful. I’m sorry I was playing the game of 100 questions. Thank you again for ensuring I understood the implications of whatever direction I/ we decided to go. What you have built with your talent will be very helpful to myself and others!

Hey Peter,

Can you think of any reason, your creation would possibly not be recording any rain for the imperial unit system? We have had good amounts of rain over the last few days and it seems to be displaying “0” for the rain amounts. Thank you for any guidance you can provide. Below is my “rainfactor” sensor with attributes.

The temperatures look to be spot on.

Thank you again @petergridge

The lat and long being used is: 36.21437° N, -83.29194° E if you need it for any research.

Hi, I have run the sensor with your configuration and I am getting some rain data. It appears that no rain was logged for the preceding days though.

here are the api calls made, you just need to add your api key

I apologize for this. It is working on my side also. Like you were saying. It’s crazy how much rainfall can change over such small distances.

Thank you so much for helping me get this going.

I really do appreciate it Peter.

If you don’t mind, I may end up having one more question related to this tomorrow. Thank you for the time you have spent with me on this. I actually am learning some useful things that I can use in the future!

@petergridge

Do you have the below working in your setup?

For some reason, nothing seems to have been loaded to the “daily rainfall” sensor based on the graph.

I have the code below in a “weather.yaml” file which is in my packages directory and pulled into my config through standard means.
I thought about using the second code snippet (I stole from somewhere to see if it will work); tomorrow morning, I will see if the 0.19 loads to the sensor and report back what I find. (Maybe you’ll see something in this post that doesn’t make sense-- I am very new to all of this)

I also have the 2nd code snippet in my code.

I have attached 3 screenshots also of graphs and states page.

I will report back tomorrow what happens with tonights rain with regards to loading to the sensors. Just wanted to preface what was going on while it was fresh on my mind.

template:
    - trigger:
      - platform: time_pattern
        # This will update every night
        hours: '0'
        minutes: '0'
      sensor:
      - name: "Daily Max Temp"
        unit_of_measurement: "°F"
        state: "{{ state_attr('sensor.rainfactor', 'day_0_max') }}"
        state_class: measurement
      - name: "Daily Min Temp"
        unit_of_measurement: "°F"
        state: "{{ state_attr('sensor.rainfactor', 'day_0_min') }}"
        state_class: measurement
      - name: "Daily Rainfall"
        unit_of_measurement: "in"
        state: "{{ state_attr('sensor.rainfactor', 'day_0_rain') }}"
        state_class: measurement
  - platform: statistics
    name: "Historic Rainfall"
    entity_id: sensor.daily_rainfall
    state_characteristic: total
    max_age:
      hours: 24
        # state: "{{ state_attr('sensor.rainfactor', 'day_0_rain') }}"

Graph of Daily Rainfall

Graph of Historic Rainfall

States of Daily Rainfall and Historic Rainfall