Problem with dzVents HTTP Request  [Solved]

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

Moderator: leecollings

Post Reply
bolek
Posts: 3
Joined: Thursday 25 March 2021 18:57
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Problem with dzVents HTTP Request

Post by bolek »

Hi. I have a problem with a script that gets a value from api ethermine (https://api.ethermine.org/) . The script returns 0 value. If i asks other api, e.g. https://corona.lmao.ninja/v3/covid-19/c ... s/slovakia the script returns valid values.


Code: Select all

return {
    on = {
        timer = { 'every 1 minutes' },
         system = {
            'start',
        },
        httpResponses = { 'currentHashrate' }
    },
    execute = function(domoticz, item)
        if (item.isTimer) then
            domoticz.openURL({
                url = 'https://api.ethermine.org/miner/mymineraddress/currentStats/',
                method = 'GET',
                callback = 'currentHashrate'
            })
        end

        if (item.isHTTPResponse and item.ok) then
            domoticz.devices('Hashrate').updateCustomSensor(item.json['currentHashrate'])
        end
    end
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Problem with dzVents HTTP Request

Post by waaren »

bolek wrote: Thursday 25 March 2021 19:12 Hi. I have a problem with a script that gets a value from api ethermine (https://api.ethermine.org/) . The script returns 0 value. If i asks other api, e.g. https://corona.lmao.ninja/v3/covid-19/c ... s/slovakia the script returns valid values.
When I use the HTTP call from the script ( https://api.ethermine.org/miner/myminer ... rentStats/ ), the response is

Code: Select all

{
      "status": "ERROR",
       "error": "Invalid address"
}
so I assume your HTTP cal is different. What do you see when you enter that other one in the browser address bar?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
bolek
Posts: 3
Joined: Thursday 25 March 2021 18:57
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Problem with dzVents HTTP Request

Post by bolek »

you need to replace "mymineraddress" with your wallet address. E.g https://api.ethermine.org/miner/0xEAC84 ... rentStats/

and the resoponse is

Code: Select all

{"status":"OK","data":{"time":1616700600,"lastSeen":1616700568,"reportedHashrate":1415928031,"currentHashrate":1415055555.5555553,"validShares":1269,"invalidShares":0,"staleShares":7,"averageHashrate":1392374999.9999993,"activeWorkers":6,"unpaid":27800501218674976,"unconfirmed":null,"coinsPerMin":0.00005827565773093277,"usdPerMin":0.09365422678280473,"btcPerMin":0.0000018088764159681532}}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Problem with dzVents HTTP Request

Post by waaren »

bolek wrote: Thursday 25 March 2021 20:37 The resoponse is

Code: Select all

{"status":"OK","data":{"time":1616700600,"lastSeen":1616700568,"reportedHashrate":1415928031,"currentHashrate":1415055555.5555553,"validShares":1269,"invalidShares":0,"staleShares":7,"averageHashrate":1392374999.9999993,"activeWorkers":6,"unpaid":27800501218674976,"unconfirmed":null,"coinsPerMin":0.00005827565773093277,"usdPerMin":0.09365422678280473,"btcPerMin":0.0000018088764159681532}}
The value you are looking for is a level deeper in the JSON.

Can you try this one?

Code: Select all

local scriptVar = 'currentHashrate'

return
{
    on =
    {
        timer =
        {
            'every 1 minutes', -- change to required frequency
        },
        httpResponses =
        {
            scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,  -- set to domoticz.LOG_ERROR when all ok
        marker = 'miner',
    },

    execute = function(domoticz, item)
        if item.isTimer then
            domoticz.openURL(
            {
                url = 'https://api.ethermine.org/miner/mymineraddress/currentStats/',
                callback = scriptVar,
            })
        end

        if (item.isHTTPResponse and item.ok) then
            domoticz.devices('Hashrate').updateCustomSensor(domoticz.utils.round(item.json.data.currentHashrate, 3))
        end
    end
}

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
bolek
Posts: 3
Joined: Thursday 25 March 2021 18:57
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Problem with dzVents HTTP Request  [Solved]

Post by bolek »

Now works. Thank you very much. I have one more question, the return value is in this form 1.99167e+08. But when I set my virtual device to "text", and raplece "domoticz.devices('Hashrate').updateCustomSensor(domoticz.utils.round(item.json.data.currentHashrate, 3))" to "domoticz.devices('Hashrate').updateText(domoticz.utils.round(item.json.data.currentHashrate, 3))" the value is correct 199166666.6667. How can I covert floating value to an integer? Sorry but my domoticz skills are very limited.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Problem with dzVents HTTP Request

Post by waaren »

bolek wrote: Friday 26 March 2021 8:01 I have one more question. How can I covert floating value to an integer? Sorry but my domoticz skills are very limited.
the conversion to scientific notation is something that is done deeper in domoticz for counter- and custom sensor type of devices. This cannot be influenced by dzVents.

Conversion (which will obviously include rounding) to integer in dzVents (= Lua) can be done using the math.floor() function.

Code: Select all

domoticz.devices('Hashrate').updateCustomSensor(math.floor(tonumber(item.json.data.currentHashrate) + 0.5)) 
or

Code: Select all

domoticz.devices('Hashrate').updateText(math.floor(tonumber(item.json.data.currentHashrate) + 0.5))
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest