#Jinja macro catch error

1 messages · Page 1 of 1 (latest)

wintry iron
#

I have a Jinja macro that in some cases casts an type error when calculating a timestamp - can I catch these somehow (like a try catch)?

light cove
#

AFAIK there's no way to do that in jinja - do you know why it's throwing the errors so can test for those inputs before they cause an issue?

wintry iron
#

Yes I do - currently trying to see what I can do about that - but for some reason my if sentences for the result from the macro doesn't really work :/ Just tried (just for the sake of test) to return 0 when the conditions isn't met, but

{% set cheapest = FindCheapestPeriod(earliestStartTime , (dtime-periodLength) , periodLength, false) %}
{% if cheapest == "0" %}
{{ oldStart }}
{% else %}
{{ cheapest }}
{% endif %}

still returns cheapest and not oldStart

light cove
#

Try removing the " around the zero

wintry iron
#

Same result

light cove
#

That's checking for = a string 0 not the number 0

#

Hmmm

#

Is this @pure karma's thing? He probably knows how to fix it

#

passes the buck

pure karma
#

Don't know what FindCheapestPeriod is. Mine is cheapest_energy_hours

#

but you probably have a whitespace issue

#
{% set cheapest = FindCheapestPeriod(earliestStartTime , (dtime-periodLength) , periodLength, false) %}
**{{ cheapest }}**
#

try that in devtools > templates, I expect you will have whitespace around the result

#

you need to add a dash to every time you use the curly brackets in your templates

#

so {%- -%} and {{- -}}

#

or trim the result

#
{% set cheapest = FindCheapestPeriod(earliestStartTime , (dtime-periodLength) , periodLength, false) %}
**{{ cheapest | trim }}**
humble linden