How to specify a binding in $getVar()?

126 views
Skip to first unread message

Karen K

unread,
Nov 23, 2022, 2:06:03 PM11/23/22
to weewx-user
I tried $getVar('day(data_binding="my_binding").myobs).max. This results in the error message ERROR weewx.cheetahgenerator: **** Reason: cannot find 'day(data_binding="my_binding")'. I guess that is not the right way to do it. So how do I specify a binding if using $getVar()?

Tom Keffer

unread,
Nov 23, 2022, 4:40:39 PM11/23/22
to weewx...@googlegroups.com
$getVar is pretty dumb. It takes whatever expression you give it, picks out the part before the first period, then searches for that in the search list. So, if you give it 'day.myobs' it searches for 'day' and finds it in the search list. The engine then goes on to evaluate the results for an attribute "myobs". It all works.

But, if you give it "day(data_binding='my_binding').myobs", then it will literally search for "day(data_binding='my_binding')" in the search list and, of course, not find it.

It's not clear what you're trying to do here, but I'll assume you have a string that you assembled somehow that looks like "day(data_binding='my_binding').myobs" and now you want to evaluate it. If that's the case, you can't. Instead, you'll have to break it apart and use getattr() (NOT TESTED):

getattr("day(data_binding='my_binding')", "myobs").max

If the aggregation is also in the form of a string, then you'll have to break that out as well:

getattr(getattr("day(data_binding='my_binding')", "myobs"), "max")

Hope that helps.

-tk



On Wed, Nov 23, 2022 at 11:06 AM Karen K <kk44...@gmail.com> wrote:
I tried $getVar('day(data_binding="my_binding").myobs).max. This results in the error message ERROR weewx.cheetahgenerator: **** Reason: cannot find 'day(data_binding="my_binding")'. I guess that is not the right way to do it. So how do I specify a binding if using $getVar()?

--
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/8553aeba-a233-4eca-9c7c-abd9b87d489en%40googlegroups.com.

Karen K

unread,
Nov 24, 2022, 12:57:45 AM11/24/22
to weewx-user
Yes, of course, your assumption is right. I looked into hilo.inc of the Seasons skin and tried to do the same for "my_binding".

tke...@gmail.com schrieb am Mittwoch, 23. November 2022 um 22:40:39 UTC+1:
Instead, you'll have to break it apart and use getattr() (NOT TESTED):

getattr("day(data_binding='my_binding')", "myobs").max

I tested it with the string 

<p>$getattr("day(data_binding='snmp_binding')","upsOutputVoltage").max</p>

Unfortunately it did not work. The error messages were:

Nov 24 06:40:54 LokalWiki weewx[111567] ERROR weewx.cheetahgenerator: Evaluation of template /etc/weewx/skins/airQ/IT.html.tmpl failed with exception '<class 'AttributeError'>'
Nov 24 06:40:54 LokalWiki weewx[111567] ERROR weewx.cheetahgenerator: **** Ignoring template /etc/weewx/skins/airQ/IT.html.tmpl
Nov 24 06:40:54 LokalWiki weewx[111567] ERROR weewx.cheetahgenerator: **** Reason: 'str' object has no attribute 'upsOutputVoltage'
Nov 24 06:40:54 LokalWiki weewx[111567] ERROR weewx.cheetahgenerator: **** Traceback (most recent call last):
Nov 24 06:40:54 LokalWiki weewx[111567] ERROR weewx.cheetahgenerator: **** File "/usr/share/weewx/weewx/cheetahgenerator.py", line 348, in generate
Nov 24 06:40:54 LokalWiki weewx[111567] ERROR weewx.cheetahgenerator: **** unicode_string = compiled_template.respond()
Nov 24 06:40:54 LokalWiki weewx[111567] ERROR weewx.cheetahgenerator: **** File "cheetah__etc_weewx_skins_airQ_IT_html_tmpl_1669268454_729354_33635.py", line 550, in respond
Nov 24 06:40:54 LokalWiki weewx[111567] ERROR weewx.cheetahgenerator: **** AttributeError: 'str' object has no attribute 'upsOutputVoltage'

I checked against 

<p>$day(data_binding='snmp_binding').upsOutputVoltage.max</p>

and that is compiled without errors.

The method, the old Seasons skin of WeeWX 4.5 and before did it, works, too.

I hope you have some idea.

bell...@gmail.com

unread,
Nov 24, 2022, 5:36:18 PM11/24/22
to weewx-user
When I have any of the WeeWX tags ($day, outTemp, etc.) in a variable, I do something like this.

#set $time_period = "$day"
#set $observation = "outTemp"
#set $source = "<p>" + $time_period + "(data_binding='wx_binding')." + $observation + ".max</p>"
#include source = $source

Note, it can make debugging templates even more challenging…
rich

Karen K

unread,
Nov 25, 2022, 12:54:16 PM11/25/22
to weewx-user
What I found out to work is that:

#set $timespan=$day(data_binding='my_binding')
#set $obstype='myobs'
$getattr($timespan,$obstype).max

To use that in a setting like in hilo.inc:

#for $timespan in ($day(data_binding='my_binding'),$week(data_binding='my_binding'),...)
<p>$getattr($timespan,'myobs').max</p>
#end for

If the time context is required, too, one can do it like in the old Seasons skin:

#for $timespan,$timespanname in (($day(data_binding='my_binding'),'day'),($week(data_binding='my_binding'),'week'),...)
<p class="hilo_$timespanname">$getattr($timespan,'myobs').max</p>
#end for

I hope that summary can inspire people who face similar problems.

Karen K

unread,
Nov 26, 2022, 3:24:07 AM11/26/22
to weewx-user
There is an even simpler approach, as the timespan name is available in the context attribute:

#for $timespan in ($day(data_binding='my_binding'),$week(data_binding='my_binding'),...)
<p class="hilo_$timespan.context">$getattr($timespan,'myobs').max</p>
#end for

Reply all
Reply to author
Forward
0 new messages