cyrils / renogy-bt

Python library to read Renogy compatible BT-1 or BT-2 bluetooth modules
GNU General Public License v3.0
94 stars 39 forks source link

Additional logging for setup/debug #90

Closed nowcodingaway closed 1 week ago

nowcodingaway commented 1 week ago

In case it is of any help or inspiration to others, I am sharing what I have done to assist with checking that the CRONS are running as expected and to be able to unpick errors if anything stops working.

I also share this in case it helps drive any improvements to the logging system or the readme/setup instructions.

I could not tell if this was already being logged anywhere, please let me know if what I have done is redundant.

Here is what I have done.

in example.py I included: from datetime import datetime At the top

I then added additional logging after around line 16:

current_date = datetime.now().strftime("%Y-%m-%d")
logging.basicConfig(filename='/home/admin/Renogy/logs/' + current_date + '-runlog-' + config_file + '.log',
                    filemode='a',
                    format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
                    datefmt='%H:%M:%S',
                    level=logging.INFO)

logging.info("Renogy Started Logging With Config " + config_file)

I created a 'logs' folder in the same location as the project files.

Now as the script gets called from my cron, it will create a log for today, and it will do it per instance. So for example, I have one cron loading the battery config and another loading the controller config. I will end up with a log for each of those.

Each time a new day arrives, a new log file will be created for each of them.

Screenshot 2024-11-21 at 10 29 06 am

I am running a Pi 5, and the python scripts are running in a venv / virtual environment. For anyone that is interested, this is how my working cron lines ended up

*/1 * * * *  /home/admin/Renogy/venv/bin/python /home/admin/Renogy/example.py controller-config.ini #runs every 1 mins
*/1 * * * * /home/admin/Renogy/venv/bin/python /home/admin/Renogy/example.py battery-config.ini #runs every 1 mins

I will likely set up another cron to clean out old log files that are 30 days old at some point.

Once I know that everything is running well for a while, I will likely change down the logging to just capture errors, and potentially send a notification somewhere.

I am a beginner at best so this is me muddling my way through. I find that having more logs and information helps me better understand how things are working and resolve problems, but right now I am also learning the logging system and approaches themselves. So please don't judge me too harshly :)

p.s wasn't sure if this should be in 'issues' or 'discussions' - apologies if this is incorrectly submitted, and do let me know the correct approach moving forward.

nowcodingaway commented 1 week ago

I now realise that if you are using polling, CRON is likely not the right way to go about this, and rather, systemd / running the commands once at startup might be better.

I saw the issue #77 (systemd example) but face the same challenge of how to pass the flag, and ultimately start 2 services. One for CTRL and one for BATT.

EDIT: I have now added my solution / what is working for me using systemd over on issue #77