RPI4 Fresh openhabian no sound no audio

Hi!
With a fresh openhabian (64bit) installed on my RPI4 4GB, I can’t make audio working with openhab.
It looks like sounds can be heard only when played via openhabian user and I can’t force openhab to make sounds hearable.
Everything seems to work ok as below, but I hear sound only when playing through openhabian user. When trying to play sound through openhab, root or some other newly created user I can’t hear anything. Does anybody have a clue how to fix it?

[10:17:10] root@openhabian:/usr/share/openhab/addons# groups openhabian
openhabian : openhab adm tty dialout cdrom sudo audio video plugdev games users input render bluetooth netdev spi i2c gpio openhabian
[10:20:38] root@openhabian:/usr/share/openhab/addons# sudo -u openhabian speaker-test -t wav -c 2

speaker-test 1.2.8

Playback device is default
Stream parameters are 48000Hz, S16_LE, 2 channels
WAV file(s)
Rate set to 48000Hz (requested 48000Hz)
Buffer size range from 480 to 32768
Period size range from 480 to 32768
Using max buffer size 32768
Periods = 4
was set period_size = 8192
was set buffer_size = 32768
 0 - Front Left
 1 - Front Right
Time per period = 2.404455
 0 - Front Left
 1 - Front Right
Time per period = 3.070715
 0 - Front Left
 1 - Front Right
[10:21:03] root@openhabian:/usr/share/openhab/addons# groups openhab
openhab : openhab tty dialout audio bluetooth gpio
[10:21:22] root@openhabian:/usr/share/openhab/addons# sudo -u openhab speaker-test -t wav -c 2

speaker-test 1.2.8

Playback device is default
Stream parameters are 48000Hz, S16_LE, 2 channels
WAV file(s)
Rate set to 48000Hz (requested 48000Hz)
Buffer size range from 480 to 32768
Period size range from 480 to 32768
Using max buffer size 32768
Periods = 4
was set period_size = 8192
was set buffer_size = 32768
 0 - Front Left
 1 - Front Right
Time per period = 2.407208
 0 - Front Left
 1 - Front Right
Time per period = 3.081668
 0 - Front Left
 1 - Front Right

Ok, solved. It looks like the sound card (Headphones in my case) was not set up as default sound card. More precisely, neither sound card was set up as default. When I set default sound card as Headphones the sound is hearable. Below is the instruction fixing that issue, however, due to “openhab” user has no home directory, go straight to step 2.

If you prefer to set the default sound card for the current user only, you can modify the .asoundrc file in your home directory instead of /etc/asound.conf. The steps are similar:

  1. Edit the .asoundrc file: Open or create the .asoundrc file in your home directory:
nano ~/.asoundrc
  1. Set the default sound card: Add the same lines to set the headphones as the default sound card:
defaults.pcm.card 1
defaults.ctl.card 1
  1. Save and exit: Save the changes and exit the text editor.
  2. Restart ALSA: Restart the ALSA sound system to apply the changes:
sudo /etc/init.d/alsa-utils restart

Also make sure that “openhab” has been added to “audio” group.

usermod -a -G audio openhab

If you need to check which card is set as default use

aplay -l

and see which card is marked as “default”. If none of the cards is marked as default, choose the card which you wish to be set as default and modify step 2 with its number accordingly.

That’s all.