core: Template: automatic analysis does NOT find all entities that affect the state in the template

The problem

The template does not find all entities that affect the state in the template. In the example below, only 3 entities were identified by the automatic analysis:

  • binary_sensor.aeon_sensor
  • light.dimmer
  • sensor.motions_in_15_min

While there are two more entities that affect the state in the template:

  • binary_sensor.motion_in_room
  • group.all_persons

Environment

  • Home Assistant Core release with the issue: 0.115.x
  • Last working Home Assistant Core release (if known): 0.114.x
  • Operating environment (OS/Container/Supervised/Core): Supervised
  • Integration causing this issue: template
  • Link to integration documentation on our website: https://www.home-assistant.io/integrations/binary_sensor.template/

Problem-relevant configuration.yaml

'{{as_timestamp(now()) - as_timestamp(states.binary_sensor.aeon_sensor.last_changed|default(now())) < 930 or is_state("binary_sensor.aeon_sensor", "on") or (states.light.dimmer.state == "on" and states.sensor.motions_in_15_min.state|int < 1 and (states.binary_sensor.aeon_sensor.last_changed.timestamp()- states.binary_sensor.motion_in_room.last_changed.timestamp()) > 60 and states.group.all_persons.state == "home" ) }}'

Traceback/Error logs


Additional information

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (6 by maintainers)

Most upvoted comments

I think this may be functioning mostly as intended (despite seeming confusing). My understanding is that the set of entities listened for is now dynamic, based on the set of entities actually evaluated during the last template render.

For the original’s poster’s example: Because the first two cases evaluated to false, it starts evaluating the “and” cases. The first clause states("light.dimmer") == "on" evaluated to true, so it moved onto states.sensor.motions_in_15_min.state|int < 1 which was false and short circuits the rest. So the remaining entities don’t yet get a listener attached, but it does not matter, because they cannot make any difference unless and until states.sensor.motions_in_15_min’s state becomes 1 or greater. If that happens, then it will re-evaluate, and will end up adding listeners for more entities.

Following this sort of logic, the first two screenshots also make sense, given that none of the entities exist on that system.

However, that “all states changed events” one though looks really unfortunate, and undesirable. Trying to evaluate some non-existent entity’s state probably should not be able to escalate to an all states listener, but it looks like that is what happened?