Page 5 sur 6

Re: API ecowatt RTE

Publié : 26 mai 2024, 20:04
par Martial
Merci pour l'info @Neutrino, effectivement, si ce n'est que ça...

Re: API ecowatt RTE

Publié : 27 mai 2024, 06:07
par denis_brasseur
Messages d'erreur chez moi en remplaçant v4 par v5.
Mon script s'exécute route les heures, et message d'erreur 1 appel sur 2.
2024-05-27 03:49:02.192 Error: Error opening url: https://digital.iservices.rte-france.co ... v5/signals
2024-05-27 05:49:01.158 Error: Error opening url: https://digital.iservices.rte-france.co ... v5/signals
2024-05-27 05:49:01.303 Error: dzVents: Error: (3.1.8) ecowatt 1.0: HTTP/1.1 response: 403 ==>> Forbidden
2024-05-27 05:49:01.306 Error: dzVents: Error: (3.1.8) ecowatt 1.0: An error occurred when calling event handler monecowatt
2024-05-27 05:49:01.306 Error: dzVents: Error: (3.1.8) ecowatt 1.0: /home/pi/domoticz/scripts/dzVents/scripts/monecowatt.lua:81: attempt to index a nil value (field 'json')

Re: API ecowatt RTE

Publié : 27 mai 2024, 15:10
par Neutrino
Il faut aussi s'abonner à l'API v5 sur le site de rte-france.

Re: API ecowatt RTE

Publié : 28 mai 2024, 06:51
par denis_brasseur
Merci Neutrino, c'est mieux après abonnement !

Re: API ecowatt RTE

Publié : 04 oct. 2025, 07:59
par Chrominator
Je passais voir s'il y avait du nouveau ; le script plante depuis longtemps,

Code : Tout sélectionner

ecowatt 1.0: HTTP/1.1 response: 401 ==>> Unauthorized
Je suis abonné à la V5, je ne parviens pas à comprendre ce qui ne va pas.

Code : Tout sélectionner

2025-10-04 07:45:00.207 dzVents: ecowatt 1.0: ------ Start external script: ecowatt.lua:, trigger: "every 15 minutes"
2025-10-04 07:45:00.208 dzVents: Debug: ecowatt 1.0: access_token age: 5399
2025-10-04 07:45:00.208 dzVents: Debug: ecowatt 1.0: access_token : >>>>>>>>>>>>>>TOKEN<<<<<<<<<<<<<<
2025-10-04 07:45:00.208 dzVents: Debug: ecowatt 1.0: Requesting data from RTE
2025-10-04 07:45:00.208 dzVents: Debug: ecowatt 1.0: OpenURL: url = https://digital.iservices.rte-france.com/open_api/ecowatt/v5/signals
2025-10-04 07:45:00.208 dzVents: Debug: ecowatt 1.0: OpenURL: method = GET
2025-10-04 07:45:00.208 dzVents: Debug: ecowatt 1.0: OpenURL: post data = nil
2025-10-04 07:45:00.208 dzVents: Debug: ecowatt 1.0: OpenURL: headers = !#Authorization: Bearer >>>>>>>>>>>>>>TOKEN<<<<<<<<<<<<<<!#Content-Type: application/soap+xml;charset=UTF-8
2025-10-04 07:45:00.208 dzVents: Debug: ecowatt 1.0: OpenURL: callback = ecowatt
2025-10-04 07:45:00.209 dzVents: ecowatt 1.0: ------ Finished ecowatt.lua

Code : Tout sélectionner

2025-10-04 07:45:01.600 dzVents: ecowatt 1.0: ------ Start external script: ecowatt.lua: HTTPResponse: "ecowatt"
2025-10-04 07:45:01.600 Error: dzVents: ecowatt 1.0: HTTP/1.1 response: 401 ==>> Unauthorized
2025-10-04 07:45:01.600 dzVents: Debug: ecowatt 1.0:
2025-10-04 07:45:01.600 Error: dzVents: ecowatt 1.0: An error occurred when calling event handler ecowatt
2025-10-04 07:45:01.600 Error: dzVents: ecowatt 1.0: /home/pi/domoticz/scripts/dzVents/scripts/ecowatt.lua:84: attempt to index a nil value (field 'json')
Le script est assez fidèle à celui d'origine, je n'ai changé que les conditions d'exécution.

Code : Tout sélectionner

local scriptName = 'ecowatt'
local scriptVersion = '1.0'
--[[
 
- Créez un compte sur le site de RTE (https://data.rte-france.com/web/guest)
- Allez sur l'API Ecowatt (https://data.rte-france.com/catalog/-/api/consumption/Ecowatt/v4.0)
- Cliquez sur "Abonnez-vous à l'API", créez une nouvelle application
- Récuperez votre clé en cliquant sur "Copier en base 64"

]]

--                                        Dummy à créer :
local AlertEcoWatt = "EcoWatt" -- Alert device J
local AlertEcoWatt2 = nil -- Alert device J+1

local cleAPI = "je_cache_ma_clé_bien_sur=="

return {
	active = function(domoticz)
				return domoticz.variables('Internet_available').value == 'True'
			 end,
	logging = {
		level = domoticz.LOG_DEBUG, -- Uncomment to override the dzVents global logging setting
		marker = scriptName..' '..scriptVersion
	},	
	on = {
		--timer = {'at *:49'},
		timer = {'every 15 minutes'},
		httpResponses = {
			scriptName,
			scriptName..'at',
		},	
	},
	data = {

		--access_token = {}
		access_token = { history = true, maxItems = 1, maxMinutes = 110 }
	},		

	execute = function(domoticz, item) 
		if item.isTimer then
			if domoticz.data.access_token.size == 0 then 
				--or domoticz.data.access_token.get(1).time.secondsAgo > 6000 then
			
				local url = "https://digital.iservices.rte-france.com/token/oauth/"
				domoticz.log('Requesting data from RTE', domoticz.LOG_DEBUG)
				domoticz.openURL({
					url = url,
					method = 'POST',
					headers =  { ['Content-Type'] = 'application/x-www-form-urlencoded',
								['Authorization'] = 'Basic '..cleAPI},
					
					callback = scriptName..'at',
				})
			else
				domoticz.log('access_token age: '..domoticz.data.access_token.get(1).time.secondsAgo, domoticz.LOG_DEBUG)
				domoticz.log('access_token : '..domoticz.data.access_token.get(1).data, domoticz.LOG_DEBUG)
				--local url = "https://digital.iservices.rte-france.com/open_api/ecowatt/v4/sandbox/signals"
				local url = "https://digital.iservices.rte-france.com/open_api/ecowatt/v5/signals"
				domoticz.log('Requesting data from RTE', domoticz.LOG_DEBUG)
				domoticz.openURL({
					url = url,
					method = 'GET',
					headers =  { ['Content-Type'] = 'application/soap+xml;charset=UTF-8',
								['Authorization'] = 'Bearer '..domoticz.data.access_token.get(1).data},
					
					callback = scriptName,
				})
			end
			
			
		end

		if not item.isHTTPResponse then return end

		local response = item
		if response.trigger == scriptName..'at' then
			local contents = response.json
			domoticz.data.access_token.add(contents.access_token)
			domoticz.log('access_token : '..domoticz.data.access_token.getLatest().data, domoticz.LOG_DEBUG)
			
		elseif response.trigger == scriptName then
			domoticz.log(response.data, domoticz.LOG_DEBUG)
			local contents = response.json.signals
			
			domoticz.log("jour : "..contents[1].jour )
			domoticz.log("dvalue : "..contents[1].dvalue.." message : "..contents[1].message)

			if contents[1].dvalue == 1 then
				domoticz.devices(AlertEcoWatt).updateAlertSensor(domoticz.ALERTLEVEL_GREEN, contents[1].message)
			elseif contents[1].dvalue == 2 then
				domoticz.devices(AlertEcoWatt).updateAlertSensor(domoticz.ALERTLEVEL_ORANGE, contents[1].message)
			elseif contents[1].dvalue == 3 then
				domoticz.devices(AlertEcoWatt).updateAlertSensor(domoticz.ALERTLEVEL_RED, contents[1].message)
			else
				domoticz.devices(AlertEcoWatt).updateAlertSensor(domoticz.ALERTLEVEL_GREY, contents[1].message)
			end
		

			if AlertEcoWatt2 then
				domoticz.log("jour2 : "..contents[2].jour )
				domoticz.log("dvalue : "..contents[2].dvalue.." message : "..contents[2].message)
				if contents[2].dvalue == 1 then
					domoticz.devices(AlertEcoWatt2).updateAlertSensor(domoticz.ALERTLEVEL_GREEN, contents[2].message)
				elseif contents[2].dvalue == 2 then
					domoticz.devices(AlertEcoWatt2).updateAlertSensor(domoticz.ALERTLEVEL_ORANGE, contents[2].message)
				elseif contents[2].dvalue == 3 then
					domoticz.devices(AlertEcoWatt2).updateAlertSensor(domoticz.ALERTLEVEL_RED, contents[2].message)
				else
					domoticz.devices(AlertEcoWatt2).updateAlertSensor(domoticz.ALERTLEVEL_GREY, contents[2].message)
				end
			end
		

		
		elseif (not response.ok) or (response.trigger ~= scriptName) then
			domoticz.log('Last http response was not what expected. Trigger: '..response.trigger, domoticz.LOG_ERROR)
			domoticz.log(response.data, domoticz.LOG_ERROR)
			return
		

		end
		
	end
}

Re: API ecowatt RTE

Publié : 12 oct. 2025, 18:55
par Neutrino
Recrée une clé API peut-être ?

Re: API ecowatt RTE

Publié : 13 oct. 2025, 05:46
par Chrominator
Merci d'avoir pris le temps de regarder.

En réalité c'est aléatoire, j'ai souvent ce message "HTTP/1.1 response: 401 ==>> Unauthorized", mais aussi quelques succès dans l'exécution de ce script.
D'une exécution toutes les 15 mn je suis passé à une fois par heure, mais ça ne change rien.
C'est peut-être côté serveur qu'il y a un souci du coup.

Re: API ecowatt RTE

Publié : 13 oct. 2025, 13:19
par Neutrino
J'ai la même chose en faite. :mrgreen:
C'est le token qui semble expirer plus vite qu'avant.
J'ai modifié

Code : Tout sélectionner

maxMinutes = 30
au lieu de 110.
A voir.

Re: API ecowatt RTE

Publié : 13 oct. 2025, 16:07
par Chrominator
J'ai adapté aussi mon script, merci.
A suivre donc.

Re: API ecowatt RTE

Publié : 14 oct. 2025, 07:50
par Chrominator
Neutrino a écrit : 13 oct. 2025, 13:19 J'ai la même chose en faite. :mrgreen:
C'est le token qui semble expirer plus vite qu'avant.
J'ai modifié

Code : Tout sélectionner

maxMinutes = 30
au lieu de 110.
A voir.
J'ai l'impression que c'est une bonne pioche, je n'ai plus eu l'erreur depuis la modif du script.
Encore merci ! :)