Good. For the future: this is why it's better to store the amount
accumulated during the archive period for extensive quantities like rain
and energy, rather than the cumulative value. This way, you can just do a
simple sum over the desired time period to get total energy consumption.

But, if you have the cumulative value in Python, why are you messing around
with RecordBinders and the like? Why not just calculate the delta and
include it in the archive record like any other quantity?

-tk

On Wed, Jan 11, 2017 at 4:09 PM, Louis De Lange <[email protected]> wrote:

> Or I could just add .raw at the end of the call to RecordBinder...
>
> last_cum_energy = rb.latest().cumEnergy.*raw*
>
>
> Thanks, Tom - as always.  I did not really want to do the calculations in
> the template because I want to store the incremental energy use in the
> database so I can make hourly bar style graphs like weewx does for rain.
> Reading through your response made the light go on - adding .raw at the end
> returns a number and that all I needed
>
>
>
> On 11 January 2017 at 15:32, Thomas Keffer <[email protected]> wrote:
>
>> Hello, Louis,
>>
>> The role of ValueHelper is explained in the Customization Manual
>> <http://weewx.com/docs/customizing.htm#how_tags_work>. It's the
>> ValueHelper that carries the information you specified in skin.conf
>> about what units and formats you want. It's what makes sure the output is
>> always in the chosen units, using the chosen formatting. Perhaps it would
>> help to look at its implementation
>> <https://github.com/weewx/weewx/blob/master/bin/weewx/units.py#L843>.
>>
>> If Cheetah needs to convert a ValueHelper to a string, it calls __str__,
>> which is when the default formatting functions are invoked. But, notice it
>> also has member functions like format, nolabel, raw, etc. These are the 
>> formatting
>> option <http://weewx.com/docs/customizing.htm#formatting_options> listed
>> in the Customizing Manual, that allow you to override the defaults. It is
>> these functions that get invoked if you write something like
>> $current.outTemp.nolabel(".2f")
>>
>> Back to your problem with calculating the differences. There are two ways
>> you can approach this.
>>
>> 1. Do the calculation in the templates. This is probably the simplest.
>> Get the current value, and subtract from it the value, say, one hour ago
>> (NOT TESTED):
>>
>> #set diff = $current.cumEnergy.raw - $hours_ago($hours_ago=1).cumEn
>> ergy.last.raw
>>
>> <p>The energy consumed over the last hour was: $diff</p>
>>
>>
>> Note the use of the .raw suffix. This returns a *number* instead of a
>> ValueHelper, allowing you to do arithmetic on it. Of course, after the
>> subtraction, you are left with a new number, not a ValueHelper, so it's
>> up to you to make sure it's in the right units and uses the right
>> formatting.
>>
>> 2. Define a search list extension (SLE). This is described in the manual
>> <http://weewx.com/docs/customizing.htm#defining_new_tags>.
>>
>> Hope this helps.
>>
>> -tk
>>
>>
>>
>> On Wed, Jan 11, 2017 at 2:55 PM, Louis De Lange <[email protected]>
>> wrote:
>>
>>> This question is more related to python programming than using weewx,
>>> but I cant find a solution - so I hope that Tom and the other gurus can
>>> point me in the right direction.
>>>
>>> I am writing a function to capture power consumption from the utility
>>> meter.  As part of that I am storing the cumulative energy reading for each
>>> archive period in the archive.
>>>
>>> To get the incremental energy reading between archive periods I read the
>>> last archive record of the cumulative energy with the "latest" function in
>>> weewx.tags.  Reading and printing the latest archive record of cumulative
>>> energy works fine - here is a snippet of the code to do that (thanks to
>>> Tom's answers to previous threads)
>>>
>>>       # Create a RecordBinder, bound to the database and timestamp
>>>
>>>       rb = weewx.tags.RecordBinder(db_lookup, timestamp)
>>>       print "The last cumulative electrical energy record =",
>>> rb.latest().cumEnergy
>>>
>>> When I try to subtract the value for rb.latest().cumEnergy from the
>>> current cumulative energy reading just obtained from the meter, I get a
>>> type error.
>>>
>>> Then I added the following code to try and figure out what the out type
>>> of rb.latest().cumEnergy is
>>>
>>>       last_cum_energy = rb.latest().cumEnergy
>>>       print type(last_cum_energy)
>>>
>>> This does not help because the output of the last print command is
>>>
>>> <class 'weewx.units.ValueHelper'>
>>>
>>>
>>> I tried doing a float modification, but that does not work.
>>>
>>> Any help to turn the output of the function into something I can do
>>> arithmetic with will be appreciated.
>>>
>>> --
>>> 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.
>>>
>>
>> --
>> 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.
>>
>
> --
> 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.
>

-- 
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.

Reply via email to