Then you would have to write an xtypes extension
<https://github.com/weewx/weewx/wiki/WeeWX-V4-user-defined-types>.

It would look something like this (NOT TESTED):

import weedb
import weewx.xtypes

class LastNonZero(weewx.xtypes.XType):

    def get_aggregate(self, obs_type, timespan, aggregate_type, db_manager,
**option_dict):
        if aggregate_type != 'lastnonzero':
            raise weewx.UnknownAggregation(aggregate_type)

        interpolate_dict = {
            'aggregate_type': aggregate_type,
            'obs_type': obs_type,
            'table_name': db_manager.table_name,
            'start': timespan.start,
            'stop': timespan.stop
        }

        select_stmt = "SELECT %(obs_type)s FROM %(table_name)s " \
                      "WHERE dateTime > %(start)s AND dateTime <= %(stop)s
" \
                      "AND %(obs_type)s IS NOT NULL " \
                      "AND %(obs_type)s != 0 " \
                      "ORDER BY dateTime DESC LIMIT 1" % interpolate_dict

        try:
            row = db_manager.getSql(select_stmt)
        except weedb.NoColumnError:
            raise weewx.UnknownType(obs_type)

        value = row[0] if row else None

        u, g = weewx.units.getStandardUnitType(db_manager.std_unit_system,
obs_type,
                                               aggregate_type)
        return weewx.units.ValueTuple(value, u, g)

Then you could use it:

<p>This month's last non-null and non-zero temperature is
$month.outTemp.lastnonzero</p>



On Tue, Nov 17, 2020 at 6:46 AM steeple ian <[email protected]> wrote:

> Thanks Tom,
>
> It worked fine, I did actually try that and it returned a zero (not
> realising I had a zero in the database) and thought it had not worked
> correctly. So if I want to eliminate the zeros as well?
>
> On Tue, 17 Nov 2020 at 12:38, Tom Keffer <[email protected]> wrote:
>
>> The aggregation type 'last' should do what you want:
>>
>> <p>The last non-null temperature this month is: $month.outTemp.last</p>
>>
>>
>> On Tue, Nov 17, 2020 at 4:25 AM [email protected] <[email protected]>
>> wrote:
>>
>>> Hi,
>>>
>>> Trying to find a 'WeeWX way' to find the last non-NULL record from the
>>> database using a .tmpl file.
>>>
>>> Nothing obvious jumps out for me from the documentation. Does anybody
>>> have any ideas?
>>>
>>> Thanks,
>>> Ian
>>>
>>> --
>>> 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 on the web visit
>>> https://groups.google.com/d/msgid/weewx-user/6f4aed6f-824c-4ea3-8114-0c979e2e263cn%40googlegroups.com
>>> <https://groups.google.com/d/msgid/weewx-user/6f4aed6f-824c-4ea3-8114-0c979e2e263cn%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
>> 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 on the web visit
>> https://groups.google.com/d/msgid/weewx-user/CAPq0zEDUB8krNBpD88f%3Do2ARnr5O_0%3DKd5mhvWRRMG%3DN5dotzw%40mail.gmail.com
>> <https://groups.google.com/d/msgid/weewx-user/CAPq0zEDUB8krNBpD88f%3Do2ARnr5O_0%3DKd5mhvWRRMG%3DN5dotzw%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> 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 on the web visit
> https://groups.google.com/d/msgid/weewx-user/CADASSaQHNqwQ8RumEkeBOpSh7bEumo%3DG-hC3XuyiiHMa9tkdvg%40mail.gmail.com
> <https://groups.google.com/d/msgid/weewx-user/CADASSaQHNqwQ8RumEkeBOpSh7bEumo%3DG-hC3XuyiiHMa9tkdvg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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 on the web visit 
https://groups.google.com/d/msgid/weewx-user/CAPq0zECTU7Nn6g3c%2BdiQFQyKpum0gKT7NEaV3ybpeMW11zfL9Q%40mail.gmail.com.

Reply via email to