That did it, thanks, Frantisek! I now check that if 'rain' is in event.record, then check if it is non-Null, and if those two conditions are true, 'rain' has a valid value and I can use it for arithmetic.
On Saturday, May 4, 2024 at 4:17:10 AM UTC-5 František Slimařík wrote: > Hello, > > event.record is dictionary. If you do event.record["rain" ] = something > you add new key/value into dictionary (if doesn´t exists or overwrite > current value). In case packet doesn´t contain no value for rain, key/value > pair is not added. If you do something and you are not sure if key exist in > dictionary you should do: > > if "rain" in event.record: > do_something > > > pá 3. 5. 2024 v 21:31 odesílatel Michael Frotscher <[email protected]> > napsal: > >> Even more unexpected, it seems I can write to those "undefined" >> variables, but trying to read them gives me a Key Error. >> I have created a new field in the database called "raintotal", where I >> want to store the running total of the rainfall. >> Trying to test this for being "None", gives: >> CRITICAL __main__: **** if event.record['raintotal'] == None: >> CRITICAL __main__: **** ~~~~~~~~~~~~^^^^^^^^^^^^^ >> CRITICAL __main__: **** KeyError: 'raintotal' >> CRITICAL __main__: **** Exiting. >> >> But if I just write to it: >> >> event.record['raintotal'] = 12345 >> >> That ends up in the database and doesn't throw an error. >> >> On Friday, May 3, 2024 at 2:06:22 PM UTC-5 Michael Frotscher wrote: >> >>> Same here, 'rain' is None unless it's raining, then it'll show data. I >>> haven't checked the raw packets, but in the archive table the numbers are >>> all multiples of my tipping bucket size, so that all makes sense. >>> That's why I added the "check for Null/None" if-clause. >>> But to be populated with data, 'rain' has to be defined. And weewx >>> crashes on that >>> if event.record['rain'] != None: >>> clause. Do I need to check for a packet? like event.package? >>> On Friday, May 3, 2024 at 12:29:38 PM UTC-5 František Slimařík wrote: >>> >>>> I believe "rain" is specific in this case. I was checking raw packets >>>> previously on my device and normally is rain "none" if it´s not raining ;) >>>> So it depends what your device is sending to weewx. >>>> >>>> raw packet: {'dateTime': 1642794129, 'usUnits': 1, 'rain_total': 0.0, >>>> 'barometer': 30.22, 'temperature_out': 28.7, 'dewpoint': 20.1, >>>> 'humidity_out': 69.0, 'wind_speed': 2.6, 'wind_gust': 2.6, 'wind_dir': >>>> 268.0, 'solar_radiation': 0.0, 'uv': 0.0, 'temperature_in': 70.1, >>>> 'humidity_in': 51.0, 'rain': None} >>>> >>>> pá 3. 5. 2024 v 15:37 odesílatel Michael Frotscher <[email protected]> >>>> napsal: >>>> >>>>> Hmm, >>>>> ok, so it works for "pressure", which contains data in every archive >>>>> period and is not None. >>>>> I've tried to substitute that with "rain", which is what I really >>>>> want. That is usually "Null/None" in the database, but gets the amount of >>>>> a >>>>> bucket tip added to it every time that happens. >>>>> It is fed by MQTT, which publishes the bucket amount on every tip, and >>>>> that works. It even sums them up correctly if several bucket tips occur >>>>> in >>>>> an archive period. >>>>> >>>>> However, trying to retrieve that with "if event.record['rain'] != None >>>>> :" >>>>> I get a Key Error. Meaning the variable doesn't exist. Isn't every >>>>> weewx measurement in the dictionary? >>>>> That happens with any measurement that's in the database but normally >>>>> "Null". >>>>> >>>>> I've tried to define it as Null/None in the init-part of the service >>>>> rain = None >>>>> >>>>> But still get the key error. >>>>> >>>>> Thanks! >>>>> On Friday, May 3, 2024 at 7:30:12 AM UTC-5 Michael Frotscher wrote: >>>>> >>>>>> Thanks, guys! >>>>>> I see my mistake now. It's not about strings/floats at all, but I >>>>>> wrongly assumed that all measurements that have values assigned to them >>>>>> (via LOOP or whatever) would already be defined as global variables. >>>>>> That's obviously not the case, but assigning the last value to the >>>>>> variable via "event.record['pressure']" (as Frantisek pointed out) is >>>>>> necessary. I'm still not sure why that needs to be in single quotes, >>>>>> indicating a string and not a number. >>>>>> I would have expected something like "event.record(pressure)". >>>>>> >>>>>> I have to admit that I have not found this in any examples out there, >>>>>> and not in the documentation for sure. >>>>>> >>>>>> But it's now doing what I want. >>>>>> >>>>>> On Friday, May 3, 2024 at 12:16:50 AM UTC-5 František Slimařík wrote: >>>>>> >>>>>>> I guess you want something like this: >>>>>>> >>>>>>> if event.record['pressure'] != None: >>>>>>> newpressure = (event.record['pressure'] * 10) >>>>>>> event.record['pb'] = newpressure >>>>>>> >>>>>>> Dne pátek 3. května 2024 v 3:38:01 UTC+2 uživatel Tom Keffer napsal: >>>>>>> >>>>>>>> On Thu, May 2, 2024 at 6:32 PM Michael Frotscher <[email protected]> >>>>>>>> wrote: >>>>>>>> >>>>>>>>> Here's the full code of my service: >>>>>>>>> >>>>>>>>> >>>>>>>> def new_archive_packet(self, event): >>>>>>>>> >>>>>>>>> if 'pressure' != None: >>>>>>>>> newpressure = ('pressure' * 10) >>>>>>>>> event.record['pb'] = newpressure >>>>>>>>> >>>>>>>> >>>>>>>> >>>>>>>> Strictly interpreted, you first ask whether the string "'pressure'" >>>>>>>> is equal to None. It's not, so we proceed to the next statement. Now >>>>>>>> you're >>>>>>>> trying to multiply a string ('pressure') by 10. Surprisingly, this >>>>>>>> will >>>>>>>> succeed, but likely does not give you the results you expect. The >>>>>>>> variable >>>>>>>> "newpressure" will actually be set to the string >>>>>>>> 'pressurepressurepressurepressurepressurepressurepressurepressurepressurepressure'. >>>>>>>> >>>>>>>> That is, the string 'pressure' concatenated 10 times. >>>>>>>> >>>>>>>> You want a *variable* pressure, not the literal string 'pressure'. >>>>>>>> Where will it come from? >>>>>>>> >>>>>>>> I would suggest taking an online Python course if this is >>>>>>>> unfamiliar to you. >>>>>>>> >>>>>>>> -tk >>>>>>>> >>>>>>>> >>>>>>>> -- >>>>> 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/8a2868e8-7f39-4038-acd5-c783572e2226n%40googlegroups.com >>>>> >>>>> <https://groups.google.com/d/msgid/weewx-development/8a2868e8-7f39-4038-acd5-c783572e2226n%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/cc1d11ae-3adf-46f5-a65a-c6da05880e73n%40googlegroups.com >> >> <https://groups.google.com/d/msgid/weewx-development/cc1d11ae-3adf-46f5-a65a-c6da05880e73n%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/44a5a9c6-491f-4c10-a777-78f310a2a04dn%40googlegroups.com.
