[SOLVED] List index out of range in python on get.value

im trying to get a varible from ubidots on omega2, this variable is a json dict and the values are 1 or 0 (its a switch). When a start the code remind me list index out of range error. (sorry for my english :frowning: )

Error: File ā€œmain.pyā€, line 23 in main if tapparellasu[0][ā€˜valueā€™]: IndexError: list index out of range

The output of: print (tapparellasu) is [] and the output of print TapparellaSu is the name of the Ubidots variable (and so the program can get the ubidots variable but i can read the value from the dict.)

The code:

import os
import json
import onionGpio
import time
import ubidots
from ubidots import ApiClient
api = ApiClient(token='wefwfwfwefwefwefwefrwgewrgerfgwef')
gpio6 = onionGpio.OnionGpio(6)
gpio7 = onionGpio.OnionGpio(7)
tapparellasu = 0
status = gpio6.setOutputDirection(0)
status = gpio7.setOutputDirection(0)


def __main__():
    TapparellaSu = api.get_variable('uywqegfuywefkwaeuiyfyuwefjukghwef')
    tapparellasu = TapparellaSu.get_values(1)
    if tapparellasu[0]['value']:
        status = gpio6.setValue(1)
    else:
        status = gpio6.setValue(0)
    print tapparellasu[0]['value']
    time.sleep(1)
if __name__ == '__main__':
    __main__()

Thanks for your help!

Hello @MattiaOmega, the error that you get is because the method get_values() is not getting any value from your account, please ensure that your variable id is correct (it should be numerical and not alpha-numerical like the one at your code, check this article: Where can I find the Variable ID? | Ubidots Help Center). I will also advice you to include this line:

inside your main() function to maintain the connection to the server in your infinite loop.

Regards

1 Like

Thanks for reply! Iā€™ve write a random code in token and variable id for sicurity! :slight_smile: im sure that omega speak whit ubidits becouse the output of print TapparellaSu is the name of my ubidots variable!!
Another idea?

Iā€™m running this script and it seems to work properly for me, please try it:

import os
import json
import time
import ubidots
from ubidots import ApiClient
tapparellasu = 0
DELAY = 5
TOKEN = 'put_here_your_token'
VAR_ID = 'put_here_your_variable_id'


def __main__():
    api = ApiClient(token=TOKEN)
    TapparellaSu = api.get_variable(VAR_ID)
    tapparellasu = TapparellaSu.get_values(1)
    print "Value obtained: {}".format(tapparellasu[0]['value'])
    time.sleep(DELAY)

if __name__ == '__main__':
    __main__()

Results:

2 Likes

Hello and thaks for help me! Iā€™ve try to use your code but
it continues to give me the same issue. Iā€™m using python 2.7 and ubidots 1.6.6ā€¦ maybe the problem is in client? Iā€™ve re try to print TapparellaSu after api.get_variabile and the output is the name of the variable that iā€™ve build on ubidotsā€¦ and so communication is okā€¦ i need to write another file for json dict? i dont understand where is the problemā€¦ looks like all okā€¦ variable id is the alfanumeric word called ID under variable Label true?
iā€™ve found this tips on omega2 websiteā€¦ i hope can help you
https://onion.io/2bt-pushing-data-to-the-ubidots-iot-platform/

Hello, Iā€™ve recorded a small video to show you the issue that you are experiencing, the method get_values() throws an exception if your variable does not have data as you can see at the beginning of the video, once I send data to the variable using a slider the method finds values and you donā€™t get any exception.

Now, the get_value() method does not return the variable name, it returns a list with a dictionary inside like this:

[{u'url': u'http://things.ubidots.com/api/v1.6/values/5977696cc03f9773ce87bcd2', u'timestamp': 1500997996466, u'created_at': u'2017-07-25T15:53:16.467000', u'context': {}, u'value': 67.0}]

that is why you can access to the value in this way:

tapparellasu[0]['value']

Inside the dictionary there is not any related to your variableā€™s name, so I believe that something wrong is being implemented at your side. Please check the attached code, look at the video and try again.

Regards.

test-lib.mp4.zip (1.4 MB)
test-python.py.zip (390 Bytes)

1 Like

Thanks for the code snippet. I have a program that has been running for over a month that is saving data and getting data from Ubidots. This week the get_values call is not returning any data from the variable Iā€™ll call ā€œlockā€. Since my code hasnā€™t changed, it seems Ubidots has changed something on their side.

Using your code snippet I am still getting no data from the lock variable. I followed the example of trying to populate the variable with data by using a slider widget - still canā€™t get data. I also tried that same scenario with a brand new variable with the same result. Then I tried pulling data from a different variable Iā€™ve been writing to with my program - that worked. When I switched back to the lock variable, it doesnā€™t work.

Has anyone else noticed existing code with get_values no longer working? If so, has anyone figured out how to fix it?

1 Like

Greetings @MattiaOmega and @sparkworksdev,

We have dept into your issues and have released an updated that should solve the issues that you experienced. It is not necessary to update your python library version, simply try again your actual scripts.

Regards

2 Likes

Goodmorning!
I can confirm that the issue has been fixed!
Thank you very much for helping, well done!

Mine is fixed as well. Thank you for addressing the issue so quickly!