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()