Calculating within a template, while needing a table to pull the variables from

74 views
Skip to first unread message

Andrew McGinnis

unread,
Mar 5, 2025, 4:58:47 PM3/5/25
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?

gjr80

unread,
Mar 5, 2025, 5:26:02 PM3/5/25
to weewx-user
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 in the Customisation guide.

Gary

bell...@gmail.com

unread,
Mar 5, 2025, 7:19:49 PM3/5/25
to weewx-user
While not answering your question, I do have a XType extension, https://github.com/bellrichm/weewx-aqi-xtype, that I think does what you want. It is not widely adopted, so it is a bit rough around the edges (feedback is always welcome).
If you do want to try it, I recommend installing the latest pre-release, https://github.com/bellrichm/weewx-aqi-xtype/releases/tag/v2.0.0-rc02.
rich

John Kline

unread,
Mar 5, 2025, 8:20:02 PM3/5/25
to weewx...@googlegroups.com, weewx-user
I think you need to change:
   #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

to:
#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']
--
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+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/weewx-user/dd0ff07e-070e-4c3e-8045-93c579bbf93bn%40googlegroups.com.

Andrew McGinnis

unread,
Mar 6, 2025, 10:48:24 AM3/6/25
to weewx...@googlegroups.com
The lack of brackets was the issue. Needed to be bp['BP_lo'], etc. for them.  Thank you!

And thanks for the updated numbers; my googling got me to another page that wasn't the EPA, and had only the prior breakpoints. 

You received this message because you are subscribed to a topic in the Google Groups "weewx-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/weewx-user/zb97bFS27Kw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to weewx-user+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/weewx-user/1131C0D0-D1AB-4266-94CE-99326E71FF46%40johnkline.com.
Reply all
Reply to author
Forward
0 new messages