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


#1

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!


#2

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: http://help.ubidots.com/faqs-and-troubleshooting/where-can-i-find-the-variable-id). 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


#3

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?


#4

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:


#5

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/


#6

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)


#7

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?


#8

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


#9

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


#10

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