Could not render template


#1

After updating from 0.84.6 to 0.85.1 I got a problems with a template sensor. It worked well with HA version 0.84.6, but after update to 0.85.1 the sensor is ‘unknown’ and I get the error message

2019-01-14 20:41:54 WARNING (MainThread) [homeassistant.components.sensor.template] Could not render template Alarm tilkoblet, the state is unknown.

In 0.84.6, my sensor was defined as this:

- platform: template
  sensors:
    alarm_tilkoblet:
      friendly_name: Alarm tilkoblet
      value_template: >
        {% if 'Systemet MYSYSTEM ble tilkoblet' in states.sensor.verisure_email.attributes.body %}
          {{ True }}
        {% elif 'Systemet MYSYSTEM er frakoblet' in states.sensor.verisure_email.attributes.body %}
          {{ False }}
        {% endif %}

I thought maybe it was a startup thing, so I enclosed the template as follows:

- platform: template
  sensors:
    alarm_tilkoblet:
      friendly_name: Alarm tilkoblet
      value_template: >
        {% if states.sensor.verisure_email.attributes.body is defined %}
          {% if 'Systemet MYSYSTEM ble tilkoblet' in states.sensor.verisure_email.attributes.body %}
            {{ True }}
          {% elif 'Systemet MYSYSTEM er frakoblet' in states.sensor.verisure_email.attributes.body %}
            {{ False }}
          {% endif %}
        {% else %}
          {{ False }}
        {% endif %}

The result is, however, the same.

Any pointers would be welcome.


#2

Ah, never mind. It’s the Slugify thing.

sensor.verisure_email

is now

sensor.verisure_e_mail

Issue solved.


#3

You have to be careful using is defined. E.g., even if sensor.xyz doesn’t exist, states.sensor.xyz is defined will return True, because states.sensor.xyz will be None, and None is defined. Your test should probably be states.sensor.verisure_email.attributes is defined, or states.sensor.verisure_email is not none. FYI, you can’t do states.sensor.verisure_email.attributes.body is defined, because if sensor.verisure_email doesn’t exist, that will cause an error.

Also your template is unnecessarily complicated. Probably this will do what you want:

- platform: template
  sensors:
    alarm_tilkoblet:
      friendly_name: Alarm tilkoblet
      value_template: >
        {{ 'Systemet MYSYSTEM ble tilkoblet' in (state_attr('sensor.verisure_email', 'body') or '') }}

If sensor.verisure_email doesn’t exist, or if it doesn’t have a body attribute, then the state_attr function will return None. In that case, the or '' comes into play, making sure that the in test has a string on both sides. So, if the entity exists, and it has a body attribute, and the specified string is in it, then the result will be True. Otherwise, it will be False.

EDIT: Just saw your second reply. I’d still suggest changing as described above (for the reasons mentioned), but of course, changing sensor.verisure_email to sensor.verisure_e_mail.


#4

Thank you, @pnbruckner!

Although my immediate problem was solved when I discovered that the entity name had changed, I can see, from your post, that my solution is not as robust as I would have liked it to be.

I will change my template using the suggestions in your post. Your input is much appreciated.