Convert values before loading into database

110 views
Skip to first unread message

Thomas Carlin

unread,
Oct 2, 2024, 10:51:53 PM10/2/24
to weewx-user
I have a sensor that is adding data to WeeWX with a custom driver.  Unfortunately, the sensor provides values in various metric units, C, hPa, µg/m³, things like that.  Obviously it is not a big deal to write these conversions into the driver, but i am curious if there is a prebuilt mechanism to convert them before they go into the database.  Obviously, we can do the conversions after the fact, but logically it is better to convert and store everything in the same units.  

Do i need to do the conversions in the driver, or is there a way built into WeeWX?

Many thanks!

John Kline

unread,
Oct 2, 2024, 11:15:41 PM10/2/24
to weewx...@googlegroups.com
IIRC,

When your driver creates loop and archive observations, you need to set usUnits to reflect the correct system.  For example, the vantage drive sets:
'usUnits': weewx.US
In the loop data and archive records.

If custom driver is in metric, set:
‘UsUnits’: weewx.METRIC

If you do this, weewx will do the conversions and store values in the database in the system you specified when you created the database.

But there is more.  You have to use the correct metric units.  To do this, see the weewx/units.py file.  Look up the observation in that file, find the group, and then look up what that group expects.

For example, rain is:
"rain"                      : "group_rain",

And, if you look up group_rain for metric, you’ll find group_rain is expecting cm:
"group_rain"        : "cm",

# This dictionary maps unit groups to a standard unit type in the
# metric unit system:           
MetricUnits = ListOfDicts({     
    "group_altitude"    : "meter",
    "group_amp"         : "amp",
    "group_angle"       : "degree_angle",
    "group_boolean"     : "boolean",
    "group_concentration": "microgram_per_meter_cubed",
    "group_count"       : "count",
    "group_data"        : "byte",
    "group_db"          : "dB",
    "group_degree_day"  : "degree_C_day",
    "group_deltatime"   : "second",
    "group_direction"   : "degree_compass",
    "group_distance"    : "km",
    "group_elapsed"     : "second",
    "group_energy"      : "watt_hour",
    "group_energy2"     : "watt_second",
    "group_fraction"    : "ppm",
    "group_frequency"   : "hertz",
    "group_illuminance" : "lux",
    "group_interval"    : "minute",
    "group_length"      : "cm",
    "group_moisture"    : "centibar",
    "group_percent"     : "percent",
    "group_power"       : "watt",
    "group_pressure"    : "mbar",
    "group_pressurerate": "mbar_per_hour",
    "group_radiation"   : "watt_per_meter_squared",
    "group_rain"        : "cm",
    "group_rainrate"    : "cm_per_hour",
    "group_speed"       : "km_per_hour",
    "group_speed2"      : "km_per_hour2",
    "group_temperature" : "degree_C",
    "group_time"        : "unix_epoch",
    "group_uv"          : "uv_index",
    "group_volt"        : "volt",
    "group_volume"      : "liter"
})


On Oct 2, 2024, at 7:51 PM, Thomas Carlin <thomas...@gmail.com> wrote:

I have a sensor that is adding data to WeeWX with a custom driver.  Unfortunately, the sensor provides values in various metric units, C, hPa, µg/m³, things like that.  Obviously it is not a big deal to write these conversions into the driver, but i am curious if there is a prebuilt mechanism to convert them before they go into the database.  Obviously, we can do the conversions after the fact, but logically it is better to convert and store everything in the same units.  

Do i need to do the conversions in the driver, or is there a way built into WeeWX?

Many thanks!

--
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 weewx-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/weewx-user/e8e19545-1c1b-43b7-8a07-472f5daa4232n%40googlegroups.com.

Graham Eddy

unread,
Oct 3, 2024, 12:54:27 AM10/3/24
to WeeWX User
excellent observation. note that weewx provides two similar but not identical metric unit systems: METRIC and METRICWX
⊣GE⊢

Tom Keffer

unread,
Oct 3, 2024, 9:54:28 AM10/3/24
to weewx...@googlegroups.com
This information is also summarized in the documentation: Units

Thomas Carlin

unread,
Oct 3, 2024, 9:53:25 PM10/3/24
to weewx-user
Thanks all,
I spent some time looking at the vantage driver tonight, and I should have been more clear. This is not a full driver, to replace the Vantage driver or anything like that. I wrote this using the documentation for "Adding a second data source", and loop through half a dozen pieces of data, and run event.record once for each.

Is there still a way to accomplish the conversion built in?

Graham Eddy

unread,
Oct 3, 2024, 9:57:54 PM10/3/24
to WeeWX User
the only relevant difference between a driver (primary source of data) and a service (your need, which augments data) is that the driver sets the unit system in the packet but the service merely follows whatever is already set in the pack (identified by usUnits field)
⊣GE⊢

Thomas Carlin

unread,
Oct 3, 2024, 11:27:33 PM10/3/24
to weewx...@googlegroups.com
That is what I thought I was seeing as I went through it this evening. Thank you for confirming.

Are the conversion functions accessible from the service through a library, to keep everything consistent, and the conversion formulas all in the same place?

--
You received this message because you are subscribed to a topic in the Google Groups "weewx-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/weewx-user/UwDK6vcUot4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to weewx-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/weewx-user/929A900A-1BF0-4B28-BCF3-53E3BF7BA3FC%40geddy.au.


--
Thomas Carlin
970-401-3805

Graham Eddy

unread,
Oct 3, 2024, 11:37:16 PM10/3/24
to WeeWX User
see bottom part of weewx/units.py e.g. convert(val_t, target_unit)
⊣GE⊢
Reply all
Reply to author
Forward
0 new messages