Gateway event script

Hello everybody,

I `ve the next code in a Gateway event script with a timer with 30 seconds :
It´s about reading data from a JSON file from a “aemet web service” to have the temperatura and humidity.

try:
source = system.net.httpGet(“https://opendata.aemet.es/opendata/api/observacion/convencional/datos/estacion/3195/?api_key=eyJhbKLjKwPK_fjRd1vHNFGB_aivzgWE”)
url=system.util.jsonDecode(source)

url= (url["datos"])
#a=0
datos=source = system.net.httpGet(url)
lista=system.util.jsonDecode(datos)
#for a in lista:
#	a+=1
#	print(a,valor)
#	temp=lista[(a)]['ta']
#	humed=lista[(a)]['hr']
temp=lista[22]['ta']
humed=lista[22]['hr']

t1=float(temp)
h1=float(humed)

print(t1,h1)
ruta=["Temp","Humedad"]
valores=[t1,h1]
system.tag.writeAll(ruta,valores)

except:
faults_counter=system.tag.read(“faults”).value
faults_counter+=1
system.tag.write(“faults”,faults_counter)

When i execute it in a button it works succesfully, but when i try to run it in the Gateway event script timer it doesn´t work.
My ignition plattfor is a 7.7.8.
Do you have any idea? Thank you very much.
Kind regards.

Did you check what the RunTime console is logging? Maybe there is an error, unfortunately when you put script inside the Gateway/Client Events Scripts, in case of error there will be no popup error ( like the one that appears if there is an error in a script inside a button )

Thank you very much for atention.

I dont´t know whta the error means, do you?
I attach the error file.

Ierror.txt (2.3 KB)

Here’s your problem:

File "<TimerScript:OFICINAS_ENGIE_MADRID/Temp_aemet @30,000ms >", line 3, in <module>t ÆIOError: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

There’s likely something wrong with the target website’s HTTPS certificate. Also, you should edit your original post to omit your API key.

Hi Eduardo,

Phil is probably correct, I just want to note that the httpGet function also has a bypassCertValidation option, where you can bypass certificate validation.

1 Like

Yeah, it seems the root CA for that site’s certificate is not trusted by default (not in the Java or Safari CA store, anyway).

You’ll have to manually add it to the Java CA store or, preferably, use the bypassCertValidation parameter.

Hi Kevin,

I have made that change but it´s not working yet, here you are.
Pls look my attached file about the new error.new error.txt (2.2 KB)

import system
#import re
try:
	strurl="https://opendata.aemet.es/opendata/api/observacion/convencional/datos/estacion/3195/?api_key=eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJlZHVhcmRvLm9qYWx2b0BlbmdpZS5jb20iLCJqdGkiOiI4ODUxMzM4NS0yZGM3LTRmNjMtOGI2ZC1iM2ZhM2Y3YTU2ZTgiLCJpc3MiOiJBRU1FVCIsImlhdCI6MTU0MTY3NTE3MSwidXNlcklkIjoiODg1MTMzODUtMmRjNy00ZjYzLThiNmQtYjNmYTNmN2E1NmU4Iiwicm9sZSI6IiJ9.Oqo0jtNRwslzq_LqKLjKwPK_fjRd1vHNFGB_aivzgWE"
	source=system.net.httpGet(strurl,bypassCertValidation=0)
	url= (url["datos"])
	#a=0
	datos=source = system.net.httpGet(url)
	lista=system.util.jsonDecode(datos)
	print len(lista)
	#for a in lista:
	#	a+=1
	#	print(a,valor)
	#	temp=lista[(a)]['ta']
	#	humed=lista[(a)]['hr']
	temp=lista[22]['ta']
	humed=lista[22]['hr']
	
	t1=float(temp)
	h1=float(humed)
	
	print(t1,h1)
	ruta=["Temp_ext_wunderground","Humedad_ext_wunderground"]
	valores=[t1,h1]
	system.tag.writeAll(ruta,valores)
except:
	a=system.tag.read("Eventos").value
	a+=1
	system.tag.write("Eventos",a)

Thank you very much.

You're setting bypassCertValidation false - meaning that it will not bypass certificate validation. Send 1 or True instead.

Hi PGriffith,

Yes, you were right.

Now it´s working succesfully.

Thank you very much to all.
:laughing:

1 Like