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

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

Moderator: leecollings

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

Electricity consumption & cost per day, month and year

Post by Hzuu »

dzVentz timer script for showing electricity consumption & cost per day, month and year in 6 virtual custom sensors.
Change 'inputDevice' in the script to the name of your P1 electricity device.
Create 3 virtual custom sensors (Aslabel: kWh) 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.

Electricity Custom Sensors.jpg
Electricity Custom Sensors.jpg (189.15 KiB) Viewed 8573 times

Code: Select all

---------------------------------------------------------------------------
-- electricity Verbruik & Kosten script (dzVentz-Timer)
-- 
-- Convert electricity consumption & cost into Custom Virtual Sensors
-- Create 3 x Virtual Custom Sensor outputDeviceConsumptionX (Aslabel: kWh)
-- Create 3 x Virtual Custom Sensor outputDeviceCostX (Aslabel: EUR)
-- 
---------------------------------------------------------------------------

local scriptVar = 'ElectricityConsumptionAndCost'

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 =
    {
        electricity = 
        {
            initial = {},
        },
    },

    execute = function(dz, item)

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

        -- Electricity costs per unit ------
        local costElectricityUnitT1 = 0.2209
        local costElectricityUnitT2 = 0.2209
        local costElectricityUnitR1 = 0.0800
        local costElectricityUnitR2 = 0.0800

        -- Fixed cost per day -------------------------------------------------------------------------------------------------------
        local costElectricityFixedDay = (0.08765 + 0.69031 - 0.85376) -- (Transport cost + Grid operator cost - Energy tax reduction)

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

                electricityURL = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=counter&range=' .. period .. '&idx=' .. id
                dz.openURL({ url = electricityURL, 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.electricity.consumptionTodayT1) + (dz.data.electricity.consumptionTodayT2)
                currentConsumption = currentConsumption - (dz.data.electricity.consumptionTodayR1) - (dz.data.electricity.consumptionTodayR2)
                dz.log('currentConsumption (today): '  .. currentConsumption .. ' kWh', dz.LOG_DEBUG)

            end

            if device == outputDeviceConsumptionMonth then
                currentConsumption = (dz.data.electricity.consumptionMonthT1) + (dz.data.electricity.consumptionMonthT2)
                currentConsumption = currentConsumption - (dz.data.electricity.consumptionMonthR1) - (dz.data.electricity.consumptionMonthR2)
                dz.log('currentConsumption (month): '  .. currentConsumption .. ' kWh', dz.LOG_DEBUG)
                
            end

            if device == outputDeviceConsumptionYear then
                currentConsumption = (dz.data.electricity.consumptionYearT1) + (dz.data.electricity.consumptionYearT2)
                currentConsumption = currentConsumption - (dz.data.electricity.consumptionYearR1) - (dz.data.electricity.consumptionYearR2)
                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.electricity.consumptionTodayT1 * dz.data.electricity.costElectricityUnitT1) + (dz.data.electricity.consumptionTodayT2 * dz.data.electricity.costElectricityUnitT2)
                currentCost = currentCost - (dz.data.electricity.consumptionTodayR1 * dz.data.electricity.costElectricityUnitR1) - (dz.data.electricity.consumptionTodayR2 * dz.data.electricity.costElectricityUnitR2)
                currentCost = currentCost + (dz.data.electricity.costElectricityFixedDay)
                dz.log('currentCost (today): '  .. currentCost .. ' EUR', dz.LOG_DEBUG)

            end

            if device == outputDeviceCostMonth then
                currentCost = (dz.data.electricity.consumptionMonthT1 * dz.data.electricity.costElectricityUnitT1) + (dz.data.electricity.consumptionMonthT2 * dz.data.electricity.costElectricityUnitT2)
                currentCost = currentCost - (dz.data.electricity.consumptionMonthR1 * dz.data.electricity.costElectricityUnitR1) - (dz.data.electricity.consumptionMonthR2 * dz.data.electricity.costElectricityUnitR2)
                currentCost = currentCost + (dz.data.electricity.costElectricityFixedDay * dz.data.electricity.daysCounterMonth)
                dz.log('currentCost (month): '  .. currentCost .. ' EUR', dz.LOG_DEBUG)
                
            end

            if device == outputDeviceCostYear then
                currentCost = (dz.data.electricity.consumptionYearT1 * dz.data.electricity.costElectricityUnitT1) + (dz.data.electricity.consumptionYearT2 * dz.data.electricity.costElectricityUnitT2)
                currentCost = currentCost - (dz.data.electricity.consumptionYearR1 * dz.data.electricity.costElectricityUnitR1) - (dz.data.electricity.consumptionYearR2 * dz.data.electricity.costElectricityUnitR2)
                currentCost = currentCost + (dz.data.electricity.costElectricityFixedDay * dz.data.electricity.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 todayVolumeT1, todayVolumeT2, todayVolumeR1, todayVolumeR2 = 0, 0, 0, 0
            local monthVolumeT1, monthVolumeT2, monthVolumeR1, monthVolumeR2 = 0, 0, 0, 0
            local yearVolumeT1, yearVolumeT2, yearVolumeR1, yearVolumeR2 = 0, 0, 0, 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
                    todayVolumeT1 = todayVolumeT1 + inputDevice.v
                    todayVolumeT2 = todayVolumeT2 + inputDevice.v2
                    todayVolumeR1 = todayVolumeR1 + inputDevice.r1
                    todayVolumeR2 = todayVolumeR2 + inputDevice.r2

                end

                if inputDevice.d:match(currentMonthIdentifier) then
                    monthVolumeT1 = monthVolumeT1 + inputDevice.v
                    monthVolumeT2 = monthVolumeT2 + inputDevice.v2
                    monthVolumeR1 = monthVolumeR1 + inputDevice.r1
                    monthVolumeR2 = monthVolumeR2 + inputDevice.r2
                    daysCounterMonth = daysCounterMonth + 1

                end

                if inputDevice.d:match(dz.time.year) then
                    yearVolumeT1 = yearVolumeT1 + inputDevice.v
                    yearVolumeT2 = yearVolumeT2 + inputDevice.v2
                    yearVolumeR1 = yearVolumeR1 + inputDevice.r1
                    yearVolumeR2 = yearVolumeR2 + inputDevice.r2
                    daysCounterYear = daysCounterYear + 1

                end

            end

            dz.log('daysCounterMonth: '  .. daysCounterMonth, dz.LOG_DEBUG)
            dz.log('daysCounterYear: '  .. daysCounterYear, dz.LOG_DEBUG)

            dz.data.electricity.consumptionTodayT1 = todayVolumeT1
            dz.data.electricity.consumptionTodayT2 = todayVolumeT2
            dz.data.electricity.consumptionTodayR1 = todayVolumeR1
            dz.data.electricity.consumptionTodayR2 = todayVolumeR2
            dz.data.electricity.consumptionMonthT1 = monthVolumeT1
            dz.data.electricity.consumptionMonthT2 = monthVolumeT2
            dz.data.electricity.consumptionMonthR1 = monthVolumeR1
            dz.data.electricity.consumptionMonthR2 = monthVolumeR2
            dz.data.electricity.consumptionYearT1 = yearVolumeT1
            dz.data.electricity.consumptionYearT2 = yearVolumeT2
            dz.data.electricity.consumptionYearR1 = yearVolumeR1
            dz.data.electricity.consumptionYearR2 = yearVolumeR2
            dz.data.electricity.costElectricityUnitT1 = costElectricityUnitT1
            dz.data.electricity.costElectricityUnitT2 = costElectricityUnitT2
            dz.data.electricity.costElectricityUnitR1 = costElectricityUnitR1
            dz.data.electricity.costElectricityUnitR2 = costElectricityUnitR2
            dz.data.electricity.costElectricityFixedDay = (costElectricityFixedDay or 0)
            dz.data.electricity.daysCounterMonth = daysCounterMonth
            dz.data.electricity.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
}

tombost
Posts: 2
Joined: Monday 28 January 2019 16:11
Target OS: -
Domoticz version:
Contact:

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

Post by tombost »

Thanks it's work great !
Maybe you can optimize and search automatically the cost in domoticz settings:

Code: Select all

local function getCosts(id) -- these costs should be set in domoticz settings
            if next(dz.data.energyCosts) == nil  or dz.data.energyCosts.creationTime < ( dz.time.dDate - dz.time.secondsSinceMidnight ) then
                local costURL = "http://192.168.1.40:8080/json.htm?param=getcosts&type=command&idx=" .. id
                triggerJSON(costURL, scriptVar .. "_cost")
            end
            return ( next(dz.data.energyCosts) ~= nil )
        end
fzoet
Posts: 10
Joined: Saturday 10 October 2020 16:48
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

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

Post by fzoet »

This looks nice but I get wrong values. My power usage today is positive, and at the same time the cost is negative. Which would be nice but the power company does not work this way.

So I have been breaking my head over the cose, but i do not know enough about dzvents scripting and cannot find anything on the internet about the blocks with which this program has been set up. For example, how can I find the various counter names (keys to grab the values)?

Can you explain the various parts of code, what they do, so I can debug the code?

thanks
JuanUil
Posts: 497
Joined: Friday 22 May 2015 12:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11083
Location: Asten NB Nederland
Contact:

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

Post by JuanUil »

Nice !
Your mind is like a parachute,
It only works when it is opened!

RPI4 several Fibaro, KaKu, Neocoolcam switches, Z-Wave, Zigbee2Mqtt, Ikea bulbs and remote, Zigbee temp nodes
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

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

Post by waaren »

fzoet wrote: Tuesday 13 October 2020 10:56 This looks nice but I get wrong values. My power usage today is positive, and at the same time the cost is negative. Which would be nice but the power company does not work this way.
I leave it to the OP to explain the code but you might want to look at line

Code: Select all

local costElectricityFixedDay = (0.08765 + 0.69031 - 0.85376) -- this computes to -0.0758 
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
fzoet
Posts: 10
Joined: Saturday 10 October 2020 16:48
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

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

Post by fzoet »

waaren wrote: Tuesday 13 October 2020 12:20
fzoet wrote: Tuesday 13 October 2020 10:56 This looks nice but I get wrong values. My power usage today is positive, and at the same time the cost is negative. Which would be nice but the power company does not work this way.
I leave it to the OP to explain the code but you might want to look at line

Code: Select all

local costElectricityFixedDay = (0.08765 + 0.69031 - 0.85376) -- this computes to -0.0758 
@waaren, thanks. This is definitely part of the error. The tax reduction should not be included in the fixed energy cost: Energy tax is paid over every kWh power (and m3 gas) that is used. for 2020: € 0,11822 per kWh. The reduction is on the total energy bill, so including gas usage.

I took it out of the equation and numbers look more correct now. At least it shows a cost for the used power now, instead of negative numbers despite using power.
salvacalatayud
Posts: 112
Joined: Monday 26 June 2017 21:16
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Spain
Contact:

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

Post by salvacalatayud »

Hi all,

I get this error:

Code: Select all

2020-10-22 17:12:00.150 Error: dzVents: Error: (3.0.14) ElectricityConsumptionAndCost: An error occurred when calling event handler Consumo y costes electricidad
2020-10-22 17:12:00.150 Error: dzVents: Error: (3.0.14) ElectricityConsumptionAndCost: ...ents/generated_scripts/Consumo y costes electricidad.lua:68: attempt to perform arithmetic on a nil value (field 'consumptionTodayT1')
Thanks a lot
salvacalatayud
Posts: 112
Joined: Monday 26 June 2017 21:16
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Spain
Contact:

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

Post by salvacalatayud »

EDIT: I solved it, it was a problem with another scrip.
AYAMY
Posts: 21
Joined: Sunday 27 May 2018 11:51
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post by AYAMY »

Code: Select all

2021-04-23 18:14:00.669 Error: dzVents: Error: (3.1.7) ElectricityConsumptionAndCost: An error occurred when calling event handler Calcolo6Costi
2021-04-23 18:14:00.670 Error: dzVents: Error: (3.1.7) ElectricityConsumptionAndCost: ...ticz/scripts/dzVents/generated_scripts/Calcolo6Costi.lua:68: attempt to perform arithmetic on a nil value (field 'consumptionTodayT1')
2021-04-23 18:14:00.864 Error: dzVents: Error: (3.1.7) ElectricityConsumptionAndCost: An error occurred when calling event handler Calcolo6Costi
2021-04-23 18:14:00.864 Error: dzVents: Error: (3.1.7) ElectricityConsumptionAndCost: ...ticz/scripts/dzVents/generated_scripts/Calcolo6Costi.lua:151: attempt to perform arithmetic on a nil value (field 'v2')
i have the same error (LINE 68) and (LINE 151)
could you help me
PS i'm using as Input device (P1) a dummy that contains (throught a DxVents Script) the sum of 8 different meters (and that one script works well)

Code: Select all

---------------------------------------------------------------------------
-- electricity Verbruik & Kosten script (dzVentz-Timer)
-- 
-- Convert electricity consumption & cost into Custom Virtual Sensors
-- Create 3 x Virtual Custom Sensor outputDeviceConsumptionX (Aslabel: kWh)
-- Create 3 x Virtual Custom Sensor outputDeviceCostX (Aslabel: EUR)
-- 
---------------------------------------------------------------------------

local scriptVar = 'ElectricityConsumptionAndCost'

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 =
    {
        electricity = 
        {
            initial = {},
        },
    },

    execute = function(dz, item)

        -- Variables -------------------------------------------------------
        local inputDevice = dz.devices('Somma Watt') -- Input device (P1)
        local outputDeviceConsumptionToday = dz.devices('consumption today') -- Output device consumption today
        local outputDeviceConsumptionMonth = dz.devices('consumption month') -- Output device consumption month
        local outputDeviceConsumptionYear = dz.devices('consumption year') -- Output device consumption year
        local outputDeviceCostToday = dz.devices('cost today') -- Output device cost today
        local outputDeviceCostMonth = dz.devices('cost month') -- Output device cost month
        local outputDeviceCostYear = dz.devices('cost year') -- Output device cost year

        -- Electricity costs per unit ------
        local costElectricityUnitT1 = 0.2209
        local costElectricityUnitT2 = 0.2209
        local costElectricityUnitR1 = 0.0800
        local costElectricityUnitR2 = 0.0800

        -- Fixed cost per day -------------------------------------------------------------------------------------------------------
        local costElectricityFixedDay = (0.08765 + 0.69031 - 0.85376) -- (Transport cost + Grid operator cost - Energy tax reduction)

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

                electricityURL = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=counter&range=' .. period .. '&idx=' .. id
                dz.openURL({ url = electricityURL, 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.electricity.consumptionTodayT1) + (dz.data.electricity.consumptionTodayT2)
                currentConsumption = currentConsumption - (dz.data.electricity.consumptionTodayR1) - (dz.data.electricity.consumptionTodayR2)
                dz.log('currentConsumption (today): '  .. currentConsumption .. ' kWh', dz.LOG_DEBUG)

            end

            if device == outputDeviceConsumptionMonth then
                currentConsumption = (dz.data.electricity.consumptionMonthT1) + (dz.data.electricity.consumptionMonthT2)
                currentConsumption = currentConsumption - (dz.data.electricity.consumptionMonthR1) - (dz.data.electricity.consumptionMonthR2)
                dz.log('currentConsumption (month): '  .. currentConsumption .. ' kWh', dz.LOG_DEBUG)
                
            end

            if device == outputDeviceConsumptionYear then
                currentConsumption = (dz.data.electricity.consumptionYearT1) + (dz.data.electricity.consumptionYearT2)
                currentConsumption = currentConsumption - (dz.data.electricity.consumptionYearR1) - (dz.data.electricity.consumptionYearR2)
                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.electricity.consumptionTodayT1 * dz.data.electricity.costElectricityUnitT1) + (dz.data.electricity.consumptionTodayT2 * dz.data.electricity.costElectricityUnitT2)
                currentCost = currentCost - (dz.data.electricity.consumptionTodayR1 * dz.data.electricity.costElectricityUnitR1) - (dz.data.electricity.consumptionTodayR2 * dz.data.electricity.costElectricityUnitR2)
                currentCost = currentCost + (dz.data.electricity.costElectricityFixedDay)
                dz.log('currentCost (today): '  .. currentCost .. ' EUR', dz.LOG_DEBUG)

            end

            if device == outputDeviceCostMonth then
                currentCost = (dz.data.electricity.consumptionMonthT1 * dz.data.electricity.costElectricityUnitT1) + (dz.data.electricity.consumptionMonthT2 * dz.data.electricity.costElectricityUnitT2)
                currentCost = currentCost - (dz.data.electricity.consumptionMonthR1 * dz.data.electricity.costElectricityUnitR1) - (dz.data.electricity.consumptionMonthR2 * dz.data.electricity.costElectricityUnitR2)
                currentCost = currentCost + (dz.data.electricity.costElectricityFixedDay * dz.data.electricity.daysCounterMonth)
                dz.log('currentCost (month): '  .. currentCost .. ' EUR', dz.LOG_DEBUG)
                
            end

            if device == outputDeviceCostYear then
                currentCost = (dz.data.electricity.consumptionYearT1 * dz.data.electricity.costElectricityUnitT1) + (dz.data.electricity.consumptionYearT2 * dz.data.electricity.costElectricityUnitT2)
                currentCost = currentCost - (dz.data.electricity.consumptionYearR1 * dz.data.electricity.costElectricityUnitR1) - (dz.data.electricity.consumptionYearR2 * dz.data.electricity.costElectricityUnitR2)
                currentCost = currentCost + (dz.data.electricity.costElectricityFixedDay * dz.data.electricity.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 todayVolumeT1, todayVolumeT2, todayVolumeR1, todayVolumeR2 = 0, 0, 0, 0
            local monthVolumeT1, monthVolumeT2, monthVolumeR1, monthVolumeR2 = 0, 0, 0, 0
            local yearVolumeT1, yearVolumeT2, yearVolumeR1, yearVolumeR2 = 0, 0, 0, 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
                    todayVolumeT1 = todayVolumeT1 + inputDevice.v
                    todayVolumeT2 = todayVolumeT2 + inputDevice.v2
                    todayVolumeR1 = todayVolumeR1 + inputDevice.r1
                    todayVolumeR2 = todayVolumeR2 + inputDevice.r2

                end

                if inputDevice.d:match(currentMonthIdentifier) then
                    monthVolumeT1 = monthVolumeT1 + inputDevice.v
                    monthVolumeT2 = monthVolumeT2 + inputDevice.v2
                    monthVolumeR1 = monthVolumeR1 + inputDevice.r1
                    monthVolumeR2 = monthVolumeR2 + inputDevice.r2
                    daysCounterMonth = daysCounterMonth + 1

                end

                if inputDevice.d:match(dz.time.year) then
                    yearVolumeT1 = yearVolumeT1 + inputDevice.v
                    yearVolumeT2 = yearVolumeT2 + inputDevice.v2
                    yearVolumeR1 = yearVolumeR1 + inputDevice.r1
                    yearVolumeR2 = yearVolumeR2 + inputDevice.r2
                    daysCounterYear = daysCounterYear + 1

                end

            end

            dz.log('daysCounterMonth: '  .. daysCounterMonth, dz.LOG_DEBUG)
            dz.log('daysCounterYear: '  .. daysCounterYear, dz.LOG_DEBUG)

            dz.data.electricity.consumptionTodayT1 = todayVolumeT1
            dz.data.electricity.consumptionTodayT2 = todayVolumeT2
            dz.data.electricity.consumptionTodayR1 = todayVolumeR1
            dz.data.electricity.consumptionTodayR2 = todayVolumeR2
            dz.data.electricity.consumptionMonthT1 = monthVolumeT1
            dz.data.electricity.consumptionMonthT2 = monthVolumeT2
            dz.data.electricity.consumptionMonthR1 = monthVolumeR1
            dz.data.electricity.consumptionMonthR2 = monthVolumeR2
            dz.data.electricity.consumptionYearT1 = yearVolumeT1
            dz.data.electricity.consumptionYearT2 = yearVolumeT2
            dz.data.electricity.consumptionYearR1 = yearVolumeR1
            dz.data.electricity.consumptionYearR2 = yearVolumeR2
            dz.data.electricity.costElectricityUnitT1 = costElectricityUnitT1
            dz.data.electricity.costElectricityUnitT2 = costElectricityUnitT2
            dz.data.electricity.costElectricityUnitR1 = costElectricityUnitR1
            dz.data.electricity.costElectricityUnitR2 = costElectricityUnitR2
            dz.data.electricity.costElectricityFixedDay = (costElectricityFixedDay or 0)
            dz.data.electricity.daysCounterMonth = daysCounterMonth
            dz.data.electricity.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
}

flappie11
Posts: 1
Joined: Monday 07 March 2022 11:22
Target OS: Linux
Domoticz version:
Contact:

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

Post by flappie11 »

hi all

i get a error on line 207 " ConsumptionGetGraphData(inputDevice.id, 0)"

what i have to do to resolve this .

this is the error

2022-03-07 11:24:00.427 Error: dzVents: Error: (3.1.8) ElectricityConsumptionAndCost: An error occurred when calling event handler meterstanden OKE
2022-03-07 11:24:00.427 Error: dzVents: Error: (3.1.8) ElectricityConsumptionAndCost: ...z/scripts/dzVents/generated_scripts/meterstanden OKE.lua:207: attempt to call a nil value (global 'ConsumptionGetGraphData')

who can help me with this

kind regards

leon
rmayne
Posts: 6
Joined: Wednesday 15 May 2019 11:39
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post by rmayne »

Same trouble : Log give

2023-03-21 16:45:00.558 Error: Error parsing http request address: ::ffff:192.168.2.126
2023-03-21 16:45:00.560 Error: Error opening url: http://192.168.2.126:8080/json.htm?type ... ear&idx=66
2023-03-21 16:45:00.657 Error: dzVents: Error: (3.1.8) PowerConsumptionAndCost: HTTP/1.1 response: 400 ==>> Bad Request
User avatar
waltervl
Posts: 5721
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

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

Post by waltervl »

rmayne wrote: Tuesday 21 March 2023 16:46 Same trouble : Log give

2023-03-21 16:45:00.558 Error: Error parsing http request address: ::ffff:192.168.2.126
2023-03-21 16:45:00.560 Error: Error opening url: http://192.168.2.126:8080/json.htm?type ... ear&idx=66
2023-03-21 16:45:00.657 Error: dzVents: Error: (3.1.8) PowerConsumptionAndCost: HTTP/1.1 response: 400 ==>> Bad Request
Check your domoticz security settings (menu setup - settings, tab security). Especially trusted network field to add your local network and Allow Basic Authentication over plain HTTP, should be on (default off)
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
rmayne
Posts: 6
Joined: Wednesday 15 May 2019 11:39
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post by rmayne »

Ok I found the solution. I replace the call 192.168.2.126 by 127.0.0.1
mojso
Posts: 92
Joined: Friday 08 November 2019 23:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post by mojso »

After two last uldate of domoticz i get this error


(3.1.8) ElectricityConsumptionAndCost: An error occurred when calling event handler ElectricityCost
2024-06-17 16:25:01.510 Error: dzVents: Error: (3.1.8) ElectricityConsumptionAndCost: ...cz/scripts/dzVents/generated_scripts/ElectricityCost.lua:167: attempt to perform arithmetic on a nil value (field 'v')
User avatar
gizmocuz
Posts: 2480
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

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

Post by gizmocuz »

Not 100% sure what's this script is about, but if you update to the latest beta, configure your (dynamic) price devices under Settings->Meters, then from that moment prices are also calculated for each kWh/Gass/Water/P1 meter.
At the moment you can see the prices when you open the log (chart) (and soon the reports as well).
At a later moment in time the price information could be added to each widget in the web gui
Quality outlives Quantity!
User avatar
McMelloW
Posts: 434
Joined: Monday 20 November 2017 17:01
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.1
Location: Harderwijk, NL
Contact:

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

Post by McMelloW »

mojso wrote: Monday 17 June 2024 16:26 After two last uldate of domoticz i get this error


(3.1.8) ElectricityConsumptionAndCost: An error occurred when calling event handler ElectricityCost
2024-06-17 16:25:01.510 Error: dzVents: Error: (3.1.8) ElectricityConsumptionAndCost: ...cz/scripts/dzVents/generated_scripts/ElectricityCost.lua:167: attempt to perform arithmetic on a nil value (field 'v')
@mojso After upgrading to 16089 I have the same errors
Is this problem solved in your situation? What have you done?

Thanks in advance
Greetings McMelloW
User avatar
waltervl
Posts: 5721
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

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

Post by waltervl »

Check the Json, it probably changed values v has become v1?
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
mojso
Posts: 92
Joined: Friday 08 November 2019 23:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post by mojso »

McMelloW wrote: Thursday 20 June 2024 22:20
mojso wrote: Monday 17 June 2024 16:26 After two last uldate of domoticz i get this error


(3.1.8) ElectricityConsumptionAndCost: An error occurred when calling event handler ElectricityCost
2024-06-17 16:25:01.510 Error: dzVents: Error: (3.1.8) ElectricityConsumptionAndCost: ...cz/scripts/dzVents/generated_scripts/ElectricityCost.lua:167: attempt to perform arithmetic on a nil value (field 'v')
@mojso After upgrading to 16089 I have the same errors
Is this problem solved in your situation? What have you done?

Thanks in advance
waltervl wrote: Thursday 20 June 2024 23:33 Check the Json, it probably changed values v has become v1?

waltervl is right the v value is now v1
User avatar
McMelloW
Posts: 434
Joined: Monday 20 November 2017 17:01
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.1
Location: Harderwijk, NL
Contact:

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

Post by McMelloW »

mojso wrote: Friday 21 June 2024 11:15
McMelloW wrote: Thursday 20 June 2024 22:20
mojso wrote: Monday 17 June 2024 16:26 After two last uldate of domoticz i get this error


(3.1.8) ElectricityConsumptionAndCost: An error occurred when calling event handler ElectricityCost
2024-06-17 16:25:01.510 Error: dzVents: Error: (3.1.8) ElectricityConsumptionAndCost: ...cz/scripts/dzVents/generated_scripts/ElectricityCost.lua:167: attempt to perform arithmetic on a nil value (field 'v')
@mojso After upgrading to 16089 I have the same errors
Is this problem solved in your situation? What have you done?

Thanks in advance
waltervl wrote: Thursday 20 June 2024 23:33 Check the Json, it probably changed values v has become v1?

waltervl is right the v value is now v1
OK Guys, thanks for the answer, what causes this change from v to v1
For my understanding, do I have to change form v to v1 in the script before or after the updatebeta?
Greetings McMelloW
mojso
Posts: 92
Joined: Friday 08 November 2019 23:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post by mojso »

McMelloW wrote: Friday 21 June 2024 11:45
mojso wrote: Friday 21 June 2024 11:15
McMelloW wrote: Thursday 20 June 2024 22:20
@mojso After upgrading to 16089 I have the same errors
Is this problem solved in your situation? What have you done?

Thanks in advance
waltervl wrote: Thursday 20 June 2024 23:33 Check the Json, it probably changed values v has become v1?

waltervl is right the v value is now v1
OK Guys, thanks for the answer, what causes this change from v to v1
For my understanding, do I have to change form v to v1 in the script before or after the updatebeta?
I made a change in the script, in line 150, 158, 167
if you plan to update domoticz immediately, you can do it before or after,
I have last beta update of Domoticz
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest