Python Basic Examples dont work

Im trying to get the average value from my dashboard (variable that im sending from NodeMcu).

But any of the basic examples work.

The terminal just say: Minimum not found, maximum not found and average not found.

Im putting the device_token in Environment Variables name and in value my token: 6addedf3-0dbc-4591-9184-1fe1a86ac2bc.

Hi @PedroRafaim,
The examples are working here.

Did you updated your variables in the script?

Also, this script only works if your variable in the device bucket is in Number format.
You can check if it is a string or number by using the GET data route.
https://api.tago.io/data?authorization=6addedf3-0dbc-4591-9184-1fe1a86ac2bc

1 Like

The script also doesn’t find a average and a max. value.

image

Thank You!!! @vitor

I still have a problem.

The minimum value is wrong and i dont know how to fix it, im a beginner in Tago.

My code:

Analysis Example

Minimum, maximum, and average

Get the minimum, maximum, and the average value of the variable temperature from your device,

and save these values in new variables

Instructions

To run this analysis you need to add a device token to the environment variables,

To do that, go to your device, then token and copy your token.

#* Go the the analysis, then environment variables,

type device_token on key, and paste your token on value

from tago import Device

from tago import Analysis

The function myAnalysis will run when you execute your analysis

def myAnalysis(context, scope):

reads the value of device_token from the environment variable

device_token = list(filter(lambda device_token: device_token[‘key’] == ‘device_token’, context.environment))

device_token = device_token[0][‘value’]

if not device_token:

return context.log("Missing device_token Environment Variable.")

my_device = Device(device_token)

This is a filter to get the minimum value of the variable temperature in the last day

minFilter = {

'variable': 'variavel',

'qty': 1

}

Now we use the filter for the device to get the data

check if the variable min has any value

if so, we crete a new object to send to Tago

min_result = my_device.find(minFilter)

if len(min_result[“result”]) and min_result[‘status’] is True:

# context.log(min_result["result"])

min_result = min_result["result"][0]

minValue = {

  'variable': 'temperature_minimum',

  'value': min_result["value"],

  'unit': 'F',

}



# now we insert the new object with the minimum value

result = my_device.insert(minValue)

if result['status'] is not True:

  return

  # context.log(result['result'])

else:

  context.log('Temperature Minimum Updated')

else:

context.log('Minimum value not found')

This is a filter to get the maximum value of the variable temperature in the last day

maxFilter = {

'variable': 'value',

'query': 'max',

'start_date': '1 day',

}

max_result = my_device.find(maxFilter)

if len(max_result[“result”]) and max_result[‘status’] is True:

max_result = max_result["result"][0]

minValue = {

  'variable': 'temperature_maximum',

  'value': max_result["value"],

  'unit': 'F',

}



# now we insert the new object with the Maximum value

result = my_device.insert(minValue)

if result['status'] is not True:

  context.log(result['result'])

else:

  context.log('Temperature Maximum Updated')

else:

context.log('Maximum value not found')

This is a filter to get the last 1000 values of the variable temperature in the last day

avgFilter = {

'variable': 'value',

'qty': 1000,

'start_date': '1 day',

}

avg = my_device.find(avgFilter)

if len(avg[“result”]) and avg[‘status’] is True:

temperatureSum = 0

for item in avg["result"]:

  temperatureSum = float(temperatureSum) + float(item["value"])

temperatureSum = temperatureSum / len(avg)

context.log(temperatureSum)



avgValue = {

  'variable': 'temperature_average',

  'value': temperatureSum,

  'unit': 'F',

}

result = my_device.insert(avgValue)

if result['status'] is not True:

  context.log(result['result'])

else:

  context.log('Temperature Average Updated')

else:

context.log('No result found for the avg calculation')

The analysis token in only necessary to run the analysis outside TagoIO

Analysis(‘MY-ANALYSIS-TOKEN-HERE’).init(myAnalysis)

I don’t think that you have any issue with the python code.

The problem is the format you’re sending data to TagoIO. It is in string format. String is when you add quotes to your value.

String value:

{
 value": "60",
 variable": "temperature"
}

Number value:

{
 value": 60,
 variable": "temperature"
}

I can see with the token that you posted here that all your data is string. The query min/max doesn’t work with strings.
Also, I highly recommend you to erase the token from your device and create a new one, since anyone can have access to your device data from here.

1 Like

Thank you for the attention @vitor !!! Im gonna fix the things you listed.

Hey @vitor, how can I solve the problem you have commented (my variable is string) in the analysis? Is there a way I can transform my String Values into Int inside the Tago platform? Or I will need to send an Int format by the way.