You're going to have to give us more information about where your variable
"pressure" came from.

Other bits, probably not related to your problem.

1. Never assume a variable always contains a valid value. Every instrument
fails.

2. To compare with None use

if pressure is not None:
  foo = do arithmetic
else:
  foo = do nothing

3. Pressure with single quotes ('pressure') is a string. Variables are
unadorned.

4. The variable pressure in my example is undefined because in my little
example I didn't define it. I'm assuming you defined it somewhere, which
brings us back to my first paragraph above.


On Thu, May 2, 2024 at 6:22 PM Michael Frotscher <[email protected]>
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/09981f6c-0d06-4a4c-b438-91c46cc01c84n%40googlegroups.com
> <https://groups.google.com/d/msgid/weewx-development/09981f6c-0d06-4a4c-b438-91c46cc01c84n%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/CAPq0zECE7wJvdByZ9_aWwMyk2uD3Yr0%3DQwPP2rAzwx7nht6sEg%40mail.gmail.com.

Reply via email to