Jump to content
🌟 NOTIFICATION/Benachrichtigung: Welcome to our New Store! - shelly.com 🌟 ×

Using Schedule.Update in a script for multiple Schedule IDs on Gen 2+ Devices


Recommended Posts

Hi,

Apologies if this appears to be covered elsewhere, I did look but could only find this thread.

By way of background I discovered after a long search that Shelly Gen 1 devices had a really nice way to turn off ALL schedules with:

http://192.168.1.xxx/settings/relay/0?schedule=false

which could be used as a simple single line URL called from e.g. a Button Press linked to a Switch.

Unfortunately it seems Gen 2+ devices do not have this global schedule on/off function and referencing the note in the thread above (thank you to the responder), while it works well when posted into a browser or a single schedule ID, does not work as a batch for say 9 IDs (i.e. 9 schedules for one Switch, Central Heating).

http://ip_from_your_plus1pm/rpc/Schedule.Update?id=1&enable=false

for schedule=off

Id= Schedule-ID - 1 for the first....

I tried (I am not clear on the scripting syntax at all, sorry) to make a script (essentially for a batch of IDs) that I could call via a URL, but I am at a loss as to how to write it as a batch script or to run it from a saved URL script (where is it saved to and how do you call it?). I keep getting -3 reference_errors and the like,

http://admin:remote_shelly_pw@192.168.47.127/rpc/Schedule.Update?id=2&enable=false
http://admin:remote_shelly_pw@192.168.47.127/rpc/Schedule.Update?id=3&enable=false
http://admin:remote_shelly_pw@192.168.47.127/rpc/Schedule.Update?id=4&enable=false
http://admin:remote_shelly_pw@192.168.47.127/rpc/Schedule.Update?id=5&enable=false
http://admin:remote_shelly_pw@192.168.47.127/rpc/Schedule.Update?id=6&enable=false
http://admin:remote_shelly_pw@192.168.47.127/rpc/Schedule.Update?id=7&enable=false
http://admin:remote_shelly_pw@192.168.47.127/rpc/Schedule.Update?id=8&enable=false
http://admin:remote_shelly_pw@192.168.47.127/rpc/Schedule.Update?id=9&enable=false
http://admin:remote_shelly_pw@192.168.47.127/rpc/Schedule.Update?id=10&enable=false

I would really appreciate some pointers to get this up and running, I have been reading aa lot of what turne dou to be red herrings over the last week.


I would prefer to stay in the Shelly ecosystem for the moment if it is a simple script, not HA for the moment at least.

Thank you!

Link to comment
Share on other sites

The following should work:

Go to the 'Scripts' tab of your Shelly and click 'Create Script'. Copy and paste the following code into the script window, give it some name in the corresponding field and click 'Save' and 'Start':

function switchSchedules(value) {
  Shelly.call("Schedule.List", {}, function (result) {
    for (let job of result.jobs) {
      job.enable = value;
      Shelly.call("Schedule.Update", job);
    }
  });
}

Then, go back to the Scripts tab and make sure that the text below the script name says 'running'. You should also activate the 'Run on startup' toggle to make sure that the script restarts after a reboot.

You now have a switchSchedules function that can be called with either 'true' (to enable all Schedules) or 'false' (to disable them). You can do that with Script.Eval rpc calls:

http://192.168.xxx.xxx/rpc/Script.Eval?id=1&code=switchSchedules%28true%29  # enables all schedules
http://192.168.xxx.xxx/rpc/Script.Eval?id=1&code=switchSchedules%28false%29 # disables all schedules

Note: the 'id=1' in the URL refers to the internal script number of the Shelly. If you only have one script, that number will be 1 - otherwise, you have to count...

Link to comment
Share on other sites

Posted (edited)

First of all, thank you for taking the time to put this together, it's a very elegant solution, although I am pretty sure I would never have been able to put together that function in a month of Sundays!

I ran it from a browser to test it and it runs with a "pretty-print" result "

{
  "result": "undefined"
}

It works, but seems to only disable the first 4 schedules as shown, then stops?

The script itself stops running and the console says "Too many calls".

I attach the files showing the sequence of operations I followed as you noted above.

I only need it to turn off (and later turn on again) the schedules that turn the relay on (3 of 9), so I was wondering if, to make it less processing (calls) intensive, whether the function could operate ONLY on those ones (that turn it on) rather than attempting all 9?

Final and separate question, %28true%29 (pct28truepct29), is that what I actually write or like %20 is a blank, does %28 and %29 mean something else? I tried quotes "true" but that didn't work.

Thank you again, it's amazing what these things can do, but they do seem a wee bit limited by processing power.

Yup1-Function in Place.jpg

Yup2-Running.jpg

Yup3-Too Many Calls.jpg

Yup4-Does First 4.jpg

Yup5-Script Stops.jpg

Yup6-What is Pct28 and Pct29.jpg

Yup7.jpg

Edited by SmurfnSurf
Link to comment
Share on other sites

Ha, this is what I get for testing it with only two schedules... Let's start with the simple questions:

Getting "undefined" back in the browser means that the call was successful, so this is fine.

%28 and %29 are the left and right parantheses - so 'switchSchedules%28true%29' is exactly the same as 'switchSchedules(true)', which is the JavaScript syntax for calling the function 'switchSchedules' with the parameter 'true'.

Most browsers and tools will actually be fine with using the parantheses directly instead of their %-equivalent, but sometimes this may cause issues - which is why I chose the safe route. If you ever need an overview over the various URL encoding values, you can find a handy table here.

Now to the actual problem: The Shellys have a documented hard limit of 5 simultaneous RPC calls (I'm sure I have read that before, but I have clearly forgotten about it).

In our script, we have one "Schedules.List" call, followed by four successful "Schedules.Update" calls. The fifth "Schedule.Update" exceeds the limit and causes the script to fail.

Getting around this limit in a general way is actually surprisingly hard, mainly because the Shelly has a whole bunch of restrictions in its JavaScript version. So, if you only need three schedules switched, the simplest solution is to specify these schedules in the script. For that, replace the current script code with the following:

function switchSchedules(value) {
  let schedules = [1, 3, 6, 7]; // <-- example: change only the 1st, 3rd, 6th and 7th schedule
  Shelly.call("Schedule.List", {}, function (result) {
    for (let job of result.jobs) {
      if (schedules.indexOf(job.id) !== -1) {
        job.enable = value;
        Shelly.call("Schedule.Update", job);
      }
    }
  });
}

You can define the IDs of the schedules that you want to switch between the square brackets in the second line. You must specifiy at least one and (obviously) not more than four IDs. Don't forget to save and start the script after you have made the change and make sure that it is running.

With that, you can then use the URLs as described above to switch only the defined schedules.

Please let me know if this works and solves your problem.

Link to comment
Share on other sites

Posted (edited)
11 hours ago, towiat said:

Please let me know if this works and solves your problem.

Hi,

RIght, thank you once again for giving it a go and your explanations :-).

Unfortunately it doesn't seem to do anything this time? No schedules (at all) get turned off, see pics for logs?

Can I do anything to help troubleshoot it?

Quick Qs:

How do I actually call the script from a URL (I’m putting this in a Shelly Button 1) “link” later.

Is it just:

http://admin:password@192.168.47.127/rpc/nameofthescriptarethereanyswitches

Not to expand on your generosity, but I have been having a bit of an Authorization issue with it on, I use admin:password@ which worked for some calls but Chrome threw a 401 error on some other calls e.g. these both work in a Browser, all I do is type admin and the PWD in the browser dialogue.

http://192.168.47.127/rpc/Switch.GetConfig?id=0 

http://192.168.47.127/rpc/Schedule.List

But as noted in the separate post, this doesn't seem to work with other URL calls.

https://community.shelly.cloud/topic/2556-authentication-dialogue-box-does-not-work-for-switchsetconfig-change-of-in_mode/#comment-12813

If you know of a quick solution to this it’d be appreciated but not a must, I’ll keep looking.

Ta

Switch01.jpg

Switch02.jpg

Edited by SmurfnSurf
Link to comment
Share on other sites

You still have to call the Script.Eval URLs from the first post to actually trigger the switch (sorry, I thought this was clear from context):

http://192.168.xxx.xxx/rpc/Script.Eval?id=1&code=switchSchedules%28true%29  # enables the defined schedules
http://192.168.xxx.xxx/rpc/Script.Eval?id=1&code=switchSchedules%28false%29 # disables the defined schedules

The only job of the script is to provide the switchSchedule function - it does absolutely nothing unless this function is called via the above URLs.

Regarding authentication: sorry, I have no experience with that feature. I am, however, very certain that the presence of this (or any other) script should have no impact whatsoever on the authentication process - such a behavior would clearly be a bug (and an unfortunate one at that for your specific problem).

Link to comment
Share on other sites

Posted (edited)
47 minutes ago, wooly said:

have you tried to insert a pause in the loop, so asking to wayt 5 seconds between each call ?

I haven't but if you could suggest how I should do that in the script, I can try it!

Edited by SmurfnSurf
Link to comment
Share on other sites

Posted (edited)
2 hours ago, towiat said:

You still have to call the Script.Eval URLs from the first post to actually trigger the switch (sorry, I thought this was clear from context):

http://192.168.xxx.xxx/rpc/Script.Eval?id=1&code=switchSchedules%28true%29  # enables the defined schedules
http://192.168.xxx.xxx/rpc/Script.Eval?id=1&code=switchSchedules%28false%29 # disables the defined schedules

The only job of the script is to provide the switchSchedule function - it does absolutely nothing unless this function is called via the above URLs.

Regarding authentication: sorry, I have no experience with that feature. I am, however, very certain that the presence of this (or any other) script should have no impact whatsoever on the authentication process - such a behavior would clearly be a bug (and an unfortunate one at that for your specific problem).

Oh goodness me, I'm really sorry, totally missed that I needed to issue those, too many hours staring at this little screen!

It totally works, see attached! Thank you :-). I now have a working script it seems, just need to get my head around the authentication issue. 

It would be cool if they could all be disabled / enabled with a 5 sec wait loop, but now that this works, less urgency on that.

Awesome, thanks again.

Bingo.jpg

Done.jpg

Edited by SmurfnSurf
Link to comment
Share on other sites

You're welcome!

Without going into too much detail: You actually can not just wait a few seconds in a JavaScript program. This is intentional and a consequence of the architecture of JavaScript (there are ways around that, but they do not work on a Shelly).

You could use a Timer to delay execution of code, but the Shellys have another limit that says that only five Timers may be active at the same time - so we would just hit that limit instead of the original one.

In general, none of these issues would be a problem in a 'normal' JavaScript environment, but the limited hardware of the Shelly devices obviously requires some compromises (which doesn't change the fact that it is still impressive what these things can do).

Anyway, I'm glad that this is useful and wish you good luck with your other issues!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...