SONOS Volume-Mute-Toggle via a Home Assistant script

After I switched my SONOS control to Home Assistant (HA) in the R2, I was faced with the task of being able to make the toggle for the speaker mute of the corresponding R2 button via an HA script. This toggle was not so easy, as the mute status of the SONOS speakers is not so easy to query. However, it works via a template structure in the script.

If someone wants to do the same, the script looks like this:

The script then looks like this in the (german) HA user interface:

This is the unfolded IF condition:

In the example above, the entry “your_sonos_speaker” must of course be replaced by your correct SONOS speaker entity id.

EDIT:
If anyone here knows an easier way to use this volume mute toggle for SONOS speakers in HA scripts, I would be happy to receive feedback from you.