Can't get rhasspy to work after autostart - Manual start works without problems

Hi together,
i’ve a rhasspy 2.5.11 .deb installation on a Bullseye light. Everything works well as long as I start it in a terminal session ‘rhasspy --profile de’ as ‘pi’ user. But as many users that I found here I can’t get it autostarted. The problem is that aplay doesn’t work after autostart with rasspy but it works in a terminal. Rhasspy shows "AudioServerException: Command ‘[‘aplay’, ‘-q’, ‘-t’, ‘wav’, ‘-D’, ‘pulse’]’ returned non-zero exit status 1 " when I make a speak test. I#ve tried several described scenarios with cron. rc.local and as service without success.
This seems to show the problem:

[DEBUG:2022-08-01 10:02:13,802] asyncio: Using selector: EpollSelector
[DEBUG:2022-08-01 10:02:13,803] rhasspyremote_http_hermes: Connected to MQTT broker
[DEBUG:2022-08-01 10:02:13,807] rhasspyremote_http_hermes: Subscribed to hermes/intent/#
[DEBUG:2022-08-01 10:02:13,809] rhasspyremote_http_hermes: Subscribed to rhasspy/handle/toggleOn
[DEBUG:2022-08-01 10:02:13,810] rhasspyremote_http_hermes: Subscribed to rhasspy/handle/toggleOff
[DEBUG:2022-08-01 10:02:40,826] rhasspywake_porcupine_hermes: -> HotwordDetected(model_id='/home/pi/.config/rhasspy/profiles/de/porcupine/jarvis_raspberry-pi.ppn', model_version='', model_type='personal', current_sensitivity=0.6, site_id='default', session_id=None, send_audio_captured=None, lang=None, custom_entities=None)
[DEBUG:2022-08-01 10:02:40,828] rhasspywake_porcupine_hermes: Publishing 262 bytes(s) to hermes/hotword/jarvis_raspberry-pi/detected
[DEBUG:2022-08-01 10:02:40,848] rhasspydialogue_hermes: <- HotwordDetected(model_id='/home/pi/.config/rhasspy/profiles/de/porcupine/jarvis_raspberry-pi.ppn', model_version='', model_type='personal', current_sensitivity=0.6, site_id='default', session_id=None, send_audio_captured=None, lang=None, custom_entities=None)
[DEBUG:2022-08-01 10:02:40,852] rhasspydialogue_hermes: Playing sound /home/pi/.config/rhasspy/profiles/de/guuude-thorsten.wav
[DEBUG:2022-08-01 10:02:40,865] rhasspydialogue_hermes: -> HotwordToggleOff(site_id='default', reason=<HotwordToggleReason.PLAY_AUDIO: 'playAudio'>)
[DEBUG:2022-08-01 10:02:40,869] rhasspydialogue_hermes: Publishing 44 bytes(s) to hermes/hotword/toggleOff
[DEBUG:2022-08-01 10:02:40,875] rhasspydialogue_hermes: -> AsrToggleOff(site_id='default', reason=<AsrToggleReason.PLAY_AUDIO: 'playAudio'>)
[DEBUG:2022-08-01 10:02:40,877] rhasspydialogue_hermes: Publishing 44 bytes(s) to hermes/asr/toggleOff
[DEBUG:2022-08-01 10:02:40,881] rhasspydialogue_hermes: -> AudioPlayBytes(27692 byte(s)) to hermes/audioServer/default/playBytes/5c49823c-b4bd-4c57-a75d-1e83bf5e4b94
[DEBUG:2022-08-01 10:02:40,884] rhasspydialogue_hermes: Waiting for playFinished (id=5c49823c-b4bd-4c57-a75d-1e83bf5e4b94, timeout=0.876938775510204)
[DEBUG:2022-08-01 10:02:40,896] rhasspywake_porcupine_hermes: <- HotwordToggleOff(site_id='default', reason=<HotwordToggleReason.PLAY_AUDIO: 'playAudio'>)
[DEBUG:2022-08-01 10:02:40,897] rhasspywake_porcupine_hermes: Disabled
[DEBUG:2022-08-01 10:02:40,900] rhasspyspeakers_cli_hermes: <- AudioPlayBytes(27692 byte(s))
[DEBUG:2022-08-01 10:02:40,902] rhasspyspeakers_cli_hermes: ['aplay', '-q', '-t', 'wav', '-D', 'pulse']
[DEBUG:2022-08-01 10:02:40,902] rhasspyasr_kaldi_hermes: <- AsrToggleOff(site_id='default', reason=<AsrToggleReason.PLAY_AUDIO: 'playAudio'>)
[DEBUG:2022-08-01 10:02:40,904] rhasspyasr_kaldi_hermes: Disabled (AsrToggleReason.PLAY_AUDIO)
ALSA lib pulse.c:242:(pulse_connect) PulseAudio: Unable to connect: Connection refused

aplay: main:830: audio open error: Connection refused
[ERROR:2022-08-01 10:02:41,005] rhasspyspeakers_cli_hermes: handle_play
Traceback (most recent call last):
  File "/usr/lib/rhasspy/rhasspy-speakers-cli-hermes/rhasspyspeakers_cli_hermes/__init__.py", line 88, in handle_play
    subprocess.run(self.play_command, input=wav_bytes, check=True)
  File "/usr/lib/rhasspy/usr/local/lib/python3.7/subprocess.py", line 512, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['aplay', '-q', '-t', 'wav', '-D', 'pulse']' returned non-zero exit status 1.
[DEBUG:2022-08-01 10:02:41,027] rhasspyspeakers_cli_hermes: -> AudioPlayError(error="Command '['aplay', '-q', '-t', 'wav', '-D', 'pulse']' returned non-zero exit status 1.", site_id='default', context='5c49823c-b4bd-4c57-a75d-1e83bf5e4b94', session_id='5c49823c-b4bd-4c57-a75d-1e83bf5e4b94')
[DEBUG:2022-08-01 10:02:41,028] rhasspyspeakers_cli_hermes: Publishing 224 bytes(s) to hermes/error/audioServer/play

Any help is appreciated. Thanks in advance … Jo

I assume you’ve read the Rhasspy as a service (without Docker) thread ? I am still running my headless satellites as i described there (running with root privileges).

The problem might be your pulseaudio server.
That is a user specific service and probably not started up when you run headless.

Try to play directly to ALSA instead.

Hi,
thanks for your hints so far. Indeed I’ve read topic ‘Rhasspy as a service’ because that’s my scenario but I had no succses. I did a lot trial and error but in the meantime i have no further idea. At least i tried WallyDWs tip without pulseaudio but i can’t get my bluetooth connection working without it. It’s a pity, maybe i should open a issue on github to get the reason for this message.
AudioServerException: Command ‘[‘aplay’, ‘-q’, ‘-t’, ‘wav’, ‘-D’, ‘pulse’]’ returned non-zero exit status 1

Read this on Pulseaudio on headless.
Notice the comments.

Hi Guys, thanks for all your tips an ideas but I was not able to bring them to success. So far the bad news. Nevertheless, the good news are that i got it running. After fiddling around lots of hours i used at least the Bullseye full desktop version. Starting rhasspy as service, with rc.local or a cronjob shows the same problem. Then i tried it with a .desktop file and voilà -bingo- everything works as expected now. I don’t know what is different in this case but those that are familiar with the rhasspy and Linux internals may have a idea what the root cause could be.
Thanks again for all you supporters … Jo