Page 1 of 1
How read Sonos data every minute
Posted: Monday 13 December 2021 19:53
by Johan1974
Hello,
How can I read the status of my sonos every minute in a dzvents script, and make the values visible in a switch.?
I read the data from the sonos with '
http://192.168.1.21:5005/state' and get the following data:
Code: Select all
{"volume":15,"mute":false,"equalizer":{"bass":0,"treble":4,"loudness":true,"speechEnhancement":false,"nightMode":false},"currentTrack":{"artist":"Radio 10","title":"Rod Stewart - Da Ya Think I'm Sexy","albumArtUri":"/getaa?s=1&u=x-sonosapi-stream%3atunein%253a7073%3fsid%3d303%26flags%3d8224%26sn%3d1","duration":0,"uri":"x-sonosapi-stream:tunein%3a7073?sid=303&flags=8224&sn=1","trackUri":"x-sonosapi-stream:tunein%3a7073?sid=303&flags=8224&sn=1","type":"radio","stationName":"Radio 10","absoluteAlbumArtUri":"http://192.168.1.156:1400/getaa?s=1&u=x-sonosapi-stream%3atunein%253a7073%3fsid%3d303%26flags%3d8224%26sn%3d1"},"nextTrack":{"artist":"","title":"","album":"","albumArtUri":"","duration":0,"uri":""},"trackNo":1,"elapsedTime":259,"elapsedTimeFormatted":"00:04:19","playbackState":"PLAYING","playMode":{"repeat":"none","shuffle":false,"crossfade":false},"sub":{"gain":-4,"crossover":0,"polarity":0,"enabled":true}}
And for example, if then stationName = "Radio 10" the dummy selector with the name "Station" is set to 20
Re: How read Sonos data every minute
Posted: Monday 13 December 2021 20:13
by waltervl
Why would you like to have it in a switch? Is a text sensor not easier to handle? Just put the info in text and ready...
There is a Sonos Python plugin (no longer maintained) but should still work. I am not sure it does what you want.
viewtopic.php?f=65&t=18710
Re: How read Sonos data every minute
Posted: Monday 13 December 2021 20:51
by waltervl
To put the text into a text sensor use the following script:
Code: Select all
return {
on = {
timer = {
'every minute' -- just an example to trigger the request
},
httpResponses = {
'Sonostrigger' -- must match with the callback passed to the openURL command
}
},
logging = {
level = domoticz.LOG_INFO,
marker = 'template',
},
execute = function(domoticz, item)
if (item.isTimer) then
domoticz.openURL({
url = 'http://192.168.1.21:5005/state',
method = 'GET',
callback = 'Sonostrigger', -- see httpResponses above.
})
end
if (item.isHTTPResponse) then
if (item.ok) then
if (item.isJSON) then
-- local Testdata = '{"volume":15,"mute":false,"equalizer":{"bass":0,"treble":4,"loudness":true,"speechEnhancement":false,"nightMode":false},"currentTrack":{"artist":"Radio 10","title":"Rod Stewart - Da Ya Think Im Sexy","albumArtUri":"/getaa?s=1&u=x-sonosapi-stream%3atunein%253a7073%3fsid%3d303%26flags%3d8224%26sn%3d1","duration":0,"uri":"x-sonosapi-stream:tunein%3a7073?sid=303&flags=8224&sn=1","trackUri":"x-sonosapi-stream:tunein%3a7073?sid=303&flags=8224&sn=1","type":"radio","stationName":"Radio 10","absoluteAlbumArtUri":"http://192.168.1.156:1400/getaa?s=1&u=x-sonosapi-stream%3atunein%253a7073%3fsid%3d303%26flags%3d8224%26sn%3d1"},"nextTrack":{"artist":"","title":"","album":"","albumArtUri":"","duration":0,"uri":""},"trackNo":1,"elapsedTime":259,"elapsedTimeFormatted":"00:04:19","playbackState":"PLAYING","playMode":{"repeat":"none","shuffle":false,"crossfade":false},"sub":{"gain":-4,"crossover":0,"polarity":0,"enabled":true}}'
-- local rt = domoticz.utils.fromJSON(Testdata)
-- local someValue = rt.currentTrack.artist -- just an example
local someValue = item.json.currentTrack.stationName
-- update some device in Domoticz
domoticz.devices('MyTextDevice').updateText(someValue)
domoticz.log('Text: ' .. someValue , domoticz.LOG_INFO)
end
else
domoticz.log('There was a problem handling the request', domoticz.LOG_ERROR)
domoticz.log(item, domoticz.LOG_ERROR)
end
end
end
}
Re: How read Sonos data every minute
Posted: Tuesday 14 December 2021 20:40
by Johan1974
I made a Text dummy called 'SonosTest'
The text that becomes visible in the dummy is "nil" and not the value of the volume..
Code: Select all
domoticz.devices('SonosTest').updateText(volume)
domoticz.log('Text: ' .. someValue , domoticz.LOG_INFO)
Re: How read Sonos data every minute
Posted: Wednesday 15 December 2021 0:42
by waltervl
You cannot put volume directly in the update string
You have to read it first from the Json
Eg
local someValue = item.json.volume
Re: How read Sonos data every minute
Posted: Wednesday 15 December 2021 19:19
by Johan1974
I changed everything, but I still get the text 'nil' in the dummy.
Code: Select all
return {
on = {
timer = {
'every minute' -- just an example to trigger the request
},
httpResponses = {
'Sonostrigger' -- must match with the callback passed to the openURL command
}
},
logging = {
level = domoticz.LOG_INFO,
marker = 'template',
},
execute = function(domoticz, item)
if (item.isTimer) then
domoticz.openURL({
url = 'http://192.168.1.21:5005/state',
method = 'GET',
callback = 'Sonostrigger', -- see httpResponses above.
})
end
if (item.isHTTPResponse) then
if (item.ok) then
if (item.isJSON) then
-- local Testdata = '{"volume":15,"mute":false,"equalizer":{"bass":0,"treble":4,"loudness":true,"speechEnhancement":false,"nightMode":false},"currentTrack":{"artist":"Radio 10","title":"Rod Stewart - Da Ya Think Im Sexy","albumArtUri":"/getaa?s=1&u=x-sonosapi-stream%3atunein%253a7073%3fsid%3d303%26flags%3d8224%26sn%3d1","duration":0,"uri":"x-sonosapi-stream:tunein%3a7073?sid=303&flags=8224&sn=1","trackUri":"x-sonosapi-stream:tunein%3a7073?sid=303&flags=8224&sn=1","type":"radio","stationName":"Radio 10","absoluteAlbumArtUri":"http://192.168.1.156:1400/getaa?s=1&u=x-sonosapi-stream%3atunein%253a7073%3fsid%3d303%26flags%3d8224%26sn%3d1"},"nextTrack":{"artist":"","title":"","album":"","albumArtUri":"","duration":0,"uri":""},"trackNo":1,"elapsedTime":259,"elapsedTimeFormatted":"00:04:19","playbackState":"PLAYING","playMode":{"repeat":"none","shuffle":false,"crossfade":false},"sub":{"gain":-4,"crossover":0,"polarity":0,"enabled":true}}'
-- local rt = domoticz.utils.fromJSON(Testdata)
-- local someValue = rt.currentTrack.artist -- just an example
local someValue = item.json.volume
-- update some device in Domoticz
domoticz.devices('SonosTest').updateText(volume)
domoticz.log('Text: ' .. someValue , domoticz.LOG_INFO)
end
else
domoticz.log('There was a problem handling the request', domoticz.LOG_ERROR)
domoticz.log(item, domoticz.LOG_ERROR)
end
end
end
}
Re: How read Sonos data every minute
Posted: Thursday 16 December 2021 1:27
by waltervl
Change line
Code: Select all
domoticz.devices('SonosTest').updateText(volume)
Into
Code: Select all
domoticz.devices('SonosTest').updateText(someValue)