Problem with namespace in template

I’m having real difficulty using namespaces in templates. I’m running the latest HomeAssistant (61.0) and Jinja2 2.10 so I think it should work. Whilst I can set the values, I can’t work out how to retrieve them. I’ve checked the Jinja2 documentation and can’t see what I’m doing wrong.

{%- set ns = namespace(mystate=1) -%}
{{ ns }}

gives

<Namespace {'mystate': 1}>

However trying to use or get the value of mystate doesn’t work, e.g.

{%- set ns = namespace(mystate=1) -%}
{{ ns.mystate }}

doesn’t give anything. Nor does any other attempt I’ve made to get the value of mystate. Anybody know how to get this working? Or does it work elsewhere and it’s just my setup is broken?

That name space appears to be a dictionary. To accessing items would work like a dictionary.

{{ ns[‘mystate’] }}

You are treating it like an object. I could be wrong as I’ve never used this namespace thing, but just give it a whirl.

Eh, I just found namespace in the jinja documentation and it doesn’t appear to work in hass. The method I posted will not work. I copied the example from the api documentation into the templates page and that did not return any value when accessing namespace.

Yeah, I’d tried a few different ways. Thanks for looking though - at least I now know that it’s not just me!

yeah it seems like an odd function… I just switched to appdaemon to avoid jinja. Jinja is too limited.

1 Like

I’ve changed my code to avoid the problem. Always good when that’s an option. I’m going to have a look at appdaemon too. Looks pretty nice, including the dashboard. Thanks for the pointer.