0.83 breaking change - Template binary sensor will now fall back to manual updates only if we cannot extract the entity IDs to track from the templates

Hi,

I’ve installed version 0.83 with the breaking change mentioned on the subject. I have the following binary sensor template:

binary_sensor:
  - platform: template
    sensors:
      school_day:
        friendly_name: "School day"
        value_template: "{{ (now().strftime('%a') != 'Sat') }}"

and I’m getting this warning on my log:
Template binary sensor school_day has no entity ids configured to track nor were we able to extract the entities to track from the value template(s). This entity will only be able to be updated manually.

But I’m not clear what do I need to do with the Entity Ids in order to fix it.

Any help will be appreciated.

Make an automation with time trigger (once per day is enough for your template sensor) and call the service

service: homeassistant.update_entity
entity_id: sensor.school_day

Or just use that one:

Sebastian

For completeness: I think it should be sufficient to attach some dummy entity_id to your template sensor (like sun.sun), even when it’s never used for calculating the value.

Thank you both.

With “sun.sun” I don’t see the error. I will wait for 100% confirmation for next Saturday.

@sebk-666 -
“Workday Binary Sensor” doesn’t suit my needs as my country is not listed on the list of countries (Required). I guess it is possible to edit the files, but I found my one line binary template sensor to be an easier solution.

Cheers.

Why wait for next Saturday? Make a second sensor whose value_template checks for 'Mon' and see if it works.

BTW, the purpose of specifying an entity_id (in a template_sensor) is to make things more efficient for Home Assistant. Instead of it having to guess which entity it’s supposed to monitor (by exploring the template’s code), you’re specifying the precise entity (or entities). When that entity changes state, Home Assistant will evaluate your template_sensor.

You have chosen to specify the sun.sun entity. That means every time the state of sun.sun changes, Home Assistant will evaluate your template_sensor. The documentation for the sun component suggests it has many state changes throughout the day (like elevation) so your template_sensor is sure to be evaluated more than once during the day.

Arguably, it will be evaluated more often than is necessary to determine if it is Saturday. That’s why the suggestion offered by @VDRainer is the most efficient. You only need to evaluate your template_sensor once, just after midnight, to determine whether the new day is Saturday.

1 Like

@123
Thank you very much for the comprehensive answer! and you are right of course about 'Mon' :slight_smile:

I’ve been wondering about this. Is it good practice to always include the entity_ids. Does it have a real effect on either startup or runtime performance, or is it really only a theoretical efficiency?

I never include them unless I have to as it makes the code easier to read (IMO) but I’d be happy to change that if there was a benefit.

I believe the handling of template_sensors changed in 0.81.

If I’m not mistaken, Home Assistant still examines the template_sensor’s template and attempts to select the relevant entities to monitor. If it can’t find one, then it logs a warning message indicating you’ll have to update the template_sensor manually. Explicitly listing the entities within the template_sensor is best (for efficiency). It also makes you mindful of which sensors you’re asking Home Assistant to monitor.

People often use now() for triggering the template_sensor but that’s a function, not an entity; the template_sensor won’t trigger from now(). In this situation, I’ve seen the suggestion to avoid the use of now() and add a Time & Date sensor to your configuration. Then you can use the entity sensor.time or sensor.date_time (whichever is better suited to the task) in the template instead of now().

It doesn’t work this trick.

Update for sensor.raccolta_differenziata fails
Traceback (most recent call last):
File “/srv/homeassistant/lib/python3.6/site-packages/homeassistant/helpers/entity.py”, line 221, in async_update_ha_state
await self.async_device_update()
File “/srv/homeassistant/lib/python3.6/site-packages/homeassistant/helpers/entity.py”, line 347, in async_device_update
await self.async_update()
File “/srv/homeassistant/lib/python3.6/site-packages/homeassistant/components/sensor/template.py”, line 196, in async_update
self._state = self._template.async_render()
File “/srv/homeassistant/lib/python3.6/site-packages/homeassistant/helpers/template.py”, line 137, in async_render
return self._compiled.render(kwargs).strip()
File “/srv/homeassistant/lib/python3.6/site-packages/jinja2/asyncsupport.py”, line 76, in render
return original_render(self, *args, **kwargs)
File “/srv/homeassistant/lib/python3.6/site-packages/jinja2/environment.py”, line 1008, in render
return self.environment.handle_exception(exc_info, True)
File “/srv/homeassistant/lib/python3.6/site-packages/jinja2/environment.py”, line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File “/srv/homeassistant/lib/python3.6/site-packages/jinja2/_compat.py”, line 37, in reraise
raise value.with_traceback(tb)
File “”, line 9, in top-level template code
File “/srv/homeassistant/lib/python3.6/site-packages/jinja2/sandbox.py”, line 427, in call
return __context.call(__obj, *args, **kwargs)
TypeError: ‘int’ object is not callable

How to fix it?

People can’t use time or date_time for weekday, is it?
For example, I have a sensor that remember me which recycle bin I have to take out of home in which day. So if it’s monday I have to take off paper, on wednesday plastic and so on. I think I can’t do this with the sensors above.