Display power usage and power return

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

Moderator: leecollings

Post Reply
HvdW
Posts: 615
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Display power usage and power return

Post by HvdW »

Huizebruin.nl has created a very good script to display Power Usage and Power return both for peak rate and night rate.
However I needed something that calculates the difference for usage and return for day and night rate.
Setup 2 dummy devices like Sum day Rate and Sum night Rate (Som Hoog en Som Laag in the script)

Code: Select all

--[[ 
     dzVents script dzVents script time
     Split P1 Smart Meter Electricity in usage1, usage 2, return 1 and return 2.
     Script downloaded from huizebruin.nl
	 www.huizebruin.nl/domoticz/slimme-meter-(p1)-opsplitsen-naar-4-tellers-domoticz-met-lua/


    1-0:1.8.1(00185.000*kWh) (Totaal usage tarief 1 (night)) - usage1
    1-0:1.8.2(00084.000*kWh) (Totaal usage tarief 2 (day))   - usage2
    1-0:2.8.1(00013.000*kWh) (Totaal return tarief 1 (night)) - return1
    1-0:2.8.2(00019.000*kWh) (Totaal return tarief 2 (day))   - return2
    
            --[[   
    Power usage 1 = Dal gebruik     : 30163692  
    Power usage 2 = Piek gebruik    : 16186108    
    Power return 1 = Dal terug      : 11095413    
    Power return 2 = Piek terug     : 26657672    
]]--
 Example start values                       -- 01-01-2023       -- 04-07-2023
local startUsageLow = 29885000      -- 29048762         --29885000
local startUsageHigh = 15983000     -- 15357636         --15983000
local startReturnLow = 10747000     -- 9937177          --10747000
local startReturnHigh = 25826000    -- 24233004         --25826000

local fetchIntervalMins = 20    --   1 = every minute, 10 = every 10 minutes, etc.)

return {
    on =      {
                        timer = { 'every ' .. fetchIntervalMins .. ' minutes' }
              },
             
    logging = {
                         level = domoticz.LOG_DEBUG, domoticz.LOG_INFO,  
                         marker = 'Separate meter readings '
              },

    execute = function(dz, item)

        local P1  = dz.devices('Power') -- Electra, P1 Smart Meter device (idx or "name") (required)
        local saldoHigh = dz.devices('Saldo Hoog')  -- verschil teruglevering - gebruik
        local saldoLow = dz.devices('Saldo Laag')  -- verschil teruglevering - gebruik
        local sumSaldoHigh = (P1.return2 - startReturnHigh) - (P1.usage2 - startUsageHigh)
        local sumSaldoLow = (P1.return1 - startReturnLow) - (P1.usage1 - startUsageLow)

        dz.log('Saldo High      : ' ..sumSaldoHigh, dz.LOG_INFO)
        dz.log('Saldo Low       : ' ..sumSaldoLow, dz.LOG_INFO)
        
        dz.devices('Saldo High').updateCounter(sumSaldoHoog)
        dz.devices('Saldo Low').updateCounter(sumSaldoLaag)

     end
}

-- einde Script
This script has been writen to get insight in Solar power surplus for day and night electricity.

You can start measuring from january first or from the day you energy provider starts a new yearly period.
The annual account of your energy provider shows the numbers you're seaching for.
The numbers have to put into the script.
Domoticz saldo.jpg
Domoticz saldo.jpg (29.61 KiB) Viewed 1741 times
The top numbers show positive when the solar power output is higher than usage and negative when usage is higher than PVOutput.
The numbers in the middle show the calculated difference between the start of the measurement (in this case 07-07-2023) and today.
You can see that I have a suplus of day energy of 648 kW and a surplus of 62 kW of nigt (weekend) energy.
My daily score is positive for day and negative for the evening and night hours.
Bugs bug me.
anixi
Posts: 7
Joined: Sunday 07 February 2021 21:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

Re: Display power usage and power return

Post by anixi »

Hi there I am a real starter with Dzvents and looking for (I think) a simple script: I want to substract 3500 from my P1 smart meter L2 usage and show the result in "l2_corrected, Idx 784. Any suggestions ? Thanks!
HvdW
Posts: 615
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: Display power usage and power return

Post by HvdW »

Dzvents is straightforward.
It took me years to learn a bit of programming in Domoticz with dzvents.
Nowadays I love it.
Learn from the dzvents wiki and examples.
In this example I substracted 1.5 degrees from the outside temperature.

Code: Select all

return {
    active = true,
    on = 
	{
	    devices = {'Buitentemperatuur'},
	},

    logging = {
        level = domoticz.LOG_DEBUG,
        -- change LOG_DEBUG to LOG_ERROR to stop logging in the log
        marker = 'calculate and display power',
    },

    execute = function(domoticz, sensor)
        local outside_temperature = domoticz.devices('Buitentemperatuur').temperature
        domoticz.log('Outside temperature is ' .. outside_temperature, domoticz.LOG_DEBUG)
        local constructed_temperature = outside_temperature - 1.5
        domoticz.log('Buitentemperatuur is ' .. outside_temperature, domoticz.LOG_DEBUG)
        domoticz.log('Constructed temperature is ' .. constructed_temperature, domoticz.LOG_DEBUG)
	    domoticz.devices('Supertemp').updateTemperature(outside_temperature - 1.5)
    end
}
blok0.jpg
blok0.jpg (24.18 KiB) Viewed 1188 times
It's not the thing you really want, but it's just a hint to help you on your way.
You can add the values below and display return2 in Supertemp like this:

Code: Select all

	    domoticz.log('Counter delivered today is                        :' .. domoticz.devices('Power').counterDeliveredToday, domoticz.LOG_DEBUG) 
	    domoticz.log('Counter today today     is                        :' .. domoticz.devices('Power').counterToday, domoticz.LOG_DEBUG) 
	    domoticz.log('Usage 1                 is                        :' .. domoticz.devices('Power').usage1, domoticz.LOG_DEBUG) 
	    domoticz.log('Usage 2                 is                        :' .. domoticz.devices('Power').usage2, domoticz.LOG_DEBUG)
	    domoticz.log('Return 1                is                        :' .. domoticz.devices('Power').return1, domoticz.LOG_DEBUG) 
	    domoticz.log('Return 2                is                        :' .. domoticz.devices('Power').return2, domoticz.LOG_DEBUG)
	    -- updateP1(usage1, usage2, return1, return2, cons, prod): Function. Updates the device. Supports command options.
	    domoticz.devices('Supertemp').updateTemperature(domoticz.devices('Power').return2 - 3500)
Information about Power delivery and so on can be found in the dzvents wiki.
Good luck and enjoy.
Bugs bug me.
anixi
Posts: 7
Joined: Sunday 07 February 2021 21:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

Re: Display power usage and power return

Post by anixi »

Hi HvdW, thanks for your quick reply and first help. I could replicate your script with the same outcome in my system. Based upon this I have written the attached script to substract 3500 from my L2 Usage (idx 784) and publish this in the dummy sensor L2_corrected.

Code: Select all

return {
    active = true,
    on = 
	{
	    devices = {'Usage L2'},
	},

    logging = {
        level = domoticz.LOG_DEBUG,
        -- change LOG_DEBUG to LOG_ERROR to stop logging in the log
        marker = 'calculate and display power',
    },

    execute = function(domoticz, sensor)
        local Usage_2 = domoticz.devices(784).updateEnergy
        local constructed_usage = Usage_2 - 3500
        domoticz.log('Usage L2 is ' .. Usage_2, domoticz.LOG_DEBUG)
        domoticz.log('constructed_usage is ' .. constructed_usage, domoticz.LOG_DEBUG)
        domoticz.devices('L2_corrected').updateEnergy(Usage_2 - 3500)
    end
}
Unfortunately, it only works partly, log output is:
2024-01-03 16:22:55.405 Status: dzVents: Info: Handling events for: "Usage L2", value: "157.0"
2024-01-03 16:22:55.405 Status: dzVents: Info: calculate and display power: ------ Start internal script: Script #3: Device: "Usage L2 (slimmemeter)", Index: 784
2024-01-03 16:22:55.406 Status: dzVents: Info: calculate and display power: ------ Finished Script #3
2024-01-03 16:22:55.406 Error: dzVents: Error: (3.1.8) calculate and display power: An error occurred when calling event handler Script #3
2024-01-03 16:22:55.406 Error: dzVents: Error: (3.1.8) calculate and display power: ...domoticz/scripts/dzVents/generated_scripts/Script #3.lua:16: attempt to perform arithmetic on a function value (local 'Usage_2')


I don't have a clue where to look anymore and was hoping you can help me out....
willemd
Posts: 650
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: Display power usage and power return

Post by willemd »

You have defined
local Usage_2 = domoticz.devices(784).updateEnergy
which is a function to update the device 784, not the value of device 784.

To get the value of device 784 you should use
local Usage_2 = domoticz.devices(784).actualWatt
(provided your device is of the type "electric usage")

Have a look at the dzVents documentation
https://www.domoticz.com/wiki/DzVents:_ ... tric_usage
anixi
Posts: 7
Joined: Sunday 07 February 2021 21:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

Re: Display power usage and power return

Post by anixi »

Thanks willemd, that was indeed the tric!
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest