I can’t work out why my macro won’t work from custom_templates
but is fine in dev tools.
And for completeness…
I can’t work out why my macro won’t work from custom_templates
but is fine in dev tools.
And for completeness…
Please format the YAML in posts as described here. Please post the YAML for your template sensor.
Thanks for your help and the slightly patronising quote of the pinned post which I saw when it was first posted
The yaml is provided formatted albeit not in an editable way (I only posted it for completeness).
But even if it was it will useless to anyone else unless they just so happen to have the exact same sensor set up as I do.
And in any case this isn’t a question about my yaml. That works just fine as I have shown in the dev template screenshots and I have a ton of other macros that work from custom_templates
so I know what I’m doing.
Except there is something here that I am obviously not seeing in this example.
you’ve been here long enough to post formatted code…
Anyways, I’d wager it’s because you’re using # comments in a jinja file that does not support # comments.
Outside that, your macro can be greatly simplified, you’re using all old practices and you aren’t removing whitespace which will cause problems if you try to cast the output as a number.
{%- macro smartweather_light_level(number) %}
{%- set obs = state_attr('sensor.smartweather_' ~ number, 'obs') %}
{%- if obs is list and obs | length > 0 and obs[0].brightness is defined %}
{{- obs[0].brightness }}
{%- else -%}unknown{%- endif -%}
{%- endmacro %}
i was planning to investigate, but without the code to copy/paste I’d have to have typed it all in manually which I’m not prepared to do so I gave up on trying to help you.
There is a reason for the guidelines…
Seconded, I was feeling nice rewriting it.
Thirded, I wanted to help and also learn about macros because I don’t use them. So having an example to work through would have given me that knowledge. I also to dev work so I can run it in the debugger if I can’t figure out why it doesn’t work.
Well outside that the screenshot also shows there may be something below the macro, which would cause the macro file to produce an error. Assuming that’s not the case, then the issue is the unsupported comments.
Only way to test that would be to just try the import in the template editor with nothing else.
OK…
I am not sure it is a template editor issue as I only spotted the problem because my actual HA config caused the same error.
And I will back down and apologise on the formatted yaml front.
Here it is (inluding what follows it for petro)
#=====================
#=== Get Sensor value
#=====================
{% macro smartweather_light_level(number) %}
{% set obs = state_attr('sensor.smartweather_' ~ number, 'obs') %}
{% if obs is sequence and
obs is iterable and
obs[0].brightness is defined %}
{{ obs[0].brightness }}
{% else %}
unknown
{% endif %}
{% endmacro %}
#============================
#=== Get Sensor availability
#============================
{% macro smartweather_availability(number) %}
{% set obs = state_attr('sensor.smartweather_' ~ number 'obs') %}
{{
states('sensor.smartweather_' ~ number) not in ['unknown','unavailable'] and
obs is sequence and
obs is iterable and
obs[0].brightness is defined
}}
{% endmacro %}
Also, today I was just rationalising some very old code. Goiing through and ‘modernising’ will come after.
there’s your issue. missing comma like the error says
I had no idea a separate macro could cause an error so wasn’t even looking that far.
hey ho.
Thanks.
And apologies again to everyone else
Heh, and that error wasn’t in the screenshot version of the macro…
And it wouldn’t have been in the formatted code had I posted it in the OP because:
So you could argue this had a better chance of being solved as quickly as it did because I didn’t format the code.
Don’t understand your point. Your final screenshot appeared to be the macro file but was not missing the comma.
That’s exactly my point.
The macro in the screenshot was the macro that was failing even though there was nothing wrong with it.
It was the second unrelated macro in the same file that had the missing comma.
But never mind, it’s all good now. Petro did what Petro does. But I think it is the first time I’ve seen him spot the problem when the problem wasn’t even visible.
That’s all from me on this.
I learnt something and I got my problem fixed.