How to execute the Sonos volumeUp command in a routine?

Sorry to hijack the topic a bit, just wanted to share… and I think it is loosely related to OP’s post and could also be beneficial to others who stumble in here.

At the time, I was trying to have announcements (alarm, mail, door, washer, etc.) broadcast via Sonos, but could not find a way to adjust the Sonos volume up to a reasonable level in conjunction with the announcement, than back down to the previous volume after the announcement. Sure seems like setting the volume of an announcement should be included with the “Play message on speaker” option, but don’t get me started on all the issues with the Sonos integration, especially compared to what we all had working great with groovy.

Anyway, this was particularly a problem because for whatever reason the announcement volume/voice seems to be much lower then any playing media. Also, my wife and kids often leave the Sonos volume much lower for music then needed for any announcements (for example: they were listening to light music or white noise prior to turning it off). I know I could maybe compensate for this by programmatically resetting the Sonos volume at some set interval, but that has the potential for a rude awakening if someone is trying to relax at the reset time(s).

I first attempted to use the option to set the volume in a routine by selecting “Control Devices” and setting my Sonos volume to 65, followed by setting and playing the announcement, and then setting the Sonos back to a static volume level appropriate for music… but because of the delay, it would momentarily blast any music playing at 65, speak the announcement, blast the music again for a moment before resetting the volume. It also took 2 or more routines for each announcement because you can’t pick the Sonos device to set or reset the volume more then once per routine. This method was also not ideal because, at the end, it reset the volume to a static number and not the previous volume prior to the announcement.

I thought about pausing any music that might be playing prior to the announcement and un-pausing it following the announcement, but if music wasn’t playing prior, un-pausing after any announcements would have the unexpected behavior of starting the last played and stopped music from hours or days earlier. Man, is anyone else terribly missing Webcore and the ability to set and use variables?

Obviously setting the volume of announcements and returning the Sonos to it’s previous volume should be included and standard within the “Play message on speaker” option, but since it is not for some odd reason, here is how I hacked it with some minor help from ChatGPT on the code formatting. My hack is also relevant here because it shows another example and unique use of the solution recommended by @orangebucket above.

Due to the lack of ability to natively store and use variables in rules, I installed @Mariano_Colmenarejo’s “Aplicaciones Virtuales Mc”, which has the ability to create a “Number Field (Stored Variables)” device.

Using @TAustin’s “vEdge Creator: a virtual device generator for end users”, I created 2 virtual momentary buttons.

One that I named “Get Sonos Volume (Rules API)”, and the second “Reset Sonos Volume (Rules API)”. I then used @TAustin’s “API Browser+” to create/publish 2 new rules.

The first rule garbs the current Sonos Volume (both groupVolume and the regular Volume) when the corresponding virtual momentary button is pressed. It then stores for later use the values of the groupVolume and Volume in the 1st and 2nd slots of the “Number Field (Stored Variables)” device. I also added a brief delay/sleep to account for some system latency when using the momentary button/rule in my routines. Here is my JSON code (I had a tiny bit of help from ChatGPT on structure):

{
    "name": "Get Sonos Volume",
    "actions": [
        {
            "if": {
                "equals": {
                    "left": {
                        "string": "pushed"
                    },
                    "right": {
                        "device": {
                            "devices": [
                                "Device ID for the momentary button titled Get Sonos Volume (Rules API)"
                            ],
                            "component": "main",
                            "capability": "button",
                            "attribute": "button"
                        }
                    }
                },
                "then": [
                    {
                        "command": {
                            "devices": [
                                "Device ID for the Number Field (Stored Variables) device"
                            ],
                            "commands": [
                                {
                                    "component": "main",
                                    "capability": "legendabsolute60149.numberFieldOne",
                                    "command": "setNumberFieldOne",
                                    "arguments": [
                                        {
                                            "device": {
                                                "devices": [
                                                    "Device ID for the Sonos device"
                                                ],
                                                "component": "main",
                                                "capability": "mediaGroup",
                                                "attribute": "groupVolume"
                                 }
                              }
                           ]
                        }
                     ]
                  }
               },
                    {
                        "command": {
                            "devices": [
                                "Device ID for the Number Field (Stored Variables) device"
                            ],
                            "commands": [
                                {
                                    "component": "main",
                                    "capability": "legendabsolute60149.numberFieldTwo",
                                    "command": "setNumberFieldTwo",
                                    "arguments": [
                                        {
                                            "device": {
                                                "devices": [
                                                    "Device ID for the Sonos device"
                                                ],
                                                "component": "main",
                                                "capability": "audioVolume",
                                                "attribute": "volume"
                                 }
                              }
                           ]
                        }
                     ]
                  }
               },
                    {
                        "sleep": {
                            "duration": {
                                "value": {
                                    "integer": 1
                                },
                                "unit": "Second"
                            }
                        }
                    }
            ]
         }
      }
   ]
}

The second rule resets the Sonos Volume back (both groupVolume and the regular Volume) when the corresponding virtual momentary button is pressed. It pulls the previously stored values of the groupVolume and Volume from the 1st and 2nd slots of the “Number Field (Stored Variables)” device and resets both on the Sonos. I also added a brief delay/sleep to account for some system latency here as well when using the momentary button/rule in my routines. Here is my JSON code (I had a tiny bit of help from ChatGPT on structure):

{
    "name": "Reset Sonos Volume",
    "actions": [
        {
            "if": {
                "equals": {
                    "left": {
                        "string": "pushed"
                    },
                    "right": {
                        "device": {
                            "devices": [
                                "Device ID for the momentary button titled Reset Sonos Volume (Rules API)"
                            ],
                            "component": "main",
                            "capability": "button",
                            "attribute": "button"
                        }
                    }
                },
                "then": [
                    {
                        "sleep": {
                            "duration": {
                                "value": {
                                    "integer": 2
                                },
                                "unit": "Second"
                            }
                        }
                    },
                    {
                        "command": {
                            "devices": [
                                "Device ID for the Sonos device"
                            ],
                            "commands": [
                                {
                                    "component": "main",
                                    "capability": "mediaGroup",
                                    "command": "setGroupVolume",
                                    "arguments": [
                                        {
                                            "device": {
                                                "devices": [
                                                    "Device ID for the Number Field (Stored Variables) device"
                                                ],
                                                "component": "main",
                                                "capability": "legendabsolute60149.numberFieldOne",
                                                "attribute": "numberFieldOne"
                                            }
                                        }
                                    ]
                                }
                            ]
                        }
                    },
                    {
                        "command": {
                            "devices": [
                                "Device ID for the Sonos device"
                            ],
                            "commands": [
                                {
                                    "component": "main",
                                    "capability": "audioVolume",
                                    "command": "setVolume",
                                    "arguments": [
                                        {
                                            "device": {
                                                "devices": [
                                                    "Device ID for the Number Field (Stored Variables) device"
                                                ],
                                                "component": "main",
                                                "capability": "legendabsolute60149.numberFieldTwo",
                                                "attribute": "numberFieldTwo"
                                            }
                                        }
                                    ]
                                }
                            ]
                        }
                    }
                ]
            }
        }
    ]
}

Now, in any routines for my announcements, I just add a “Get Sonos Volume (Rules API)” virtual momentary button press right before the “Play message on speaker”, and a “Reset Sonos Volume (Rules API)” virtual momentary button press right after.

Whew! Now that I have memorialized this all here for anyone else who may be trying to accomplish something similar, watch the next Sonos beta driver add the ability to temporarily adjust announcement volume. Fingers crossed!

5 Likes