<<<WeeWX Guides>>>

Extending High/Low in Seasons

Background

Always looking to expand and extend the capabilities of WeeWX, I am always curios to know how todays temperatures compare to previous years. The data is availale in the Monthly and Yearly Reports, but to be honest it is quite tedious going through all of those. So I decided to see if I could add that data to the High/Low page.

There is some information in the user guide about this but that is only half the story.

These instructions document how I got this up and running

Modify stats.py

If you look in the examples subdirectory you will find a program called stats.py, this is very close to what I want and only requires a few changes. We are going to modify that program and then tell WeeWX to use it later.

import datetime
import time

from weewx.cheetahgenerator import SearchList
from weewx.tags import TimespanBinder
from weeutil.weeutil import TimeSpan

class MyStats(SearchList):                                                   # 1

    def __init__(self, generator):                                           # 2
        SearchList.__init__(self, generator)

    def get_extension_list(self, timespan, db_lookup):                       # 3

        # First, create TimespanBinder object for all time. This one is easy
        # because the object timespan already holds all valid times to be
        # used in the report.
        all_stats = TimespanBinder(timespan,
                                   db_lookup,
                                   context='alltime',
                                   formatter=self.generator.formatter,
                                   converter=self.generator.converter,
                                   skin_dict=self.generator.skin_dict)       # 4

        # Now get a TimespanBinder object for the last seven days. This one we
        # will have to calculate. First, calculate the time at midnight, seven
        # days ago. The variable week_dt will be an instance of datetime.date.
        week_dt = datetime.date.fromtimestamp(timespan.stop) - \
                    datetime.timedelta(weeks=1)                              # 5
        # Convert it to unix epoch time:
        week_ts = time.mktime(week_dt.timetuple())                           # 6

        # Form a TimespanBinder object, using the time span we just
        # calculated:
        seven_day_stats = TimespanBinder(TimeSpan(week_ts, timespan.stop),
                                         db_lookup,
                                         context='seven_day',
                                         formatter=self.generator.formatter,
                                         converter=self.generator.converter,
                                         skin_dict=self.generator.skin_dict) # 7

        # Now create a small dictionary with keys 'alltime' and 'seven_day':
        search_list_extension = {'alltime'   : all_stats,
                                 'seven_day' : seven_day_stats}              # 8

        # Finally, return our extension as a list:
        return [search_list_extension]                                       # 9
    

I am not sure if the supplied program has some bugs in it or not, but the two highlighted changes are required for this to work

Modify Statistics.inc

The changes we have just made will tell WeeWX to populate some new variables with the data we are looking to display on our Website, but in order to do that we have to modify the WEB Page where we intend to display the data. In my case this will be the statistics page and to do that we need to edit statistics.inc

Surprisingly this only requires a couple of minor changes at the top of the file

#errorCatcher Echo

#set $archive_data = [$day, $seven_day, $month, $year, $alltime]

<div id="statistics_widget" class="widget">
  <div class="widget_title">
    Statistics
  </div>
  <div class="widget_contents">

    <table>
      <tbody>
        <tr>
          <td></td>
          <td></td>
          <th class="hilo_day">Today</th>
          <th class="hilo_seven_day">Last 7 Days</th>
          <th class="hilo_month">This Month</th>
          <th class="hilo_year">This Year</th>
          <th class="hilo_alltime">All Time</th>
        </tr>
        <tr>
          <td class="label">$obs.label.outTemp</td>
          <td class="units">$unit.label.outTemp</td>
          #for $archive in $archive_data
          <td class="data new_row">$archive.outTemp.max.format(add_label=False)<br/>
            <span class="timestamp">$archive.outTemp.maxtime</span><br/>
            $archive.outTemp.min.format(add_label=False)<br/>
            <span class="timestamp">$archive.outTemp.mintime</span>
          </td>
          #end for
        </tr>
.
.
.
    

The code here first sets up an array, which is the first change we make to include our new seven_day and alltime variables

The only other change is to add the new headings into the table as shown by the second and third highighted code. The final part of this include file simply loops through the array creating the table which will be displayed on our WEB page.

If you want to alter what is displayed on this page then this is where you do that, but that should be self explanatory and easily tested through trial and error

Bringing it all together

The final part of this process is to tell WeeWX to run the modified python script, stats.py. This is done by simply copying the file into the bin/user subdirectory and telling WeeWX to use the new python script.

First copy new file into the user/bin subdirectory as shown below.

pi@raspberrypi:~ $ cp stats.py /home/weewx/bin/user

pi@raspberrypi:~ $ ls -al /home/weewx/bin/user
total 64
drwxr-xr-x  3 pi root  4096 Feb 20 12:06 .
drwxr-xr-x 11 pi root  4096 Feb 20 12:06 ..
-rwxr-xr-x  1 pi pi    3110 Feb 20 12:20 cputemp.py
-rwxr-xr-x  1 pi pi    4169 Feb 20 12:20 ecowitt.py
-rw-r--r--  1 pi root   541 Jun  2  2020 extensions.py
-rwxr-xr-x  1 pi pi    9912 Feb 20 12:20 s3backup.py
-rwxr-xr-x  1 pi pi   13121 Feb 20 12:20 s3upload.py
-rwxr-xr-x  1 pi pi    3992 Feb 20 12:20 stats.py
    

Then update the skin.conf file that corrosponds to the section where you modified statistics.inc, In my case this is the skin.conf in the Seasons subdirectory, and add the instructions to run the new python script as shown below.

pi@raspberrypi:~ $ vi /home/weewx/skins/Seasons/skin.conf

###############################################################################

# The CheetahGenerator creates files from templates.  This section
# specifies which files will be generated from which template.

[CheetahGenerator]

    # Possible encodings are 'html_entities', 'utf8', or 'strict_ascii'
    encoding                = html_entities
    search_list_extensions  = user.stats.MyStats

    [[SummaryByMonth]
.
.
.