LUA Script for daily gas consumption based on the degree days

Moderator: leecollings

Post Reply
SilentNL
Posts: 6
Joined: Thursday 25 June 2015 10:08
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

LUA Script for daily gas consumption based on the degree days

Post by SilentNL »

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…)
Degreeday.PNG
Degreeday.PNG (22.53 KiB) Viewed 1794 times
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
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!
Wienen
Posts: 5
Joined: Wednesday 17 October 2018 13:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: LUA Script for daily gas consumption based on the degree days

Post by Wienen »

When browsing Google about Domoticz, gas consumption and wheater temperatur i've stumbled upon this thread. This is exactly what i want to see in my Domiticz. I'm aware of the upload script to Mindergas.nl but i don't want to have to go there to see the graphs. So i think you have build a great solution. :D

Unfortunately i'm fairly new to Domoticz and have done some reading but couldn't figure out how to exactly configure this. Could you please point me in the right direction?

Thanks in advance!
Joep123
Posts: 56
Joined: Monday 26 March 2018 18:44
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: LUA Script for daily gas consumption based on the degree days

Post by Joep123 »

I get every 2nd reading an error:

Code: Select all

2018-10-18 22:30:00.468 Error: Domoticz(pid:509, tid:17589('luaThread')) received fatal signal 11 (Segmentation fault)
2018-10-18 22:30:00.468 Error: siginfo address=0x5f7cc318, address=0x72c754
When I disable the script Domoticz works fine without any error.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest