[SOLVED] Get error when sending data from raspberry pi to ubidots server

Hello, i got a message error while sending data flow meter sensor from raspberry pi to ubidots server

flowval.save_value({‘value’:flow})
AttributeError: ‘NoneType’ object has no attribute ‘save_value’

Here my codes :

import serial
import time
from ubidots import ApiClient
import random

serPort = serial.Serial(‘/dev/ttyACM0’, 9600)
dS = None

def getVarbyNames(varName,dS):
for var in dS.get_variables():
if var.name == varName:
return var
return None

try:
api = ApiClient(“43936d6645f2b8455336df48b448edc945fa3c07”)

for curDs in api.get_datasources():
if curDs.name == “HidroponikTA”:
ds = curDs
break
if dS is None:
dS = api.create_datasource({“name”:“HidroponikTA”})

lightval = getVarbyNames(“Light”,dS)
if lightval is None:
lightval = dS.create_variable({“name”:“Light”,“unit”:“lux”})

humidityval = getVarbyNames(“Humidity”, dS)
if humidityval is None:
humidityval = dS.create_variable({“name”:“Humidity”,“unit”:“%”})

tempval = getVarbyNames(“Temperature”, dS)
if tempval is None:
tempval = dS.create_variable({“name”:“Temperature”,“unit”:“C”})

phval = getVarbyNames(“PH”, dS)
if phval is None:
phval = dS.create_variable({“name”:“PH”,“unit”:" "})

flowval = getVarbyNames(“Flow”, dS)
if flow is None:
flow = dS.create_variable({“name”:“Flow”,“unit”:“L/h”})

except:
print(“cant connect”)

while True :
sensorRead = serPort.readline()
sensorVar = sensorRead.split(" ")
light = int(sensorVar[0])
humidity = float(sensorVar[1])
temp = float(sensorVar[2])
ph = float(sensorVar[3])
flow = int(sensorVar[4])
var = ‘light: %d , humidity: %f , temp: %f , ph: %f , flow: %d’ % (light, humidity, temp, ph, flow)

print var

lightval.save_value({‘value’:light})
humidityval.save_value({‘value’:humidity})
tempval.save_value({‘value’:temp})
phval.save_value({‘value’:ph})
flowval.save_value({‘value’:flow})

thanks

Check this line

if flow is None:
    flow = dS.create_variable({"name":"Flow","unit":"L/h"})

If you enter to the getVarbyNames method and it returns None, you’ll never be able to create the variable. Look at your variable names, you’re assigning at flowval but you’re later checking at flow and also assigning to flow. It must be like this:

if flowval is None:
    flowVal = dS.create_variable({"name":"Flow", "unit": "L/h"})

Hope this helps you

thanks for answering, the code have worked now