Created
July 20, 2023 01:05
-
-
Save xanderificnl/ae5a4da6e1ae01a1aa3af6b906dfc90b to your computer and use it in GitHub Desktop.
Fetch EasyEnergy prices (for openhab-jruby)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Group EnergyMarkets "Energy Markets" | |
# Group NaturalGasMarketPrices "Natural Gas" (EnergyMarkets) | |
# Group EnergyMarketPrices "Energy" (EnergyMarkets) | |
# Number EnergyPrice "Energy Price [%.3f €/kWh]" (EnergyMarketPrices) | |
# Number EnergyBasePrice "Energy Base Price [%.3f €/kWh]" (EnergyMarketPrices) | |
# Number EnergyProcurementSurcharge "Energy Procurement Price [%.3f €/kWh]" (EnergyMarketPrices) | |
# Number EnergyTaxSurcharge "Energy Tax Surcharge [%.3f €/kWh]" (EnergyMarketPrices) | |
# Number EnergyGreenSurcharge "Energy Green Surcharge [%.3f €/kWh]" (EnergyMarketPrices) | |
# Number GasPrice "Gas Price [%.3f €/m³]" (NaturalGasMarketPrices) | |
# Number GasBasePrice "Gas Base Price [%.3f €/m³]" (NaturalGasMarketPrices) | |
# Number GasProcurementSurcharge "Gas Procurement Price [%.3f €/kWh]" (NaturalGasMarketPrices) | |
# Number GasTaxSurcharge "Gas Tax Surcharge [%.3f €/m³]" (NaturalGasMarketPrices) | |
# Number GasRegionalTax "Gas Regional Tax [%.3f €/m³]" (NaturalGasMarketPrices) | |
begin | |
debug = false | |
gemfile do | |
source "https://rubygems.org" | |
gem "json", "~> 2.6", require: true | |
gem "nokogiri", "~> 1.15", require: true | |
gem "open-uri", "~> 0.3.0", require: true | |
end | |
def url_open(url) | |
logger.info("Downloading #{url}") | |
URI.open(url) | |
end | |
def cache_file(name) | |
"/cache/#{name}.json" | |
end | |
def download(url, target) | |
File.write(cache_file(target), url_open(url).string) | |
end | |
def fetch(target) | |
JSON.load File.read(cache_file(target)) | |
end | |
require "cgi" | |
url_prefix = (debug ? "http://192.168.88.254:8000" : "https://mijn.easyenergy.com/nl") | |
#url_prefix = "/api/tariff" | |
markets = { | |
:gas => "leba", | |
:energy => "apx" | |
} | |
[:gas, :energy].each do |target| | |
mark = target.to_s | |
rule "Fetch daily #{mark} prices" do | |
on_load | |
every :day, at: "06:30" | |
every :day, at: "15:30" | |
run do | |
market = markets[target] | |
start = DateTime.now.new_offset(0) - 1/24r # subtract an hour | |
finish = start + 1 # add a day | |
url = url_prefix + (debug ? "" : "/api/tariff") | |
url = "#{url}/get%stariffs?startTimestamp=%s&endTimestamp=%s&grouping=&includeVat=true" \ | |
% [market, start, finish].map { |c| CGI.escape(c.to_s) } | |
download(url, mark) | |
end | |
end | |
rule "Set hourly #{mark} price" do | |
on_load | |
every :hour | |
run do | |
tag = (target == :gas ? GasBasePrice : EnergyBasePrice) | |
finish = DateTime.now.new_offset(0) | |
start = finish - 1/24r | |
document = fetch(mark) | |
if document.length > 0 | |
prices = document.select do |price| | |
(start..finish).cover? DateTime.parse(price["Timestamp"]) | |
end | |
unless prices.length > 0 | |
raise "No price available for #{mark} between #{start} and #{finish}" | |
end | |
tag << prices.first["TariffUsage"].to_f | |
else | |
raise "No prices available for #{mark}" | |
end | |
end | |
end | |
end | |
rule "Fetch surcharges for energy & gas" do | |
every :day | |
run do | |
document = Nokogiri::HTML url_open("#{url_prefix}/energietarieven") | |
xpath = "/html/body/div[4]/div/div/div/p[2]/text()[%s]" | |
surcharges = { | |
"EnergyProcurementSurcharge" => xpath % 5, | |
"EnergyTaxSurcharge" => xpath % 6, | |
"EnergyGreenSurcharge" => xpath % 7, | |
"GasProcurementSurcharge" => xpath % 8, | |
"GasTaxSurcharge" => xpath % 9, | |
"GasRegionalTax" => xpath % 10 | |
} | |
surcharges.each do |surcharge, path| | |
document.xpath(path).each do |line| | |
line = line.content.gsub(",", ".") | |
price = line.match(/\d+.\d+ ct/).values_at(0).first.to_f / 100 | |
items[surcharge] << price.truncate(5) | |
end | |
end | |
end | |
end | |
rescue => details | |
error = "easy-energy.rb: #{details.full_message}" | |
logger.warn(error) | |
notify(error) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment