Sensor not updated, when entity references are inside a jinja template

Hey,

I created a binary sensor that combines the state of several entities, in my case I’m trying to figure out if I got rooms where the heater is active (target temperature almost missed + valve is on).

That works good so far, but as the code to combine the data takes some lines, I decided to put it into a jinja template/script file.

With that change, the sensor stopped from being updated when one of the source entities are changed. It seems that the entity relation down towards the binary sensor is not longer recognized.

It will only update the sensor, when I reload the yaml config.

Here is my setup:
custom_templates/heating.jinja

{%- set valves = namespace(entries=["sensor.room_a_valvetappet", "sensor.room_a_valvetappet_2", "sensor.room_b_valvetappet", "sensor.room_c_valvetappet"]) %}
{%- set activeValvesList = namespace(entries=[]) %}
{%- for valve in valves.entries -%}
    {%- set current_valve = states(valve) -%}
    {%- if current_valve is not none and is_number(current_valve) and current_valve|int > 0 %}
        {%- set item = {"room": area_name(valve) } -%}
        {%- set activeValvesList.entries = activeValvesList.entries + [item] -%}
    {%- endif %}
{%- endfor -%}
{%- set lowerTemperatureList = namespace(entries=[]) %}
{%- for state in states.climate -%}
    {%- set current_target_temperature = state_attr(state.entity_id, "temperature") -%}
    {%- set current_temperature = state_attr(state.entity_id, "current_temperature") -%}
    {%- if current_target_temperature is not none and current_temperature is not none and is_state(state.entity_id, ["heat", "auto"]) and current_target_temperature > 5 -%}
        {%- set diff = current_target_temperature - current_temperature -%}
    {%- else -%}
        {%- set diff = none -%}
    {%- endif %}
    {%- if diff is not none and diff >= -0.5 %}
        {%- set item = {"room": area_name(state.entity_id)} -%}
        {%- set lowerTemperatureList.entries = lowerTemperatureList.entries + [item] -%}
    {%- endif %}
{%- endfor -%}
{%- set requestHeatingList = namespace(entries=[]) %}
{%- for lowTemperatureRoom in lowerTemperatureList.entries -%}
    {%- set roomHasOpenValve = false -%}
    {%- for activeValveRoom in activeValvesList.entries -%}
        {%- if lowTemperatureRoom.room == activeValveRoom.room %}
        {%- set item = {"room": lowTemperatureRoom.room} -%}
        {%- set requestHeatingList.entries = requestHeatingList.entries + [item] -%}
        {%- endif %}
    {%- endfor -%}
{%- endfor -%}

binary sensor config

template:
    - binary_sensor:
          - name: "Rooms with active heating"
            unique_id: "template.sensor.heating_ready_and_needed"
            state: >
                {% from 'heating.jinja' import requestHeatingList %}
                {{- requestHeatingList.entries|list|count  -}}
            attributes:
                rooms: >
                    {% from 'heating.jinja' import requestHeatingList %}
                    {{- requestHeatingList.entries|map(attribute='room')|unique|list  -}}

What can I do to get the binary sensor updated, when something on the template listed entities changes?
Because when the list is part of the actual config, the sensor gets updated as planned.

Thank you!