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']

Also, you are using the wrong pm 2.5 values to calculate AQI.  The EPA the AQI calculation in Feb 2024.  See:

John

On Mar 5, 2025, at 1:58 PM, Andrew McGinnis <[email protected]> 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/dd0ff07e-070e-4c3e-8045-93c579bbf93bn%40googlegroups.com.

--
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/1131C0D0-D1AB-4266-94CE-99326E71FF46%40johnkline.com.

Reply via email to