need help with weewx to do energy consumption calculation

199 views
Skip to first unread message

Mks Mk

unread,
Sep 6, 2022, 9:07:32 PM9/6/22
to weewx-user
we want to expand the use of weewx to include energy consumption and cost calculation but we are not sure how to do that.
our setup consists of:
1-weewx on raspberry pi 4
2-SDR dongle
3-energy monitor device kit with RF transmitter and display

we are capturing the signal of acurite weather station and the energy transmitter and weewx reading is matching what the energy display reading in kilowatt per hour and the reading gets updated every 20 seconds.

we need weewx to calculate the consumption of energy during 24 hours and the total consumption for the month (30 days) but we are sure if weewx can do this task and we are not sure how to do this.
any thoughts or suggestions to accomplish the task

thank you

Tom Keffer

unread,
Sep 6, 2022, 9:29:45 PM9/6/22
to weewx-user
The unit "Kilowatt per hour" doesn't make a lot of sense (perhaps you mean kilowatt-hours?), but putting that aside, usually you record (in the database) either the amount of energy used during the archive period (typically 5 minutes), or you record an "accumulated energy" since the last reset of the monitor.

If the former, you end up with tags such as $month.delta_energy.sum, if the latter, $month.accumulated_energy.diff

Either way, the units are energy, typically watt-seconds, watt-hours, or kilowatt-hours.

We'd need more information to get more specific. In particular, you didn't say who built the monitor.

-tk




--
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/2fd95946-f1e0-48cb-b95d-3ece52a6e512n%40googlegroups.com.

Mks Mk

unread,
Sep 6, 2022, 11:01:53 PM9/6/22
to weewx-user
yes it is kilowatt-hours (kw) the kit is made by efergy.
we have added electricity column to the database and the sensor data in (kw) is logged to database every 5 minutes and it is in agreement to what the kit display is showing in (kw).
the display shows the consumption (kw) per day , week, month ,year but we can not adjust the tariff for our need and we believe it is hardcoded in the firmware.
in order to get the cost per month we need to have the kw consumed during  the month but we do not know how to do it within weewx.

gjr80

unread,
Sep 7, 2022, 3:36:06 AM9/7/22
to weewx-user
If you already have the data from the Energy going into a field named electricity then you re 90% there. As Tom said, if your WeeWX field (ie electricity) contains the number of kWh consumed in an archive period (I assume this is five minutes) then using the tag $month.electricity.sum in a WeeWX report template will display the month-to-date electricity usage. If the electricity field holds a cumulative kWh value (ie the electricity used in a five minute archive period is the difference between successive values) then the $month.electricity.diff tag will display the month-to-date electricity usage. These tags will actually return a formatted string, if you want to  do some maths on the month-to-date usage value then owe need to do things a little differently. There are a couple of different ways you could do this but the easiest approach for some simple maths is to include some in-line python code in a WeeWX report template. Let's say the cost of electricity is 20 cents per kWh, the electricity field contains the archive period electricity usage and you want to display something like:

Usage this month: 283 kWh
Cost this month: $ 56.60

In this case you would add the following lines of code to your template (untested):

#set $mtd_cost = round($month.electricity.sum.raw * 0.20, 2)
Usage this month: $month.electricity.sum
Cost this month: $ $mtd_cost

The first line of code obtains the month-to-date electricity usage (ie the sum) as a number (.raw gives us a number instead of a string) which we multiply by 0.2 (the cost of 1kWh) and then round the result to two decimal places. This value is assigned to the variable mtd_cost. The second line displays the month-to-date usage in kWh, I expect that rather than displaying 283 kWh it probably displays something like 283.134356. No problems, we just need to let WeeWX know that the field electricity contains values in kWh - we will come back to that later. The third line displays the month-to-date cost in dollars using the value we calculated in the first line. WeeWX has no knowledge of currency so we have to manually format the cost ourselves through use of the round() function in the first line and the dollar sign literal (the first dollar sign) in the third line (note the space between the dollar signs on the third line, this is important).

This code has a couple of limitations. As I mentioned, unless we let WeeWX know that the electricity field contains values in kWh the automatic formatting of $month.electricty.sum won't work properly. Secondly, if your Efergy ever has a hiccup, or for some reason there is no data in field electricity for the month, the tag $month.electricity.sum.raw will return the python value None and the calculation in the first line of code will return an error and the template will abort.

To tell WeeWX that field electricity contains values in kWh add the following code to the bottom of /home/weewx/bin/user/extensions.py or /usr/share/weewx/user/extensions.py (depending on how you installed WeeWX):

# override the default units for group_energy and set to kWh
weewx.units.USUnits['group_energy'] = 'kilo_watt_hour'
weewx.units.MetricUnits['group_energy'] = 'kilo_watt_hour'
weewx.units.MetricWXUnits['group_energy'] = 'kilo_watt_hour'

# set default format and label for kilowatt hours
weewx.units.default_unit_format_dict['kilo_watt_hour'] = '%.1f'
weewx.units.default_unit_label_dict['kilo_watt_hour'] = ' kWh'

# define conversion functions for energy
weewx.units.conversionDict['watt_hour'] = {'kilo_watt_hour': lambda x: x / 1000.0}
weewx.units.conversionDict['kilo_watt_hour'] = {'watt_hour': lambda x: x * 1000.0}

# assign database field electricity to group_energy
weewx.units.obs_group_dict['electricity'] = 'group_energy'


The above code:
(1) tells WeeWX to override the units used for energy from the default Wh to kWh
(2) sets the default formatting and unit label for fields in kWh to one decimal place and 'kWh' respectively
(3) defines conversion functions allowing conversion between Wh and kWH and vice versa (not strictly required in your case but handy to have since we are in here)
(4) tells WeeWX that the field electricity belongs to group_energy (which we have already set to use kWh by default). Refer to the section Assigning a unit group in the Customisation Guide for a little more background.

To handle the condition where $month.electricity.sum.raw is None we need to change our in-line python code. Change the first line:

#set $mtd_cost = round($month.electricity.sum.raw * 0.20, 2)

to something like:

#if $month.electricity.sum.raw is not None
#set $mtd_cost = round($month.electricity.sum.raw * 0.20, 2)
#else
#set $mtd_cost = '---'
#end if

Now if $month.electricity.sum.raw returns the value None we simply display --- as the month-to-date cost otherwise we calculate it as before.

That is probably a lot to take in, especially if you are not familiar with python and WeeWX. See how you go and post back here if you have any issues or further questions.

Gary

Karen K

unread,
Sep 7, 2022, 6:46:32 AM9/7/22
to weewx-user
mksm...@gmail.com schrieb am Mittwoch, 7. September 2022 um 05:01:53 UTC+2:
yes it is kilowatt-hours (kw) the kit is made by efergy

kilowatt-hours and kw is not the same.

power: kilowatt kW 
energy: kilowatt-hours kWh 

It is essential to know what of them the device supplies, because the calculation is quite different if you have one or the other. In case it is energy (in kilowatt-hours kWh) you need to sum up the values as said by Tom and Gary. In case it is accumulated energy (in kilowatt-hours kWh, too) you need to calculate the difference as described by Tom and Gary, too. In case it is power you need to integrate the readings over time to get the total energy. The latter can be done by energy_integral aggregation type provided by the weewx-GTS extension.

Mks Mk

unread,
Sep 7, 2022, 7:45:00 AM9/7/22
to weewx-user
The sensor signal contain Amps reading which is analyzed by rtl_433 but we adjusted the SDR driver to convert and send KW instead, which is what the lcd display is showing and the SDR driver is sending KW reading to weewx every 20 seconds, we should have mentioned this earlier (our apology).

the 1st part of Gary suggestion was added to current.inc but the 2nd part crashed weewx when we added the code to extensions.py
we stopped weewx and run it again but failed to start with following error:

Sep  7 13:00:02 Mks systemd[1]: Starting LSB: weewx weather system...
Sep  7 13:00:02 Mks weewx[28024]: Starting weewx weather system: weewx
Sep  7 13:00:02 Mks weewx[28034]: Traceback (most recent call last):
Sep  7 13:00:02 Mks weewx[28034]:   File "/usr/share/weewx/weewxd", line 29, in <module>
Sep  7 13:00:02 Mks weewx[28034]:     import user.extensions
Sep  7 13:00:02 Mks weewx[28034]:   File "/usr/share/weewx/user/extensions.py", line 20, in <module>
Sep  7 13:00:02 Mks weewx[28034]:     weewx.units.USUnits['group_energy'] = 'kilo_watt_hour'
Sep  7 13:00:02 Mks weewx[28034]: NameError: name 'weewx' is not defined
Sep  7 13:00:02 Mks weewx[28035]:  failed!
Sep  7 13:00:02 Mks systemd[1]: weewx.service: Control process exited, code=exited, status=1/FAILURE
Sep  7 13:00:02 Mks systemd[1]: weewx.service: Failed with result 'exit-code'.

we appreciate you all for your usual help and support.

Mks Mk

unread,
Sep 7, 2022, 8:22:06 AM9/7/22
to weewx-user
the sensor reading is not constant but fluctuating based on power demand.

elect.png

gjr80

unread,
Sep 7, 2022, 8:22:46 AM9/7/22
to weewx-user
My mistake, I forgot an import. Try adding the following line before the extensions.py code I listed previously:

import weewx.units

As for what is being stored in WeeWX field electricity it's not clear to me whether it is kW or kWh. The former is a measure of power and the latter a measure of energy (or power used over a period of time) - quite different things. Your first post was not clear, your second post seemed to confirm it was kWh and the last post seems to indicate it is kW. You need a value in kWh otherwise you Efergy display and WeeWX may agree, but your cost calculation will be nonsense.

Gary

Greg Troxel

unread,
Sep 7, 2022, 8:59:57 AM9/7/22
to Mks Mk, weewx-user

Mks Mk <mksm...@gmail.com> writes:

> yes it is kilowatt-hours (kw) the kit is made by efergy.
> we have added electricity column to the database and the sensor data in
> (kw) is logged to database every 5 minutes and it is in agreement to what
> the kit display is showing in (kw).
> the display shows the consumption (kw) per day , week, month ,year but we
> can not adjust the tariff for our need and we believe it is hardcoded in
> the firmware.
> in order to get the cost per month we need to have the kw consumed during
> the month but we do not know how to do it within weewx.

I know this is the weewx list, and I know that any program that stores
data and does calculations can be extended to do anything, but I would
suggest that you look at Home Assistant (HA), which is aligned to overall
home monitoring and control rather than weather. It has an energy
dashboard already set up which probably does what you want.

Also, it's easy to integrate weewx and HA via the mqtt weewx plugin. I
send weather data that way and ingest it to HA for use in automations
and seeing it on my phone. But weewx owns the weather station and the
responsibility for long-term storage of weather data, and production of
weewx-style web pages, NOAA charts, etc.
signature.asc

Mks Mk

unread,
Sep 7, 2022, 9:14:24 AM9/7/22
to weewx-user
the addition to  extensions.py worked this time.

regarding the data produced by the SDR driver is in agreement with the display ( the display shows "kw") 

consum.png

in weewx database these numbers are stored there (parts of these data are below)

dateTime     usUnits interval   electricity
1662544500    1    5    11.1180575
1662545100    1    5    3.4304875
1662545700    1    5    18.377194
1662546000    1    5    6.157195
1662546300    1    5    14.887015
1662546600    1    5    15.50398667
1662546900    1    5    11.88355667
1662547200    1    5    11.76374833
1662547500    1    5    11.74191333
1662547800    1    5    11.86812
1662548100    1    5    4.113761667
1662548400    1    5    20.969465
1662548700    1    5    0.551613333
1662549000    1    5    13.065745
1662549300    1    5    14.59239833
1662549600    1    5    3.075086667
1662549900    1    5    18.3986
1662550200    1    5    9.164375
1662550500    1    5    12.008095
1662550800    1    5    10.84667833
1662551100    1    5    14.874585
1662551400    1    5    0.549743333
1662551700    1    5    14.52300667
1662552000    1    5    0.55561
1662552300    1    5    7.805838333
1662552600    1    5    21.9475575
1662552900    1    5    8.644086
1662553200    1    5    13.64986333
1662553500    1    5    10.8020275
1662553800    1    5    13.393798
1662554100    1    5    11.498542

we need to calculate the monthly kw used and regarding the cost issue , excel with some future calibration to data should make it close enough for our like

thank you

gjr80

unread,
Sep 8, 2022, 3:21:50 AM9/8/22
to weewx-user
Judging by the plot you posted earlier and the above database extract those figures are also certainly in kW not kWh. They are certainly not cumulative and to be using 21kWh in a five minute period is is unlikely unless you are talking about some sort of industrial application.

In that case you have a few choices. As kk4468460 mentioned you could install her weewx-GTS extension and it would allow you to use a tag such as $month.electricity.energy_integral to display the month-to-date electricity usage. This would necessitate some changes to the code I provided above. In extensions.py you would now need something like (untested):

import weewx.units

# override the default units for group_power and set to kW
weewx.units.USUnits['group_power'] = 'kilowatt'
weewx.units.MetricUnits['group_power'] = 'kilowatt'
weewx.units.MetricWXUnits['group_power'] = 'kilowatt'

# assign database field electricity to group_power
weewx.units.obs_group_dict['electricity'] = 'group_power'

This tells WeeWX that field electricity contains power values in kW. The other code that was removed is superfluous given weewx-GTS is being used.

Your template code then becomes (again untested):

#if $month.electricity.has_data and $month.electricity.energy_integral.raw is not None
#set $mtd_cost = round($month.electricity.energy_integral.raw * 0.20, 2)
#else
#set $mtd_cost = '---'
#end if
Usage this month: $month.electricity.energy_integral
Cost this month: $ $mtd_cost

In this case we have substituted $month.electricity.energy_integral.raw of $month.electricity.sum.raw and slightly altered the check for electricity usage data, but otherwise it is largely the same.

I mentioned some other approaches, these will largely involve writing some python code to extend WeeWX or funding some calculations using the WeeWX StdCalibrate service. Despite only a very small part of weewx-GTS being used I think it is a better approach.

Gary

Mks Mk

unread,
Sep 12, 2022, 1:51:27 PM9/12/22
to weewx-user
after installing (weewx-GTS extension) also followed the instructions on this post, we have one more question which is how to get these numbers to be displayed in Kilowatt hour instead of Watt hour.
we added the tag "electricity" to current.inc and it gets generated like the image below:

elect.png
also added the tag "electricity" to NOAA monthly report and it gets generated like the image below:

noaa_report.png

thank you

gjr80

unread,
Sep 12, 2022, 3:45:14 PM9/12/22
to weewx-user
My mistake, change $month.electricity.energy_integral.raw and $month.electricity.energy_integral to $month.electricity.energy_integral.kilowatt_hour.raw and $month.electricity.energy_integral.kilowatt_hour respectively. No need to restart WeeWX.

Gary

Mks Mk

unread,
Sep 12, 2022, 11:49:50 PM9/12/22
to weewx-user

we truly appreciate your support and cannot thank you enough.

God bless you


Reply all
Reply to author
Forward
0 new messages