Sonos TimePosition/CounterPosition Variables when using UPnP Proxy

Just purchased a SONOS and hooked up the super impressive Sonos Plugin! I went ahead and configured the uPnP Event Proxy to provide instant feedback. I’d like to push the Sonos currently playing metadata to an external display that also tracks a current track’s position. I’ve got this working when a song plays, but when a user pauses or manually changes the position of the track, I need to know what the track position is.

I only need to send the postion at play time (0h00m00s), at pause time (don’t know how to get where the song is currently at).

I know that there are some variables on the Sonos Plug that are not supported when the uPnP Event Proxy is being used. I’m guessing that one of these could help in my use case if I weren’t using the proxy. However, I’d like to use the proxy for its many other benefits.
RelativeTimePosition
AbsoluteTimePosition
RelativeCounterPosition
AbsoluteCounterPosition

Question 1:
Is there something in the Sonos plug where I could request a tracks current position or is there a way that I could make the uPnP call to get a track’s current position?

Question 2:
For non Play/Pause events, how could I determine that a track’s position was manually changed? Is there an event or a variable that I could watch or alternatively a uPnP call?

Any thoughts or suggestions would be appreciated!

Thanks!

I never used it but you have the event “playing state” that you can use to trigger a Vera scene when pause is requested for example.

Regarding the position in a track, one solution is to call the action Pool (service urn:micasaverde-com:serviceId:HaDevice1). It will update all variables including RelativeTimePosition, AbsoluteTimePosition, RelativeCounterPosition and AbsoluteCounterPosition. It is equivalent to what is called every 15 seconds when the event proxy is not used.

I could even create a new action that you could request when needed. This action will do the necessary UPnP request(s) and fill only the four variables. It will avoid to make a lot of UPnP calls just to get position in a track.

I have not yet studied the case, but maybe I can get the information through a notification when the position is changed by the user. I will check that.

Thanks for the quick response!

I was originally triggering off of the events related to “playing state” such as "Device is Playing/Stopped/Paused which worked well. However, through the events I couldn’t figure out how to the determine when the currently playing track changed so I switched to watching variables. Originally I thought this might have been through the “Device is Transitioning” event, but that didn’t seem to work for me as I had hoped.

I’m currently keying off of (service urn:“urn:upnp-org:serviceId:AVTransport”, “TransportState”) to get the PLAYING/PAUSED_PLAYBACK/STOPPED events and that’s working well for me. Creating a new action that I could request when needed to get the current timings would be an exteremly helpful addition to what I’m doing!

There’s a third time I would also need to get the current timings that I realized after my last post I hadn’t mentioned, but you alluded to. Do you have any ideas on how to sense when the position is changed by the user? I’d need some variable to watch or some kind of event I could key off of. I was looking through the Vera logs and did see that when I manually change the position of a track in Sonos, I see an entry in the logs that has quite a bit of information. It starts off with “JobHandler_LuaUPnP::HandleActionRequest”.

What I have done is to get time position each time I get a notification. So when a track change or the user changes position in the track, the plugin is notified and I update time positions.
As a conclusion, if you “watch” the variable, you should see updates.
But of course, the variable is not modified every x seconds.

New code is not yet committed.

When you receive updates from Sonos over uPnP, is that a type of subscription? Are you setting it up to send you additional time information during those updates or do you in turn make queries to get updated time information? I’m just curious on how this works.

Looking forward to these updates. This should really enhance my use case. Appreciate all the work you’ve done!!!

The subscription to notifications is by UPnP service. The plugin is subscribing to few events to get informed of different kind of information (rendering - mainly volume, network and group topology, A/V transport - what is played by the Sonos, “Queues”, …),
Currently, time information is not attached to the events I subscribe to. So I make a query to retrieve the values, each time I receive a notification relative to the A/V transport. When the user changes time position, I am notified with this event.
There may be a proper way to do it.

@lolodomo - Thanks for adding code to support my use case! I installed beta 1-122 as referenced in:
[url=http://forum.micasaverde.com/index.php?topic=16839]http://forum.micasaverde.com/index.php?topic=16839[/url]

I then set up variable watches for:

luup.variable_watch('songChangeEvent', "urn:upnp-org:serviceId:AVTransport", "RelativeTimePosition", 276)
luup.variable_watch('songChangeEvent', "urn:upnp-org:serviceId:AVTransport", "AbsoluteTimePosition", 276)
luup.variable_watch('songChangeEvent', "urn:upnp-org:serviceId:AVTransport", "RelativeCounterPosition", 276)
luup.variable_watch('songChangeEvent', "urn:upnp-org:serviceId:AVTransport", "AbsoluteCounterPosition", 276)
luup.variable_watch('songChangeEvent', "urn:upnp-org:serviceId:AVTransport", "TransportState", 276)

function songChangeEvent(lul_device, lul_service, lul_variable, lul_value_old, lul_value_new)

luup.log("SONOS : Current Status Callback EVENT ****", 1)

luup.log("lul_device : " .. lul_device, 1)
luup.log("lul_service : " .. lul_service, 1)
luup.log("lul_variable : " .. lul_variable, 1)
luup.log("lul_value_old : " .. lul_value_old, 1)
luup.log("lul_value_new : " .. lul_value_new, 1)
end

In my limited testing, I only saw an event from the “RelativeCounterPosition” which did trigger when changes were made to the position of the track! For my use case, this seems to meet my needs. I never saw any changes to the other variables and honestly was never sure about the differences in them: “RelativeTimePosition”, “AbsoluteTimePosition”, “AbsoluteCounterPosition”. I’m just pointing this out in case that doesn’t seem correct.

I’ve discovered a different challenge for my use case, I’m not sure if it was introduced the latest version. When I watch the variable “TransportState” to tell me if playback has “STOPPED”, “PLAYING”, or “PAUSED_PLAYBACK” I’m not able to retrieve the “CurrentTrackDuration”. At the time I read “CurrentTrackDuration”, it returns “NOT_IMPLEMENTED”. I’m wondering if this is a timing issue on my part or if you may have changed some code so this variable is not set when the “TransportState” variable change occurs.

Any thoughts would be appreciated. Once again thanks for all your effort!

I cannot answer without testing.

Here is how the standard UPnP AV defines these state variables:

2.2.22.RelativeTimePosition This state variable contains the current position, in terms of time, from the beginning of the current track. For tape-based media that do not support multiple tracks on a single tape, this state variable contains the position, in terms of time, from a zero reference point on the tape. The time format is the same as for state variable CurrentTrackDuration. If the service implementation doesn?t support relative time-based position information then this state variable must be set to value ?NOT_IMPLEMENTED?. 2.2.23.AbsoluteTimePosition This state variable contains the current position, in terms of a time, from the beginning of the media. The time format is the same as for state variable CurrentTrackDuration. If the service implementation doesn?t support any kind of position information then this state variable must be set to value ?NOT_IMPLEMENTED?. Devices that don?t have time position information, but which are able to detect whether they are at the end of the media or not should use special value ?END_OF_MEDIA? when actually at the end, and ?NOT_IMPLEMENTED? otherwise. 2.2.24.RelativeCounterPosition This state variable contains the current position, in terms of a dimensionless counter, from the beginning of the current track. For tape-based media that do not support multiple tracks on a single tape, this state variable contains the position, in terms of a dimensionless counter, from a zero reference point on the tape. If the service implementation doesn?t support relative count-based position information then this state variable must be set to the maximum value of the i4 data type. 2.2.25.AbsoluteCounterPosition This state variable contains the current position, in terms of a dimensionless counter, from the beginning of the loaded media. If the service implementation doesn?t support absolute count-based position information then this state variable must be set to the maximum value of the i4 data type.

“dimensionless counter” is not clear for me.

By the way, if we come back to Sonos, it looks like that AbsoluteTimePosition and AbsoluteCounterPosition are not implemented.
And it looks like RelativeTimePosition and RelativeCounterPosition have always the same value, a time duration format like 0:00:00 for example.

So I would propose to suppress variables AbsoluteTimePosition, AbsoluteCounterPosition and RelativeCounterPosition from Vera devices.

“CurrentTrackDuration” seems to be correctly updated.
Maybe it is not implemented for certain kind of media. For TuneIn radio or Web radio, I got 0:00:00 for example.
You got NOT_IMPLEMENTED with what service ?

I will restore the GetPositionInfo action. That way, you could ask for fresh data in the variable RelativeTimePosition when you want.

“CurrentTrackDuration” seems to be correctly updated.
Maybe it is not implemented for certain kind of media. For TuneIn radio or Web radio, I got 0:00:00 for example.
You got NOT_IMPLEMENTED with what service ?
[/quote]

Thanks for looking into this. I’ll have to go back and rerun my tests on different sources and get back to you. I like having a service to call to get an update if necessary and agree that if Sonos is not updating some values it makes things less confusing to not have them.

With all the references to tape machines in the uPnP spec, I’m wondering if “dimensionless counter” is one of those old style four digit mechanical counters like “0000” that could be reset at any time?

[quote=“hifs, post:12, topic:176735”]

“CurrentTrackDuration” seems to be correctly updated.
Maybe it is not implemented for certain kind of media. For TuneIn radio or Web radio, I got 0:00:00 for example.
You got NOT_IMPLEMENTED with what service ?

Thanks for looking into this. I’ll have to go back and rerun my tests on different sources and get back to you. I like having a service to call to get an update if necessary and agree that if Sonos is not updating some values it makes things less confusing to not have them.[/quote]

I am sorry but in my code, I first force the value of CurrentTrackDuration to NOT_IMPLEMENTED if I receive a value for CurrentTrackURI in the notification, before setting the value coming from the notification. So if your notification contains CurrentTrackURI but not CurrentTrackDuration, the result will be NOT_IMPLEMENTED.
I don’t remember exactly why. That could be something to fix.