Template switch — Template functions but doesn't update

I’m trying to wrap my brain around this one but cannot figure it out for the life of me.
I’m attempting to create a simple template switch that toggles the grouping of all Sonos devices in my home.

My initial working code was as follows:

sonos_group:
    friendly_name: "Sonos Grouped"
    value_template: >
        {% set devices = ["media_player.living_room", "media_player.kitchen", "media_player.bedroom"] %}

        {%- for device in states if device.entity_id|lower in devices and device.attributes.is_coordinator == false -%}
            {{ True if loop.last }}
        {%- else -%}
            {{ False }}
        {%- endfor -%}
    turn_on:
        service: media_player.sonos_join
        data:
            master: media_player.living_room
            entity_id:
              - media_player.living_room
              - media_player.kitchen
              - media_player.bedroom
    turn_off:
        service: media_player.sonos_unjoin
        data:
            entity_id:
              - media_player.living_room
              - media_player.kitchen
              - media_player.bedroom

I’ve since tweaked this to make it a bit tidier by defining a group with only the Sonos devices, so that the list can be obtained using {{ states.group.sonos.attributes.entity_id|list }}.

Howerver, this version no longer works…

sonos_group:
    friendly_name: "Sonos Grouped"
    value_template: >
        {% set devices = states.group.sonos.attributes.entity_id|list %}

        {%- for device in states if device.entity_id|lower in devices and device.attributes.is_coordinator == false -%}
            {{ True if loop.last }}
        {%- else -%}
            {{ False }}
        {%- endfor -%}
    turn_on:
        service: media_player.sonos_join
        data_template:
            master: "{{ states.group.sonos.attributes.entity_id[0] }}"
            entity_id: "{{ states.group.sonos.attributes.entity_id|join(',') }}"
    turn_off:
        service: media_player.sonos_unjoin
        data_template:
            entity_id: "{{ states.group.sonos.attributes.entity_id|join(',') }}"

Whilst the turn_on and turn_off functions work fine, in this new version, the value_template that monitors the state, does not. Instead, toggling the switch will keep the switch at the state before which it was toggled.

If I revert the {% set devices… %} line to the previous version, the problem is resolved.

Testing the value_template in /dev-template yields the same result between both.
Both {% set devices… %} line result in a list of Songs devices — the difference is one is pulled through from another (shared) object, the first is explicitly defined.

Cannot figure out why one is working and one is different — they appear the same on the surface. Nothing is flagging as an error within the logs.

Any thoughts?

Looking into this further, I think it’s just a timing issue.

If I use an entity_id tag as part of the template switch, and bind this to a ‘uptime’ sensor that lists how long HA has been alive for, this fixes the issue. I guess the template isn’t executed every second or so…