Sonos soundbars have a great features for TV night watching for families. Night mode and Speech enhancement can help you watch movies while kids are sleeping in adjacent room. These features can managed via Sonos app but why should i do it manually when i am building smart home via Homey ? Unluckily this features are not supported in Sonos app for Homey. I reached Athom and they claimed to put it in their backlog but can not guarantee when it will be delivered. So i started to look for alternate solution… and with help from colleague who found Sonos HTTP API, i was able to make it fully automated =) and here you have a short guide how you can do it, till Athom builds this features into Sonos app
What you need?
- device in your local network where you can run Sonos HTTP API Server (can be Windows/Mac/Linux computer, NAS,…)
- new True/False variable in Homey, i am using TVnight
- Homeyscript enabled on your Homey
- CountDown installed on your Homey (Simple Sys Log is optional)
- 2 or 3 new flows as described bellow
Lets start:
1. install Sonos HTTP API server
choose device where you can run the API server and install it (if you have no idea how, use ChatGPT and ask for step by step guide how to install it on your device)
here is what you need for installation: https://github.com/jishi/node-sonos-http-api
In my case i am using my always on Windows PC
2. Setup flow to change the variable
Flow which sets the variable to True in the evening and False in the morning based on required time
In my setup TVnight variable is changed to True every day at 21:30, and changes to False in the morning at 7:00 during working days and 9:00 during days off (i am using isTomorrowOffDay variable to distinguish non/working days)
Flow can be also triggered manually and it swaps the variable value. For all my automated scripts I do create also manual override eg. You have guests and want to enable it sooner, you just run this flow from the mobile app before 21:30, it flips the variable and enables TVnight sooner
3. Flow to execute soundbar features when needed
Simple flow triggered by the variable change or by turning on TV, which checks if the night mode is required and via the Homeyscript turns On or Off both Night mode and Speech Enhancement on the soundbar thanks to local Sonos HTTP API server
and here is the script used in Homeyscript
// TV night mode switch based on TVnight variable
const tvNightVariableName = 'TVnight'; // Variable name to track night mode state
const baseUrl = 'http://192.168.0.208:5005'; // PC IP running Sonos HTTP API
const roomName = 'Living Room';
async function toggleSonosSilentMode() {
// Fetch all logic variables
let allVariables;
try {
allVariables = await Homey.logic.getVariables();
console.log("Fetched all variables successfully.");
} catch (error) {
console.error(`Error fetching all variables: ${error.message}`);
return "Error fetching variables.";
}
// Locate the TVnight variable by name
const tvNightVariable = Object.values(allVariables).find(variable => variable.name === tvNightVariableName);
if (!tvNightVariable) {
console.error(`Variable with name ${tvNightVariableName} not found.`);
return "TVnight variable not found.";
}
// Determine current state of TVnight and set corresponding Sonos settings
const isNightModeEnabled = tvNightVariable.value;
const sonosMode = isNightModeEnabled ? 'on' : 'off';
try {
// Send requests to Sonos API to set nightmode and speechenhancement
await fetch(`${baseUrl}/${encodeURIComponent(roomName)}/nightmode/${sonosMode}`);
await fetch(`${baseUrl}/${encodeURIComponent(roomName)}/speechenhancement/${sonosMode}`);
console.log(`Set nightmode and speechenhancement to ${sonosMode} for ${roomName}.`);
// Return confirmation message
const resultMessage = `TV night mode is ${isNightModeEnabled ? "ON" : "OFF"}`;
console.log(resultMessage);
return resultMessage;
} catch (error) {
console.error(`Error toggling Sonos settings: ${error.message}`);
return "Error toggling Sonos settings.";
}
}
// Execute function and return result
const silentModeToggledMessage = await toggleSonosSilentMode();
console.log(`Result: ${silentModeToggledMessage}`);
return silentModeToggledMessage;
in the script adjust:
row 2 with your own variable (if you copied mine, leave it as it is)
row 3 change localhost to IP of your device where Sonos HTTP API server is running
row 4 change to Sonos zone where your soundbar is located (once you start Sonos HTTP API server you can check your zones on:
http://localhost:5005/zones)
In my case i am checking if TV is on (variable may change when TV is off, flow stops), if Sonos is playing (in case TV is on but headphones are connected, flow stops), and if all true based on variable both features are either turned on or off.
4. OPTIONAL: regularly check your Sonos HTTP API server
This flow is optional in case you want to monitor if the Sonos HTTP API server is running. As you can see in my previous flow, i am logging error if the Homeyscript fails (eg. HTTP server is not responding), but still i do not trust the windows PC to be 100% reliable and want to get notified if the server fails sooner than in execution. With the help of CountDown app, i do regular checks if the Sonos HTTP API server is responding and if not error is triggered to timeline (and i do have some other flow to create push notification if error occurs in the timeline - my custom pattern to handle for me relevant errors)
I hope this helps you enjoy more your TV with Sonos soundbar with automation by Homey.
this is my first how to guide here so feel free to give me feedback, so i can make the next one even better.