I am using a raspberry pi with python to send dots which seems to work fine. when I try to read a switch state I use the
var.get_values(1) function which I assume would get the last and current switch value. It get the list of dictionary values and but I only get zeros.
I am assuming that the switch goes from 1 to zero once its read by the device?
Is there another more detailed description of the module api?
Hi @ssagerian,
Here’s an example to read the last value of a Switch:
from ubidots import ApiClient
api = ApiClient("a21ebafxxxxd195c0044fcc3b9f6dabxxxx3af3")
control_variable = api.get_variable("553169cb7625421be86b9592")
status = control_variable.get_values(1)
print status[0]['value']
if status[0]['value']:
print "Switch is ON"
else:
print "Switch is OFF"
The variable doesn’t automatically go from 1 to zero when read from the device. If your application requires this behaviour, you can use the “save_value” function in your IF statement:
if status[0]['value']:
print "Switch is ON"
control_variable.save_value({"value":0})
A description of the Python API client can be found here: http://ubidots.com/docs/libraries/python.html#libraries-python-official
Hi Augustin, thanks for the quick reply.
I am seeing different behavior despite using exactly the same code as you posted (except for the ids).
while watching the widget, when I click on it, it goes from the off state to the on state, moments later (10 seconds) it goes off again, presumably because it was read by my endpoint device. I dont save a value of zero to it.
Also, I am still only reading zeros at the end point device, I have checked the id of the variable and they match.
any other actions you can suggest, or things to check on ?
thanks
Steve
Well, you were correct (go figure!). I found that I was setting the value later on to zero…now its working…thanks again, great website!
Steve
One other question, since I can set the value of the var, what happens if is I set it to something other than one or zero?
Glad it worked now! you can send any value to it, as any other Ubidots variable. Furthermore, you can send arbitrary “key”:“value” pairs if that makes sense to your application (see “context”).
In this case we are only sending “1” or “0” because it is a variable to control something, so the logic in the controller takes an action -or not- depending on the binary state.
If you use an “Indicator” widget then it will turn on for any value “> 0” or off for any value “=< 0”