Les prérequis :
- installer sqlite3
- avoir au moins 2 jours de log (ou alors adapter le principe décrit ci-dessous : crontab à 23h58 et ne garder que date('now') dans la requête)
Principe du script :
- Analyse des données de la veille
- Script lancé en crontab tous les jours vers 2h
- Dès que la précipitation > 0 mm, je calcule le cumul tombé entre deux informations DarkSky
- J'agrège le tout pour la journée étudiée
- Je formate un ordre de MàJ
- Je mets à jour la base Domoticz
La crontab de pi :
Code : Tout sélectionner
# Ajuste tous les jours à 2h08 la base Domoticz (Cumul Pluie)
8 2 * * * /home/domoticz/scripts/RegulBase.sh > /tmp/RegulBase.log 2>&1
Code : Tout sélectionner
#!/bin/bash
echo $(date +%d-%m-%Y" "%H:%M:%S:%3N) ": début Select"
cd /home/domoticz/pi
cat RegulPluie.sql | sudo sqlite3 domoticz.db > Result.sql
echo $(date +%d-%m-%Y" "%H:%M:%S:%3N) ": début MàJ"
cat Result.sql | sudo sqlite3 domoticz.db
echo $(date +%d-%m-%Y" "%H:%M:%S:%3N) ": fin MàJ"
nb : devicerowid=37 ==> correspond à mon device "Pluie" de DarkSky
Code : Tout sélectionner
select "update rain_calendar set total="||a.pluie||" where devicerowid="||a.devicerowid
||" and date="""||a.jour||""";"
from
(
select a.devicerowid,date(a.date) as jour, round(sum(a.Pluie),2) as pluie
from
(
select a.devicerowid,a.date, (strftime('%s',b.date)-strftime('%s',a.date))*a.total/3600 as Pluie
from rain a, rain b
where a.devicerowid=37
and a.total>0
and date(a.date)=date('now','-1 days')
and b.devicerowid=a.devicerowid
and b.date=(select min(temp.date)
from rain temp
where temp.devicerowid=a.devicerowid
and temp.date>a.date
)
) a
group by 1,2
) a;
Exemple chez moi (oui oui il a bcp plu !!) :
Code : Tout sélectionner
pi@raspberrypi:/home/domoticz/pi $ cat RegulPluie.sql | sudo sqlite3 domoticz.db
update rain_calendar set total=23.59 where devicerowid=37 and date="2019-02-01";
pi@raspberrypi:/home/domoticz/pi $
Enjoy