PiotrMachowski / Home-Assistant-Lovelace-HTML-Jinja2-Template-card

This card displays provided Jinja2 template as an HTML content of a card. It uses exactly the same engine as Home Assistant in Developer tools.
MIT License
68 stars 10 forks source link

Preventing rendering until HA has started. #11

Closed genem2 closed 3 years ago

genem2 commented 3 years ago

Not an issue - just seeking guidance:

With 2021.4, and it popping Warnings for undefined variables in Templates, any ideas how we can keep this card from rendering until HA has completed startup?

I tried wrapping my template into an if, but that didn't work:

{% if (as_timestamp(now()) - as_timestamp(states('sensor.uptime'))) |int  < 30 %}
  Waiting to complete start-up ...
{% else %}
  Over 30 sec, assume it's now up.
  Render my template...
{% endif %}

The template code within the else is still being examined before HA completes starting, so the full template is dumped into the log file thanks to this new warning feature.

If I remember to close the browser - to prevent Lovelace from trying to show the card - the log is clear.

genem2 commented 3 years ago

I found this workaround. Maybe this will help someone in the future.

In configuration.yaml, I disable the template warning/error by setting the log level to fatal.

logger:        # Supports Levels: critical, fatal, error, warning, warn, info, debug, notset
  logs:
    homeassistant.helpers.template: fatal

Then, in a HA startup script, I reset it back to error

  - service: logger.set_level
    data:
      homeassistant.helpers.template: error