Any better ways to have accumulated / weekly precipitation data?

Getting accumulated precipitation seems to be a safely guarded metric from the public APIs. Openweathermap charges an insane amount to access this data, it isn’t available in dark sky, etc.

I would like to know when I have had less than 1” of rainfall a week, to trigger irrigation routines.

Has anything advanced that I am unaware of for getting local accumulated rainfall stats via an API without using a local weather station?

My backup plan is to setup a local accurite rainfall bucket sensor and intercept the readings with a SDR & rtl_433.

Smartweather has actual rainfall figures for some stations.
I use REST sensors to gather data from around me and then average it.

Look on the website https://smartweather.weatherflow.com/map/1234/ to see if there are any stations near you.

How about storing the data from rain sensor somewhere?
Not sure, just thinking

i have a rainwise MKIII, i started storing the daily rainfall in a sqlite3 database daily (if rain>0). then i can sum total rainfall, first day of rain and total rain days for display … the program at the bottom extracts the data from my mkiii which you can ignore and creates the database then updates rainfall and days of rain into HA for display
i also have command line sensors for first day of rain, total and days using the bash commands below along with yaml config

  - platform: command_line
    name: First Day of Rain
    command: '/home/homeassistant/.homeassistant/bin/first.sh'
    scan_interval: 14400
    value_template: >
      {% if value == 'empty' %}
        No rain yet this year
      {% else %}
        {% set y = value.split(' ')[0].split('/')[0] %}
        {% set m = value.split(' ')[0].split('/')[1].lstrip('0') %}
        {% set d = value.split(' ')[0].split('/')[2].lstrip('0') %}
        {% set m_names = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] %}
        {{ m_names[(m|int)-1] }} {{ d }}, {{ y }}
      {% endif %}

  - platform: command_line
    name: Yearly Rainfall
    command: '/home/homeassistant/.homeassistant/bin/sum.sh'
    scan_interval: 3600
    unit_of_measurement: '"'
    value_template: "{{ ((value|float) + (states('sensor.mkiii_rainfall')|float))|round(2) }}"

  - platform: command_line
    name: Days of Rain
    command: '/home/homeassistant/.homeassistant/bin/days.sh'
    scan_interval: 14400
    value_template: >
      {% if value == 0 %}
        No rain yet this year
      {% elif value|int == 1 %}
        One day of rain this year
      {% elif value|int == 2 %}
        Two days of rain this year
      {% elif value|int == 3 %}
        Three days of rain this year
      {% elif value|int == 4 %}
         Four days of rain this year
      {% elif value|int == 5 %}
        Five days of rain this year
      {% elif value|int == 6 %}
        Six days of rain this year
      {% elif value|int == 7 %}
        Seven days of rain this year
      {% elif value|int == 8 %}
        Eight days of rain this year
      {% elif value|int == 9 %}
        Nine days of rain this year
      {% else %}
        {{ value }} days of rain this year
      {% endif %}
#!/bin/bash
# sum accumulated rain data from the current year

year=`/bin/date +'%Y'`
raindatabase='/home/homeassistant/.homeassistant/bin/rain/raindatabase.db'
command="SELECT SUM(Amount) FROM Rainfall WHERE Year=$year;"
sum=`/bin/echo $command | /usr/bin/sqlite3 $raindatabase`
if [ -z "$sum" ]
then
  /bin/echo "no rain data"
else
  /bin/echo $sum
fi
#!/bin/bash

year=`/bin/date +'%Y'`
raindatabase='/home/homeassistant/.homeassistant/bin/rain/raindatabase.db'
command="SELECT datetime FROM Rainfall WHERE Year=$year LIMIT 1;"
response=`/bin/echo $command | /usr/bin/sqlite3 $raindatabase`
if [ -z "$response" ]
then
  /bin/echo "empty"
else
  /bin/echo $response
fi
#!/bin/bash
# sum accumulated rain data from the current year

year=`/bin/date +'%Y'`
raindatabase='/home/homeassistant/.homeassistant/bin/rain/raindatabase.db'
command="SELECT SUM(Amount) FROM Rainfall WHERE Year=$year;"
sum=`/bin/echo $command | /usr/bin/sqlite3 $raindatabase`
if [ -z "$sum" ]
then
  /bin/echo "no rain data"
else
  /bin/echo $sum
fi
#!/srv/homeassistant/bin/python

from bs4 import BeautifulSoup
import sqlite3 as lite
import urllib3
import json
import os.path
import requests
import json

# Rainwise MK-III weather station address
url = "http://192.168.1.31"

# database
database = "/home/homeassistant/.homeassistant/bin/rain/raindatabase.db"
TOKEN = "my token"
ha_url = "http://192.168.1.123:8123/api/services/homeassistant/update_entity"

def update():
  headers = {}
  headers['authorization'] = 'Bearer ' + TOKEN
  headers['content-type'] = 'application/json'
  payload = '{"entity_id" : "sensor.yearly_rainfall"}'
  response = requests.request("POST", ha_url, data=payload, headers=headers)

def days():
  headers = {}
  headers['authorization'] = 'Bearer ' + TOKEN
  headers['content-type'] = 'application/json'
  payload = '{"entity_id" : "sensor.days_of_rain"}'
  response = requests.request("POST", ha_url, data=payload, headers=headers)

# main

http = urllib3.PoolManager()
r = http.request('GET', url)
#content = urllib3.urlopen(url).read()
#soup = BeautifulSoup(content, 'lxml')
soup = BeautifulSoup(r.data, 'lxml')

rainfall = soup.find(id='rfd').string.split(' ')[0]
year = soup.find(id='time').string.split(' ')[0].split('/')[0]
date = soup.find(id='time').string.split(' ')[0]
time = soup.find(id='time').string.split(' ')[1]
datetime = date + ' ' + time

if (os.path.isfile(database)):
# copy database before adding record
  os.system('/bin/cp ' + database + ' ' + database + '.save')
  con = lite.connect(database)
  cur = con.cursor()
  if float(rainfall) > 0.00:  # don't insert zero rainfall into database
    command = "INSERT INTO Rainfall VALUES(" + "\'" + year + "\',\'" + datetime + "\'," + rainfall + ")"
    cur.execute(command)
    con.commit()
    con.close()
    update() # update rainfall HA state
    days() # update days of rain  HA state
  else:
    con.close()
else:
  print("database not found, creating: ", database)
  con = lite.connect(database)
  cur = con.cursor()
  cur.execute("CREATE TABLE Rainfall(Year TEXT, Datetime TEXT, Amount REAL)")
  con.commit()
  con.close()

exit()

@klogg can you show me how to set up REST sensors on this site? https://tempestwx.com/station/28261/
can i see the config?

I’m no expert in this.

Does tempestwx have an api that allows rest sensors? If so then it should be documented on the
website.

What i mean is how did you do it? I want the “precip_accum_local_yesterday” value from this: https://swd.weatherflow.com/swd/rest/observations/station/28261?api_key=Api-key

OpenWeatherMap has a history API: https://openweathermap.org/api/one-call-api#history

With it, you can query up to 5 days back and capture rainfall amounts (although the current.rain property only appears if there was actual rain on that day and if the location supports it).

If that is your personal API key in the URL you posted, I’d recommend removing it from your post.

Thx its not my api key however i am not sure if i am allowed to post a public dev key.
I am really lost on how to query it. I have found a get around/solution. https://github.com/briis/smartweather

1 Like

This is probably not a ‘get around’ but the right way to do it. I starting using the REST api when briis first created this component as I wanted other data he wasn’t providing at the time (he may do now). But all credit goes to him for highlighting the existence of Smartweather in the first place. I’d stick with the custom component if you can. briis does some other excellent weather components too…

1 Like