I read several add'l values in from static files. Every once in a great 
while, one of the files has extraneous character in it and it can't be read 
as a float() and extensions.py crashes weewx. So after reading about 
try/except/finally from several sources, I wanted to put the section where 
it reads the float in a try statement, and if it fails, the exception 
action is to assign a nominal float value for that weewx cycle, and write 
it to the database. However when I put in the try: it immediately causes 
weewx to exit, at all debug levels. 
If you are more knowledgeable on Python 3 than I am, please look at my 
extensions.py attachment and tell me how I need to restructure that code 
block so it will work. Note, I have tried a couple different positions for 
the initial try: block, no success. 
Further note please, that this only applies to the lines where I 
open '/home/pi/cputemp-opi5p.dat' and read the value into degf2 for it to 
store it in 'extraTemp7'. Note further that this code is working for all 
seven parameters being read and placed into the data, however, occasionally 
(about once a week), somehow the system reads in erroneous data (such 
as '94.8\x00' instead of '94.8')  and causes the python to exit. 
I have looked at places where this data is created and cannot find any 
evidence of that invalid value.

Here is the pertinent part of the log at failure time:
pi@PI4B1:~ $ systemctl status weewx
● weewx.service - WeeWX weather system
     Loaded: loaded (/etc/systemd/system/weewx.service; enabled; vendor 
preset: enabled)
     Active: failed (Result: exit-code) since Tue 2025-04-15 16:00:16 CDT; 
4h 18min ago
       Docs: https://weewx.com/docs
    Process: 268425 ExecStart=/home/weewx/bin/weewxd /home/weewx/weewx.conf 
(code=exited, status=1/FAILURE)
   Main PID: 268425 (code=exited, status=1/FAILURE)
        CPU: 4h 37min 25.648s

Apr 15 16:00:16 PI4B1 weewxd[268425]: weewx[268425] CRITICAL __main__:     
****      self.engine.dispatchEvent(weewx.Event(weewx.NEW_ARCHIVE_RECORD,
Apr 15 16:00:16 PI4B1 weewxd[268425]: weewx[268425] CRITICAL __main__:     
****    File "/home/weewx/bin/weewx/engine.py", line 245, in dispatchEvent
Apr 15 16:00:16 PI4B1 weewxd[268425]: weewx[268425] CRITICAL __main__:     
****      callback(event)
Apr 15 16:00:16 PI4B1 weewxd[268425]: weewx[268425] CRITICAL __main__:     
****    File "/home/weewx/bin/user/extensions.py", line 98, in 
new_archive_packet
Apr 15 16:00:16 PI4B1 weewxd[268425]: weewx[268425] CRITICAL __main__:     
****      degf2 = float(float(f.read()))                #this is OPI5P
Apr 15 16:00:16 PI4B1 weewxd[268425]: weewx[268425] CRITICAL __main__:     
****  ValueError: could not convert string to float: '94.8\x00'
Apr 15 16:00:16 PI4B1 weewxd[268425]: weewx[268425] CRITICAL __main__:     
****  Exiting.
Apr 15 16:00:16 PI4B1 systemd[1]: weewx.service: Main process exited, 
code=exited, status=1/FAILURE
Apr 15 16:00:16 PI4B1 systemd[1]: weewx.service: Failed with result 
'exit-code'.
Apr 15 16:00:16 PI4B1 systemd[1]: weewx.service: Consumed 4h 37min 25.648s 
CPU time.

-- 
You received this message because you are subscribed to the Google Groups 
"weewx-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/weewx-user/b4cf50ae-4061-48c9-afff-4f12ee4d751dn%40googlegroups.com.
#!/usr/bin/env python
#    Copyright (c) 2009-2015 Tom Keffer <[email protected]>
#
#    See the file LICENSE.txt for your full rights.
#

"""User extensions module

This module is imported from the main executable, so anything put here will be
executed before anything else happens. This makes it a good place to put user
extensions.
"""

import locale
# This will use the locale specified by the environment variable 'LANG'
# Other options are possible. See:
# http://docs.python.org/2/library/locale.html#locale.setlocale
locale.setlocale(locale.LC_ALL, '')

import weewx.units
#weewx.units.obs_group_dict['waterTemp'] = 'group_temperature'
#weewx.units.obs_group_dict['LakeElev'] = 'group_altitude'
import re
import os

from weewx.engine import StdService
weewx.units.obs_group_dict['LakeElev'] = 'group_lake'
weewx.units.USUnits['group_lake'] = 'feetMSL'
weewx.units.default_unit_format_dict['feetMSL'] = '%.2f'
weewx.units.default_unit_label_dict['feetMSL'] = ' ftMSL'


class AddMyData(StdService):

	def __init__(self, engine, config_dict):
	# Initialize my superclass first:
		super(AddMyData, self).__init__(engine, config_dict)
	# Bind to any new archive record events:
		self.bind(weewx.NEW_ARCHIVE_RECORD, self.new_archive_packet)

	def new_archive_packet(self, event):

#	putting / declaring a variable here crashes it

	#(code that reads two measurements from a file)
		with open("/home/pi/wxdata/weewxmbdata.dat") as f:
			value1 = 600.00  #initialize anyway
			value2 = 123.45  #initialize anyway
			for line in f:
				numbers_str = line.split()
				numbers_float = [float(x) for x in numbers_str]
				value1 = numbers_float[0]
				value2 = numbers_float[1]

			event.record['LakeElev'] = value1
#			event.record['lakeWaveheight'] = value2

		with open("/home/pi/cputemp-pi4b1.dat") as f:
			value3 = 50.0
			for line in f:
				numbers_str = line.split()
				numbers_float = [float(x) for x in re.findall("\d+\.\d+", line)]
				value3 = numbers_float[0]
				value3 = (value3 * 9/5) + 32
			event.record['extraTemp3'] = value3


#
		with open("/home/pi/cputemp-pi3b2.dat") as f:
			value4 = 50.0
			for line in f:
				numbers_str = line.split()
				numbers_float = [float(x) for x in re.findall("\d+\.\d+", line)]
				value4 = numbers_float[0]
				value4 = (value4 * 9/5) + 32
			event.record['extraTemp4'] = value4


# #weewx was -occasionally- complaining about "UnboundLocalError: local variable 'value5' referenced before assignment"
#   I couldn't find a clue as to what was different about value5 from 4 or 3 ... so try changing to 5x!
		with open("/home/pi/cputemp-pi5r.dat") as f:
			value5x = 80.0   # initialize a "ringer" value, just in case
			for line in f:
				numbers_str = line.split()
				numbers_float = [float(x) for x in re.findall("\d+\.\d+", line)]
				value5x = numbers_float[0]
				value5x = (value5x * 9/5) + 32
			event.record['extraTemp5'] = value5x
#
# orangepi5B now places actual float in its .dat, necessitating cast as float
#Modify this and place in RPI3 extraTemp2
		with open('/home/pi/cputemp-opi5.dat') as f:   #sensors -f requested temps in F
			degf = 80.0	# initialize a "ringer" value, just in case
			degf = float(float(f.read()))		#this is OPI5B
			event.record['extraTemp6'] = degf

		with open('/home/pi/cputemp-opi5p.dat') as f:   #sensors -f requested temps in F
#			try:
				degf2 = 80.0 	# initialize a "ringer" value, just in case
				degf2 = float(float(f.read()))		#this is OPI5P
				print("degf2 okay")
#			except:
#				degf2 = 100.0
#				print("degf2 exception!")
#			finally:
				event.record['extraTemp7'] = degf2

		with open('/home/pi/cputemp-ser8.dat') as f:   #sensors -f requested temps in F
			degf3 = float(float(f.read()))          #this is SER8
			event.record['extraTemp8'] = degf3

Reply via email to