setMasterVolume error message

I am trying to implement a rule found in the demo.rules to set the master volume level on the host machine.

rule "Volume control"
	Item Volume received command
	if(receivedCommand instanceof PercentType) {
		setMasterVolume(receivedCommand as PercentType)
	} else {
		if(receivedCommand==INCREASE) increaseMasterVolume(20)
		if(receivedCommand==DECREASE) decreaseMasterVolume(20)	
	postUpdate(Volume, masterVolume * 100)

I keep getting “[o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule ‘Volume control’: Cannot determine master volume level”. Is anyone implementing this or have any idea what is going on?


1 Like

What kind of machine are you running on. Does this machine have a sound device?

It seems that the volume actions cannot get a handle on your system’s volume controllers.

Sorry, meant to say. Pi2 running raspbian. I’ve got user openhab added to the sound group, so I can stream radio stations just fine…just can’t manipulate the volume level.

Hopefully someone can help. I’ve not done anything volume wise. These are built in core actions so there is no extra config either. I’m stumped.

A workaround is to set the volume using executeCommandLine

I have a Pi2 running Raspbian Jessie (lite) and I have a speaker plugged in to a super cheap (£1.50) USB Soundcard Dongle thing like this:

##Setting the volume level using the command line

I will assume you have sound setup already and the USB soundcard is your default ALSA device.

ssh into your Pi and type amixer

This was my output

pi@raspberrypi:~ $ amixer
Simple mixer control 'Headphone',0
  Capabilities: pvolume pswitch pswitch-joined
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 151
  Front Left: Playback 120 [79%] [-5.88dB] [on]
  Front Right: Playback 120 [79%] [-5.88dB] [on]
Simple mixer control 'Mic',0
  Capabilities: pvolume pvolume-joined cvolume cvolume-joined pswitch pswitch-joined cswitch cswitch-joined
  Playback channels: Mono
  Capture channels: Mono
  Limits: Playback 0 - 32 Capture 0 - 16
  Mono: Playback 23 [72%] [34.36dB] [off] Capture 0 [0%] [0.00dB] [on]
Simple mixer control 'Auto Gain Control',0
  Capabilities: pswitch pswitch-joined
  Playback channels: Mono
  Mono: Playback [on]

You can control any of the mixer channels, but I only want to set the level for Headphone

The command to use is:

amixer set Headphone 50

Note that 50 is not 50% (though you can add the percentage if you want)

The amixer output from earlier has the line: Limits: Playback 0 - 151

Meaning 151 is the max level

So to set the volume at max you would use

amixer set Headphone 151

though I wouldn’t recomend it :slight_smile:

You can see the change by popping open another terminal and running alsamixer when you send the command then the level will adjust.

Sending the command in openHAB

using executeCommandLine

In openHAB you can adjust the volume using a rule with the action:

executeCommandLine("amixer set Headphone 60")

Handy if you want to lower the volume before text to speech in order to make it less jarring. Or to raise the volume if using an alarm or doorbell.

using the value of an item

You can also adjust the volume through the UI.

// Items
Number Pi_Volume "Pi Player [%.1f]" (gAll)
// sitemap
Selection  item=Pi_Volume  mappings=[0="Off",20="20",40="40",60="60",80="80",100="100",120="120",140="140",151="151" ]  // amount of options is up to you

// rules

rule "Change Pi Volume"
  Item Pi_Volume changed
   executeCommandLine("amixer set Headphone " + Pi_Volume.state.toString())

Note, whilst you can probably do the same using the percentage and a dimmer/slider setup, I was having trouble with the whole NumberType/int/decimal/percentage defining and converting of variable requirements that openHAB has. I was also worried that it would easily crash and when I was playing the slider, it was firing off executeCommandLine at a very fast rate. And a simple solution is really all I needed.

I hope it comes in useful for someone.



I solved that as this method:

Scripts in bash shel for volume included:

/usr/bin/amixer -c 0 set PCM 1dB+ |grep “Mono: Playback” |awk ‘/ / {printf “%s”, $4}’|sed ‘s/[^0-9]//g’

#/usr/bin/amixer sset Master 2%- |grep " Front Left: Playback " |awk ‘/ / {printf “%s”, $5}’|sed ‘s/[^0-9]//g’

/usr/bin/amixer -c 0|grep “Mono: Playback” |awk ‘/ / {printf “%s”, $4}’|sed ‘s/[^0-9]//g’

Dimmer Volume “Volume [%s %%]”

Switch Mute “Mute” {exec=">[OFF:/usr/bin/amixer -c 0 set PCM 1000dB-]"}

rule "Volume control"
Item Volume received command or
Time cron “0 0/1 * * * ?” or
System started


if(receivedCommand==INCREASE) {
var String results = executeCommandLine("./configurations/tools/",500)
} else if (receivedCommand==DECREASE) {
var String results = executeCommandLine("./configurations/tools/",500)
} else {
var String status = executeCommandLine("./configurations/tools/",500)