Here's the full code of my service:
For testing purposes I try to retrieve the pressure value, do the
arithmetic and write it to a different, unused datafield. The MySQLdb is in
there to get the last non-null value of the field im interested in, but
that's not implemented so far.
#!/usr/bin/env python
import MySQLdb
import weewx
from weewx.engine import StdService
class AddRaintotal(StdService):
def __init__(self, engine, config_dict):
# Initialize my superclass first:
super(AddRaintotal, self).__init__(engine, config_dict)
# Bind to any new archive record events:
self.bind(weewx.NEW_ARCHIVE_RECORD, self.new_archive_packet)
# self.bind(weewx.NEW_ARCHIVE_RECORD, self.new_archive_record)
#(The example uses new_archive_record, but that gives me another, totally
different error)
def new_archive_packet(self, event):
if 'pressure' != None:
newpressure = ('pressure' * 10)
event.record['pb'] = newpressure
On Thursday, May 2, 2024 at 8:22:10 PM UTC-5 Michael Frotscher wrote:
> Tom,
>
> Thanks for the reply.
> The exact error is: CRITICAL __main__: **** ValueError: could not
> convert string to float: 'pressure'
> and weewx crashes when it encounters that.
>
> I have considered that the value might be Null (or None, rather), which is
> why I chose a variable that's fed from my barometer, so should always
> contain a valid value.
> But I have also added a check for that into my code, with the same result:
>
> if 'pressure' != None:
> foo = do arithmetic
> else:
> foo = do nothing
>
> FWIW, I'm using the "electricity" service example, and the code in
> question is in the "def new_archive_record(self, event):" section.
>
> But is it correct to refer to the variable as 'pressure'? With the single
> quotes?
> Because if I don't, like you have in your example, I get this:
>
> NameError: name 'pressure' is not defined
>
>
> On Thursday, May 2, 2024 at 7:52:25 PM UTC-5 Tom Keffer wrote:
>
>> When a value comes off the database it's always either a number, or the
>> Python value "None", with very few exceptions. You didn't show us the
>> error, but the issue is likely to be that "pressure" has the value "None",
>> which signals bad or missing data.
>>
>> Be sure to read the document *Notes for developers
>> <https://www.weewx.com/docs/5.0/devnotes/>*. It includes some
>> information about None.
>>
>> So, your example becomes,
>>
>> *foo = pressure * 10 if pressure is not None else None*
>>
>> Then, of course, you have to remember that "foo" could be None. This is a
>> very common pattern in WeeWX.
>>
>> Then again, perhaps the error is completely different and it's not due to
>> the value None. If you're still having trouble, show us the error.
>>
>> -tk
>>
>>
>> On Thu, May 2, 2024 at 5:44 PM Michael Frotscher <[email protected]>
>> wrote:
>>
>>> All,
>>> I'm trying to write a simple service that creates a new derived
>>> measurement based on values already in the weewx database. Ultimately, I
>>> want to get a running total of the rainfall, but I'm not there yet. Also,
>>> not a Python developer by trade, but I'm trying.
>>>
>>> My issue is: when I try to do arithmetic, the variables have, of course,
>>> to be numbers, be it int or float. I'll take the 'pressure' measurement as
>>> an example. Looking into the database, it seems to be a float, but it could
>>> also be a string.
>>> But trying to use it that way, or trying to convert the string into a
>>> float, always gives me errors (could not convert string to float). I've
>>> tried stripping invisible whitespaces from the string (if it is one), but
>>> the error remains.
>>> Here's the relevant code, one of the many I've tried.
>>>
>>> So how do I get a weewx-variable into usable form? I'm probably missing
>>> something pretty basic here.
>>> Thanks!
>>> BTW, is there a way to test-run a service file? Like have the python
>>> code run in the weewx-environment?
>>>
>>> foo = (float('pressure') * 10)
>>>
>>> or
>>>
>>> pressure_trim = 'pressure'.strip()
>>> foo = (float(pressure_trim) * 10)
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "weewx-development" 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-development/f08bf769-d2b2-4840-b0a3-2feeef33ea67n%40googlegroups.com
>>>
>>> <https://groups.google.com/d/msgid/weewx-development/f08bf769-d2b2-4840-b0a3-2feeef33ea67n%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>
--
You received this message because you are subscribed to the Google Groups
"weewx-development" 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-development/09036574-39e1-4466-a0fc-654ab299befan%40googlegroups.com.