please help me find why this is not working as hoped, preventing a binary_sensors from erroring now and then with:
2021-03-19 09:16:20 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'NoneType'') while processing template 'Template("{% set updater = states('sensor.sensors_huidig_verbruik_summed') %} {{(now() -
state_attr('automation.update_light_motion','last_triggered')).total_seconds()
< 10}}")' for attribute '_state' in entity 'binary_sensor.motion_light'
my binary sensor is:
motion_light:
friendly_name: Motion light
value_template: >
{% set updater = states('sensor.sensors_huidig_verbruik_summed') %}
{{(now() -
state_attr('automation.update_light_motion','last_triggered')).total_seconds()
< 10}}
availability_template: >
{% set id = state_attr('automation.update_light_motion','last_triggered') %}
{% set un = ['unknown','unavailable'] %}
{{id not in un and id is not none}}
and the set updater
is there, to have the binary sensor update, because it otherwise wouldn’t, given the fact it is based on now()
Most of the times this works just fine, and the automations based on the binary work too. It’s just that the error is displayed in the log every now and then, and I cant explain why
Maybe I need to throw it all in the template itself, as an if statement, like:
{% set updater = states('sensor.sensors_huidig_verbruik_summed') %}
{% set id = state_attr('automation.update_light_motion','last_triggered') %}
{% set un = ['unknown','unavailable'] %}
{% if id not in un and id is not none %}
{{(now() -
state_attr('automation.update_light_motion','last_triggered')).total_seconds()
< 10}}
{% else %} False
{% endif %}
?