Subtracting masterr entity from sonos_group

Hi there.
I have a setup with my sonos speakers, where the bathroom speaker would join the livingroom speaker on motion, if livingroom speaker is playing.

I have noticed, that if the livingroom speaker is not the master in a group, then livingroom stops playing, because it leaves the excisting group, which means that the bathroom speaker will not play anything.

What I am trying to do, is to read the masterr in the sonos_group the livingroom speaker is a part of, and make sure, that the bathroom speaker is added as a slave to the actual master speaker.

So currently, my sonos group attribute for the entity media_player.stue looks like this:
sonos_group:

  - media_player.stue
  - media_player.sovevaerelse
  - media_player.koekken

It seems, that the first entity is always the master, at least from my tests. But if the master is changed to koekken, for some reason, then I need the automation triggered from motion on the bathroom to set koekken as the master.

is that possible somehow?

I figured it out.

I found this post, which lead me to the solution. https://community.home-assistant.io/t/getting-nested-value/123763

service: sonos.join
    data_template: 
      master: > 
        {{ state_attr('media_player.stue', 'sonos_group')[0] }}
      entity_id: media_player.koekken