Go Back   Sonos Forums > The Unsupported Area

Reply
 
Thread Tools Display Modes
  #1  
Old Jun 20th, 2012, 12:51 PM
jishi jishi is offline
Member
 
Join Date: Nov 2010
Location: Stockholm, Sweden
Posts: 494
Default Automatically start players when arriving home using Android-phone and on{X}

Microsoft has released an Android application for automation using javascript which is a pretty cool idea.

I just tried it out and wanted to share my simple idea to start my Sonos player as soon as I get WiFi connection (I know there was a question regarding starting it when you get home before on the forum).

This is a simple script that would just try to start all players that you add to the script manually (into the players array):

Code:
// Players that you want to autostart 
// Players that are grouped, or contains no queue or paused item will not be affected
var players = [
    "192.168.1.151",
    "192.168.1.152",
    "192.168.1.154"
];

// Between which hours on the day we should try to autostart the players
// Example below, if time is between 17:00-19:59
var startHour = 17;
var endHour = 20;

// SSID of your home network. This is case sensitive!
// Since 0.60 or something, we now get SSID qouted in the string which is a breaking change.
// This might change again.
var ssid = "'\"JS 5G\"";


////////// DO NOT MODIFY BELOW UNLESS YOU KNOW WHAT YOU ARE DOING

function invokePlay() {    
    for (var i in players) {
        var ip = players[i];
        device.ajax({            
                url: 'http://'+ip+':1400/MediaRenderer/AVTransport/Control',
                type: 'POST',
                headers: {
                    'CONTENT-TYPE': 'text/xml; charset="utf-8"',
                    'CONNECTION': 'close',
                    'SOAPACTION': '"urn:schemas-upnp-org:service:AVTransport:1#Play"'
                    
                },
                data: '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:Play xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID><Speed>1</Speed></u:Play></s:Body></s:Envelope>'
        });
    }
}


device.network.on("wifiOn", function (ns) {    
    var now = new Date();
    var hour = now.getHours();
    
    var lastStarted = device.localStorage.getItem('lastStarted') || 0;
    
    lastStartedDate = new Date(lastStarted);
    
    var lastStartedSince = (now.valueOf() - lastStarted) / 1000;
    console.log(now.toUTCString(), lastStartedDate.toUTCString(), lastStartedSince);
    
    if (ns.ssid != ssid) {
        console.log("won't try to start since not on home network", ssid, ns.ssid);
        return;
    }
    
    
    // If it's between defined hours, and last time we started it was more than 20 hours ago
    if (hour < startHour || hour >= endHour || lastStartedSince < 72000){
        console.log("Not applicable to start players because of time interval", now.toUTCString(), hour, lastStartedSince);
        return;        
    } 
    
    device.notifications.createNotification('Starting players...').show();
    console.log('invoking wifiOn');
    // To prevent another autostart in the next 20 hours
    device.localStorage.setItem('lastStarted', now.valueOf());
    invokePlay();
});
Just replace the IP-addresses with IPs to your coordinator players that you want to start up.

Since version 0.55 of on{X}, they now include the SSID of your network when notifying about wifi connection. This means that you can limit it to your home network. I updated the script to reflect that.

Have any other suggestions for automation? Let's share ideas!

Cheers, Jimmy

Last edited by jishi; Feb 7th, 2013 at 02:13 AM. Reason: since version 0.60 you get SSID quoted for some reason.
Reply With Quote
  #2  
Old Jun 21st, 2012, 09:42 AM
buzz buzz is online now
Moderator
 
Join Date: Feb 2005
Location: US
Posts: 12,329
Default

jishi,

Great idea.

Rather than scare regular users, we should keep this sort of discussion in The Unsupported Area.

And, for the less than hard core hacker, I'll mention that this scheme is best coupled with DHCP IP address reservation in the router.
Reply With Quote
  #3  
Old Jul 18th, 2012, 05:05 AM
goran goran is offline
Member
 
Join Date: May 2012
Posts: 7
Smile fantastic

This is exactly what I was looking for, thank you!
will try this out tonight.. my original idea was to use NFC tags to start sonos playback when I get home, but with this you don't even have to scan the tag, it should happen automatically as soon as you connect to your home SSID. I think I saw some recipes on the on{x] where people are checking the SSID you have connected to, so it looks like it should be possible
Goran
Reply With Quote
  #4  
Old Jul 21st, 2012, 12:48 PM
jishi jishi is offline
Member
 
Join Date: Nov 2010
Location: Stockholm, Sweden
Posts: 494
Default

I updated the script with the following:

Time limitation (only try to start players between 17-20 / 5pm and 8pm), this is easily changeable.

Prevent it from trying to autostart several times during the evening (in the event of signal loss). It will only try once every 20h, which basically means once every day (when you get home from work!)
Reply With Quote
  #5  
Old Jul 22nd, 2012, 05:25 AM
jishi jishi is offline
Member
 
Join Date: Nov 2010
Location: Stockholm, Sweden
Posts: 494
Default

hm, I tried updating the title for the thread, but it only updated the title for the first post.

Guess it has to be done by a moderator? Can anyone assist me with that?
Reply With Quote
  #6  
Old Jul 22nd, 2012, 06:02 AM
buzz buzz is online now
Moderator
 
Join Date: Feb 2005
Location: US
Posts: 12,329
Default

Done.
Reply With Quote
  #7  
Old Aug 1st, 2012, 02:14 PM
jishi jishi is offline
Member
 
Join Date: Nov 2010
Location: Stockholm, Sweden
Posts: 494
Default

Since the last update, they now give you which SSID you have connected to, this gives you better control of when to actually try to start the players (only on your home network).

I updated the script to include this.
Reply With Quote
  #8  
Old Aug 8th, 2012, 12:51 AM
jishi jishi is offline
Member
 
Join Date: Nov 2010
Location: Stockholm, Sweden
Posts: 494
Default

Made a mistake in the SSID-check. Updated the script.
Reply With Quote
  #9  
Old Aug 8th, 2012, 04:37 AM
agb007 agb007 is offline
Member
 
Join Date: Nov 2011
Location: Göteborg, Sverige
Posts: 77
Default

I'm not an Android user but I love the idea of this little script.
So little but so cool!

Gött jobbat
agron
__________________
Son's room: Philips SLA-5520 (audio) Zyxel (video)
Kitchen: Logitech Squeezebox Radio
Livingroom: Sonos Connect:amp
Philips SLM-5500 (video), eeebox server
New Part: Sonos Play 3, Sonos Dock
Reply With Quote
  #10  
Old Sep 3rd, 2012, 01:58 AM
jishi jishi is offline
Member
 
Join Date: Nov 2010
Location: Stockholm, Sweden
Posts: 494
Default

I realized that my check for previous attempts to start players wasn't working, since you can't store objects in the localStorage.

It should work better with the updated version.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 12:00 PM.