open multiple urls and combine results  [Solved]

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

Moderator: leecollings

Post Reply
User avatar
raymond1972
Posts: 32
Joined: Wednesday 28 February 2018 13:00
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

open multiple urls and combine results

Post by raymond1972 »

Hi All,

I'm currently the happy owner of two solar systems which I already manage to read in domoticz.
Now I want to combine the reading in one dummy device.
In order to do that I was programming in dzvents to get the results of the CounterToday value of both the inverters.

see the attached script.

Code: Select all

return {
    on = {
        timer = { 'every 1 minutes' },
        httpResponses = {
			'trigger' -- must match with the callback passed to the openURL command
		}
    },
    execute = function(domoticz, item)
        if (item.isTimer) then
            domoticz.openURL({
                url = 'http://xx.x.x.xxx:8080/json.htm?username=xxxxx=&password=xxxxx=&type=devices&rid=679',
                method = 'GET',
                callback = 'trigger',
            })
        end
        if (item.isHTTPResponse and item.ok) then
            local results = item.json.result
            -- loop through the nodes and print some info
            for i, node in pairs(results) do
                --local lastUpdate = Time(node.LastUpdate)
                --print(node.Name ..' vandaag opgeleverd: ' ..node.CounterToday)
                opbrengst1 = node.CounterToday
                opbr1 = opbrengst1.gsub(opbrengst1, "( kWh)", "", 1)
                opb1 = opbr1 + 0
                print('opb1 :'..opb1)
            end
            for i, node in pairs(results) do
                results[i] = nil
            end
            local results = nil
        end
        if (item.isTimer) then
            domoticz.openURL({
                url = 'http://xx.x.x.xxx:8080/json.htm?username=xxxxx=&password=xxxxx=&type=devices&rid=680',
                method = 'GET',
                callback = 'trigger',
            })
        end
        if (item.isHTTPResponse and item.ok) then
            --local Time = require('Time')
            local results = item.json.result
            -- loop through the nodes and print some info
            for i, node in pairs(results) do
                -- convert the time stamp in the raw data into a
                -- dzVents Time object
                --local lastUpdate = Time(node.LastUpdate)
                print(node.Name ..' vandaag opgeleverd: ' ..node.CounterToday)
                local opbrengst2 = node.CounterToday
                opbr2 = opbrengst2.gsub(opbrengst2, "( kWh)", "", 1)
                opb2 = opbr2 + 0
            end
            for i, node in pairs(results) do
                results[i] = nil
            end
        end
    print('opb1 :'..opb1 ..'&'..'opb2 :'..opb2)
    opbtot = opb1 + opb2
    print('Zonnepanelen opbrengst: ' ..opbtot)
    end
}
When I debug this all went ok until the translation from string to number value.
the number value is printed twice!

Code: Select all

2020-06-04 12:02:00.471 Status: EventSystem: Script event triggered: timer_script_new
2020-06-04 12:02:00.555 Status: dzVents: opb1 :2.183
2020-06-04 12:02:00.621 Status: dzVents: opb1 :1.626
Can anyone help me out to achieve:
Open url device 1 get value of CounterToday as number
Open url device 2 get value of CounterToday as number
calculate the combined new value of CounterToday
write value back to dummy device

Thanx in advance.
HW:Raspberry Pi 3 B, Raspian stretch, Nano, Aeon Labs Aeotec Gen 5 Z-Wave USB
Raspberry Pi 4, Raspian buster, Nano, Aeon Labs Aeotec Gen 5 Z-Wave USB
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: open multiple urls and combine results

Post by waaren »

raymond1972 wrote: Thursday 04 June 2020 12:09 I'm currently the happy owner of two solar systems which I already manage to read in domoticz.
Now I want to combine the reading in one dummy device.
In order to do that I was programming in dzvents to get the results of the CounterToday value of both the inverters.
You most likely don't need the openURL's at all.

What are the device types and subtypes of the devices that you want to combine and what is the type / subtype of the coombined device?
(as seen on the devices tab)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
raymond1972
Posts: 32
Joined: Wednesday 28 February 2018 13:00
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: open multiple urls and combine results

Post by raymond1972 »

Hi Waaren,

thank you for your quick response.
the types are:

680 Zonnepanelen Overkapping 00000001 1 Zonnepanelen overkapping General kWh 393.491 kWh
679 Zonnepanelen Woonhuis 00000001 1 Zonnepanelen woonhuis General kWh 15743.851 kWh

combined device:
731 Script device 00082731 1 Zonopbrengst General kWh 0, 5663.0
HW:Raspberry Pi 3 B, Raspian stretch, Nano, Aeon Labs Aeotec Gen 5 Z-Wave USB
Raspberry Pi 4, Raspian buster, Nano, Aeon Labs Aeotec Gen 5 Z-Wave USB
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: open multiple urls and combine results

Post by waaren »

raymond1972 wrote: Thursday 04 June 2020 12:25 680 Zonnepanelen Overkapping 00000001 1 Zonnepanelen overkapping General kWh 393.491 kWh
679 Zonnepanelen Woonhuis 00000001 1 Zonnepanelen woonhuis General kWh 15743.851 kWh

731 Script device 00082731 1 Zonopbrengst General kWh 0, 5663.0
Can you check this one?

Please note: The dayTotal will not be accurate on the first day.

Code: Select all

return
{
    on =
    {
        devices =
        {
            'Zonnepanelen overkapping',
            'Zonnepanelen woonhuis',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
    },

    execute = function(dz, item)
        dz.log('Device ' .. item.name .. ' triggered this script', dz.LOG_DEBUG)

        local solarIn1 = dz.devices('Zonnepanelen overkapping')
        local solarIn2 = dz.devices('Zonnepanelen woonhuis')

        local solarOut = dz.devices('Zonopbrengst')

        local power   = solarIn1.WhActual + solarIn2.WhActual
        local energy  = solarIn1.WhTotal  + solarIn2.WhTotal

        solarOut.updateElectricity(power, energy)

    end
}
The target device (731) must be set to receive the energy read from device (like below picture)
settings.png
settings.png (16.73 KiB) Viewed 1719 times
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
raymond1972
Posts: 32
Joined: Wednesday 28 February 2018 13:00
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: open multiple urls and combine results

Post by raymond1972 »

Hi Waaren,

That script sure looks a lot more simplified!
At this moment it does not update the virtual sensor.
script is added as dzvents-device
I notice two things:
1. the function refers to dz, instead of domoticz,
2. the values are in Wh instead of kWh
Do those things matter?

the virtual device is configured as follows:


Kind regards,
Raymond
Attachments
Schermafbeelding 2020-06-04 om 13.45.39.png
Schermafbeelding 2020-06-04 om 13.45.39.png (28.66 KiB) Viewed 1715 times
HW:Raspberry Pi 3 B, Raspian stretch, Nano, Aeon Labs Aeotec Gen 5 Z-Wave USB
Raspberry Pi 4, Raspian buster, Nano, Aeon Labs Aeotec Gen 5 Z-Wave USB
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: open multiple urls and combine results

Post by waaren »

raymond1972 wrote: Thursday 04 June 2020 13:45 At this moment it does not update the virtual sensor.
the script will trigger when one of the solar power devices are updated. So if the are updated every 10 seconds it will trigger every 10 seconds. If they are updated once per hour the script will trigger once per hour.

You can check the log to see if the script is triggered.
I notice two things:
1. the function refers to dz, instead of domoticz,
2. the values are in Wh instead of kWh
1. dz is here the name of first parm of the execute function. The name does not matter as long as the same name is used consistent in the execute function.

2. That is how it is coded. The translation to kWh is performed deeper in the domoticz code
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
raymond1972
Posts: 32
Joined: Wednesday 28 February 2018 13:00
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: open multiple urls and combine results  [Solved]

Post by raymond1972 »

Hi Waaren,

Thanx a lot.
I just corrected Zonnepanelen Overkapping -> Zonnepanelen overkapping and Zonnepanelen Woonhuis -> Zonnepanelen woonhuis in the script and now it's working.

Thanks for helping me out!
HW:Raspberry Pi 3 B, Raspian stretch, Nano, Aeon Labs Aeotec Gen 5 Z-Wave USB
Raspberry Pi 4, Raspian buster, Nano, Aeon Labs Aeotec Gen 5 Z-Wave USB
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest