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
Mono:
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
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"
when
Item Pi_Volume changed
then
executeCommandLine("amixer set Headphone " + Pi_Volume.state.toString())
end
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.