Page 1 of 1

Separate First and second API Responses

Posted: Thursday 05 October 2023 9:07
by masselb
Hello Domoticz Friends,

I need some help of a script wizzard to solve a problem with 2 scripts I have. The first script gets values out of the Solax Cloud and updates some sensors. This one works like a charm, I am very happy with it :) Looks like this;

========================================================================

-- Script to retrieve real-time data from a Solax solar inverter and update a Domoticz device

Code: Select all

return {
-- Define triggers for the script
on = {
timer = { 'every 2 minutes' },
httpResponses = { 'trigger' }
},

-- Configure logging settings
logging = {
level = domoticz.LOG_INFO,
marker = 'Solax-X3-Hybrid'
},

-- Define the main function of the script
execute = function(domoticz, item)
-- Handle the timer trigger
if item.isTimer then
domoticz.openURL({
url = 'https://www.solaxcloud.com/proxyApp/proxy/api/getRealtimeInfo.do?tokenId=TOKEN&sn=inverter1',
method = 'GET',
callback = 'trigger'
})
end

-- Handle the HTTP response trigger
if item.isHTTPResponse then
-- Check if the response was successful
if item.ok then
-- Check if the response is in JSON format
if item.isJSON then
-- Extract the relevant data from the response
local inverterData = item.json.result

if inverterData == nil then
-- Log a message indicating failure
domoticz.log('Problemas de comunicação')
domoticz.log(item)
else
-- Update the Domoticz device with the retrieved data
local device = domoticz.devices('Solax X3 String1')
device.updateElectricity(inverterData.powerdc1)
local device = domoticz.devices('Solax X3 String2')
device.updateElectricity(inverterData.powerdc2)
local device = domoticz.devices('X3 Powerbank transfer')
device.updateElectricity(inverterData.batPower)
local device = domoticz.devices('Solax Powerbank')
device.updatePercentage(inverterData.soc)
local device = domoticz.devices('House Load')
device.updateElectricity(inverterData.acpower)
local device = domoticz.devices('Grid_Feed')
device.updateElectricity(inverterData.feedinpower)


-- Log a message indicating success
domoticz.log('Data updated successfully', domoticz.LOG_INFO)
end
end
end
end
end
}
=========================================================================

Yesterday, I have Installed a second Inverter and of course I need some values from that one too.
Putting the GET in a browser works, however a copy of the script doesn't of course. So I made a copy of the running script from Inverter1,
altered the String and teh values I need, made the sensors in Domoticz. It runs just like the first script, however now the updating of my sensors gets tangled up. So I guess I need to change the output in the second script so it would be available as a parralel to the first (?)

I have played around a little, however as I am no scripting man I guess it would be better to ask anyone what I need to change in the second script.

It looks like this (for now):



-- Script to retrieve real-time data from a Solax solar inverter and update a Domoticz device

Code: Select all

return {
-- Define triggers for the script
on = {
timer = { 'every 3 minutes' },
httpResponses = { 'trigger' }
},

-- Configure logging settings
logging = {
level = domoticz.LOG_INFO,
marker = 'Solax-X1-Hybrid'
},

-- Define the main function of the script
execute = function(domoticz, item)
-- Handle the timer trigger
if item.isTimer then
domoticz.openURL({
url = 'https://www.solaxcloud.com/proxyApp/proxy/api/getRealtimeInfo.do?tokenId=TOKEN&sn=inverter2',
method = 'GET',
callback = 'trigger'
})
end

-- Handle the HTTP response trigger
if item.isHTTPResponse then
-- Check if the response was successful
if item.ok then
-- Check if the response is in JSON format
if item.isJSON then
-- Extract the relevant data from the response
local inverterData = item.json.result

if inverterData == nil then
-- Log a message indicating failure
domoticz.log('Communication Problems')
domoticz.log(item)
else
-- Update the Domoticz device with the retrieved data
local device = domoticz.devices('Solax X1 String')
device.updateElectricity(inverterData.powerdc1)
local device = domoticz.devices('X1 Powerbank transfer')
device.updateElectricity(inverterData.batPower)
local device = domoticz.devices('Solax X1 Powerbank')
device.updatePercentage(inverterData.soc)
local device = domoticz.devices('L1 Load X1_Group')
device.updateElectricity(inverterData.acpower)


-- Log a message indicating success
domoticz.log('Data updated successfully', domoticz.LOG_INFO)
end
end
end
end
end
}

Again, this script works as good as the first but the values out of the API have the same name so it updates the wrong sensors. If I understand the function of it, I need to have a second result file/table being inported into domoticz?

Thx in advance for helping!

Re: Separate First and second API Responses

Posted: Thursday 05 October 2023 9:26
by waltervl
First, please put your code in code brackets. It makes it much more readable.

Second,the callback/HttpResponses 'trigger' should be unique per dzvents script.
So for string 1 make it for example TriggerX3 and for the other TriggerX1

Now dzvents gets mixed up because of the same trigger.

Re: Separate First and second API Responses

Posted: Thursday 05 October 2023 9:34
by masselb
Good Morning Walter,

First, excuse me for not using code brackets (if i know how I will next time).

Second, thank you very much for adressing the needed change. This was exacly what I was looking for I will test it right away.

Third, can I get you a coffee?

Re: Separate First and second API Responses  [Solved]

Posted: Thursday 05 October 2023 9:39
by waltervl
Thanks for the coffee! :)
Do not forget to change the word 'trigger' on all places in your scripts. Also check your other dzvents scripts if they use the same trigger.

Re: Separate First and second API Responses

Posted: Thursday 05 October 2023 9:51
by masselb
:D Every Morning a Coffee Morning :D

So, I have tested it and you were very right, thank you very much for helping me get this started. It seems the values are correct and I can now take the next step in automating. I have checked the log;

Code: Select all

2023-10-05 09:46:00.287 Status: dzVents: Info: Solax-X3-Hybrid: ------ Finished Solax_X3_Values
2023-10-05 09:46:00.287 Status: EventSystem: Script event triggered: H:\Domoticz\dzVents\runtime\dzVents.lua
2023-10-05 09:46:00.442 Status: dzVents: Info: Handling httpResponse-events for: "triggerX3"
2023-10-05 09:46:00.442 Status: dzVents: Info: Solax-X3-Hybrid: ------ Start internal script: Solax_X3_Values: HTTPResponse: "triggerX3"
2023-10-05 09:46:00.458 Status: dzVents: Info: Solax-X3-Hybrid: Data updated successfully
2023-10-05 09:46:00.458 Status: dzVents: Info: Solax-X3-Hybrid: ------ Finished Solax_X3_Values
2023-10-05 09:46:00.458 Status: EventSystem: Script event triggered: H:\Domoticz\dzVents\runtime\dzVents.lua
2023-10-05 09:46:02.838 SIMBA remote: Usage (Huisverbruik Grid meter)
Looking Good! Thanks again :)