Automation/Script to fade-in Sonos

Hey everybody,

How may I fade-in my sonos speaker slowly to a specified volume_level in an automation or script?


Usually I have to adjust the volume like this:

- service: media_player.volume_set
    entity_id: media_player.wohnzimmer
    volume_level: 0.15

Is it possible to use a for … endfor loop for it?

Also looking to do something like this with scripts. I quite like how setting alarms on sonos fade in and how timers fade out. Perfect for an alarm automation.

Unless I’m wrong, I’m not sure this is possible with scripts unless you manually set it to run with delays, but I think that might be a bit too convoluted for something as simple as fading up or down. If someone knows a way to do this, I’d love to actually try it!

Another way I can think of doing this is with a function node with Node Red. I’ve yet to test it, but you could try this and see if it works;

let vol = 0.00; // Volume Level
let maxVol = 0.3; //Maximum Volume Level
let interval = 300; //miliseconds

var timer = setInterval(fade, interval); //Set Timer

function fade() {

    //If Volume is greater or equal to Max Volume
    if(vol >= maxVol){
        clearInterval(timer); // Stop Timer
        node.send({payload:parseFloat(vol.toFixed(2))}); //send payload asynchronously
        vol=vol + 0.01; //add by 0.01