How read Sonos data every minute

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
Johan1974
Posts: 37
Joined: Monday 04 December 2017 20:56
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

How read Sonos data every minute

Post 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
User avatar
waltervl
Posts: 6001
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: How read Sonos data every minute

Post 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
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
waltervl
Posts: 6001
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: How read Sonos data every minute

Post 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
}
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Johan1974
Posts: 37
Joined: Monday 04 December 2017 20:56
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: How read Sonos data every minute

Post 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) 
User avatar
waltervl
Posts: 6001
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: How read Sonos data every minute

Post 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
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Johan1974
Posts: 37
Joined: Monday 04 December 2017 20:56
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: How read Sonos data every minute

Post 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
}  
User avatar
waltervl
Posts: 6001
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: How read Sonos data every minute

Post by waltervl »

Change line

Code: Select all

domoticz.devices('SonosTest').updateText(volume)
Into

Code: Select all

domoticz.devices('SonosTest').updateText(someValue)
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest