Hi,
I presume you are talking about the add sensor
<https://github.com/weewx/weewx/wiki/add-sensor> entry in the wiki?
Assuming this is the case. There are 3 places in weewx where units are
critical. The data coming from the station, the data being stored in the
database and the data displayed on reports. weewx uses 3 unit systems; US,
METRIC or METRICWX. These 3 unit systems define the units used by the
different observation groups, for example, under the US unit system all
temperatures are in degrees F, but under METRIC and METRICWX all
temperatures are in degrees C. Records coming from the station will be in
one of these 3 unit systems, what system it is largely depends on the
driver, the driver author or what side of the bed the author got out of the
day he wrote the driver. Once weewx has the record from the driver and is
ready to save it to the database, the unit system of the record is checked
against the unit system in the database and if necessary the record is
converted to the unit system used by the database. So if the station
emitted a record using the METRIC unit system, and the database uses the US
unit system (the default), the record would be converted to the US unit
system (so all temperatures are converted from C to F, speeds from km/h to
mph etc) and the record is then saved to the database. When it comes to
reporting the report generation system has its own system of formatting and
converting to display the underlying data (which may in US units, or METRIC
or METRICWX) in whatever units you wish. If you understand this basic
concept you will better understand what is happening to your data in
pond.txt and more importantly why.
In your case, it sounds like you have decided to use a METRIC database,
that is fine there is no particular advantage to this (in fact it can be a
hindrance in some situations). It depends on what driver/station you have,
but I suspect that the records coming from your station us the US unit
system. So the 'add sensor' code simply reads a number from the file, in
this case 1.0, and then places that number in a field in the record. That
field, by the sounds of it, will be a group_rain or group_length field that
when used under the US unit system is in inches. So weewx believes you have
added a value of 1.0 inches (remember your record uses the US unit system).
When the time comes to store that data in your database, weewx checks the
unit system of the record, sees it is US which is different to the METRIC
unit system used by the database and weewx converts the values. weewx
believes it is converting your 1.0 value from inches to cm and hence it
becomes 2.54cm (1.0 inch = 2.54cm).
So how to you fix your problem. There are a few options that vary in
complexity and simplicity. These are:
1. Make sure the value you put in /var/tmp/pond.txt is in inches. This is
the simplest but perhaps not acceptable to you - you would have to convert
the 34cm of snow to inches!
2. Continue to put cm values in var/tmp/pond.txt but put some code in your
read_file() method to do the conversion. Something like this (untested):
import syslog
import weewx
from weewx.wxengine import StdService
from weewx.units import ValueTuple, convert
class PondService(StdService):
def __init__(self, engine, config_dict):
super(PondService, self).__init__(engine, config_dict)
d = config_dict.get('PondService', {})
self.filename = d.get('filename', '/var/tmp/pond.txt')
syslog.syslog(syslog.LOG_INFO, "pond: using %s" % self.filename)
self.bind(weewx.NEW_ARCHIVE_RECORD, self.read_file)
def read_file(self, event):
try:
with open(self.filename) as f:
value = f.read()
syslog.syslog(syslog.LOG_DEBUG, "pond: found value of %s" %
value)
# Convert our value to a type ValueTuple. We know it is in cm
and
# let's use group_rain (could use group_length too)
value_vt = ValueTuple(float(value), 'cm', 'group_rain')
# Now convert the cm value to the same units as used in our
record
# The unit system of the record is in the records 'usUnits'
field
value_conv = convert(value_vt, event.record['usUnits'])
event.record['some_field_name'] = value_conv
except Exception, e:
syslog.syslog(syslog.LOG_ERR, "pond: cannot read value: %s" % e)
Hopefully that gets the point across. You could further refine this in a
number of ways. You could have it read the units from pond.txt, so when
your US friends read your snow and use inches it still works (rather than
doing a double conversion). You could add some conditional checks or
try..except statements to handle invalid data or None coming in from your
file (ie what to do if you get a non-numeric answer).
If it was me I would be going down the path of option 2. It is far more
elegant and capable.
Gary
On Saturday, 14 January 2017 01:36:17 UTC+10, Hartmut Schweidler wrote:
>
> I use "add sensor" from weewx/wiki
>
> when I enter a value of 1.0 cm in "/var/tmp/pond.txt" appears in the
> database 2.54.
>
> This is the value in inches. But I have a system in METRIC.
>
> Where is my fault?
>
> What I've overlooked. My idea is an entry for snow in my database.
>
> Please A Note
> Hartmut
>
--
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].
For more options, visit https://groups.google.com/d/optout.