Andrew McGinnis
unread,Mar 5, 2025, 4:58:47 PM3/5/25Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to weewx-user
I'm trying to get a calculated AQI value shown on my index.html page, but getting index.html.tmpl right is failing me.
In the .tmpl file, I have at the top:
#def getAQI(C_p)
#set breakpoints = [
{'BP_lo': 0.0, 'BP_hi': 12.0, 'I_lo': 0, 'I_hi': 50},
{'BP_lo': 12.1, 'BP_hi': 35.4, 'I_lo': 51, 'I_hi': 100},
{'BP_lo': 35.5, 'BP_hi': 55.4, 'I_lo': 101, 'I_hi': 150},
{'BP_lo': 55.5, 'BP_hi': 150.4, 'I_lo': 151, 'I_hi': 200},
{'BP_lo': 150.5, 'BP_hi': 250.4, 'I_lo': 201, 'I_hi': 300},
{'BP_lo': 250.5, 'BP_hi': 350.4, 'I_lo': 301, 'I_hi': 400},
{'BP_lo': 350.5, 'BP_hi': 500.4, 'I_lo': 401, 'I_hi': 500}
]
#for bp in breakpoints
#if C_p >= bp.BP_lo and C_p <= bp.BP_hi
#set aqi = ((bp.I_hi - bp.I_lo) / (bp.BP_hi - bp.BP_lo)) * (C_p - bp.BP_lo) + bp.I_lo
#return int(aqi)
#end if
#end for
#return None ## If PM2.5 is out of range
#end def
and then later in the template file, that function is called to make a card with the AQI value shown, as calculated from my $day.pm2_5out.avg value:
#if $name == "AQI"
#set aqi = $getAQI($day.pm2_5out.avg)
<div class="row">
<div class="col-6">
<h4 class="h2-responsive">This is the AQI:$aqi</h4>
<p class="text-muted">
## PM2.5: $day.pm2_5out.avg μg/m³
</p>
</div>
</div>
This is not working. Fiddling with it, at best I can get a card to generate that shows the title, and the "This is the AQI:" (and the pm2_5out.avg when uncommented), but no $aqi value. At worst, '<class 'TypeError'>' or '<class 'NameError'>' or '<class 'AttributeError'>'..... Best guess is I'm doing the lookup table wrong.
I'm fully expecting that I can do a calculation within a template, based on an $observation (a la $growdeg/1.8 to get GDD in degree_C), but some combination of syntax, placement/location in the template, or simply template limitations are stopping me.
I know there are extensions to do AQI. They seem to pull from external data sources (NowCast, etc), and extend the database schema with additional observation types. I can probably tweak them to use my recorded PM measurements. But I have the base $pm2_5out variable and equation to calculate AQI myself, and don't want to record it as an observation- just shown for ephemeral display only, in my "Current" page.
Am I way out of line, or can someone nudge me in the right direction?