Change 'inputDevice' in the script to the name of your P1 gas device.
Create 3 virtual custom sensors (Aslabel: m3) and make sure the device names of these custom sensors correspond to the names of the outputDeviceConsumptionX variables in the script.
Create 3 virtual custom sensors (Aslabel: EUR) and make sure the device names of these custom sensors correspond to the names of the outputDeviceCostX variables in the script.
You can also change the cost per unit and fixed transport cost values.
Code: Select all
--------------------------------------------------------------------------
-- Gas Kosten script (dzVentz-Timer)
--
-- Convert gas consumption into Custom Virtual Sensors
-- Create 3 x Virtual Custom Sensor outputDeviceConsumptionX (Aslabel: m3)
-- Create 3 x Virtual Custom Sensor outputDeviceCostX (Aslabel: EUR)
--
--------------------------------------------------------------------------
local scriptVar = 'GasConsumptionAndCost'
return
{
on =
{
timer = {'every 2 minutes'}, -- Run this script every 2 minutes
httpResponses = {scriptVar .. '*'},
},
logging =
{
level = domoticz.LOG_ERROR, -- Change to LOG_DEBUG to debug / Change to LOG_ERROR if script is running properly
marker = scriptVar,
},
data =
{
gas =
{
initial = {},
},
},
execute = function(dz, item)
-- Variables ---------------------------------------------
local inputDevice = dz.devices('Gas') -- Input device (P1)
local outputDeviceConsumptionToday = dz.devices('Gas Verbruik (vandaag)') -- Output device consumption vandaag
local outputDeviceConsumptionMonth = dz.devices('Gas Verbruik (maand)') -- Output device consumption month
local outputDeviceConsumptionYear = dz.devices('Gas Verbruik (jaar)') -- Output device consumption year
local outputDeviceCostToday = dz.devices('Gas Kosten (vandaag)') -- Output device cost today
local outputDeviceCostMonth = dz.devices('Gas Kosten (maand)') -- Output device cost month
local outputDeviceCostYear = dz.devices('Gas Kosten (jaar)') -- Output device cost year
-- Gas costs per unit ----
local costGasUnit = 0.7091
-- Fixed cost per day --------------------------------------------------------------
local costGasFixedDay = (0.08765 + 0.52829) -- (Transport cost + Grid operator cost)
-- Funtion for consumption retrieval ------------
local function ConsumptionGetGraphData(id, delay)
local period = 'year'
gasURL = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=counter&range=' .. period .. '&idx=' .. id
dz.openURL({ url = gasURL, callback = scriptVar .. '_' .. id}).afterSec(delay or 0)
end
-- Function for consumption calculation --
local function makeConsumption(device)
local currentConsumption
if device == outputDeviceConsumptionToday then
currentConsumption = (dz.data.gas.consumptionToday)
dz.log('currentConsumption (today): ' .. currentConsumption .. ' m3', dz.LOG_DEBUG)
end
if device == outputDeviceConsumptionMonth then
currentConsumption = (dz.data.gas.consumptionMonth)
dz.log('currentConsumnption (month): ' .. currentConsumption .. ' m3', dz.LOG_DEBUG)
end
if device == outputDeviceConsumptionYear then
currentConsumption = (dz.data.gas.consumptionYear)
dz.log('currentConsumption (year): ' .. currentConsumption .. ' m3', dz.LOG_DEBUG)
end
return dz.utils.round(currentConsumption, 3)
end
-- Function for cost calculation --
local function makeCost(device)
local currentCost
if device == outputDeviceCostToday then
currentCost = (dz.data.gas.consumptionToday * dz.data.gas.costGasUnit)
currentCost = currentCost + (dz.data.gas.costGasFixedDay)
dz.log('currentCost (today): ' .. currentCost .. ' EUR', dz.LOG_DEBUG)
end
if device == outputDeviceCostMonth then
currentCost = (dz.data.gas.consumptionMonth * dz.data.gas.costGasUnit)
currentCost = currentCost + (dz.data.gas.costGasFixedDay * dz.data.gas.daysCounterMonth)
dz.log('currentCost (month): ' .. currentCost .. ' EUR', dz.LOG_DEBUG)
end
if device == outputDeviceCostYear then
currentCost = (dz.data.gas.consumptionYear * dz.data.gas.costGasUnit)
currentCost = currentCost + (dz.data.gas.costGasFixedDay * dz.data.gas.daysCounterYear)
dz.log('currentCost (year): ' .. currentCost .. ' EUR', dz.LOG_DEBUG)
end
return dz.utils.round(currentCost, 2)
end
-- Function for updating a Virtual Custom Sensor --
local function updateCustomSensor(device, value)
local currentValue = device.rawData[1]
if value ~= tonumber(currentValue) then -- Update only necessary when new value differs from the previous one
device.updateCustomSensor(value)
dz.log(device.name .. " ==>> Previous value: " .. currentValue .. " ; New value: " .. value,dz.LOG_DEBUG)
end
end
-- Function for JSON processing --
local function ProcessJSON(t)
local todayVolume = 0
local monthVolume = 0
local yearVolume = 0
local daysCounterMonth = 0
local daysCounterYear = 0
local currentDayIdentifier = dz.time.rawDate:sub(1,10)
local currentMonthIdentifier = dz.time.rawDate:sub(1,4) .. '%-' .. dz.time.rawDate:sub(6,7)
local day = 86400 -- (24 * 60 * 60)
for index, inputDevice in ipairs(t) do
if inputDevice.d == currentDayIdentifier then
todayVolume = todayVolume + inputDevice.v
end
if inputDevice.d:match(currentMonthIdentifier) then
monthVolume = monthVolume + inputDevice.v
daysCounterMonth = daysCounterMonth + 1
end
if inputDevice.d:match(dz.time.year) then
yearVolume = yearVolume + inputDevice.v
daysCounterYear = daysCounterYear + 1
end
end
dz.log('daysCounterMonth: ' .. daysCounterMonth, dz.LOG_DEBUG)
dz.log('daysCounterYear: ' .. daysCounterYear, dz.LOG_DEBUG)
dz.data.gas.consumptionToday = todayVolume
dz.data.gas.consumptionMonth = monthVolume
dz.data.gas.consumptionYear = yearVolume
dz.data.gas.costGasUnit = costGasUnit
dz.data.gas.costGasFixedDay = (costGasFixedDay or 0)
dz.data.gas.daysCounterMonth = daysCounterMonth
dz.data.gas.daysCounterYear = daysCounterYear
end
-- Main -----------------------------------
if item.isHTTPResponse and item.isJSON then
ProcessJSON(item.json.result)
elseif item.isTimer or item.isDevice then
ConsumptionGetGraphData(inputDevice.id, 0)
updateCustomSensor(outputDeviceConsumptionToday, makeConsumption(outputDeviceConsumptionToday))
updateCustomSensor(outputDeviceConsumptionMonth, makeConsumption(outputDeviceConsumptionMonth))
updateCustomSensor(outputDeviceConsumptionYear, makeConsumption(outputDeviceConsumptionYear))
updateCustomSensor(outputDeviceCostToday, makeCost(outputDeviceCostToday))
updateCustomSensor(outputDeviceCostMonth, makeCost(outputDeviceCostMonth))
updateCustomSensor(outputDeviceCostYear, makeCost(outputDeviceCostYear))
else
dz.log('Error retrieving data. Result is: ' .. item.statusText ..' ; Response is: ' .. item.data,LOG_ERROR)
end
end
}