Python code working outside of Indigo but not as Embedded

Posted on
Mon Feb 19, 2018 9:35 am
Suncroft offline
Posts: 39
Joined: Dec 08, 2014
Location: North Yorkshire, UK

Python code working outside of Indigo but not as Embedded

Hi,

I am new to python and I am currently trying to get Electric usage into an Indigo Variable via an existing python script which talks to my Loop energy monitor. This works fine when running from the host command line as follows:

Python 2.7.10 (default, Feb 7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyloopenergy
>>> import time
>>>
>>> elec_serial = 'xxxxxxxxxx';
>>> elec_secret = 'yyyyyyyyyy';
>>>
>>> le = pyloopenergy.LoopEnergy(elec_serial, elec_secret)
>>>
>>> print(le.electricity_useage)
1.284

But when I try to run the same via an Indigo embedded python script I get no errors but also no update to the variable, it just reads - no value -

import pyloopenergy
import time

elec_serial = 'xxxxxxxxxx';
elec_secret = 'yyyyyyyyyy';

le = LoopEnergy(elec_serial, elec_secret)

indigo.variable.updateValue(932431696, le.electricity_useage)


Any thoughts where I may be going wrong? If I replace le.electricity_useage in the variable update with a string then the variable updates with the string so the variable id is correct.

Thanks!

Posted on
Mon Feb 19, 2018 9:43 am
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python code working outside of Indigo but not as Embedde

I think variables are all strings when they're stored. Try:

Code: Select all
indigo.variable.updateValue(932431696,str(le.electricity_useage))

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

Posted on
Mon Feb 19, 2018 5:27 pm
Suncroft offline
Posts: 39
Joined: Dec 08, 2014
Location: North Yorkshire, UK

Re: Python code working outside of Indigo but not as Embedde

Thanks Joe, I did already try that and just tried it again now. The only difference is that the Variable is now populated with 'None' rather than '-no value -'. I think that for some reason le.electricity_useage is returning back no value when run from within the Indigo embedded script. Any other thoughts appreciated.

Thanks

Posted on
Mon Feb 19, 2018 5:37 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python code working outside of Indigo but not as Embedde

Log the value you get:

Code: Select all
indigo.server.log(u"le.electricity_useage = %f" % le.electricity_useage)


Also, you might want to check that you're getting a valid object (not None) returned by the call to LoopEnergy().

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

Posted on
Mon Feb 19, 2018 7:02 pm
DaveL17 offline
User avatar
Posts: 6741
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Python code working outside of Indigo but not as Embedde

I noticed that in your shell implementation, you call:
Code: Select all
le = pyloopenergy.LoopEnergy(elec_serial, elec_secret)

But in your Indigo script, you call:
Code: Select all
le = LoopEnergy(elec_serial, elec_secret)

Try updating your embedded script with the former and see what you get.

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

[My Plugins] - [My Forums]

Posted on
Tue Feb 20, 2018 4:44 am
Suncroft offline
Posts: 39
Joined: Dec 08, 2014
Location: North Yorkshire, UK

Re: Python code working outside of Indigo but not as Embedde

Hi Joe, unfortunately I get the error 'embedded script: a float is required' with that logging line. I had previously tried logging the output of le.electricity_useage with the line 'indigo.server.log(le.electricity_useage)' but just got an empty entry in the log as follows:

20 Feb 2018, 01:11:39
Script

I think le.electricity_useage is returning an empty string but don't understand why its not working when run as an embedded script. Unless Indigo does not support the full python enviroment? I am not sure how to debug it further from within Indigo

@Dave, thats for the info, I had been using 'le = pyloopenergy.LoopEnergy(elec_serial, elec_secret)' but when I cut and paste into this thread, at the time I had copied the python code for the pylon-energy module into my embedded script so was not needing to reference pyloopenergy. Anyway unfortunately neither work and I am back to importing the module and my code is as follows:

import pyloopenergy
import time

elec_serial = 'xxxxxxxx';
elec_secret = 'yyyyyyyy';

le = pyloopenergy.LoopEnergy(elec_serial, elec_secret)

indigo.server.log(le.electricity_useage)

indigo.variable.updateValue(932431696,str(le.electricity_useage))

So to recap with this code I get no entry in the Indigo log and a value of 'none' in my variable.

Thanks

Posted on
Tue Feb 20, 2018 4:56 am
DaveL17 offline
User avatar
Posts: 6741
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Python code working outside of Indigo but not as Embedde

Please try running with this line in place of Joe's suggested log line:

Code: Select all
indigo.server.log(str(type(le.electricity_useage))

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

[My Plugins] - [My Forums]

Posted on
Tue Feb 20, 2018 5:18 am
Suncroft offline
Posts: 39
Joined: Dec 08, 2014
Location: North Yorkshire, UK

Re: Python code working outside of Indigo but not as Embedde

Hi Dave,

I get this in the Indigo log.

Script <type 'NoneType'>

So I guess pyloopenergy us returning an empty value right?

Thanks for the help so far

Posted on
Tue Feb 20, 2018 6:42 am
DaveL17 offline
User avatar
Posts: 6741
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Python code working outside of Indigo but not as Embedde

It appears that the function you're calling is returning None, which in Python is an object that indicates no value (it's not the same as an empty string or the value False). This may be telling us that the function you're calling is being called correctly, but is returning the value None. Given that Indigo is not returning an ImportError, it appears that Indigo can see the library you're calling, but as a test, please open the Indigo Scripting Shell from the Plugins menu and enter the following command:

Code: Select all
import pyloopenergy


If that returns without error, please enter each line of your script in turn and see what results you get.

EDIT:
p.s.: You'll need to be at your server machine or connected through screen sharing or another remote desktop client. This test won't work if you're connecting to the server with a remote Indigo client.

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

[My Plugins] - [My Forums]

Posted on
Tue Feb 20, 2018 7:29 am
Suncroft offline
Posts: 39
Joined: Dec 08, 2014
Location: North Yorkshire, UK

Re: Python code working outside of Indigo but not as Embedde

So this is interesting it works when I run it via the plugin scripting shell as advised and the variable in indigo updates with the correct value.

Python 2.7.10 (default, Feb 7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)]
Connected to Indigo Server v7.1.1, api v2.0 (localhost:1176)
>>> import pyloopenergy
>>> import time
>>> elec_serial = 'xxxxxxxxxxxxxxx';
>>> elec_secret = 'yyyyyyyyyyyyyy';
>>>
>>> le = pyloopenergy.LoopEnergy(elec_serial, elec_secret)
>>> indigo.server.log(str(type(le.electricity_useage)))
>>>
>>> indigo.variable.updateValue(932431696,str(le.electricity_useage))
>>>

Would you know then why the same script does not work as an embedded python script?

Thanks

Posted on
Tue Feb 20, 2018 7:33 am
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python code working outside of Indigo but not as Embedde

Are you sure your embedded script has this line:
Code: Select all
 le = pyloopenergy.LoopEnergy(elec_serial, elec_secret)

and not this line:
Code: Select all
 le = LoopEnergy(elec_serial, elec_secret)


Also, using code tags when posting Python code really helps to make it more readable.

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

Posted on
Tue Feb 20, 2018 9:41 am
Suncroft offline
Posts: 39
Joined: Dec 08, 2014
Location: North Yorkshire, UK

Re: Python code working outside of Indigo but not as Embedde

Yes I am sure I am using "le = pyloopenergy.LoopEnergy(elec_serial, elec_secret)" in the embedded python script. Here is a direct cut and paste of what I have

Code: Select all
import pyloopenergy
import time

elec_serial = 'xxxxxxxx';
elec_secret = 'yyyyyyyy';

le = pyloopenergy.LoopEnergy(elec_serial, elec_secret)

indigo.server.log(str(type(le.electricity_useage)))

indigo.variable.updateValue(932431696,str(le.electricity_useage))


If I cut and paste the same code into the scripting shell it works fine.

Thanks

Posted on
Tue Feb 20, 2018 9:52 am
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Python code working outside of Indigo but not as Embedde

So, you're positive that both scripts are identical? Embedded scripts are run in the same environment as the scripting shell so I'm quite confused as to why it's returning different results.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Feb 20, 2018 1:47 pm
Suncroft offline
Posts: 39
Joined: Dec 08, 2014
Location: North Yorkshire, UK

Re: Python code working outside of Indigo but not as Embedde

jay (support) wrote:
So, you're positive that both scripts are identical? Embedded scripts are run in the same environment as the scripting shell so I'm quite confused as to why it's returning different results.


Hi Jay, yes I am 100% sure both scripts are identical. If I cut and paste the script into the scripting shell it works, if I paste it into the embedded script it does not. I also tried executing the script as a file but same issue also. Not sure how to debug further exactly what is happening here.

Thanks

Posted on
Tue Feb 20, 2018 1:55 pm
DaveL17 offline
User avatar
Posts: 6741
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Python code working outside of Indigo but not as Embedde

What are you cutting and pasting from? There could be hidden characters that the embedded route doesn’t like.


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]

Who is online

Users browsing this forum: No registered users and 10 guests

cron