Sonos

Sonos sound system control integration with LogicMachine #

Install Sonos app from the LogicMachine application store.

Another way is to control Sonos via scripting. Below are both examples shown.

Sonos control app #

sonos_hifi img_01

sonos_hifi img_02

sonos_hifi img_03

sonos_hifi img_04

Sonos control functions and examples #

Here is how to use Sonos app via script:

The app runs a daemon which keeps communication with each player/group. The daemon will be closed down after 120 seconds if no update is sent so we need to run this script every 60 seconds to keep communication up and running.

require('custom.sonos.lib')

group_ids = sonos_app.GetStoragePlayerGroups()
now = os.time()

for id, group in pairs(group_ids) do
   sonos_app.SendApiActionCommand(id, 'watchPlayerGroup', now)
end

Find group ID value #

To control the players we need to know the group ID value.

require('custom.sonos.lib')

group_id = sonos_app.GetStoragePlayerGroups()

log(group_id)

Basic control #

Simple commands (play, pause, skipToPrev, skipToNext) can be sent using this script. Provide a valid group_id value.

require('custom.sonos.lib')

group_id = '...'

sonos_app.SendApiActionCommand(group_id, 'play')

Set volume to 50% #

Provide a valid group_id value.

require('custom.sonos.lib')

group_id = '...'
volume = 50

sonos_app.SendApiActionCommand(group_id, 'setVolume', volume)

Control a named group #

The group_id is in the table from the groupID but it can be read automatically when we know the group name so an example start/pause script would look like this.

require('custom.sonos.lib')

group_name = 'Kitchen'

for id, group in pairs(sonos_app.GetStoragePlayerGroups()) do
  if group.groupName == group_name then
    group_id = id
    break
  end
end

if group_id then
  value = event.getvalue()
  -- TRUE = play, FALSE = pause
  cmd = value and 'play' or 'pause'
  sonos_app.SendApiActionCommand(group_id, cmd)
end

Read saved playlist table #

Provide a valid group_id value.

require('custom.sonos.lib')

group_id = '...'

playlists = sonos_app.GetStoragePlayerGroupData(group_id, 'Playlists')
log(playlists)

Play a saved playlist #

Provide a valid group_id and playlist_id values.

require('custom.sonos.lib')

group_id = '...'
playlist_id = '...'

sonos_app.SendApiActionCommand(group_id, 'playPlaylist', playlist_id)

Further assistance can be found in this forum thread