getting values from JSON.htm with dzVents script

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
User avatar
RonkA
Posts: 100
Joined: Tuesday 14 June 2022 12:57
Target OS: NAS (Synology & others)
Domoticz version: 2025.1
Location: Harlingen
Contact:

getting values from JSON.htm with dzVents script

Post by RonkA »

I have a 1000 liter hot water buffer tank where temperatures are measured at different heights.
With these temperatures I calculate a capacity number in the form of electrical power stored in a virtual device. When the entire vessel is at 80 degrees i consider it full, if full it contains about 50 kilowatts of heating power.
buffervat.JPG
buffervat.JPG (31.32 KiB) Viewed 682 times
So far no problem. I notice a daily loss of energy that i would like to compensate with electrical heating power from my solarpannels but how much to compensate i want to calculate.

Through my browser I can read the history of the daily highest and lowest value of this device in the past month:
http://192.168.178.2:7080/json.htm?type ... ange=month This outputs:
Spoiler: show
{
"CostEnergy" : 2200,
"CostEnergyR1" : 1100,
"CostEnergyR2" : 1100,
"CostEnergyT2" : 2200,
"CostGas" : 6218,
"CostWater" : 16473,
"Divider" : 1000.0,
"DividerEnergy" : 1000.0,
"DividerWater" : 1000.0,
"ValueQuantity" : "",
"ValueUnits" : "",
"result" :
[
{
"d" : "2023-04-08",
"u_max" : 2060.0,
"u_min" : 0.0
},
{
"d" : "2023-04-09",
"u_max" : 1690.0,
"u_min" : 0.0
},
{
"d" : "2023-04-10",
"u_max" : 2030.0,
"u_min" : 0.0
},
{
"d" : "2023-04-11",
"u_max" : 0.0,
"u_min" : 0.0
},
{
"d" : "2023-04-12",
"u_max" : 0.0,
"u_min" : 0.0
},
{
"d" : "2023-04-13",
"u_max" : 0.0,
"u_min" : 0.0
},
{
"d" : "2023-04-14",
"u_max" : 0.0,
"u_min" : 0.0
},
{
"d" : "2023-04-15",
"u_max" : 0.0,
"u_min" : 0.0
},
{
"d" : "2023-04-16",
"u_max" : 68230.0,
"u_min" : 0.0
},
{
"d" : "2023-04-17",
"u_max" : 67960.0,
"u_min" : 50070.0
},
{
"d" : "2023-04-18",
"u_max" : 50030.0,
"u_min" : 42100.0
},
{
"d" : "2023-04-19",
"u_max" : 42100.0,
"u_min" : 37100.0
},
{
"d" : "2023-04-20",
"u_max" : 37100.0,
"u_min" : 23870.0
},
{
"d" : "2023-04-21",
"u_max" : 23830.0,
"u_min" : 10570.0
},
{
"d" : "2023-04-22",
"u_max" : 10570.0,
"u_min" : 610.0
},
{
"d" : "2023-04-23",
"u_max" : 640.0,
"u_min" : 0.0
},
{
"d" : "2023-04-24",
"u_max" : 31600.0,
"u_min" : 0.0
},
{
"d" : "2023-04-25",
"u_max" : 30750.0,
"u_min" : 22860.0
},
{
"d" : "2023-04-26",
"u_max" : 22860.0,
"u_min" : 17350.0
},
{
"d" : "2023-04-27",
"u_max" : 17320.0,
"u_min" : 12360.0
},
{
"d" : "2023-04-28",
"u_max" : 48010.0,
"u_min" : 840.0
},
{
"d" : "2023-04-29",
"u_max" : 47470.0,
"u_min" : 34640.0
},
{
"d" : "2023-04-30",
"u_max" : 34640.0,
"u_min" : 27350.0
},
{
"d" : "2023-05-01",
"u_max" : 27350.0,
"u_min" : 21840.0
},
{
"d" : "2023-05-02",
"u_max" : 21840.0,
"u_min" : 17350.0
},
{
"d" : "2023-05-03",
"u_max" : 17350.0,
"u_min" : 13880.0
},
{
"d" : "2023-05-04",
"u_max" : 13910.0,
"u_min" : 11340.0
},
{
"d" : "2023-05-05",
"u_max" : 11340.0,
"u_min" : 9660.0
},
{
"d" : "2023-05-06",
"u_max" : 9660.0,
"u_min" : 8610.0
},
{
"d" : "2023-05-07",
"u_max" : 8640.0,
"u_min" : 7760.0
},
{
"d" : "2023-05-08",
"u_max" : 7760.0,
"u_min" : 7360.0
}
],
"status" : "OK",
"title" : "Graph counter month"
}
Now my wish: I want to send myself a daily report of the past day in an email with various data using dzVents, for that I have to extract the last entry of u_max and u_min from the JSON from the buffer tank.
With these values ​​I can then calculate a loss and a percentage, and with this data and other data I can then prepare an email which will be sent at the end of the day.

And this is where it goes weird, As a test i use a testscript trying to dump the json data in the log.

Code: Select all

return {
    on = {
        timer = {
            'every minute'
        }
    },
    logging = {level = domoticz.LOG_INFO},
    execute = function(domoticz, triggeredItem)
        local url = 'http://172.17.0.2:80/json.htm?type=graph&sensor=counter&idx=233&range=month'
        domoticz.openURL({
            url = url,
            method = 'GET',
            callback = function(response)
                if (response.ok and response.status == 200) then
                    domoticz.log('Raw data: ' .. response.data, domoticz.LOG_INFO)
                else
                    domoticz.log('Error opening url: ' .. url, domoticz.LOG_INFO)
                end
            end
        })
    end
}
I'm running Domoticz on a Synology NAS in a Docker container, so I can't use 192.168.178.2 to request a JSON in a script.
Also not 127.0.0.1, because that is the loopback that Docker container uses at the outside of the container, so I searched further and found that Domoticz IN the container (probably) uses 127.17.0.2. Added this IP address to trusted networks just to be sure.
Docker container uses a bridge to share internal communications with the outside world, forwarding port 80 internally to port 7080.
this testscript runs without error messages but also does not display any data(debug-log gives:)

Code: Select all

dzVents: Info: ------ Start internal script: Script #2:, trigger: "every minute"
dzVents: Debug: OpenURL: url = http://172.17.0.2:80/json.htm?type=graph&sensor=counter&idx=233&range=month
dzVents: Debug: OpenURL: method = GET
dzVents: Debug: OpenURL: post data = nil
dzVents: Debug: OpenURL: headers = nil
dzVents: Debug: OpenURL: callback = function
dzVents: Info: ------ Finished Script #2 
Now I do not know whether this is due to an error in the testscript, or whether this is due to the ip address used, or whether its possible to read data at all from a JSON.htm using a dzVents script, but i can't figure it out..

some help would be very welcome.
SolarEdge ModbusTCP - Kaku - Synology NAS - Watermeter - ESPEasy - DS18b20
Work in progress = Life in general..
User avatar
waltervl
Posts: 5711
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: getting values from JSON.htm with dzVents script

Post by waltervl »

You have some brackets on the wrong place for domoticz.openURL They should end after the callback })
Also callback is a string and not a declaration of a function.

Here is a nice example in the documentation how fetching data from Domoticz could look like:

https://www.domoticz.com/wiki/DzVents:_ ... icz_itself
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
RonkA
Posts: 100
Joined: Tuesday 14 June 2022 12:57
Target OS: NAS (Synology & others)
Domoticz version: 2025.1
Location: Harlingen
Contact:

Re: getting values from JSON.htm with dzVents script

Post by RonkA »

Thanks for the response, i saw that part of the wiki also but because i wasnt shure to get the right ip adress and port to work i searched furter.
The example has an quotation mark error in it at line 32:

Code: Select all

                print('State: ' .. node.State')
to

Code: Select all

                print('State: ' .. node.State)
With some trial and error i found the ip and portnumber are working correct in the Docker-container, now i have a working testscript that i can use:

Code: Select all

return {
    on = {
        timer = {
            'every minute'
        },
        httpResponses = {
            'counterData'
        }
    },
    execute = function(domoticz, item)
        if item.isTimer then
            domoticz.openURL({
                url = 'http://127.17.0.2:80/json.htm?type=graph&sensor=counter&idx=233&range=month',
                method = 'GET',
                callback = 'counterData'
            })
        end

        if item.isHTTPResponse and item.ok then
            local response = item.json
            if response.status == 'OK' then
                local counterData = response.result
                local lastIndex = #counterData
                if lastIndex > 0 then
                    local lastEntry = counterData[lastIndex]
                    local uMax = lastEntry.u_max or 'N/A'
                    local uMin = lastEntry.u_min or 'N/A'
                    print('Last u_max: ' .. uMax)
                    print('Last u_min: ' .. uMin)
                else
                    print('No counter data found')
                end
            else
                print('Error in JSON response: ' .. response.status)
            end
        end
    end
}
I now understand that the 'httpResponses' and 'callback' uses a word as identifier so this can be any unrelated word or number so it doesnt matter if its called 'counterData' or 'zwaveInfo' (or 'Banana'...)
Next hurdle is making a message from this and mailing it.. that part i will try to find out myself.. i hope ;)
SolarEdge ModbusTCP - Kaku - Synology NAS - Watermeter - ESPEasy - DS18b20
Work in progress = Life in general..
Post Reply

Who is online

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