OK here is the final goal....

I've been trying to complement the extension, APRS.py to have all the info 
required to properly publish an APRS weather beacon.

Originally it was missing the rain since midnight, but manage to fix that 
one.
Now i'm still missing the rain in the last 24 hrs and have been looking a 
different approach on how to resolve issues.
First I posted a request for help with that and got some answer and that I 
should learn on my now which I've been trying.
So I tried to generate the info via xstat, but with you suggestion i 
thought that I might just be simpler to add straight into the APRS.py 
code...
It runs at the same time that the .html files are generated.
 

Here is the entire code that i'm trying to add rain in the last 24 hrs:


*from* datetime *import* datetime

*import* os


*import* weeutil.weeutil

*import* weewx.engine

*import* weewx.units



*class* APRS(weewx.engine.StdService):

    *def** __init__*(self, engine, config_dict):

        super(APRS, self).__init__(engine, config_dict)

        conf = config_dict[*'APRS'*]

        self._output_filename = conf[*'output_filename'*]

        self._output_filename_tmp = self._output_filename + *'.tmp'*

        self._include_position = int(conf.get(*'include_position'*, 0))

        self._symbol_table = conf.get(*'symbol_table'*, *'/'*)

        self._symbol_code = conf.get(*'symbol_code'*, *'_'*)

        self._comment = conf.get(*'comment'*, '')


        self._message_type = *'_'* * # Weather report (no position)*

        self._time_format = *'%m%d%H%M'*

        self._latitude = None

        self._longitude = None

        self._wind_direction_marker = *'c'*

        self._wind_speed_marker = *'s'*

        *if* self._include_position:

           * # Position with timestamp (no APRS messaging)*

            self._message_type = *'/'*

            self._time_format = *'%d%H%Mz'*

            self._latitude = ''.join(weeutil.weeutil.latlon_string(

                self.engine.stn_info.latitude_f,

                (*'N'*, *'S'*), *'lat'*))

            self._longitude = ''.join(weeutil.weeutil.latlon_string(

                self.engine.stn_info.longitude_f,

                (*'E'*, *'W'*), *'lon'*))

            self._wind_direction_marker = ''

            self._wind_speed_marker = *'/'*


        self.bind(weewx.NEW_ARCHIVE_RECORD, self._handle_new_archive_record)


    *def** _handle_new_archive_record*(self, event):

        *"""Generate a positionless APRS weather report and write it to a 
file"""*

        record = event.record

        data = [

            self._message_type,

            datetime.strftime(

                datetime.utcfromtimestamp(record[*'dateTime'*]),

                self._time_format),

        ]

        *if* self._include_position:

            data.append(self._latitude)

            data.append(self._symbol_table)

            data.append(self._longitude)

            data.append(self._symbol_code)


        *if* record.get(*'windDir'*) *is* *not* None:

           * # Wind direction (in degrees)*

           * # Wind from North needs to be reported as 360.*

           * # Wind from 0 means N/A in the APRS standard.*

           * # We need to make sure it does not get to 0, so do not rely on*

           * # the format string rouding, but round to no decimals before*

           * # comparing with 0*

            wind_dir = int(round(record[*'windDir'*], 0))

            *if* wind_dir <= 0:

                wind_dir = 360

            data.append(*'%s%03u'* % (self._wind_direction_marker,

                                    wind_dir))

        *else*:

            data.append(*'%s...'* % self._wind_direction_marker)


        *if* record.get(*'windSpeed'*) *is* *not* None:

           * # Sustained one-minute wind speed (in mph)*

            data.append(*'%s%03.f'* % (self._wind_speed_marker,

                                     record[*'windSpeed'*]))

        *else*:

            data.append(*'%s...'* % self._wind_speed_marker)


        *if* record.get(*'windGust'*) *is* *not* None:

           * # Gust (peak wind speed in mph in the last 5 minutes)*

            data.append(*'g%03.f'* % record[*'windGust'*])

        *else*:

            data.append(*'g...'*)


        *if* record.get(*'outTemp'*) *is* *not* None:

           * # Temperature (in degrees Fahrenheit)*

            data.append(*'t%03.f'* % record[*'outTemp'*])

        *else*:

            data.append(*'t...'*)


        *if* record.get(*'rainRate'*) *is* *not* None:

           * # Rainfall (in hundredths of an inch) in the last hour*

            data.append(*'r%03.f'* % (record[*'rainRate'*] * 100))


        *if* record.get(*'rain'*) *is* *not* None:

           * # Rainfall (in hundredths of an inch) in the last 24hrs.*

            data.append(*'p%03.f'* % (record[*'rain'*] * 100))


#        *if* record.get(*'MISSSING INFO'*) *is* *not* None:

#           * # Rainfall (in hundredths of an inch) since midnight*

#            data.append(*'P%03.f'* % (record[*'MISSING INFO'*] * 100))


        *if* record.get(*'outHumidity'*) *is* *not* None:

           * # Humidity (in %. 00 = 100%)*

           * # We need to make sure it does not get over 99, so do not rely 
on*

           * # the format string rouding, but round to no decimals before*

           * # comparing with 100*

            humidity = int(round(record[*'outHumidity'*], 0))

            *if* humidity >= 100:

                humidity = 0

            data.append(*'h%02u'* % humidity)


        *if* record.get(*'barometer'*) *is* *not* None:

           * # Barometric pressure (in tenths of millibars/tenths of 
hPascal)*

            barometer = weewx.units.convert(

                (record[*'barometer'*], *'inHg'*, *'group_pressure'*),

                *'mbar'*)[0] * 10

            data.append(*'b%05.f'* % barometer)


        *if* self._comment:

            data.append(self._comment)


        wxdata = ''.join(data)


       * # Atomic update of self._output_filename.*

        *with* open(self._output_filename_tmp, *'w'*) *as* f:

            f.write(wxdata)

        os.rename(self._output_filename_tmp, self._output_filename)



On Friday, May 18, 2018 at 8:40:11 AM UTC-4, Andrew Milner wrote:
>
> where is this code from? when is it being run?  
>
> Originally you were trying to amend xstats - which effectively makes extra 
> tags become available to the cheetah template engine
>
> I gave a method of generating the required data without needing additional 
> 24 hour tags, by using existing tags available to the cheetah template 
> engine.
>
> You have now come up with some code which appears to be processing records 
> obtained from somewhere other than directly from the weewx database and 
> appending the output somewhere else.
>
> Being in the dark somewhat my answer is "I do not know"
>
> Sorry - cannot help more - I do not know where the code comes from, when 
> it is run or how it is used, so cannot answer.  Sorry
>
>
>
>
>
> On Friday, 18 May 2018 14:54:36 UTC+3, Lorin Tremblay wrote:
>>
>> So not being a coder or someone that know how to code....
>> How do you add it to this where the question mark are?
>>
>>         *if* record.get(*'rainRate'*) *is* *not* None:
>>
>>            * # Rainfall (in hundredths of an inch) in the last hour*
>>
>>             data.append(*'r%03.f'* % (record[*'rainRate'*] * 100))
>>
>>
>> #       *if* record.get(*'?????'*) *is* *not* None:
>>
>> #          * # Rainfall (in hundredths of an inch) last 24hrs*
>>
>> #            data.append(*'p%03.f'* % (record[*'?????'*] * 100))
>>
>>
>>         *if* record.get(*'rain'*) *is* *not* None:
>>
>>            * # Rainfall (in hundredths of an inch) since midnight*
>>
>>             data.append(*'P%03.f'* % (record[*'rain'*] * 100))
>>
>>
>> Thanks for your help andrew!
>>
>>
>>
>> On Thursday, May 17, 2018 at 9:50:38 PM UTC-4, Andrew Milner wrote:
>>>
>>> you could just use span with a day delta:
>>>
>>> eg
>>> $span($day_delta=1).rain.sum for the rain total for previous 24 hours 
>>> according to the customisation guide
>>> or
>>> $span($day_delta=1).outTemp.avg for the average temperature during 
>>> previous 24 hours
>>>
>>>
>>>
>>> On Friday, 18 May 2018 01:09:48 UTC+3, Lorin Tremblay wrote:
>>>>
>>>> Hi!
>>>>
>>>> Trying to modify the xstats to add the last 24hrs to stats.
>>>>
>>>> here is the modification that I have made, but obviously not being a 
>>>> coder there is one or more mistake, can anyone help me sort it out?
>>>>
>>>> "*#*
>>>>
>>>> *#    Copyright (c) 2009-2015 Tom Keffer *
>>>>
>>>> *#*
>>>>
>>>> *#    See the file LICENSE.txt for your full rights.*
>>>>
>>>> *#*
>>>>
>>>> *"""Extended stats based on the xsearch example*
>>>>
>>>>
>>>> *This search list extension offers extra tags:*
>>>>
>>>>
>>>> *  'alltime':    All time statistics.*
>>>>
>>>>
>>>> *  '24_hrs':  Statistics for the last 24 hours.* 
>>>>
>>>>
>>>> *  'seven_day':  Statistics for the last seven days.* 
>>>>
>>>>
>>>> *  'thirty_day': Statistics for the last thirty days.*
>>>>
>>>>
>>>> *You can then use tags such as $alltime.outTemp.max for the all-time 
>>>> max*
>>>>
>>>> *temperature, or $seven_day.rain.sum for the total rainfall in the last 
>>>> seven*
>>>>
>>>> *days, or $thirty_day.wind.max for maximum wind speed in the past 
>>>> thirty days.*
>>>>
>>>> *"""*
>>>>
>>>> *import* datetime
>>>>
>>>> *import* time
>>>>
>>>>
>>>> *from* weewx.cheetahgenerator *import* SearchList
>>>>
>>>> *from* weewx.tags *import* TimespanBinder
>>>>
>>>> *from* weeutil.weeutil *import* TimeSpan
>>>>
>>>>
>>>> *class* ExtendedStatistics(SearchList):
>>>>
>>>>     
>>>>
>>>>     *def** __init__*(self, generator):
>>>>
>>>>         SearchList.__init__(self, generator)
>>>>
>>>>   
>>>>
>>>>     *def** get_extension_list*(self, timespan, db_lookup):
>>>>
>>>>         *"""Returns a search list extension with additions.*
>>>>
>>>>
>>>> *        timespan: An instance of weeutil.weeutil.TimeSpan. This holds*
>>>>
>>>> *                  the start and stop times of the domain of valid 
>>>> times.*
>>>>
>>>>
>>>> *        db_lookup: Function that returns a database manager given a*
>>>>
>>>> *                   data binding.*
>>>>
>>>> *        """*
>>>>
>>>>
>>>>        * # First, create a 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)
>>>>
>>>>         
>>>>
>>>>        * # Now use a similar process to get statistics for the last 24 
>>>> hours.*
>>>>
>>>>         hour_dt = datetime.date.fromtimestamp(timespan.stop) - 
>>>> datetime.timedelta(hour=24)
>>>>
>>>>         hour_ts = time.mktime(hour_dt.timetuple())
>>>>
>>>>         24_hours_stats = TimespanBinder(TimeSpan(hour_ts, 
>>>> timespan.stop),
>>>>
>>>>                                           db_lookup,
>>>>
>>>>                                           context=*'24_hrs'*,
>>>>
>>>>                                           
>>>> formatter=self.generator.formatter,
>>>>
>>>>                                           
>>>> converter=self.generator.converter)
>>>>
>>>>
>>>>        * # Now create a TimespanBinder 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)
>>>>
>>>>         week_ts = time.mktime(week_dt.timetuple())
>>>>
>>>>         seven_day_stats = TimespanBinder(TimeSpan(week_ts, 
>>>> timespan.stop),
>>>>
>>>>                                          db_lookup,
>>>>
>>>>                                          context=*'seven_day'*,
>>>>
>>>>                                          
>>>> formatter=self.generator.formatter,
>>>>
>>>>                                          
>>>> converter=self.generator.converter)
>>>>
>>>>
>>>>        * # Now use a similar process to get statistics for the last 30 
>>>> days.*
>>>>
>>>>         days_dt = datetime.date.fromtimestamp(timespan.stop) - 
>>>> datetime.timedelta(days=30)
>>>>
>>>>         days_ts = time.mktime(days_dt.timetuple())
>>>>
>>>>         thirty_day_stats = TimespanBinder(TimeSpan(days_ts, 
>>>> timespan.stop),
>>>>
>>>>                                           db_lookup,
>>>>
>>>>                                           context=*'thirty_day'*,
>>>>
>>>>                                           
>>>> formatter=self.generator.formatter,
>>>>
>>>>                                           
>>>> converter=self.generator.converter)
>>>>
>>>>
>>>>         *return* [{*'alltime'*: all_stats,
>>>>
>>>>                  *'24_hrs'*: 24_hours_stats,
>>>>
>>>>                  *'seven_day'*: seven_day_stats,
>>>>
>>>>                  *'thirty_day'*: thirty_day_stats}]
>>>>
>>>>
>>>> *# For backwards compatibility:*
>>>>
>>>> ExtStats = ExtendedStatistics
>>>> "
>>>>
>>>>

-- 
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].
For more options, visit https://groups.google.com/d/optout.

Reply via email to