Sonos - Detect if source is TV from flow

I was looking for a way to detect from a flow if the source of my Sonos device is the TV
Some of the flaky workarounds I tried where:

  • LG webos app, is on gives false positives
  • Powerplug

And currently I use the following homescript that is executed every second and will check if the Sonos speaker is playing but the length of the track is null, the result is stored in a homey variable which is then pickedup in a different flow

Hopefully this helps someone or someone has a better solution :slight_smile:

async function sourceIsTvForSonos(name) {
  const devices = await Homey.devices.getDevices();
  for (const device of Object.values(devices)) {
    if (device.name == name && device.class === "speaker") {
      const result = await Homey.devices.getDevice({id: device.id});
      const isPlaying = result.capabilitiesObj?.speaker_playing.value;
      const position = result.capabilitiesObj?.speaker_position.value;
      return (isPlaying == true && position == null);
    }
  }
  return false;
}

async function setVariable(name, value) {
  const variables = await Homey.logic.getVariables();
  for (const variable of Object.values(variables)) {
    if (variable.name == name) {
      await Homey.logic.updateVariable({id: variable.id, variable: {"value": value}});
    }
  }
}

let result = await sourceIsTvForSonos("Woonkamer");
await setVariable("isTvOn", result);
1 Like

Increment #2 I now have GitHub - jishi/node-sonos-http-api: An HTTP API bridge for Sonos easing automation. Hostable on any node.js capable device, like a raspberry pi or similar. running in docker with a custom script and this finally runs stable

const json = JSON.parse(args[0]);
const playingType = json.playbackState == "PLAYING";
const trackType = json.currentTrack.type == "line_in";

return playingType && trackType;