Page 1 of 1
Problem with dzVents HTTP Request
Posted: Thursday 25 March 2021 19:12
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
}
Re: Problem with dzVents HTTP Request
Posted: Thursday 25 March 2021 19:29
by waaren
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?
Re: Problem with dzVents HTTP Request
Posted: Thursday 25 March 2021 20:37
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}}
Re: Problem with dzVents HTTP Request
Posted: Thursday 25 March 2021 21:25
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
}
Re: Problem with dzVents HTTP Request [Solved]
Posted: Friday 26 March 2021 8:01
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.
Re: Problem with dzVents HTTP Request
Posted: Friday 26 March 2021 10:35
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))