Fixing a script that used to work

Python scripts to do various things. These would be scripts that talk directly with Indigo (use indigo.* methods).
User avatar
durosity
Posts: 4385
Joined: Thu May 10, 2012 3:21 pm
Location: Newcastle Upon Tyne, Ye Ol' England.

Fixing a script that used to work

Post by durosity »

I have a wee python script that I'm sure used to work, but for whatever reason sometime in the last decade or so it's stopped working and for the life of me I can't work out why.

It's supposed to take the value of how many times Sushi has been fed, convert it to a string, then add it into a string of text that reads 'Fed 1 time, Last feed at 17:47" or whatever the values for that were at the time.

The script is this:

Code: Select all

curTime = indigo.server.getTime().time()
curTimeStr = "\n" + str(curTime)

sushi_fed_number = indigo.variables[151546714] # "cat_sushi_fed_number"
sushi_fed_number_str = "\n" + str(sushi_fed_number)

sushi_fed = indigo.variables[1274955679] # "cat_sushi_fed"

fed_str = u"Fed %s time, Last feed at %s" % (sushi_fed_number, curTimeStr)

indigo.variable.updateValue(sushi_fed, fed_str)
However this is what ends up in the variable:

Code: Select all

Fed description : 
folderId : 1345505049
globalProps : MetaProps : (dict)
     com.indigodomo.indigoserver : (dict)
     emptyDict : (dict)
id : 151546714
name : cat_sushi_fed_number
pluginProps : emptyDict : (dict)
readOnly : False
remoteDisplay : True
sharedProps : com.indigodomo.indigoserver : (dict)
value : 1 time, Last feed at 
19:59:30.194000
I'm a bit lost as to where this is all coming from. I've been rather absent from this stuff for a good few years so my memory of how it all worked is hazy to say the least, and I know in that time we've had a fair few Python upgrades so I'm not sure if that has something to do with it.

Any ideas? :mrgreen:
Computer says no.
User avatar
FlyingDiver
Posts: 7313
Joined: Sat Jun 07, 2014 10:36 am
Location: Southwest Florida, USA

Re: Fixing a script that used to work

Post by FlyingDiver »

You're referencing the variable object, not the variable value. See https://wiki.indigodomo.com/doku.php?id ... e_examples
joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177
User avatar
durosity
Posts: 4385
Joined: Thu May 10, 2012 3:21 pm
Location: Newcastle Upon Tyne, Ye Ol' England.

Re: Fixing a script that used to work

Post by durosity »

Yeah I read over the documentation first but I'm really not seeing where I'm referencing the object instead of the value.. where is it please?
Computer says no.
User avatar
FlyingDiver
Posts: 7313
Joined: Sat Jun 07, 2014 10:36 am
Location: Southwest Florida, USA

Re: Fixing a script that used to work

Post by FlyingDiver »

durosity wrote:Yeah I read over the documentation first but I'm really not seeing where I'm referencing the object instead of the value.. where is it please?
This is the object:

Code: Select all

sushi_fed_number = indigo.variables[151546714] # "cat_sushi_fed_number"
You want the value:

Code: Select all

sushi_fed_number = indigo.variables[151546714].value  # "cat_sushi_fed_number"
joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177
User avatar
durosity
Posts: 4385
Joined: Thu May 10, 2012 3:21 pm
Location: Newcastle Upon Tyne, Ye Ol' England.

Re: Fixing a script that used to work

Post by durosity »

Ah beautiful, that's sorted it!

I feel like I'm jumping back in at the deep end, it's been so long since I looked at this stuff.. I'm reading over code that I clearly wrote myself and yet have no flipping idea how I did that. I'll get there.

Thanks!

And just in case anyone else has any interest in this, it's just a bit of code that runs when a control page button is pressed to update a value of a variable to say when Sushi (the cat) was last fed. There's other variants of this that also log when she's had her medication, and is also actionable from a shortcut via Siri. Saves us accidentally double medicating her and her spending the evening whacked out on pixie sticks.

Here's the full code:

import datetime

timecur = indigo.server.getTime()
timecurstr = datetime.datetime.strftime(timecur, "%H:%M")

sushi_fed_number = indigo.variables[151546714].value # "cat_sushi_fed_number"
sushi_fed_number_str = str(sushi_fed_number)

sushi_fed = indigo.variables[1274955679] # "cat_sushi_fed"

fed_str = u"Fed %s time, Last feed at %s" % (sushi_fed_number, timecurstr)

indigo.variable.updateValue(sushi_fed, fed_str)
Computer says no.
User avatar
jay (support)
Site Admin
Posts: 18376
Joined: Wed Mar 19, 2008 11:52 am
Location: Austin, Texas
Contact:

Re: Fixing a script that used to work

Post by jay (support) »

For learning purposes, here's a modified script that uses Python 3 features as well as a few optimizations:

Code: Select all

# This always results in a string, nothing more to do other than get it
sushi_fed_value = indigo.variables[151546714].value # "cat_sushi_fed_number"
# Create the string via an f-string - could be put inline but then it's a very long line
fed_str = f"Fed {sushi_fed_value}, Last feed at {indigo.server.getTime():%H:%M}"
# Update the variable - no need to get the variable instance, just pass it's ID
indigo.variable.updateValue(1274955679, fed_str)
Without comments, just 3 lines. The f-string makes it much clearer what the string will look like since not only are the values embedded, but also the format in the case of the datetime instance.
Jay (Indigo Support)
Twitter | Facebook | LinkedIn
User avatar
durosity
Posts: 4385
Joined: Thu May 10, 2012 3:21 pm
Location: Newcastle Upon Tyne, Ye Ol' England.

Re: Fixing a script that used to work

Post by durosity »

Oh thank you, that's much more efficient!

I was planning to try and make it a bit more concise at some point, but my main aim at the moment is just getting the core functionality updated to actually work rather than have errors in the log all over the place, and get the other unimplemented functionality sorted out first.

No doubt I'll be asking a lot more questions over the coming months. I'm sure you'll love that @Jay :D

Thank you both of you for your assistance. It's slowly starting to come back to me now.
Computer says no.
Post Reply

Return to “Python Scripts”