How to format calculated values in Cheetah

91 views
Skip to first unread message

Karen K

unread,
Jun 26, 2022, 10:51:28 AM6/26/22
to weewx-user
You can calculate values using the #set directive in Cheetah. I wondered how to format those values for output, and after some testing I found the following solution. As an example the daylight duration is calculated from sunrise and sunset times:

#from weewx.units import ValueTuple,ValueHelper 
#set $rising=$almanac.sun.rise.raw
#set $setting=$almanac.sun.set.raw
#set $daylight=$setting-$rising
#set $daylight_vh=ValueHelper(ValueTuple($daylight,'second','group_deltatime'),formatter=$station.formatter) 

$daylight_vh.format("%(hour)d%(hour_label) %(minute)d%(minute_label)s %(second)d%(second_label)s","").

Is there a better solution?

In skins I see this calculated "by hand" in a series of #set statements.

Is it explained somewhere in the manuals?

gjr80

unread,
Jun 29, 2022, 12:25:32 AM6/29/22
to weewx-user
The other alternative is to use a SLE, same methodology (ie do the calculations and return a ValueHelper) applies but the code will be longer though arguably easier to read/maintain. For one-off jobs the in-line template will probably be the easiest solution. For large templates or repeated use the SLE starts to come into it's own.

I'm not aware of anywhere this is documented as such.

Gary

Karen K

unread,
Jun 29, 2022, 12:36:13 PM6/29/22
to weewx-user
Reply all
Reply to author
Forward
0 new messages