Any way to avoid these errors?

Anyone got any ideas how to supress/fix these errors in my HA logs?

They appear randomly and the template sensors do work. Baffling.

Template variable error: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'media_title' when rendering

Here’s the template sensor :

{% if states("media_player.ituner_320bt")|default('') != 'off' %}
              {% if states("media_player.ituner_320bt")|default('not playing') == 'playing' %}
                {% if state_attr('media_player.ituner_320bt', 'media_artist')|default('not playing') == none %}
                    {% if states.media_player.ituner_320bt.attributes.media_title.split(' - ')[0]|default('not playing') != 'Radio Caprice' and states.media_player.ituner_320bt.attributes.media_title.split(' - ')[0]|default('not playing') != 'RADIO BOB!' %}
                      {{ states.media_player.ituner_320bt.attributes.media_title.split(' - ')[2]|default('not playing') }}
                    {%else%}
                      {{ states.media_player.ituner_320bt.attributes.media_title.split(' - ')[3]|default('not playing') }}
                    {%endif%}
                {%else%}
                   {{ states.media_player.ituner_320bt.attributes.media_title|default('not playing') }}
                {%endif%}
              {%else%}
                not playing
              {%endif%}
          {%endif%}

I would guess if this is not there, then it errors…
So add a test if it’s is there before you do the rest of the template, and it it’s not, do something else or supply a default.

Thanks, what’s the best way to check if it’s there or not?

I have tried (on another media player) and get the same error :

{% if state_attr('media_player.volumio','media_title') != none %}
  then check everything.....
{% if 'media_title' in states['media_player.volumio']['attributes'] %}
...
{% endif %}

or

{% if state_attr('media_player.volumio', 'media_title') %}
...
{% endif %}

This you did the right way…

But this not so much. So, I guess you know the answer already.

You cheated! Editing the answer in above my post so it seems you were first :joy:

I think I would set it as a variable in the beginning of the jinja. That makes your code MUCH cleaner anyway. And if it doesn’t exist, add a string that will work but tell you the info is not there.
I’m horrible at writing this stuff for others, but there are jinja things to check if things exist.
I wrote this a while back and it would work, but there are easier ways. You could look at what this is doing and try that.

1 Like

I think I actually found a “bug”.

If you have a dashboard open with mushroom template cards on it, if you restart HA from another window, as it redraws the dashboard on the other tab - it fills the logs with errors that don’t actually exist.

Example :

Error while processing template: Template<template=({{states('sensor.office_occupancy')) renders=2>

As you can see that’s incomplete - it throws loads of errors as it goes through the line character by character. Not sure if it’s a HA error or Mushroom. I’ll log against Mushroom first.