Weatherstats.txt file - fixed to preserve totalRain
Nov 1, 2020 4:15:50 GMT -8
SDL and Powderjockey like this
Post by impactoz on Nov 1, 2020 4:15:50 GMT -8
Pierus has done a great job in trying to fix the issue with TotalRain. The standard v55 software will loose the value on TotalRain should the Pi stop the program or reboot. As described in this thread forum.switchdoc.com/thread/1378/fix-rainfall-reset-restart-boot
I gave this code a try, and it worked - it preserves TotalRain wonderfully. However too good - it gets preserved forever. The purpose of TotalRain is really about what the total rain is for today - evidence by the value and where it puts it in weatherunderground updates.
I liked what Pierus did, and followed up with investigating the code, and playing around to learn of other issues;
- TotalRain was now being preserved, but forever
- Rain60minutes was also in error - it was not being preserved after a reboot
- LightningCount was also in error - it was not being preserved after a reboot
I could see how Pierus did the fix using new libraries of np, and writing to his own file... but after looking at the post with comments from smurphy - just thought, why could we not use what was already there... Weatherstats.txt is a file in the state directory, which contained these fields, and no other code in the system required np - so do we really need another library...
I spent too long on this, a whole day - and I think I now have a better solution
- TotalRain is preserved on reboot and is reset to 0 at end of day
- Rain60minutes is preserved
- LigntningCount is preserved on reboot and is reset to 0 at the end of the day
- Utilising the weatherstats file, but improving the readability of it as suggested by smurphy.
I knew of the weatherstats file, but never used it - and was amazed to see it never gets updated. Once its created, it never gets updated again... My original file was showing a date back in 2019... Then I read a post here suggesting that you need to add a scheduling job to update it - not on by default - so really what purpose does this file serve. Not only that but the skyweather code only has code to write to it, not read from it, and certainly nothing to use / preserve values or reset them at end of day.
For me this seemed to be a better solution - reuse what was already there. So I decided to add the scheduling of updating the weatherstats file, write a routine to read some of the values of the file that can be used to set variables at startup - so they are preserved when the Pi stops / reboots, and lastly write a routine to reset values at the end of day.
While doing this, I also tidied up the format of the weatherstats file - so if your using it for something else, you may need to look at the new format to make it compatible again....
The new weatherstats file will look something like this from here on in- with my version of the code...
I placed date/time on when its updated, and show the names of the variables - makes it much more readable.... I searched and the only item I could find not being saved, which should be included was the UVIndex....
So I present to you my version, which I have tested a lot - but will not say its perfect, I dont use Blynk, and therefore I have never bothered with or used prior Rain60minutes. Its only useful by the looks at it for Blynk users...
So the changes I made;
Modified the routine writeWeatherStats()
Created a new routine readWeatherStats()
Created a new routine newdayClearStats()
A couple of variables commented out where they were being set to 0 (Which I no longer want) - Line 621 / 884 / 991
Lines 1787-1789 added
Added new scheduler to write the weatherstats every 30 seconds - line 1837
Added new scheduler to clear stats at midnight - Line 1838
My version of SkyWeather has all sorts of other goodies - like other sensors - ie VEML6075, custom routines for taking pictures, and sending information out on twitter.. So I removed these, and applied my changes to present a clean version 55 for you with the totalRain fixes.... (If I missed something, sorry - let me know)
SO Enjoy
Download the file here - rename it as SkyWeather.py - best of luck!SkyWeather.txt (63.24 KB)
I gave this code a try, and it worked - it preserves TotalRain wonderfully. However too good - it gets preserved forever. The purpose of TotalRain is really about what the total rain is for today - evidence by the value and where it puts it in weatherunderground updates.
I liked what Pierus did, and followed up with investigating the code, and playing around to learn of other issues;
- TotalRain was now being preserved, but forever
- Rain60minutes was also in error - it was not being preserved after a reboot
- LightningCount was also in error - it was not being preserved after a reboot
I could see how Pierus did the fix using new libraries of np, and writing to his own file... but after looking at the post with comments from smurphy - just thought, why could we not use what was already there... Weatherstats.txt is a file in the state directory, which contained these fields, and no other code in the system required np - so do we really need another library...
I spent too long on this, a whole day - and I think I now have a better solution
- TotalRain is preserved on reboot and is reset to 0 at end of day
- Rain60minutes is preserved
- LigntningCount is preserved on reboot and is reset to 0 at the end of the day
- Utilising the weatherstats file, but improving the readability of it as suggested by smurphy.
I knew of the weatherstats file, but never used it - and was amazed to see it never gets updated. Once its created, it never gets updated again... My original file was showing a date back in 2019... Then I read a post here suggesting that you need to add a scheduling job to update it - not on by default - so really what purpose does this file serve. Not only that but the skyweather code only has code to write to it, not read from it, and certainly nothing to use / preserve values or reset them at end of day.
For me this seemed to be a better solution - reuse what was already there. So I decided to add the scheduling of updating the weatherstats file, write a routine to read some of the values of the file that can be used to set variables at startup - so they are preserved when the Pi stops / reboots, and lastly write a routine to reset values at the end of day.
While doing this, I also tidied up the format of the weatherstats file - so if your using it for something else, you may need to look at the new format to make it compatible again....
The new weatherstats file will look something like this from here on in- with my version of the code...
Updated:,2020-11-01 22:13:34.223061
totalRain:,0.0
rain60Minutes:,0.0
as3935LightningCount:,0
as3935LastInterrupt:,0
aas3935LastDistance:,0
as3935LastStatus:,Disturber detected - masking
currentWindSpeed:,0.759996962545
currentWindGust:,10.7954443225
currentWindDirection:,180
currentWindDirectionVoltage:,1.43925
bmp180Temperature:,15.19
bmp180Pressure:,1020.69
bmp180Altitude:,507.0
bmp180SeaLevel:,1084.29105992
outsideTemperature:,14.5
outsideHumidity:,75.7
HTUtemperature:,15.19
HTUhumidity:,58.622
SunlightUVIndex:,0.0
I placed date/time on when its updated, and show the names of the variables - makes it much more readable.... I searched and the only item I could find not being saved, which should be included was the UVIndex....
So I present to you my version, which I have tested a lot - but will not say its perfect, I dont use Blynk, and therefore I have never bothered with or used prior Rain60minutes. Its only useful by the looks at it for Blynk users...
So the changes I made;
Modified the routine writeWeatherStats()
Created a new routine readWeatherStats()
Created a new routine newdayClearStats()
A couple of variables commented out where they were being set to 0 (Which I no longer want) - Line 621 / 884 / 991
Lines 1787-1789 added
Added new scheduler to write the weatherstats every 30 seconds - line 1837
Added new scheduler to clear stats at midnight - Line 1838
My version of SkyWeather has all sorts of other goodies - like other sensors - ie VEML6075, custom routines for taking pictures, and sending information out on twitter.. So I removed these, and applied my changes to present a clean version 55 for you with the totalRain fixes.... (If I missed something, sorry - let me know)
SO Enjoy
Download the file here - rename it as SkyWeather.py - best of luck!SkyWeather.txt (63.24 KB)