Import Autarco solar panel data from web api Topic is solved

Moderator: leecollings

Post Reply
HappyNerd
Posts: 11
Joined: Wednesday 25 May 2022 10:10
Target OS: NAS (Synology & others)
Domoticz version: 2024.7
Location: Netherlands
Contact:

Import Autarco solar panel data from web api

Post by HappyNerd »

I am trying to import data from my autarco solar panels (through a web api from the autarco website) into domoticz. I created the virtual sensor and a lua script, but it keeps returning errors. It is driving me crazy. Any help is appreciated.

The script that I am using

Code: Select all

commandArray = {}

-- API credentials and URL
local username = '[email protected]'
local password = 'myactualpassword'
local site_id = 'myactualsiteID'
local api_url_power = 'https://my.autarco.com/api/m1/site/' .. site_id .. '/power'

-- Function to fetch data from API
function fetch_data(url)
    local handle = io.popen("curl -s -u ' .. username .. ':' .. password .. ' ' .. api_url_power ..'")
    local result = handle:read("*a")
    handle:close()
    return result
end

-- Fetch and parse power data
local power_data = fetch_data(api_url_power)
local power_json = json.decode(power_data)
local pv_now = power_json.stats.kpis.pv_now / 1000 -- Convert to kW

-- Update Domoticz virtual sensors
commandArray['UpdateDevice'] = 'myactualidx|0|' .. pv_now

return commandArray

The error message:

Code: Select all

EventSystem: in SpeelScript: [string "commandArray = {} ..."]:20: attempt to index a nil value (global 'json')
Things I already checked
The virtual domoticz device is available. It should work.
Furthermore I tested the API manually:
When I simply type in

Code: Select all

my.autarco.com/api/m1/site/myactualsiteID/power
into my browser, it asks me to logon and then the website provides a neatly formatted output:

Code: Select all

{
    "dt_config_changed": "202x-0x-10T12:41:06+00:00",
    "inverters": {
        "myactualinverterid01": {
            "sn": "myactualserialnumber01",
            "dt_latest_msg": "2024-07-06T18:38:59+00:00",
            "out_ac_power": 190,
            "out_ac_energy_total": 5506,
            "error": null,
            "grid_turned_off": false,
            "health": "OK"
        },
        "myactualinverterid02": {
            "sn": "myactualserialnumber02",
            "dt_latest_msg": "2024-07-06T11:22:46+00:00",
            "out_ac_power": 940,
            "out_ac_energy_total": 3156,
            "error": null,
            "grid_turned_off": false,
            "health": "OK"
        }
    },
    "stats": {
        "graphs": {
            "pv_power": {
                "myactualinverterid01": {
                    "2024-07-06 00:00:00": 0,
                    "2024-07-06 01:00:00": 0,
                    "2024-07-06 02:00:00": 0,
                    "2024-07-06 03:00:00": 0,
                    "2024-07-06 04:00:00": 0,
                    "2024-07-06 05:00:00": 0,
                    "2024-07-06 06:00:00": 60,
                    "2024-07-06 07:00:00": 827,
                    "2024-07-06 08:00:00": 460,
                    "2024-07-06 09:00:00": 687,
                    "2024-07-06 10:00:00": 780,
                    "2024-07-06 11:00:00": 883,
                    "2024-07-06 12:00:00": 713,
                    "2024-07-06 13:00:00": 1427,
                    "2024-07-06 14:00:00": 2320,
                    "2024-07-06 15:00:00": 2700,
                    "2024-07-06 16:00:00": 1273,
                    "2024-07-06 17:00:00": 820,
                    "2024-07-06 18:00:00": 387,
                    "2024-07-06 19:00:00": 190,
                    "2024-07-06 20:00:00": 120,
                     "2024-07-06 20:30:00": 165
                },
                "myactualinverterid02": {
                    "2024-07-06 00:00:00": 0,
                    "2024-07-06 01:00:00": 0,
                    "2024-07-06 02:00:00": 0,
                    "2024-07-06 03:00:00": 0,
                    "2024-07-06 04:00:00": 0,
                    "2024-07-06 05:00:00": 0,
                    "2024-07-06 06:00:00": 25,
                    "2024-07-06 07:00:00": 167,
                    "2024-07-06 08:00:00": 245,
                    "2024-07-06 09:00:00": 390,
                    "2024-07-06 10:00:00": 470,
                    "2024-07-06 11:00:00": 520,
                    "2024-07-06 12:00:00": 460,
                    "2024-07-06 13:00:00": 823,
                    "2024-07-06 14:00:00": 0,
                    "2024-07-06 15:00:00": 0,
                    "2024-07-06 16:00:00": 0,
                    "2024-07-06 17:00:00": 0,
                    "2024-07-06 18:00:00": 0,
                    "2024-07-06 19:00:00": 0,
                    "2024-07-06 20:00:00": 0,
                    "2024-07-06 20:15:00": 0,
                    "2024-07-06 20:30:00": null
                }
            },
            "no_comms": [
                {
                    "start": "2024-07-06 08:30:00",
                    "end": "2024-07-06 08:44:59"
                },
                {
                    "start": "2024-07-06 13:30:00",
                    "end": null
                }
            ]
        },
        "kpis": {
            "pv_now": 190
        }
    }
}
Also when I enter my domoticz docker and then manually use the curl command

Code: Select all

curl -s -u 'username':'password' https://my.autarco.com/api/m1/site/myactualsiteID/power
, the system returns output, albeit it that it seems to be more in a plain text format.

Code: Select all

{"dt_config_changed":"202x-05-10T12:41:06+00:00","inverters":{"myactualinverterid01":{"sn":"myactualserialnumber01","dt_latest_msg":"2024-07-06T18:53:59+00:00","out_ac_power":190,"out_ac_energy_total":5506,"error":null,"grid_turned_off":false,"health":"OK"},"myactualinverterid02":{"sn":"myactualserialnumber02","dt_latest_msg":"2024-07-06T11:22:46+00:00","out_ac_power":940,"out_ac_energy_total":3156,"error":null,"grid_turned_off":false,"health":"OK"}},"stats":{"graphs":{"pv_power":{"154812216300018":{"2024-07-06 00:00:00":0,"2024-07-06 00:15:00":0,"2024-07-06 00:30:00":0,"2024-07-06 00:45:00":0,"2024-07-06 01:00:00":0,"2024-07-06 01:15:00":0,"2024-07-06 01:30:00":0,"2024-07-06 01:45:00":0,"2024-07-06 02:00:00":0,"2024-07-06 02:15:00":0,"2024-07-06 02:30:00":0,"2024-07-06 02:45:00":0,"2024-07-06 03:00:00":0,"2024-07-06 03:15:00":0,"2024-07-06 03:30:00":0,"2024-07-06 03:45:00":0,"2024-07-06 04:00:00":0,"2024-07-06 04:15:00":0,"2024-07-06 04:30:00":0,"2024-07-06 04:45:00":0,"2024-07-06 05:00:00":0,"2024-07-06 05:15:00":0,"2024-07-06 05:30:00":0,"2024-07-06 05:45:00":20,"2024-07-06 06:00:00":60,"2024-07-06 06:15:00":113,"2024-07-06 06:30:00":240,"2024-07-06 06:45:00":563,"2024-07-06 07:00:00":827,"2024-07-06 07:15:00":533,"2024-07-06 07:30:00":407,"2024-07-06 07:45:00":347,"2024-07-06 08:00:00":460,"2024-07-06 08:15:00":547,"2024-07-06 08:30:00":603,"2024-07-06 08:45:00":600,"2024-07-06 09:00:00":687,"2024-07-06 09:15:00":647,"2024-07-06 09:30:00":737,"2024-07-06 09:45:00":840,"2024-07-06 10:00:00":780,"2024-07-06 10:15:00":523,"2024-07-06 10:30:00":223,"2024-07-06 10:45:00":523,"2024-07-06 11:00:00":883,"2024-07-06 11:15:00":693,"2024-07-06 11:30:00":513,"2024-07-06 11:45:00":393,"2024-07-06 12:00:00":713,"2024-07-06 12:15:00":763,"2024-07-06 12:30:00":1493,"2024-07-06 12:45:00":1413,"2024-07-06 13:00:00":1427,"2024-07-06 13:15:00":2010,"2024-07-06 13:30:00":1923,"2024-07-06 13:45:00":1493,"2024-07-06 14:00:00":2320,"2024-07-06 14:15:00":2710,"2024-07-06 14:30:00":2513,"2024-07-06 14:45:00":2733,"2024-07-06 15:00:00":2700,"2024-07-06 15:15:00":1513,"2024-07-06 15:30:00":2513,"2024-07-06 15:45:00":1393,"2024-07-06 16:00:00":1273,"2024-07-06 16:15:00":943,"2024-07-06 16:30:00":850,"2024-07-06 16:45:00":817,"2024-07-06 17:00:00":820,"2024-07-06 17:15:00":520,"2024-07-06 17:30:00":747,"2024-07-06 17:45:00":437,"2024-07-06 18:00:00":387,"2024-07-06 18:15:00":417,"2024-07-06 18:30:00":510,"2024-07-06 18:45:00":573,"2024-07-06 19:00:00":190,"2024-07-06 19:15:00":230,"2024-07-06 19:30:00":110,"2024-07-06 19:45:00":110,"2024-07-06 20:00:00":120,"2024-07-06 20:15:00":157,"2024-07-06 20:30:00":167,"2024-07-06 20:45:00":210},"1805030221110035":{"2024-07-06 00:00:00":0,"2024-07-06 00:15:00":0,"2024-07-06 00:30:00":0,"2024-07-06 00:45:00":0,"2024-07-06 01:00:00":0,"2024-07-06 01:15:00":0,"2024-07-06 01:30:00":0,"2024-07-06 01:45:00":0,"2024-07-06 02:00:00":0,"2024-07-06 02:15:00":0,"2024-07-06 02:30:00":0,"2024-07-06 02:45:00":0,"2024-07-06 03:00:00":0,"2024-07-06 03:15:00":0,"2024-07-06 03:30:00":0,"2024-07-06 03:45:00":0,"2024-07-06 04:00:00":0,"2024-07-06 04:15:00":0,"2024-07-06 04:30:00":0,"2024-07-06 04:45:00":0,"2024-07-06 05:00:00":0,"2024-07-06 05:15:00":0,"2024-07-06 05:30:00":0,"2024-07-06 05:45:00":0,"2024-07-06 06:00:00":25,"2024-07-06 06:15:00":30,"2024-07-06 06:30:00":67,"2024-07-06 06:45:00":100,"2024-07-06 07:00:00":167,"2024-07-06 07:15:00":177,"2024-07-06 07:30:00":177,"2024-07-06 07:45:00":170,"2024-07-06 08:00:00":245,"2024-07-06 08:15:00":295,"2024-07-06 08:30:00":0,"2024-07-06 08:45:00":330,"2024-07-06 09:00:00":390,"2024-07-06 09:15:00":390,"2024-07-06 09:30:00":430,"2024-07-06 09:45:00":528,"2024-07-06 10:00:00":470,"2024-07-06 10:15:00":313,"2024-07-06 10:30:00":153,"2024-07-06 10:45:00":267,"2024-07-06 11:00:00":520,"2024-07-06 11:15:00":323,"2024-07-06 11:30:00":410,"2024-07-06 11:45:00":157,"2024-07-06 12:00:00":460,"2024-07-06 12:15:00":410,"2024-07-06 12:30:00":1060,"2024-07-06 12:45:00":1040,"2024-07-06 13:00:00":823,"2024-07-06 13:15:00":1130,"2024-07-06 13:30:00":0,"2024-07-06 13:45:00":0,"2024-07-06 14:00:00":0,"2024-07-06 14:15:00":0,"2024-07-06 14:30:00":0,"2024-07-06 14:45:00":0,"2024-07-06 15:00:00":0,"2024-07-06 15:15:00":0,"2024-07-06 15:30:00":0,"2024-07-06 15:45:00":0,"2024-07-06 16:00:00":0,"2024-07-06 16:15:00":0,"2024-07-06 16:30:00":0,"2024-07-06 16:45:00":0,"2024-07-06 17:00:00":0,"2024-07-06 17:15:00":0,"2024-07-06 17:30:00":0,"2024-07-06 17:45:00":0,"2024-07-06 18:00:00":0,"2024-07-06 18:15:00":0,"2024-07-06 18:30:00":0,"2024-07-06 18:45:00":0,"2024-07-06 19:00:00":0,"2024-07-06 19:15:00":0,"2024-07-06 19:30:00":0,"2024-07-06 19:45:00":0,"2024-07-06 20:00:00":0,"2024-07-06 20:15:00":0,"2024-07-06 20:30:00":0,"2024-07-06 20:45:00":null}},"no_comms":[{"start":"2024-07-06 08:30:00","end":"2024-07-06 08:44:59"},{"start":"2024-07-06 13:30:00","end":null}]},"kpis":{"pv_now":190}}}

Anybody have any ideas?
User avatar
gizmocuz
Posts: 2350
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: Import Autarco solar panel data from web api

Post by gizmocuz »

Try not using LUA but use dzVents.
Quality outlives Quantity!
HappyNerd
Posts: 11
Joined: Wednesday 25 May 2022 10:10
Target OS: NAS (Synology & others)
Domoticz version: 2024.7
Location: Netherlands
Contact:

Re: Import Autarco solar panel data from web api

Post by HappyNerd »

Thx. I appreciate the help and I will follow your advice. I am quite new at this, so this may take a couple of days. I’ll post my progress.
HappyNerd
Posts: 11
Joined: Wednesday 25 May 2022 10:10
Target OS: NAS (Synology & others)
Domoticz version: 2024.7
Location: Netherlands
Contact:

Re: Import Autarco solar panel data from web api

Post by HappyNerd »

With a lot of help from ChatGPT and Copilot, I turned the script into a dzvents script and made some adjustments.

The good news
The script is definitely retrieving the data from the autarco website.
Also it is capable to identify the pv_now value that I require to update my virtual device.
And finally, the log doesn't show any errors

The bad news:
The script doesn't update my virtual device.
I am quite sure that it can be pinpointed to a single line of code, but I can't figure it out
It is driving me absolutely crazy :? . Any help is appreciated.

The suspected culprit

Code: Select all

commandArray['UpdateDevice'] = 'deviceIdx|0|pv_now'
Sensor characteristics:
I tried this with two types of sensors and both have the same problem: they are not updated.
Sensor 1: Type = "General", SubType = "custom sensor"
Sensor 2: Type = "Usage", SubType = "Electric"

The entire code

Code: Select all

-- API credentials and URL
local username = 'myactualusername'
local password = 'myactualpassword'
local site_id = 'myactualsite_id'
local api_url_power = 'https://my.autarco.com/api/m1/site/'.. site_id ..'/power'

-- DeviceID
local deviceIdx = 1234

-- Function to fetch data from API
function fetch_data(url)
    print("Fetching data from API...")  -- Troubleshooting line
    local handle = io.popen("curl -s -u '" .. username .. "':'" .. password .. "' " .. api_url_power)
    local result = handle:read("*a")
    handle:close()
    return result
end

-- Fetch data
local power_data = fetch_data(api_url_power)
print("Received data from API:", power_data)  -- Troubleshooting line

-- Extract pv_now value
local pv_now = tonumber(power_data:match('"pv_now":%s*(%d+)'))
if not pv_now then
    print("Error: Unable to extract pv_now value")
    return commandArray
end

print("PV power (kW):", pv_now)  -- Troubleshooting line

-- Update Domoticz virtual sensor
commandArray['UpdateDevice'] = 'deviceIdx|0|pv_now'
print("Updating virtual sensor with PV power:", pv_now)  -- Troubleshooting line

-- Log successful update
local success_message = "Updated virtual sensor with PV power: " .. pv_now
print(success_message)  -- Log success message
--otherdevices_svalues['InfoLog'] = success_message  -- Write to Domoticz info log

return commandArray
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Import Autarco solar panel data from web api

Post by waltervl »

Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
HappyNerd
Posts: 11
Joined: Wednesday 25 May 2022 10:10
Target OS: NAS (Synology & others)
Domoticz version: 2024.7
Location: Netherlands
Contact:

Re: Import Autarco solar panel data from web api

Post by HappyNerd »

Finally got it to work :idea: . I stayed with LUA script. See code below.
It may not be the most elegant script, but it does the job.
Also I am very much aware that there are many more data available in the two autarco api sites (power and energy), but for now I am very happy with the pv_now value.

I thought I'd post this result, since I have been struggling finding useful information in respect to scraping the Autarco solar panel data into domoticz. Maybe this post will help a small audience in accomplishing this same result.
If you want to push this a step further (e.g. to make use of more data from the API), need any help, I strongly advise you to use Microsoft Copilot and simply ask it to debug the script. It will definitely help you further.

Code: Select all

-- API credentials and URL
local username = 'myactualusername' --username to logon to autarco website
local password = 'myactualpassword' --password to logon to autarco website
local site_id = 'myactualsite_id' --8digit code, which you see in the webURL once you logged on to autarco website
local api_url_power = 'https://my.autarco.com/api/m1/site/'.. site_id ..'/power'

-- DeviceID and nValue
local deviceIdx = 1234 --you first need to create this virtual device, type usage, subtype electric
local nValue = 0

-- Function to fetch data from API
function fetch_data(url)
--  print("Fetching data from API...")  -- Troubleshooting line
    local handle = io.popen("curl -s -u '" .. username .. "':'" .. password .. "' " .. api_url_power)
    local result = handle:read("*a")
    handle:close()
    return result
end

-- Fetch data
local power_data = fetch_data(api_url_power)
-- print("Received data from API:", power_data)  -- Troubleshooting line

-- Extract pv_now value
local pv_now = tonumber(power_data:match('"pv_now":%s*(%d+)'))
if not pv_now then
    print("Error: Unable to extract pv_now value")
    return commandArray
end

-- Additional troubleshooting lines
-- print("PV power (kW):", pv_now)  -- Troubleshooting line
-- print ("DeviceIdx:", deviceIdx) -- Troubleshooting line

-- Update Domoticz virtual sensor
commandArray = {}
commandArray['UpdateDevice'] = deviceIdx .. '|' .. nValue .. '|' .. pv_now

-- print("Updating virtual sensor with PV power:", pv_now)  -- Troubleshooting line

-- Log successful update
local success_message = "Updated virtual sensor with PV power: " .. pv_now
print(success_message)  -- Log success message

return commandArray
HappyNerd
Posts: 11
Joined: Wednesday 25 May 2022 10:10
Target OS: NAS (Synology & others)
Domoticz version: 2024.7
Location: Netherlands
Contact:

Re: Import Autarco solar panel data from web api

Post by HappyNerd »

Finished the project.
The script allows to specify a time interval in minutes
I updated the script with bunch of comment lines.
Also made it a bit more robust:
It now writes the errors in the error log and the info messages in the info log.
Also the script now tries two more times to fetch data API data, if the first time fails.
Theoretically it should not into errors: If the API fails, it will update the PV Power value with 0.
I left in the troubleshooting lines, but they are not active.

Hope there are at least one or two Autarco/Domoticz users who may enjoy the script.

The final code:

Code: Select all

-- Initialize commandArray
commandArray = {}

-- Script logic to define the update interval
local interval = 2 -- Set the interval in minutes
local currentTime = os.date("*t")
local currentMinute = currentTime.min

--Start of the script execution
if currentMinute % interval == 0 then

    -- Declare local variables: API credentials, URL, deviceIdx and nValue
    local username      = 'myactualuserid' -- Autarco UserID (email)
    local password      = 'myactualpassword'     -- Autarco Password
    local site_id       = 'myactualsite_id' --8-digit site_id, which will show when you logon to the autaro website
    local api_url_power = 'https://my.autarco.com/api/m1/site/'.. site_id ..'/power' --One of two API's (other one is energy)
    local deviceIdx     = 1234 --My actual deviceIdx: First create new hardware (dummy), then create a virtual device (type = usage, subtype = electric)
    local nValue        = 0 -- 0 is the right value. See Domoticz website for more info on nValue and sValue in commandArray['UpdateDevice']

    -- Define function to fetch data from API with retries
    function fetch_data_with_retries(url, max_retries)
        local retry_count = 0
        local result = nil
    
        repeat
            local curl_command = string.format("curl -s -u '%s':'%s' %s 2>&1", username, password, url)
            local handle = io.popen(curl_command)
            result = handle:read("*a")
            local exit_status, exit_reason, exit_code = handle:close()
    
            if exit_status then
                -- Command executed successfully
                return result
            else
                retry_count = retry_count + 1
                if retry_count <= max_retries then
                    print("Retrying API fetch (attempt " .. retry_count .. ")...")
                else
                    local error_message = "Error executing curl command: " .. exit_reason .. "Exit code: " .. exit_code
                    assert(false, error_message)  -- Log the error message
                    return nil
                end
            end
        until retry_count > max_retries
    end

    -- Execute function to fetch data from API with retries (up to 2 times)
    local max_retries = 2
    local power_data = fetch_data_with_retries(api_url_power, max_retries)
    --print("Received data from API:", power_data)  -- Troubleshooting line

    -- Extract pv_now value from the retrieved data
    local pv_now = tonumber(power_data:match('"pv_now":%s*(%d+)'))
    if not pv_now then
        local error_message = "Error: Unable to extract pv_now value from API data. pv_now value set to 0"
        assert(false, error_message)  -- This will write the error message to the error log
        pv_now = 0 -- Set a default value to allow the script to continue
    end

    -- Additional troubleshooting lines to validate the input for the commandArray['UpdateDevice'] command
    --local success_message = "DeviceIdx: " .. deviceIdx --Troubleshooting line
    --print(success_message)  -- Log success message     --Troubleshooting line
    --local success_message = "nvalue: " .. nValue       --Troubleshooting line
    --print(success_message)  -- Log success message     --Troubleshooting line
    --local success_message = "pv_now: " .. pv_now       --Troubleshooting line
    --print(success_message)  -- Log success message     --Troubleshooting line

    -- Update Domoticz virtual sensor with pv_now value
    --local success_message = "Start updating virtual sensor with PV power: " .. pv_now
    --print(success_message)  -- Log success message
    commandArray['UpdateDevice'] = deviceIdx .. '|' .. nValue .. '|' .. pv_now

    -- Log successful update
    local success_message = "Successfully updated virtual sensor with PV power: " .. pv_now
    print(success_message)  -- Log success message

end

return commandArray
HappyNerd
Posts: 11
Joined: Wednesday 25 May 2022 10:10
Target OS: NAS (Synology & others)
Domoticz version: 2024.7
Location: Netherlands
Contact:

Re: Import Autarco solar panel data from web api

Post by HappyNerd »

Made some additional updates to further improve the script:
- Included an introduction section with a full explanation on the script itsself and how to setup the virtual device in domoticz
- Included lines so that the script only runs during daytime (including an offset)
- Updated the script to work with a more suitable virtual device type "electric (instant+counter)", so that the graphs now also show the cumulative values
For more details, just start reading the introduction section in the code. Things will be very much self-explanatory.

Final code

Code: Select all

-- Introduction
    -- This LUA "time" script is intended to be used in domoticz
    -- It uses an API to fetch solar panel data from the Autarco website and feeds these data into a virtual domoticz sensor
    -- In fact Autarco has two API's: One for (actual) power and one for (cumulative) energy
    -- This script only uses the "power" API and has domoticz calculate the cumulative values
    -- Before activating the script, first read the guidelines below
-- Before you activate this script, make sure that you first create a virtual sensor in domoticz
    -- Create new hardware - type "dummy" (settings, hardware)
    -- From that hardware create a virtual sensor by pushing the button "create virtual sensors": Type = "Electricity (actuals and counter)"
    -- Locate the sensor in the tab "settings", "hardware" and write down the deviceIdx (you'll need it in a minute)
    -- Locate the sensor in the tab "utilities" and make the following adjustments
        -- adjust the type from "usage" to "delivery"
        -- adjust the energy read from "from device" to "calculated"
-- Then create this LUA script in domoticz
    -- Settings, more options, events
    -- Use the "+" to create a LUA script (type "time")
    -- Copy this entire code into that script and save
-- Next you'll need to adjust the script to your needs
    -- Set the interval with which you want this script to run.
        -- The default is set at 5 minutes
        -- There is no need for a higher frequency as the website also only obtains its data every five minutes or so
        -- Only during testing I find it usefull to use a higher frequency (i.e. every minute)
    -- Set the offset values for sunrise and sunset.
        -- this parameter is built in to avoid the script running during the night
        -- The default is 0 minutes before sunrise and 60 minutes after sunset
    -- Set your personalized variables:
        -- Username and password that you also use to logon to the Autarco website
        -- The Autarco site_ID. that is the 8 digit number which you'll find when you logon manually to the autarco website
    -- Set the deviceIdx that needs to be updated (the one you just created)
    -- Set the maximum number of retries for fetching the data.
        -- If the script fails to obtain data from the website after the maximum number of retries, it finishes by updating the sensor with value "0"
        -- Default number of retries is 2, so the script tries 3 times in total
-- As a last step "activate" the script
-- Note: The script contains a set of troubleshooting lines. If you run into trouble, you can uncomment these to further analyze the problem.

-- Initialize commandArray
commandArray = {}

-- Script logic to define the update interval
local interval      = 5                                                                 -- Set the interval in minutes
local currentTime   = os.date("*t")
local currentMinute = currentTime.min

-- Script logic to have the script only run at x minutes before sunrise to y minutes after sunset
local currentMinutes           = currentTime.hour * 60 + currentTime.min                -- Get current time in minutes since midnight
local sunriseInMinutes         = timeofday['SunriseInMinutes']                          -- Get sunrise time in minutes since midnight
local sunsetInMinutes          = timeofday['SunsetInMinutes']                           -- Get sunset time in minutes since midnight
local offsetBeforeSunrise      = 0                                                      -- Set the offset value in minutes
local offsetAfterSunset        = 60                                                     -- Set the offset value in minutes
local adjustedSunriseInMinutes = sunriseInMinutes - offsetBeforeSunrise                 -- Calculate the start of the period for running the script
local adjustedSunsetInMinutes  = sunsetInMinutes + offsetAfterSunset                    -- Calculate the end   of the period for running the script
local function isDaytime()
    return currentMinutes >= adjustedSunriseInMinutes and currentMinutes <= adjustedSunsetInMinutes -- true if current time is between adjusted sunrise and sunset
end

-- Start of the script execution
if currentMinute % interval == 0 and isDaytime() then                                   -- execute logic else finish the script

    -- Declare local variables: API credentials, URL, deviceIdx and nValue
    local username      = 'myactualuserid'                                              -- Autarco UserID (email)
    local password      = 'myactualpassword'                                            -- Autarco Password
    local site_id       = 'myactualsiteid'                                              -- 8-digit site_id, which will show when you logon to the autaro website
    local api_url_power = 'https://my.autarco.com/api/m1/site/'.. site_id ..'/power'    -- One of two API's (other one is energy, but this script will only be using the power API)
    local deviceIdx     = 1234                                                          -- First create new hardware (dummy), then create a virtual device (type = "Electricity (actuals and counter)")
    local nValue        = 0                                                             -- 0 is the right value. See Domoticz website for more info on nValue and sValue in commandArray['UpdateDevice']

    -- Define function to fetch data from API with retries
    function fetch_data_with_retries(url, max_retries)
        local retry_count = 0
        local result = nil
    
        repeat
            local curl_command = string.format("curl -s -u '%s':'%s' %s 2>&1", username, password, url)
            local handle = io.popen(curl_command)
            result = handle:read("*a")
            local exit_status, exit_reason, exit_code = handle:close()
    
            if exit_status then                                                         -- Command executed successfully
                return result
            else
                retry_count = retry_count + 1
                if retry_count <= max_retries then
                    print("Retrying API fetch (attempt " .. retry_count .. ")...")
                else
                    local error_message = "Error executing curl command: " .. exit_reason .. "Exit code: " .. exit_code
                    assert(false, error_message)                                        -- Log the error message
                    return nil
                end
            end
        until retry_count > max_retries
    end

    -- Execute function to fetch data from API with retries (up to 2 times)
    local max_retries = 2                                                               -- Maximum number of retries before ending the script (with pv_now value = 0)
    local power_data = fetch_data_with_retries(api_url_power, max_retries)
    --print("Received data from API:", power_data)                                      -- Troubleshooting line

    -- Extract pv_now value from the retrieved data
    local pv_now = tonumber(power_data:match('"pv_now":%s*(%d+)'))
    if not pv_now then
        local error_message = "Error: Unable to extract pv_now value from API data. pv_now value set to 0"
        assert(false, error_message)                                                    -- This will write the error message to the error log
        pv_now = 0                                                                      -- Set a default value to allow the script to continue
    end

    -- Additional troubleshooting lines to validate the input for the commandArray['UpdateDevice'] command
    --local success_message = "DeviceIdx: " .. deviceIdx                                -- Troubleshooting line
    --print(success_message)  -- Log success message                                    -- Troubleshooting line
    --local success_message = "nvalue: " .. nValue                                      -- Troubleshooting line
    --print(success_message)  -- Log success message                                    -- Troubleshooting line
    --local success_message = "pv_now: " .. pv_now                                      -- Troubleshooting line
    --print(success_message)  -- Log success message                                    -- Troubleshooting line

    -- Update Domoticz virtual sensor
    --local success_message = "Start updating virtual sensor " .. deviceIdx .. " with PV power: " .. pv_now
    --print(success_message)                                                            -- Troubleshooting line
    commandArray['UpdateDevice'] = deviceIdx .. '|' .. nValue .. '|' .. pv_now .. ';' ..pv_now

    -- Log successful update
    local success_message = "Successfully updated virtual sensor " .. deviceIdx .. " with PV power: " .. pv_now
    print(success_message)                                                              -- Log success message

end

return commandArray
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest