Skip to content

Instantly share code, notes, and snippets.

@kipusoep
Last active February 16, 2023 21:34
Show Gist options
  • Save kipusoep/e1e0804ba7503e6806e8b7ec03298dfa to your computer and use it in GitHub Desktop.
Save kipusoep/e1e0804ba7503e6806e8b7ec03298dfa to your computer and use it in GitHub Desktop.
Ophalen van EnergyZero energieprijzen voor Domoticz
-- Instructions --
-- Create the dummy hardware in Domoticz if you haven't already and from there,
-- create two 'Custom Sensor' devices with the axis labels 'EUR/kWh' and 'EUR/m³'.
-- Then set the following two device ids:
local powerDeviceIdx = 222
local gasDeviceIdx = 223
return {
on = {
timer = {
'every hour'
},
httpResponses = {
'triggerPower',
'triggerGas'
},
--system = {
-- 'resetAllEvents'
--}
},
logging = {
level = domoticz.LOG_INFO,
marker = 'Energieprijzen',
},
execute = function(domoticz, item)
local Time = require('Time')
local currentTime = Time()
if (item.isHTTPResponse) then
if (item.ok) then
if (item.isJSON) then
local result_table = item.json
local price = result_table.Prices[1].price;
if (item.trigger == 'triggerPower') then
price = price * 1.09 + ( 0.01472 + 0.04010 + 0.03325 )
local device = domoticz.devices(powerDeviceIdx);
device.updateCustomSensor(price)
domoticz.log('Updated power price: ' .. price)
elseif (item.trigger == 'triggerGas') then
price = price * 1.09 + ( 0.05230 + 0.39591 + 0.09429 )
local device = domoticz.devices(gasDeviceIdx);
device.updateCustomSensor(price)
domoticz.log('Updated gas price: ' .. price)
end
else
domoticz.log('HTTP is not json', domoticz.LOG_ERROR)
end
else
domoticz.log('There was a problem handling the request: ' .. url, domoticz.LOG_ERROR)
domoticz.log(item, domoticz.LOG_ERROR)
end
else
local utc = os.date("!*t")
local timestamp = domoticz.time.rawDate .. "T" .. utc.hour .. ":00:00.000Z"
domoticz.openURL({
url = 'https://api.energyzero.nl/v1/energyprices?fromDate=' .. timestamp .. '&tillDate=' .. timestamp .. '&interval=4&usageType=1&inclBtw=false',
method = 'GET',
callback = 'triggerPower',
})
domoticz.openURL({
url = 'https://api.energyzero.nl/v1/energyprices?fromDate=' .. timestamp .. '&tillDate=' .. timestamp .. '&interval=4&usageType=4&inclBtw=false',
method = 'GET',
callback = 'triggerGas',
})
end
end
}
@kipusoep
Copy link
Author

Thanks for the suggestion, done.

@HBC1984
Copy link

HBC1984 commented Nov 6, 2022

Nice script!

//Edit

And for te dummy's like me, its a DzVents script :)

@kipusoep
Copy link
Author

kipusoep commented Nov 6, 2022

Nice script. I'm running it from a Raspberry and needed to change one thing to get it to run.

local utc = os.date("!*t") >> local utc = os.date("!*to")

And for te dummy's like me, its a DzVents script :)

I'm also running it on a raspberry, so I wonder what the difference between our setups is?

@HBC1984
Copy link

HBC1984 commented Nov 6, 2022

Nice script. I'm running it from a Raspberry and needed to change one thing to get it to run.
local utc = os.date("!*t") >> local utc = os.date("!*to")
And for te dummy's like me, its a DzVents script :)

I'm also running it on a raspberry, so I wonder what the difference between our setups is?

I was maybe to quick to answer, i changed al the things back. And all is (still) working.
Don't know why, maybe i was not waiting for the 'Hour' trigger or something like that.

The first error i was getting was:
2022-11-06 11:05:26.663 Error: EventSystem: in Dagprijzen Dynamisch: [string "local powerDeviceIdx = 548 ..."]:18: attempt to index a nil value (global 'domoticz')

But that was under Lua in Domoticz.

@HBC1984
Copy link

HBC1984 commented Nov 6, 2022

Love the script. :)

Is it possible to get a extra custom Sensor button with the average of the day?
I see it down the page in the api link. But i don;t know how to script that part.
], "intervalType": 4, "average": 0.08, "fromDate": "2022-11-06T01:00:00Z", "tillDate": "2022-11-07T00:00:01Z" }

So you now you are on the cheap or the expansive part of the day. :)

@wkossen
Copy link

wkossen commented Nov 30, 2022

But that was under Lua in Domoticz.

same error here... how can i fix that?

@kipusoep
Copy link
Author

@wkossen I guess by using dzvents instead of lua :-)

@wkossen
Copy link

wkossen commented Nov 30, 2022

That would be an interesting thing. maybe the .lua extension of the scriptname set me on the wrong path. let me try that :)
indeed. the error is gone, but no values in the custom sensors yet...

@kipusoep
Copy link
Author

Well dzvents is a superset of lua, hence the lua extensions.

@wkossen
Copy link

wkossen commented Nov 30, 2022

Well dzvents is a superset of lua, hence the lua extensions.

i'm such an amateur.... but thanks for your very fast response.... still no values though, but also no errors in log...

@kipusoep
Copy link
Author

Keep in mind it runs every whole hour. You can trigger it on save by uncommenting lines 18 ~ 20.

@barts2108
Copy link

For these 2 lines ..

price = price * 1.09 + ( 0.01472 + 0.04010 + 0.03325 )

price = price * 1.09 + ( 0.05230 + 0.39591 + 0.09429 )

Do you have any explanation what the value's are ? 1.09 I can trace back to dutch VAT (BTW) which is 9% until end of the year.

The other 3 values I have no idea what they are, and why they are there.
What I do know is that I would have put the values and the BTW as well in a variable at the top of the script (or even a user variable)

@hardcodedpassword
Copy link

for electricity these are: energy tax (should be 0.03677 p/kWh), ODE (should be 0.03048), and probability transaction costs (differs per provider).

for gas idem: taxes + ODE + transaction costs (0.36069 + 0.08586 + x )

@barts2108
Copy link

Thanks for that info... it makes more sense now.

@hardcodedpassword
Copy link

@kipusoep it would be better to have those as parameters or config

@barts2108
Copy link

barts2108 commented Dec 29, 2022

it's even a little more complicated. In the script above the energytax, ode and transaction cost are assumed includig VAT (BTW

price = price * 1.09 + ( 0.01472 + 0.04010 + 0.03325 )

If taking all prices without VAT (BTW) the calculation should be

price = (price + 0.01472 + 0.04010 + 0.03325 ) * 1.09

I hope you don't mind that I post my changes here. If so please let me know then I will remove it.

-- Instructions --
-- Create the dummy hardware in Domoticz if you haven't already and from there,
-- create two 'Custom Sensor' devices with the axis labels 'EUR/kWh' and 'EUR/m³'.
-- Then set the following two device ids:

local powerDeviceIdx  = 91
local gasDeviceIdx    = 92

local vatPercentage   = 9       -- value of the VAT (BTW) percentage (without % sign)

local elec = {
  energyTax       = 0.114079, -- must be excluding VAT (BTW) - Put the real values from your energy supplier
  ode             = 0.036300, -- must be excluding VAT (BTW) - Put the real values from your energy supplier
  transactionCost = 0.0,      -- must be excluding VAT (BTW) - Put the real values from your energy supplier
}
local gas = {
  energyTax       = 0.114079, -- must be excluding VAT (BTW) - Put the real values from your energy supplier
  ode             = 0.036300, -- must be excluding VAT (BTW) - Put the real values from your energy supplier
  transactionCost = 0.0,      -- must be excluding VAT (BTW) - Put the real values from your energy supplier
}

return {
  on = {
    timer = {
      'every hour'
    },
    httpResponses = {
      'triggerPower',
      'triggerGas'
    },
    --system = {
    --    'resetAllEvents'
    --}
  },
  logging = {
    level = domoticz.LOG_INFO,
    marker = 'Energieprijzen',
  },
  execute = function(domoticz, item)
    local Time = require('Time')
    local currentTime = Time()

    if (item.isHTTPResponse) then
      
      if (item.ok) then
        if (item.isJSON) then
          local result_table = item.json
          local price = result_table.Prices[1].price;
          
          if (item.trigger == 'triggerPower') then
            price = (price + elec.energyTax + elec.ode + elec.transactionCost) * (1.0 + (vatPercentage / 100))

            local device = domoticz.devices(powerDeviceIdx);
            device.updateCustomSensor(price)
            
            domoticz.log('Updated power price: ' .. price)
          elseif (item.trigger == 'triggerGas') then
            price = (price + gas.energyTax + gas.ode + gas.transactionCost) * (1.0 + (vatPercentage / 100))
            
            local device = domoticz.devices(gasDeviceIdx);
            device.updateCustomSensor(price)
            
            domoticz.log('Updated gas price: ' .. price)
          end
        else
          domoticz.log('HTTP is not json', domoticz.LOG_ERROR)
        end
      else
        domoticz.log('There was a problem handling the request: ' .. url, domoticz.LOG_ERROR)
        domoticz.log(item, domoticz.LOG_ERROR)
      end
    else
      local utc = os.date("!*t")
      local timestamp = domoticz.time.rawDate .. "T" .. utc.hour .. ":00:00.000Z"
      domoticz.openURL({
        url = 'https://api.energyzero.nl/v1/energyprices?fromDate=' .. timestamp .. '&tillDate=' .. timestamp .. '&interval=4&usageType=1&inclBtw=false',
        method = 'GET',
        callback = 'triggerPower',
      })
      domoticz.openURL({
        url = 'https://api.energyzero.nl/v1/energyprices?fromDate=' .. timestamp .. '&tillDate=' .. timestamp .. '&interval=4&usageType=4&inclBtw=false',
        method = 'GET',
        callback = 'triggerGas',
      })
    end
  end
}

@barts2108
Copy link

In case you only can get the taxes inclusive of VAT (BTW) you should use

price = price * (1.0 + (vatPercentage / 100)) + (elec.energyTax + elec.ode + elec.transactionCost)

and

price = price * (1.0 + (vatPercentage / 100)) + (gas.energyTax + gas.ode + gas.transactionCost)

for the calculations

@kipusoep
Copy link
Author

@barts2108 ofcourse I don't mind! Improvements are always welcome.
I was wondering; the energyTax variables you posted are pretty high, is that because of the region you live in?
For me it's approximately € 0,036 for both elec and gas. The amount you've posted is € 0,114. Or is this just an example?

@hardcodedpassword
Copy link

@barts2108
Copy link

barts2108 commented Jan 1, 2023

@kipusoep I take it from my supplier, probably value of 2023 but excl VAT (BTW), and I just see it is merged with ODE

@kipusoep
Copy link
Author

kipusoep commented Jan 1, 2023

That explains @barts2108 😏
Btw, is your gas price still being updated? Mine hasn't since yesterday 1 AM and I see errors in the log.

@kipusoep
Copy link
Author

kipusoep commented Jan 1, 2023

Ah, it seems the energyzero api doesn't return any gas prices at the moment.

@TheLion
Copy link

TheLion commented Feb 16, 2023

It would be great if the script can read the specific price for one supplier from the price feeds from Enever.nl (https://enever.nl/prijzenfeeds/) so everybody (from the Netherlands), no matter what supplier you have, can use your script. And you have the prices with and without taxes and all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment