LUA Script for daily gas consumption based on the degree days
Posted: Wednesday 15 February 2017 10:07
I wanted to calculate the degree day. So I can compare my gas usage with the outside weather. So I was uploading my P1 gasmeter counter to mindergas.nl See this wiki page: https://www.domoticz.com/wiki/Mindergas.nl
But; I wanted to register this information in Domoticz. So I created a LUA script that calculate the daily gas consumption based on the degree days and put that in a counter in Domoticz.
The script is working. See screenshot (I’m currently renovating my home, so the usage is currently not constant…) The script:
I don’t have many experience with programming in LUA. So I’m posting this script to help other users that also want to see their daily gas consumption based on the degree days.
And I’m hoping that other users will check my LUA script for errors. (For example; I use JSON to get the daily gas usage. And that requires additional libraries: https://www.domoticz.com/wiki/Remote_Co ... _Libraries I’m guessing that it can be done on a nicer method.)
Suggestions are welcome!
But; I wanted to register this information in Domoticz. So I created a LUA script that calculate the daily gas consumption based on the degree days and put that in a counter in Domoticz.
The script is working. See screenshot (I’m currently renovating my home, so the usage is currently not constant…) The script:
Code: Select all
--script_time_degreeday.lua
-- Info about calculation of degree day
--If outsideTemp (day average) > 18 degree Then degreeday = 0
--If outsideTemp (day average) < 18 degree then:
-- If OutsideTemp > InsideTemp then degreeday = 0
-- If OutsideTemp < InsideTemp then : InsideTemp - OutsideTemp = degreeday
-- Weight degreeday:
-- April t/m september: 0,8
-- Maart en oktober: 1,0
-- November t/m februari: 1,1
-- degreeday = degreeday * weight
-- Total gas daily used / degreeday = how many gas m3 is used on degreeday
-- Vars that can be changed
Debug = 0 --Enable/Disable logging
Domo_IP = ('192.168.1.180')
Domo_Port = ('8080')
Gasmeter_IDX = 75 --To read daily gas usage. I use my P1 Smart meter
local DegreeDay_IDX = 217 --Device ID to write calculated Degreeday (new virtual gas sensor)
Temp_Outside_name = ('Buiten Temperatuur') --Name of outside temp sensor. I use this: https://www.domoticz.com/wiki/Virtual_weather_devices
Temp_Inside_name = ('Kamer Temp') --Name of inside temp sensor. I use my iSense Thermostat
Gas_Meter_name = ('Gasmeter') --Name of Gas meter. For displaying total gas usage (no functional usage)
Base_Temp = 18 --is the outside temperature above which the building does not require heating. UK is 15.5 / US is 18 But it depends with building
-- End Vars
commandArray = {}
time = os.date("*t")
if((time.min % 10)==0)then --Run every 10 minutes
Base_Temp = tonumber(Base_Temp)
temp_outside = tonumber(otherdevices_svalues[Temp_Outside_name]:match("([^;]+)"))
temp_inside = tonumber(otherdevices_svalues[Temp_Inside_name]:match("([^;]+)"))
degreeday = 0
if temp_outside > Base_Temp then degreeday = 0 end
if temp_outside < Base_Temp then
if temp_outside > temp_inside then degreeday = 0 end
if temp_outside < temp_inside then degreeday = temp_inside - temp_outside end
end
month = os.date("%m")
degreeday_weight = 0
if month == "01" then degreeday_weight = ('1.1') end
if month == "02" then degreeday_weight = ('1.1') end
if month == "03" then degreeday_weight = ('1.0') end
if month == "04" then degreeday_weight = ('0.8') end
if month == "05" then degreeday_weight = ('0.8') end
if month == "06" then degreeday_weight = ('0.8') end
if month == "07" then degreeday_weight = ('0.8') end
if month == "08" then degreeday_weight = ('0.8') end
if month == "09" then degreeday_weight = ('0.8') end
if month == "10" then degreeday_weight = ('1.0') end
if month == "11" then degreeday_weight = ('1.1') end
if month == "12" then degreeday_weight = ('1.1') end
degreeday = tonumber(degreeday) * tonumber(degreeday_weight)
gas_usage = tonumber(otherdevices_svalues[Gas_Meter_name]:match("([^;]+)"))
--###############################
-- Load necessary Lua libraries
--###############################
http = require "socket.http";
socket = require "socket";
https = require "ssl.https";
JSON = require "JSON";
local t, jresponse, status, decoded_response
server_url = "http://"..Domo_IP..":"..Domo_Port
t = server_url.."/json.htm?type=graph&sensor=counter&range=week&idx="..tostring(Gasmeter_IDX)
jresponse, status = http.request(t)
decoded_response = JSON:decode(jresponse)
result = decoded_response["result"]
gas_usage_today = tonumber(result[8]["v"])
degreeday_usage = gas_usage_today / degreeday
if Debug == 1 then
print("OutsideTemp: " ..temp_outside)
print("Inside Temp: " ..temp_inside)
print("Base Temp: " ..Base_Temp)
print("Gas total usage: " ..gas_usage)
print("Month: " ..month)
print("Degreeday Weight: " ..degreeday_weight)
print("Degree Day: " ..degreeday)
print("Gas usage today: "..gas_usage_today)
print("Degreeday usage today: "..degreeday_usage)
end
degreeday_usage1 = degreeday_usage * 1000
commandArray[DegreeDay_IDX] = { ['UpdateDevice'] = DegreeDay_IDX..'|0|'..degreeday_usage1 }
end
return commandArray
And I’m hoping that other users will check my LUA script for errors. (For example; I use JSON to get the daily gas usage. And that requires additional libraries: https://www.domoticz.com/wiki/Remote_Co ... _Libraries I’m guessing that it can be done on a nicer method.)
Suggestions are welcome!