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 <http://weewx.com/docs/customizing.htm#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
On Wednesday, 7 September 2022 at 13:01:53 UTC+10 [email protected] wrote:

> 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.
>
> On Wednesday, September 7, 2022 at 4:29:45 AM UTC+3 [email protected] 
> wrote:
>
>> 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
>>
>>
>>
>>
>> On Tue, Sep 6, 2022 at 6:07 PM Mks Mk <[email protected]> wrote:
>>
>>> 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
>>>
>>> -- 
>>> 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 on the web visit 
>>> https://groups.google.com/d/msgid/weewx-user/2fd95946-f1e0-48cb-b95d-3ece52a6e512n%40googlegroups.com
>>>  
>>> <https://groups.google.com/d/msgid/weewx-user/2fd95946-f1e0-48cb-b95d-3ece52a6e512n%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>

-- 
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 on the web visit 
https://groups.google.com/d/msgid/weewx-user/6b3f9172-2808-4d5f-81f9-02c156e76eb9n%40googlegroups.com.

Reply via email to