How to Determine 'Tomorrow'

178 views
Skip to first unread message

Rory Gillies

unread,
Nov 6, 2024, 3:36:58 PM11/6/24
to weewx-user
Hi All, possibly a simple answer that I'm not seeing, but how can I report an event that is happening tomorrow? I have tide tables as part of my almanac page on my weather station website: https://www.360shetland.co.uk/weather/almanac.html

I would like to add 'tomorrow' when the next reported tidal event is after midnight. For example, in the attached screenshot I want it to read "The next HIGH tide is tomorrow at 01:58"

The code I use is:
<div class="card">
    <div class="card-body text-center">
     <h2 class="h2-responsive $Extras.color-text"><i class="wi wi-time-3" ></i> <dfn data-info="Tide times and heights are calculated every 28 day>
     <h6 class="h6-responsive $Extras.color-text">Tides for the next seven days</h6></br>
     <p>Times of high and low waters are given in local time and heights in metres above Chart Datum</p>
    #set $tides = $forecast.xtides
    #set $counter = 0
    #for $tide in $tides
    #set $counter = $counter + 1
    #if $counter == 1 and $tide.hilo == 'L'
    <h2 class="h5-responsive">The tide in Lerwick is currently</h2>
    <h2 class="h5-responsive" style="color: #1fb51f">FALLING</h2>
    <h2 class="h5-responsive">The next <span style="color: #1fb51f">LOW</span> tide is at $tide.event_ts.format("%H:%M")</h2>
    #else
    <h2 class="h5-responsive">The tide in Lerwick is currently</h2>
    <h2 class="h5-responsive" style="color: red">RISING</h2>
    <h2 class="h5-responsive">The next <span style="color: red">HIGH</span> tide is at $tide.event_ts.format("%H:%M")
    #end if
    #if $counter == 1
    #break
    #end if                  
    #end for
    </div>
</div>


Screenshot 2024-11-06 at 20.27.51.png
Any ideas?

Thanks,

Rory

Tom Keffer

unread,
Nov 6, 2024, 5:46:10 PM11/6/24
to weewx...@googlegroups.com
I can't think of any cool trick using the existing set of tags, so you would have to define a new search list extension. Call it $midnight. 

It would look something like this (NOT TESTED):

from weewx.cheetahgenerator import SearchList
from datetime import datetime, timedelta

class Midnight(SearchList):
    def midnight(self, epoch_time, direction=+1):
        """
        Determine the time at midnight
        Args:
            epoch_time (float|int): The reference time
            direction (int): The direction to search for midnight.
                For example, +1 would mean midnight tonight, +2 midnight tomorrow night, etc.

        Returns:
            float: The time at midnight in Unix epoch time
        """

        # Convert epoch to datetime
        time_dt = datetime.fromtimestamp(epoch_time)

        # Calculate midnight
        target_midnight = time_dt.replace(hour=0, minute=0, second=0, microsecond=0) + timedelta(days=direction)

        # Convert back to epoch time
        midnight_epoch = target_midnight.timestamp()


Be sure to add it to the option search_list_extensions 

To use:

#if $tide.event_ts.raw > $midnight($tide.event_ts.raw)
#set day_when="tomorrow"
#else
#set day_when="today"
#end if
<p>The next <span style="color: red">HIGH</span> tide is at $tide.event_ts.format("%H:%M") $day_when</p>

I'm sure I made some errors in there, but hopefully you get the idea.

Be sure to see the documentation on extending search lists.

-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 visit https://groups.google.com/d/msgid/weewx-user/e37f70af-d9ad-4d87-9d92-185d360e1d74n%40googlegroups.com.

Karen K

unread,
Nov 7, 2024, 1:40:50 AM11/7/24
to weewx-user
I could think about this solution within the template (not tested):

#import weeutil.weeutil

#set $endofday = $weeutil.weeutil.archiveDaySpan($current.dateTime.raw)[1]
#if $tide.event_ts.raw>$endofday
tomorrow
#else
today
#end if


Rory Gillies

unread,
Nov 7, 2024, 8:01:59 AM11/7/24
to weewx-user
Thanks both, I went down Karen K's suggestion in the template and it's working treat, thanks!

Slight modification, I set a $tideday string to 'today' then added a simple conditional if statement: 

#if $tide.event_ts.raw>$endofday
#set $tideday = 'tomorrow'
#end if


Screenshot 2024-11-07 at 12.56.28.png

Thanks again!

Rory

Tom Keffer

unread,
Nov 8, 2024, 8:14:30 AM11/8/24
to weewx...@googlegroups.com
Glad this worked out for you, but in general, one should avoid direct references to the internal WeeWX API. It leads to tight coupling between presentation and implementation, which can be brittle. 

For example, the function archiveDaySpan() used to have a parameter "grace", which was removed in 2022.

Better to write a search list extension, whose interface has not changed in over 10 years.

-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.

Karen K

unread,
Nov 8, 2024, 9:28:55 AM11/8/24
to weewx-user
Tom Keffer schrieb am Freitag, 8. November 2024 um 14:14:30 UTC+1:
For example, the function archiveDaySpan() used to have a parameter "grace", which was removed in 2022.

Better to write a search list extension, whose interface has not changed in over 10 years.

@Tom: One question: If I write a search list extension (what indeed I already did) and I do not want to re-invent core WeeWX functions, I have to refer to those functions there, too. The same applies to XType extensions. The only difference may be that WeeWX extensions require some kind of maintenance anyway.

Tom Keffer

unread,
Nov 8, 2024, 9:42:15 AM11/8/24
to weewx...@googlegroups.com
True, but there is a degree of separation: you might have to change the extension to follow changes in the code base, but the odds are that any clients who make use of your extension will be unaffected.

That's the objective of reducing coupling between interface and implementation.

-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.

Karen K

unread,
Nov 9, 2024, 3:29:26 AM11/9/24
to weewx-user
@Tom: Now I finally found a way to do it using present tags only:

#if $tide.event_ts.raw>$day.end.raw
tomorrow
#else
today
#end if

I guess, that would meet your principles. And additionally it is short and simple. 

Tom Keffer

unread,
Nov 9, 2024, 8:21:18 AM11/9/24
to weewx...@googlegroups.com
Very clever!

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