Script to increment variable with timing?

Posted on
Fri Nov 25, 2016 5:29 pm
Different Computers offline
User avatar
Posts: 2533
Joined: Jan 02, 2016
Location: East Coast

Script to increment variable with timing?

Because my lux sensor sucks, I'm trying to create a schedule that gradually increments a variable called "darkness" gradually.

No way to do that simply with a schedule because schedules are either at x time or repeating every N but not both, so I figure I'll have a schedule kick off a script. Ideally, it would be one where I could plug in how often it changes the variable by how much.

I could pound at this in AppleScript for a day, but as we're supposed to be moving to python AND I realize that this script would run for an hour or so and if the AppleScript had a problem it might hang lots of processes, I thought I'd ask how I might do this in Python.

I found this, but it is not exactly what I want and it's way over my head anyway. Thanks in advance!

SmartThings refugee, so happy to be on Indigo. Monterey on a base M1 Mini w/Harmony Hub, Hue, DomoPad, Dynamic URL, Device Extensions, HomeKitLink, Grafana, Plex, uniFAP, Fantastic Weather, Nanoleaf, LED Simple Effects, Bond Home, Camect.

Posted on
Fri Nov 25, 2016 6:30 pm
DaveL17 offline
User avatar
Posts: 6742
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Script to increment variable with timing?

Something like this?

Code: Select all
from time import sleep

start_counter = 0
max_counter = 500
increment = 100

indigo.variable.updateValue(VARIABLE_ID, str(start_counter))

for n in range(start_counter, max_counter + increment, increment):
    indigo.variable.updateValue(VARIABLE_ID, str(n))
    sleep(1)

You'll want to run it as an external script--especially if its run time will be greater than 10 seconds. Sleep is in seconds.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sat Nov 26, 2016 9:08 am
Different Computers offline
User avatar
Posts: 2533
Joined: Jan 02, 2016
Location: East Coast

Re: Script to increment variable with timing?

Let me make sure I understand:

All time values are seconds?
There's no assumption that the variable is already any particular value? (not a problem, easy enough to make it 0 to start.)
It's incrementing by 100 in that example?
max_counter is the number of seconds, or is it the maximum variable value?

In any case, thanks very much!

Edit: just tried it on a test variable, and that variable immediately goes to 500 and nothing else happens--unless the increment time is larger than about 4 minutes.

SmartThings refugee, so happy to be on Indigo. Monterey on a base M1 Mini w/Harmony Hub, Hue, DomoPad, Dynamic URL, Device Extensions, HomeKitLink, Grafana, Plex, uniFAP, Fantastic Weather, Nanoleaf, LED Simple Effects, Bond Home, Camect.

Posted on
Sat Nov 26, 2016 2:07 pm
DaveL17 offline
User avatar
Posts: 6742
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Script to increment variable with timing?

Different Computers wrote:
Let me make sure I understand:

All time values are seconds?
There's no assumption that the variable is already any particular value? (not a problem, easy enough to make it 0 to start.)
It's incrementing by 100 in that example?
max_counter is the number of seconds, or is it the maximum variable value?

In any case, thanks very much!

Edit: just tried it on a test variable, and that variable immediately goes to 500 and nothing else happens--unless the increment time is larger than about 4 minutes.

Only the sleep time is in seconds. Set that to the number of seconds you want between increments.

The start_counter, max_counter and increment value are all things that you can set to anything you like.

Lastly, save the script to a file and then link to it. That will keep it from appearing to run straight to the end. If you run it as an embedded script, Indigo won't update the UI until it's completely finished (and if it runs over 10 seconds, Indigo will kill it).

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Nov 27, 2016 12:08 pm
Different Computers offline
User avatar
Posts: 2533
Joined: Jan 02, 2016
Location: East Coast

Re: Script to increment variable with timing?

I think I get it.

So if I want to start at a darkness of 0, increase it to 100, and have it take 100 minutes to do so increasing by 1/minute, the script should be:
Code: Select all
from time import sleep

start_counter = 0
max_counter = 100
increment = 1

indigo.variable.updateValue(77395621, str(start_counter))

for n in range(start_counter, max_counter + increment, increment):
    indigo.variable.updateValue(77395621, str(n))
    sleep(60)


Right?

SmartThings refugee, so happy to be on Indigo. Monterey on a base M1 Mini w/Harmony Hub, Hue, DomoPad, Dynamic URL, Device Extensions, HomeKitLink, Grafana, Plex, uniFAP, Fantastic Weather, Nanoleaf, LED Simple Effects, Bond Home, Camect.

Posted on
Sun Nov 27, 2016 1:59 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Script to increment variable with timing?

Different Computers wrote:
I think I get it.

So if I want to start at a darkness of 0, increase it to 100, and have it take 100 minutes to do so increasing by 1/minute, the script should be:
Code: Select all
from time import sleep

start_counter = 0
max_counter = 100
increment = 1

indigo.variable.updateValue(77395621, str(start_counter))

for n in range(start_counter, max_counter + increment, increment):
    indigo.variable.updateValue(77395621, str(n))
    sleep(60)


Right?


Pretty much, but the first indigo.variable.updateValue() line is redundant. It's going to get called with value = "0", but then the one inside the loop is going to get called with the same value immediately after.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Nov 27, 2016 2:08 pm
DaveL17 offline
User avatar
Posts: 6742
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Script to increment variable with timing?

Joe's right. I had started with a different construction and should have deleted that first one.

This is all you need.
Code: Select all
from time import sleep

start_counter = 0
max_counter = 500
increment = 100

for n in range(start_counter, max_counter + increment, increment):
    indigo.variable.updateValue(VARIABLE_ID, str(n))
    sleep(1)

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Nov 27, 2016 2:16 pm
Different Computers offline
User avatar
Posts: 2533
Joined: Jan 02, 2016
Location: East Coast

Re: Script to increment variable with timing?

Even with the redundancy, it's working. I'll update the script, but all seems well. Again, many thanks!

SmartThings refugee, so happy to be on Indigo. Monterey on a base M1 Mini w/Harmony Hub, Hue, DomoPad, Dynamic URL, Device Extensions, HomeKitLink, Grafana, Plex, uniFAP, Fantastic Weather, Nanoleaf, LED Simple Effects, Bond Home, Camect.

Posted on
Sun Nov 27, 2016 9:05 pm
Different Computers offline
User avatar
Posts: 2533
Joined: Jan 02, 2016
Location: East Coast

Re: Script to increment variable with timing?

So I also tried to change this so it would dim lights at sunrise:
Code: Select all
from time import sleep

start_counter = 99
max_counter = 100
increment = 1

indigo.variable.updateValue(411448516, str(start_counter))

for n in range(start_counter, max_counter - increment, increment):
    indigo.variable.updateValue(411448516, str(n))
    sleep(60)
But sadly, this doesn't work. What am I missing?

SmartThings refugee, so happy to be on Indigo. Monterey on a base M1 Mini w/Harmony Hub, Hue, DomoPad, Dynamic URL, Device Extensions, HomeKitLink, Grafana, Plex, uniFAP, Fantastic Weather, Nanoleaf, LED Simple Effects, Bond Home, Camect.

Posted on
Sun Nov 27, 2016 9:09 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Script to increment variable with timing?

Different Computers wrote:
So I also tried to change this so it would dim lights at sunrise:
But sadly, this doesn't work. What am I missing?


Code: Select all
from time import sleep

start_counter = 99
min_counter = 0
increment = -1

for n in range(start_counter, min_counter, increment):
    indigo.variable.updateValue(411448516, str(n))
    sleep(60)

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Nov 27, 2016 9:13 pm
Different Computers offline
User avatar
Posts: 2533
Joined: Jan 02, 2016
Location: East Coast

Re: Script to increment variable with timing?

Checking it now... and it works! Thanks!

SmartThings refugee, so happy to be on Indigo. Monterey on a base M1 Mini w/Harmony Hub, Hue, DomoPad, Dynamic URL, Device Extensions, HomeKitLink, Grafana, Plex, uniFAP, Fantastic Weather, Nanoleaf, LED Simple Effects, Bond Home, Camect.

Posted on
Mon Nov 28, 2016 2:04 pm
Different Computers offline
User avatar
Posts: 2533
Joined: Jan 02, 2016
Location: East Coast

Re: Script to increment variable with timing?

I'd like to thank Dave and FlyingDiver again, as these scripts are working great at what I had in mind. And I can imagine I'll have other repurposed use for these in the future.

SmartThings refugee, so happy to be on Indigo. Monterey on a base M1 Mini w/Harmony Hub, Hue, DomoPad, Dynamic URL, Device Extensions, HomeKitLink, Grafana, Plex, uniFAP, Fantastic Weather, Nanoleaf, LED Simple Effects, Bond Home, Camect.

Posted on
Mon Nov 28, 2016 2:07 pm
DaveL17 offline
User avatar
Posts: 6742
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Script to increment variable with timing?

Glad I could help.


Sent from my iPhone using Tapatalk

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Mon Nov 28, 2016 2:14 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Script to increment variable with timing?

Ditto.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Thu Jul 27, 2017 6:08 pm
Different Computers offline
User avatar
Posts: 2533
Joined: Jan 02, 2016
Location: East Coast

Re: Script to increment variable with timing?

Revisiting this because I just got a new Luminance sensor, so I can use the script again.

Still using
Code: Select all
# Calculate the new value originally set at darknessValue = (sourceDevice.sensorValue-1000) * -0.1

darknessValue = (sourceDevice.sensorValue-1000) * -0.1

# Set the value of the destination variable
indigo.variable.updateValue(1021324478, value=str(darknessValue))


But this now gives me values to 3 decimal places, which the hue plugin does not know how to handle, dropping this error:
Code: Select all
   Hue Lights Error                Brightness value "91.839" specified in variable "darkness" for device "Bar Light Strip" is invalid.


How can I round it off in the script? I'm starting to research this, but I worry about breaking the script.

OK, start of research tells me to use the round function, and gives an example of
Code: Select all
answer = str(round(answer, 2))


So do I do
Code: Select all
darknessValue = (round(sourceDevice.sensorValue-1000,2)) * -0.1
?

Hmmmm. I tried this and it compiles and runs, but it doesn't round the number off.
Last edited by Different Computers on Thu Jul 27, 2017 6:17 pm, edited 3 times in total.

SmartThings refugee, so happy to be on Indigo. Monterey on a base M1 Mini w/Harmony Hub, Hue, DomoPad, Dynamic URL, Device Extensions, HomeKitLink, Grafana, Plex, uniFAP, Fantastic Weather, Nanoleaf, LED Simple Effects, Bond Home, Camect.

Who is online

Users browsing this forum: No registered users and 1 guest