Template sensor errors out on startup, need some help

HI,

trying to determine wether the garbage is due in 2 days, I use this template sensor:

        value_template: >
          {% set papier = (as_timestamp(strptime(states('sensor.trash_papier'), '%d-%m-%Y')) - 
                (2 * 86400 )) | timestamp_custom('%d-%m-%Y') %}
          {% set gft = (as_timestamp(strptime(states('sensor.trash_gft'), '%d-%m-%Y')) - 
                (2 * 86400 )) | timestamp_custom('%d-%m-%Y') %}
          {% set plastic = (as_timestamp(strptime(states('sensor.trash_plastic'), '%d-%m-%Y')) - 
                (2 * 86400 )) | timestamp_custom('%d-%m-%Y') %}
          {% set rest = (as_timestamp(strptime(states('sensor.trash_restafval'), '%d-%m-%Y')) - 
                (2 * 86400 )) | timestamp_custom('%d-%m-%Y') %}
          {% set date = now().strftime('%d-%m-%Y') %}
          {% if date == papier %} Papier
          {% elif date == gft %} Gft
          {% elif date == plastic %} Plastic
          {% elif date == rest %} Restafval
          {% else %} Geen
          {% endif %}

Can this be changed so it won’t create this error:

2019-07-02 08:11:34 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.afval_overmorgen fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 220, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 375, in async_device_update
    await self.async_update()
  File "/usr/src/homeassistant/homeassistant/components/template/sensor.py", line 191, in async_update
    self._state = self._template.async_render()
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 191, in async_render
    return self._compiled.render(kwargs).strip()
  File "/usr/local/lib/python3.7/site-packages/jinja2/asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'

the sensor.trash_ xxx sensors have this format:

11

since these are made by a custom component, they probably aren’t initialized at the time HA tries to read them. Still, seeing this more than a few times in the startup log, and if possible like to prevent that.

thanks for having a look!

Not tested, but i think this should work.

{% if states.sensor.trash_papier.state is defined and states.sensor.trash_gft.state is defined and ... %}
  your template
{% else %}
  waiting for sensors
{% endif%}
1 Like

thanks,

tried this now:

        value_template: >
          {% if states('sensor.trash_papier') is defined and
                states('sensor.trash_gft') is defined and
                states('sensor.trash_plastic') is defined and
                states('sensor.trash_restafval') is defined %}
            {% set papier = (as_timestamp(strptime(states('sensor.trash_papier'), '%d-%m-%Y')) - 
                  (2 * 86400 )) | timestamp_custom('%d-%m-%Y') %}
            {% set gft = (as_timestamp(strptime(states('sensor.trash_gft'), '%d-%m-%Y')) - 
                  (2 * 86400 )) | timestamp_custom('%d-%m-%Y') %}
            {% set plastic = (as_timestamp(strptime(states('sensor.trash_plastic'), '%d-%m-%Y')) - 
                  (2 * 86400 )) | timestamp_custom('%d-%m-%Y') %}
            {% set rest = (as_timestamp(strptime(states('sensor.trash_restafval'), '%d-%m-%Y')) - 
                  (2 * 86400 )) | timestamp_custom('%d-%m-%Y') %}
            {% set date = now().strftime('%d-%m-%Y') %}
            {% if date == papier %} Papier
            {% elif date == gft %} Gft
            {% elif date == plastic %} Plastic
            {% elif date == rest %} Restafval
            {% else %} Geen
            {% endif %}
          {% else %} 'Initializing...'
          {% endif %}

but unfortunately that doesn’t help:

2019-07-02 10:27:02 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.afval_overmorgen fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 220, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 375, in async_device_update
    await self.async_update()
  File "/usr/src/homeassistant/homeassistant/components/template/sensor.py", line 191, in async_update
    self._state = self._template.async_render()
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 191, in async_render
    return self._compiled.render(kwargs).strip()
  File "/usr/local/lib/python3.7/site-packages/jinja2/asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 5, in top-level template code
TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'

Use the states.sensor.entity.state notation as i wrote.
states('sensor.xxxx') is always defined, don’t know why. :upside_down_face:

1 Like

right. it makes it ‘unknown’, so should have used:

{% if states('sensor.trash_papier') != 'unknown' and
                states('sensor.trash_gft') != 'unknown'  and
                states('sensor.trash_plastic') != 'unknown'  and
                states('sensor.trash_restafval') != 'unknown'  %}

probably.

restarting with your full states.xxx.state version now.
thanks

update
no luck, same as before. btw, the != ‘unknown’ version doesn’t help either


Not sure why you’re subtracting two days from each of the sensors, but instead, why not add two days to now()?

        value_template: >
          {% set date = (as_timestamp(now())+2*86400)|timestamp_custom('%d-%m-%Y') %}
          {% if date == states('sensor.trash_papier') %} Papier
          {% elif date == states('sensor.trash_gft') %} Gft
          {% elif date == states('sensor.trash_plastic') %} Plastic
          {% elif date == states('sensor.trash_restafval') %} Restafval
          {% else %} Geen
          {% endif %}

yes, very nice.
this is the sensor the author of the CC provided, and I, must admit, never checked to make more efficient

this is way better of course, now lets see it the status error disappears

keep you posted.
thanks!

update

error has disappeared! great, one less issue in the logs
please let me ask you to have a look at this one too? Might be not as ‘easy’. Rest sensor errors on startup