Page 1 of 1

Fixing a script that used to work

Posted: Sun May 19, 2024 1:05 pm
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:

Re: Fixing a script that used to work

Posted: Sun May 19, 2024 2:32 pm
by FlyingDiver
You're referencing the variable object, not the variable value. See https://wiki.indigodomo.com/doku.php?id ... e_examples

Re: Fixing a script that used to work

Posted: Mon May 20, 2024 11:26 am
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?

Re: Fixing a script that used to work

Posted: Mon May 20, 2024 11:48 am
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"

Re: Fixing a script that used to work

Posted: Mon May 20, 2024 12:18 pm
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)

Re: Fixing a script that used to work

Posted: Mon May 20, 2024 1:32 pm
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.

Re: Fixing a script that used to work

Posted: Mon May 20, 2024 2:22 pm
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.