playSound volume oddity and google home speakers as audio sink

openHAB version: 2.4

TLDR; playSound wasn’t playing at 100% volume on google home speakers.

In my rule, I ran this code:

playSound("doorbell.mp3", new PercentType(100))

The audio sink is a google home mini speaker group (chromecast:audiogroup). However I also observed this behaviour when playing on a single device (chromecast:chromecast).

This is what happened:

  • google home speaker volume was at 45% before rule kicked in
  • playSound() was called with a volume argument of 100%
  • openhab increased google home speaker volume up to 100%
  • then the volume goes down again to around 45% (sometimes 46, 48% etc), before the sound was played
  • then the sound was played (at the reduced volume)

Could this be a bug? I haven’t yet delved into the source code to look, hoping that someone more experienced here could tell me right away.

Hi,

I’ve had some similar experience and although I do not have a conclusive answer, some of the info below might be useful:

What I have also tried is to first set the volume of the Google home to a higher value, so 100% in your example, and then use the playSound command afterwards. I imagine that is what the playSound command with the volume percentage as argument does internally as well.
Based on that, I concluded that the Google home basically gradually increases the volume to the desired volume, and that the playSound command is received while it is still increasing the volume from 45% to 100%. When that happens, the results are rather unpredictable. In any case, my guess is that this is a “feature” or bug of the Google home itself.

Maybe setting a short delay between the volume command and the playSound command might help here, but that is just a guess from my side for now, as I cannot test it right now with everyone asleep.

A delay is indeed needed otherwise it will just behave just like playSound with a volume argument. The problem is I don’t know how to enumerate the members of the audiogroup to save the original volume level on each, because I want to restore them to the original volume level after the call to playSound().

Hi Jim,
Did you ever solve this?
I’m running onto the same problem.

Hi @Jose_Olcese, I just put a delay (or timer) between set volume and playsound, and another timer to restore the previous volume. The playsound is asynchronous so you need to put a delay depending on the length of the sound file.

Having the same issue. Would you mind posting example code? Not exactly intuitive on getting the sound volume and setting volume in a rule etc.

You need an item that controls the volume. This is in jython:

itemVolume = ir.getItem("LivingRoom_GH_Volume")
prevVolume = events.storeStates(itemVolume)
sendCommand(itemVolume, PercentType(80))
ScriptExecution.createTimer(DateTime.now().plusMillis(500), lambda: Voice.say(message))
ScriptExecution.createTimer(DateTime.now().plusSeconds(3), lambda: events.restoreStates(prevVolume))

You could just store the volume level instead of using storestates/restorestates. However, with storestates, you can store multiple volume items in case you are broadcasting on a speaker group.

I think this is a bug of the playSound function, somebody can fix this?
I listen that the volume go to the desired value (100) before the sound is played, then during the play fast return to the previous value.
From the log I can see there is a delay of 3 seconds before the volume come back to the original value and my audio lenght is 3 seconds, I don’t think this is a coincidence.

Here some log if somebody need it
==> /var/log/openhab2/openhab.log <==
2021-01-01 16:11:14.329 [INFO ] [rthome.model.script.consumo-corrente] - Invio audio al gruppo google home mini distacco entro 1 minuto
==> /var/log/openhab2/events.log <==
2021-01-01 16:11:15.625 [vent.ItemStateChangedEvent] - VolumeGruppoMini changed from 21 to 100
2021-01-01 16:11:18.837 [vent.ItemStateChangedEvent] - VolumeGruppoMini changed from 100 to 90
2021-01-01 16:11:19.968 [vent.ItemStateChangedEvent] - VolumeGruppoMini changed from 90 to 85
2021-01-01 16:11:20.136 [vent.ItemStateChangedEvent] - VolumeGruppoMini changed from 85 to 62
2021-01-01 16:11:20.156 [vent.ItemStateChangedEvent] - VolumeGruppoMini changed from 62 to 44
2021-01-01 16:11:20.347 [vent.ItemStateChangedEvent] - VolumeGruppoMini changed from 44 to 39
2021-01-01 16:11:20.630 [vent.ItemStateChangedEvent] - VolumeGruppoMini changed from 39 to 24
2021-01-01 16:11:20.765 [vent.ItemStateChangedEvent] - VolumeGruppoMini changed from 24 to 21

I have the same issue when using “Voice.say(text, volume)” in ECMAScript for saying texts on my Nest Hub.
Volume goes up and then down while my text notification is being played.
Any news on that?

Hi, I think this is due to the nest speakers not applying the volume setting immediately, but kind of ramping up to it.

My theory:
You send a volume command and the speaker will change to that new volume (slowly). As soon as it receives a play sound command it plays it with whatever volume it currently has, reverting the volume command.

Try this: Send a command to change the volume first, and then issue the play sound command ten seconds later. Does this work?

Yes, this works, even with 2 seconds delay. Thanks.