[weewx-user] event.record['ET'] throws me a KeyError. Why?

2020-05-27 Thread gjr80
Hi,

Where does your service sit in the service order in relation to StdWXCalculate 
in weewx.conf under [Engine] [[Services]]? One possible reason is that if your 
station does not provide field ET (not many (any?) provide a numeric value in 
every archive record) and the StdWXCalculate service then calculate ET if it 
can. If your service that references ET is executed before StdWXCalculate then 
field ET will not (yet) exist in the archive record. In this case the solution 
is put your service after StdWXCalculate.

Gary

-- 
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 weewx-user+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/weewx-user/8be968cc-0ef0-4935-8734-c9afbf998f23%40googlegroups.com.


[weewx-user] event.record['ET'] throws me a KeyError. Why?

2020-05-26 Thread Maarten van der Hoeven
Hi group,

Having programming experience, but new to python. Want to write some piece 
of code, that calculates the amount of precipitation shortage (rain minus 
evaotranspiration). It's a measure of drought.

Using some example coding, and extended those.


See the piece of code below. event.record['barometer'] and 
event.record['outTemp'] is working (used as an example, no drought-relation 
ship); they give real values.

However, event.record['ET'] gives me a KeyError. I dont understand it. It 
is in my wview_extended schema (which I extended), its also populated in my 
database.

Why the KeyError?


import schemas.wview_extended
schema = {
'table': schemas.wview_extended.table + [('neerslagtekort', 'REAL')],
'day_summaries' : schemas.wview_extended.day_summaries + 
[('neerslagtekort', 'SCALAR')]
}

import weewx.units
weewx.units.obs_group_dict['neerslagtekort'] = 'group_rain'


neerslag = 0
verdamping = 0
neerslagtekort = 0

class AddElectricity(StdService):

def __init__(self, engine, config_dict):

  # Initialize my superclass first:
  super(AddElectricity, self).__init__(engine, config_dict)

  # Bind to any new archive record events:
  self.bind(weewx.NEW_ARCHIVE_RECORD, self.new_archive_record)

  self.last_total = None

def new_archive_record(self, event):


   barometer = event.record['barometer']
   outTemp = event.record['outTemp']
   ET = event.record['ET']
   shortage = precipitation - evaporation

   if self.last_total:
  event.record['neerslagtekort'] = shortage

   self.last_total = shortage
   print(" NEERSLAGTEKORT  ")
   print(shortage)



Traceback (most recent call last):
  File "/usr/share/weewx/weewxd", line 261, in 
main()
  File "/usr/share/weewx/weewxd", line 154, in main
engine.run()
  File "/usr/share/weewx/weewx/engine.py", line 202, in run
self.dispatchEvent(weewx.Event(weewx.POST_LOOP))
  File "/usr/share/weewx/weewx/engine.py", line 224, in dispatchEvent
callback(event)
  File "/usr/share/weewx/weewx/engine.py", line 596, in post_loop
self._software_catchup()
  File "/usr/share/weewx/weewx/engine.py", line 656, in _software_catchup
self.engine.dispatchEvent(weewx.Event(weewx.NEW_ARCHIVE_RECORD,
  File "/usr/share/weewx/weewx/engine.py", line 224, in dispatchEvent
callback(event)
  File "/usr/share/weewx/user/electricity.py", line 64, in 
new_archive_record
ET = event.record['ET']
KeyError: 'ET'


mysql> select from_unixtime(datetime), neerslagtekort,ET,rain from archive 
order by datetime desc limit 5;
+-+-+--+--+
| from_unixtime(datetime) | neerslagtekort  | ET  
 | rain |
+-+-+--+--+
| 2020-05-26 23:26:00 | -1.5559202862588535 |  0.0515329020092 
|0 |
| 2020-05-26 23:25:00 |NULL |  0.05156477243511403 
|0 |
| 2020-05-26 23:24:00 |  20 |  0.05159745765396156 
|0 |
| 2020-05-26 23:23:00 |  20 | 0.051629007161015734 
|0 |
| 2020-05-26 23:22:00 |NULL |  0.05165840624070325 
|0 |
+-+-+--+--+
5 rows in set (0.00 sec)


-- 
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 weewx-user+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/weewx-user/901f6de4-762e-4dae-8604-427b4168b25c%40googlegroups.com.