Gas consumption & cost per day, month and year  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
User avatar
Hzuu
Posts: 6
Joined: Tuesday 16 June 2020 11:10
Target OS: -
Domoticz version:
Location: The Netherlands
Contact:

Gas consumption & cost per day, month and year

Post by Hzuu »

dzVentz timer script for showing gas consumption & cost per day, month and year in 6 virtual custom sensors.
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.

Gas Custom Sensors.jpg
Gas Custom Sensors.jpg (176.98 KiB) Viewed 2395 times

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
}

rudolfpi
Posts: 8
Joined: Monday 19 March 2018 18:17
Target OS: Linux
Domoticz version:
Contact:

Re: Gas consumption & cost per day, month and year

Post by rudolfpi »

Good morning, thanks for the script! I have modified the script for Stroom Kosten (power). I'm not quite sure if everything is correct, maybe you can check? Maybe also add solar panels but have to figure out how... ;)

Code: Select all

--------------------------------------------------------------------------
-- Stroom Kosten script (dzVentz-Timer)
-- 
-- Convert power consumption into Custom Virtual Sensors
-- Create 3 x Virtual Custom Sensor outputDeviceConsumptionX (Aslabel: kWh)
-- Create 3 x Virtual Custom Sensor outputDeviceCostX (Aslabel: EUR)
-- 
--------------------------------------------------------------------------

local scriptVar = 'PowerConsumptionAndCost'

return
{
    on =
    {
        timer = {'every 1 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 =
    {
        power = 
        {
            initial = {},
        },
    },

    execute = function(dz, item)

        -- Variables ---------------------------------------------
        local inputDevice = dz.devices('Power') -- Input device (P1)
        local outputDeviceConsumptionToday = dz.devices('Stroom Verbruik (vandaag)') -- Output device consumption vandaag
        local outputDeviceConsumptionMonth = dz.devices('Stroom Verbruik (maand)') -- Output device consumption month
        local outputDeviceConsumptionYear = dz.devices('Stroom Verbruik (jaar)') -- Output device consumption year
        local outputDeviceCostToday = dz.devices('Stroom Kosten (vandaag)') -- Output device cost today
        local outputDeviceCostMonth = dz.devices('Stroom Kosten (maand)') -- Output device cost month
        local outputDeviceCostYear = dz.devices('Stroom Kosten (jaar)') -- Output device cost year

        -- Power costs per unit ----
        ---local costPowerUnit = 0.25
        
        if (dz.time == 'Between 23:00 and 07:00') or (dz.day == 'Saturday') or (dz.day == 'Sunday') then
    local costPowerUnit = 0.20292 -- Daltarief
    else costPowerUnit = 0.20292 -- Normaal tarief
    end

        -- Fixed cost per day --------------------------------------------------------------
        local costPowerFixedDay = (0.13726 + 0.63477) -- (Transport cost + Grid operator cost)

        -- Funtion for consumption retrieval ------------
        local function ConsumptionGetGraphData(id, delay)
            local period = 'year'

                powerURL = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=counter&range=' .. period .. '&idx=' .. id
                dz.openURL({ url = powerURL, 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.power.consumptionToday)
                dz.log('currentConsumption (today): '  .. currentConsumption .. ' kWh', dz.LOG_DEBUG)

            end

            if device == outputDeviceConsumptionMonth then
                currentConsumption = (dz.data.power.consumptionMonth)
                dz.log('currentConsumnption (month): '  .. currentConsumption .. ' kWh', dz.LOG_DEBUG)
                
            end

            if device == outputDeviceConsumptionYear then
                currentConsumption = (dz.data.power.consumptionYear)
                dz.log('currentConsumption (year): '  .. currentConsumption .. ' kWh', 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.power.consumptionToday * dz.data.power.costPowerUnit)
                currentCost = currentCost + (dz.data.power.costPowerFixedDay)
                dz.log('currentCost (today): '  .. currentCost .. ' EUR', dz.LOG_DEBUG)

            end

            if device == outputDeviceCostMonth then
                currentCost = (dz.data.power.consumptionMonth * dz.data.power.costPowerUnit)
                currentCost = currentCost + (dz.data.power.costPowerFixedDay * dz.data.power.daysCounterMonth)
                dz.log('currentCost (month): '  .. currentCost .. ' EUR', dz.LOG_DEBUG)
                
            end

            if device == outputDeviceCostYear then
                currentCost = (dz.data.power.consumptionYear * dz.data.power.costPowerUnit)
                currentCost = currentCost + (dz.data.power.costPowerFixedDay * dz.data.power.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.power.consumptionToday = todayVolume
            dz.data.power.consumptionMonth = monthVolume
            dz.data.power.consumptionYear = yearVolume
            dz.data.power.costPowerUnit = costPowerUnit
            dz.data.power.costPowerFixedDay = (costPowerFixedDay or 0)
            dz.data.power.daysCounterMonth = daysCounterMonth
            dz.data.power.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
}
User avatar
Hzuu
Posts: 6
Joined: Tuesday 16 June 2020 11:10
Target OS: -
Domoticz version:
Location: The Netherlands
Contact:

Re: Gas consumption & cost per day, month and year  [Solved]

Post by Hzuu »

For electricity I posted another script you can use: https://www.domoticz.com/forum/viewtopi ... 28#p250528
In this script you can also change the Transport cost (transport kosten) , Grid operator cost (netbeheer kosten) en Energy tax reduction (energiebelasting verlaging).
RedGarfield
Posts: 3
Joined: Tuesday 06 October 2020 9:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Gas consumption & cost per day, month and year

Post by RedGarfield »

I keep getting an error.

2020-10-29 14:38:00.389 Error: EventSystem: in Kosten Gas: [string "---------------------------------------------..."]:22: attempt to index a nil value (global 'domoticz')

Peter
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Gas consumption & cost per day, month and year

Post by waaren »

RedGarfield wrote: Thursday 29 October 2020 14:39 I keep getting an error.
2020-10-29 14:38:00.389 Error: EventSystem: in Kosten Gas: [string "---------------------------------------------..."]:22: attempt to index a nil value (global 'domoticz')
Did you save the script as a type dzVents ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
RedGarfield
Posts: 3
Joined: Tuesday 06 October 2020 9:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Gas consumption & cost per day, month and year

Post by RedGarfield »

Ja, als dzVents / timer
RedGarfield
Posts: 3
Joined: Tuesday 06 October 2020 9:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Gas consumption & cost per day, month and year

Post by RedGarfield »

Problem solved..During copying something went wrong.

Just before saving I hit some keys on my keyboard.
NvBgm55
Posts: 14
Joined: Wednesday 15 December 2021 20:02
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Gas consumption & cost per day, month and year

Post by NvBgm55 »

It would be nice if you could indicate within the script on which date you want to start. As it is now a year is known from the present day as far as the annual calculations are concerned I think.. So it starts when my energie contract starts
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest