templating with cheetah for last 2 days

96 views
Skip to first unread message

Hyrules Hyrules

unread,
Feb 1, 2020, 12:42:45 PM2/1/20
to weewx-user
Hi everyone,

I was wondering if there is an actual way of doing this in the cheetah templating engine.

I would like to get the following information in my file :

- last 2 days of archive
- starting at midnight and 15 minutes
- in intervals of 15 minutes
- all through today closest to the quarter of the hour (eg if it's 12:47 -> 12:45, 1:24 -> 1:15)

I'm not sure if it's possible using the pre-existing $span or $day variables. I`ve been trying some stuff apparently unsucessfully.

Any input would be appreciated.

Thanks.

Thomas Keffer

unread,
Feb 2, 2020, 9:23:01 AM2/2/20
to weewx-user
I can't think of anyway of doing this either with the existing tag system. Essentially, you're asking for a new iteration and aggregation period: 15 minutes. 

If you know Python, you could try writing a search list extension, but it would not be a simple one.  You'd want it to allow something like

#for $chunk in $chunk15
<p>The average temperature is: $chunk.outTemp</p>
#end for

where chunk15 is your extension. This is not easy, because it would have to return an iterator, as well as allow observation types as an attribute.

Sorry!

-tk




--
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 on the web visit https://groups.google.com/d/msgid/weewx-user/2a33be30-0298-4634-8064-429ff27c2101%40googlegroups.com.

Hyrules Hyrules

unread,
Feb 2, 2020, 10:04:43 AM2/2/20
to weewx-user
Basically that's what i`ve been trying to do since i`ve posted this message. I will see how I can implement that with python. I will try to create my own skin with some python functions.

I have been able to run a custom SQL on the weewx database and return exactly what I want (in a search list extension) but i have yet to transform that into something that the cheetah engine understand and
think is a valuehelper in order for me to process in the engine. I'm not an expert in python but It's not really hard to learn and understand. I know a few programming languages myself.
In order to be able to use the value I will need to process each record to convert the value to the required units (eg : farenheit to celsius ) and the transform that to a ValueHelper.

Thanks for the tip
Message has been deleted

Hyrules Hyrules

unread,
Feb 2, 2020, 2:36:53 PM2/2/20
to weewx-user
Actually it's quite easy for those who would like to do it here is the code for the search list extension. All I need is to remove the first spans in the list and voila. This gives me all spans in intervals of 15 minutes from 2 days ago to closes quarter to now.

# BASED ON BELCHERTOWN PYTHON FILE START

from __future__ import with_statement
import datetime
import time
import calendar
import json
import os
import os.path
import syslog
import sys
import locale

import weewx
import weecfg
import configobj
import weedb
import weeutil.weeutil
import weewx.reportengine
import weewx.station
import weewx.units
import weewx.tags
import weewx.uwxutils

from collections import OrderedDict

from weewx.cheetahgenerator import SearchList
from weewx.tags import TimespanBinder
from weeutil.weeutil import to_bool, TimeSpan, to_int, archiveDaySpan, archiveWeekSpan, archiveMonthSpan, archiveYearSpan, startOfDay, timestamp_to_string, option_as_list
try:
   from weeutil.config import search_up
except:
   # Pass here because chances are we have an old version of weewx which will get caught below.
    pass
   
# Check weewx version. Many things like search_up, weeutil.weeutil.KeyDict (label_dict) are from 3.9
if weewx.__version__ < "3.9":
   raise weewx.UnsupportedFeature("weewx 3.9 and newer is required, found %s" % weewx.__version__)  
   
reload(sys)
sys.setdefaultencoding("utf-8")

def logmsg(level, msg):
   syslog.syslog(level, 'Hydrometeo Extension: %s' % msg)

def logdbg(msg):
   logmsg(syslog.LOG_DEBUG, msg)

def loginf(msg):
   logmsg(syslog.LOG_INFO, msg)

def logerr(msg):
   logmsg(syslog.LOG_ERR, msg)

# Print version in syslog for easier troubleshooting
VERSION = "0.1"
loginf("version %s" % VERSION)

class HydroMeteoStats(SearchList):
   def __init__(self, generator):
       SearchList.__init__(self, generator)

    def get_extension_list(self, timespan, db_lookup):      
        today = datetime.datetime.now()
       twodaysago = datetime.datetime.now() - datetime.timedelta(days=2)
       
       twodaysago = twodaysago.replace(hour=00, minute=00, second=00,microsecond=00) #.strftime("%Y-%m-%d %H:%M:%S")
       
       tsb = TimespanBinder(TimeSpan(time.mktime(twodaysago.timetuple()), time.mktime(today.timetuple())), db_lookup,formatter=self.generator.formatter, converter=self.generator.converter)
       spans = tsb.spans(interval=900)
       last2days = spans

        search_list_extension = {'last2days' : last2days}

        return [search_list_extension]


Thomas Keffer

unread,
Feb 2, 2020, 7:39:44 PM2/2/20
to weewx-user
I had completely forgotten about the .spans() iterator! 

Well done!

--
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.
Reply all
Reply to author
Forward
0 new messages