I’m trying to stream audio from OpenHAB to a Python client using the audio WebSocket feature. I can successfully establish a WebSocket connection, but I never receive any audio data when I send commands. Looking for help to understand the correct configuration and usage pattern.
My Configuration
- OpenHAB Items (audio.items)
// Several configurations attempted:
String PythonSpeaker "Python Speaker"
- Python Test Code
import asyncio
import websockets
import json
import numpy as np
import sounddevice as sd
class OpenHABAudioClient:
def __init__(self, host="192.168.1.100", port=8080, item_name="PythonSpeaker"):
self.ws_url = f"ws://{host}:{port}/audio/{item_name}.stream"
self.sample_rate = 16000
async def connect_and_listen(self):
print(f"Connecting to {self.ws_url}")
async with websockets.connect(self.ws_url) as websocket:
print("✅ WebSocket connected!")
# Send a test command
test_command = "say:Hello from OpenHAB"
await websocket.send(test_command)
print(f"Sent command: {test_command}")
# Listen for data
message_count = 0
while message_count < 20:
try:
# Wait for message with timeout
message = await asyncio.wait_for(websocket.recv(), timeout=2.0)
if isinstance(message, bytes):
print(f"🎵 Received binary audio: {len(message)} bytes")
# Process audio data
audio_data = np.frombuffer(message, dtype=np.float32)
print(f"Audio samples: {len(audio_data)}")
# Play audio
sd.play(audio_data, samplerate=self.sample_rate)
sd.wait()
else:
print(f"📝 Received text: {message}")
message_count += 1
except asyncio.TimeoutError:
print("⏱️ No data received...")
message_count += 1
except Exception as e:
print(f"❌ Error: {e}")
break
print("Test completed")
if __name__ == "__main__":
client = OpenHABAudioClient()
asyncio.run(client.connect_and_listen())
- OpenHAB Rules (test.rules)
rule "Trigger Python Audio"
when
Item TestAudio changed
then
// Attempted various command formats:
PythonSpeaker.sendCommand("say:Testing 123")
// PythonSpeaker.sendCommand("Hello world")
// PythonSpeaker.sendCommand("tts:Hello from OpenHAB")
end
What I’ve Tried
Command Formats Attempted:
- Plain text: PythonSpeaker.sendCommand(“Hello world”)
- Say command: PythonSpeaker.sendCommand(“say:Hello world”)
- TTS command: PythonSpeaker.sendCommand(“tts:Hello world”)
- Play URL: PythonSpeaker.sendCommand(“play:https://example.com/audio.mp3”)
Connection Attempts:
- Direct WebSocket: Connection successful
- With authentication: Added Basic Auth headers (no difference)
- Different audio formats: Tried FLOAT32LE, S16LE, S24LE
Observed Behavior
WebSocket Connection: Successfully connects to ws://192.168.1.100:8080/audio/PythonSpeaker.stream
Command Accepted: OpenHAB logs show commands are received:2026-01-13 03:35:22.456 [INFO ] [openhab.event.ItemCommandEvent] - Item 'PythonSpeaker' received command say:Hello world
No Audio Data: WebSocket never sends binary audio data
No Errors: No error messages in OpenHAB logs
Silent Connection: WebSocket stays open but no data transmission
Python Output:
Connecting to ws://192.168.1.100:8080/audio/PythonSpeaker.stream
✅ WebSocket connected!
Sent command: say:Hello from OpenHAB
⏱️ No data received...
⏱️ No data received...
... (continues with no audio data)
Questions
- Is the audio WebSocket feature enabled by default? Do I need to install additional components?
- What’s the correct item configuration? Should I be using Things or just Items?
- How do I trigger audio streaming?
· Should audio automatically stream when a client connects?
· Do I need to send a specific command format?
· Is there a REST API endpoint to trigger audio? - Text-to-Speech Requirements:
· Do I need a TTS service configured?
· Which TTS engines work with the audio binding?
· Any additional configuration needed? - Audio Format:
· What’s the default audio format (sample rate, channels, encoding)?
· Can I specify the format in the WebSocket URL?
· Should I expect binary PCM or encoded audio? - Debugging:
· How can I verify the audio binding is working?
· Any specific logs to enable?
· How to test without writing a full client?
What I Need
- A working example of:
· Correct OpenHAB item/thing configuration
· Python/ESP32 code that successfully receives audio
· Command format to trigger audio playback - Understanding of:
· The complete data flow from command to WebSocket
· Required dependencies/services
· Common pitfalls and solutions
Additional Info
· I have the transform and voice addons installed
· HTTP/WebSocket ports are open and accessible
· Other bindings (HTTP, MQTT) work correctly
· I can play audio through the OpenHAB UI speakers successfully
Any help would be greatly appreciated! I’ve been stuck on this for a while and the documentation is sparse on this specific feature.