I've not used user defined functions in a Cheetah template before, but an obvious problem stands out to me. $day.pm2_5out.avg will produce a formatted string, whereas your user defined function requires a numeric parameter be passed to it. Try using:
#set aqi = $getAQI($day.pm2_5out.avg.raw). Some more background can be found here <https://weewx.com/docs/5.1/custom/cheetah-generator/#formatting-options> in the Customisation guide. Gary On Thursday, 6 March 2025 at 07:58:47 UTC+10 Andrew McGinnis wrote: > 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? -- 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 [email protected]. To view this discussion visit https://groups.google.com/d/msgid/weewx-user/415abebc-7caf-457e-97c2-ad25ffbfccdbn%40googlegroups.com.
