Why isn't the template getting notified by sun automatically?

I created a templated helper:

{{
  is_state('front_wall_light_schedule_on', 'on') or
  (is_state('motion_sensor_occupancy_front', 'on') and is_state('sun.sun', 'below_horizon'))
}}

But in the UI this claims:

This template listens for the following state changed events:

  • Entity: front_wall_light_schedule_on
  • Entity: motion_sensor_occupancy_front

Is sun actually not supported here for some reason? This seems weird since I can create another helper that I add the automation to flip… which is silly :frowning:

Actually this is weird, because another state is not showing up either:

{{ is_state('front_wall_light_schedule_on', 'on') or (is_state('motion_sensor_occupancy_front', 'on') and is_state('sun_up_state', 'off')) }}

does not work :frowning:

It looks like the template interpreter does not check whether the entities do really exist when using is_state.
For instance this template is rendering “false” although I am sure the entity does not exist:

{{ is_state('sensor.nothing', 'on') }}

And this template is rendering “unknown”:

{{ states('sensor.nothing') }}

So are you sure that these entities do exist on your system?

What happens when you do this:

{{ states('front_wall_light_schedule_on') }}
{{ states('motion_sensor_occupancy_front') }}
{{ states('sun.sun') }}

I am pretty sure that each entity ID always contains a dot, so could it be that you forgot to add the entity types?
For instance, should it be binary_sensor.front_wall_light_schedule_on for your sensor?

@viraptor It’s because your if statement fails and never hits the is_state function for sun.sun because the and fails. It will hit that part when the or statement passes.

A or b and c is essentially a or (b and c). If b is false, there’s no reason to check c because the and fails. Therefore it won’t show up in your watched entities. It is still part of the template so there’s nothing to worry about. When b becomes true, sun.sun will be checked and added to the template listener.

EDIT: The reason your b is false is because of what @Troon highlighted in the next post.

2 Likes

Your entity IDs are missing their domains (at a guess, schedule.front_wall_light_schedule_on and binary_sensor.motion_sensor_occupancy_front), like Ed said.

This:

is_state('motion_sensor_occupancy_front', 'on')

…will always be false, so the template system doesn’t need to check the sun state.

2 Likes