From db2038e14a46412d4b0e6788d91b52230cbcd504 Mon Sep 17 00:00:00 2001 From: Glenn Date: Tue, 6 Mar 2018 17:14:50 +1100 Subject: [PATCH 1/2] Expand on adding new units --- docs/customizing.htm | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/docs/customizing.htm b/docs/customizing.htm index ebd8e9e1..b6088f17 100644 --- a/docs/customizing.htm +++ b/docs/customizing.htm @@ -1241,7 +1241,6 @@ $month.outTemp.max Example Meaning - $hour($hours_ago=h) @@ -1273,6 +1272,7 @@ $month.outTemp.max $year($years_ago=1).outTemp.max The maximum temperature last year. +

Unit conversion options

@@ -6053,12 +6053,30 @@ mysql> RENAME TABLE weewx_new.archive TO weewx.archive;group_energy (which already exists), so it can enjoy the labels and formats already provided for this group. This is done by extending the dictionary weewx.units.obs_group_dict, typically by - adding Python code to the file user/extensions.py: -

+ class='code'>weewx.units.obs_group_dict.
+ We can add the required Python code to the file + user/extensions.py:

+
import weewx.units
 weewx.units.obs_group_dict['electricity'] = 'group_energy'
+

Or that same Python code can be added to our new services file + user/electricity.py, following the last + import statement:

+
import weewx
+from weewx.engine import StdService
+
+import weewx.units
+weewx.units.obs_group_dict['electricity'] = 'group_energy'
+
+class AddElectricity(StdService):
+
+    def __init__(self, engine, config_dict):
+
+        # [...]
+
+        self.last_total = total_power
+

Once the observation has been associated with a unit group, the unit labels and other tag syntax will work for that observation. So, now a tag like:

@@ -6078,13 +6096,14 @@ weewx.units.obs_group_dict['electricity'] = 'group_energy'

We assume we have a new observation type, force, - that we are measuing over time. We will create a new unit group, + that we are measuring over time, for a service named + user/rocket.py. We will create a new unit group, group_force, and new units, newton and pound. Our new observation, force, will belong to group_force, and will be measured in units of newton or pound. + class='code'>pound.

  1. As before, we start by specifying what group our new @@ -6114,6 +6133,15 @@ weewx.units.conversionDict['pound'] = {'newton': lambda x : x * 4.44822}
+

As with the previous example of group_energy, + the last step is to extend the + weewx.units.obs_group_dict dictionary so that these new units can + be used by our service.
We do this by adding the Python code from + steps 1 - 4 above, into user/extensions.py.
+ Or for easier code management, we can choose to include it directly into + our new services file user/rocket.py, at the + head of that file and just after the last import statement (as in the + previous example).

Use the new types

From 378d7b83c82be2c14a987409251b52e3c97fd019 Mon Sep 17 00:00:00 2001 From: Tom Keffer Date: Tue, 6 Mar 2018 03:45:16 -0800 Subject: [PATCH 2/2] Cut down user choices. No suggestion of putting unit extensions into user/extensions.py --- docs/customizing.htm | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/docs/customizing.htm b/docs/customizing.htm index b6088f17..c46212a9 100644 --- a/docs/customizing.htm +++ b/docs/customizing.htm @@ -6053,15 +6053,10 @@ mysql> RENAME TABLE weewx_new.archive TO weewx.archive;group_energy (which already exists), so it can enjoy the labels and formats already provided for this group. This is done by extending the dictionary weewx.units.obs_group_dict.
- We can add the required Python code to the file - user/extensions.py:

+ class='code'>weewx.units.obs_group_dict.

-
import weewx.units
-weewx.units.obs_group_dict['electricity'] = 'group_energy'
- -

Or that same Python code can be added to our new services file - user/electricity.py, following the last +

Add the following to our new services file + user/electricity.py, just after the last import statement:

import weewx
 from weewx.engine import StdService
@@ -6071,13 +6066,13 @@ weewx.units.obs_group_dict['electricity'] = 'group_energy'
 
 class AddElectricity(StdService):
 
-    def __init__(self, engine, config_dict):
+   # [...]
- # [...] - - self.last_total = total_power - -

Once the observation has been associated with a unit group, +

When our new service gets loaded by the WeeWX engine, these few lines + will get run. They tell WeeWX that our new observation type, + electricity, is part of the unit group + group_energy. + Once the observation has been associated with a unit group, the unit labels and other tag syntax will work for that observation. So, now a tag like:

$month.electricity.sum
@@ -6087,7 +6082,7 @@ class AddElectricity(StdService):

Creating a new unit group

That was an easy one, because there was already an existing - group, group_amp, that covered our new + group, group_energy, that covered our new observation type. But, what if we are measuring something entirely new, like force with time? There is nothing in the existing system of units that covers things like newtons or pounds. We will have to @@ -6136,9 +6131,8 @@ weewx.units.conversionDict['pound'] = {'newton': lambda x : x * 4.44822}

As with the previous example of group_energy, the last step is to extend the weewx.units.obs_group_dict dictionary so that these new units can - be used by our service.
We do this by adding the Python code from - steps 1 - 4 above, into user/extensions.py.
- Or for easier code management, we can choose to include it directly into + be used by our service. We do this by adding the Python code from + steps 1 - 4 above into our new services file user/rocket.py, at the head of that file and just after the last import statement (as in the previous example).