how to get value in KwH device?  [Solved]

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

Moderator: leecollings

Post Reply
MeAlbert
Posts: 65
Joined: Friday 06 December 2019 14:23
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Contact:

how to get value in KwH device?

Post by MeAlbert »

I have a heatpump and calculate constantly what power it is delivering. That works fine. So the next step is to convert it to KwH over de day (week ,month etc). I thought I could use the KwH meter for that. But I can not get it right. My code is the following.

Code: Select all

return {
	on = 
	{
		timer = { 'every minute' }
	},
	logging = 
	{
        level = domoticz.LOG_DEBUG, -- for debugging
        level = domoticz.LOG_INFO,
        marker = "WP vermogen per uur"
    },
	data = 
	{ 
	    KwH = { initial = 0},
	    counter = { initial = 0}
    },
    execute = function (domoticz, timer)
        domoticz.log ('counter = ' ..  domoticz.data.counter)
        domoticz.log ('KwH = '  ..  domoticz.data.KwH) 
        if (domoticz.data.counter < 61) then
            domoticz.data.counter = domoticz.data.counter + 1
            domoticz.log ('counter = ' ..  domoticz.data.counter)
            local WpPower = domoticz.devices(70).WhActual / 60
            domoticz.log ('WpPower = '  ..  WpPower)
            domoticz.data.KwH = domoticz.data.KwH + WpPower
            domoticz.devices(64).updateElectricity (  WpPower , domoticz.data.KwH)
        else
        domoticz.data.counter = 1
            local WpPower = domoticz.devices(70).WhActual / 60
            domoticz.log ('WpPower = '  ..  WpPower)
            domoticz.data.KwH = domoticz.data.KwH + WpPower
            domoticz.devices(64).updateElectricity (  WpPower , domoticz.data.KwH)
        end
    end
}
The counter works fine, the calculation from power to energy works fine . I can put the value in now but what to do on the end of the day?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: how to get value in KwH device?

Post by waaren »

MeAlbert wrote: Tuesday 18 February 2020 10:17 The counter works fine, the calculation from power to energy works fine . I can put the value in now but what to do on the end of the day?
If you just enter the values domoticz will take care of the process to transfer data to history tables (meter for 5 min. records and meter_calendar for day records) .

Can you try this ?

Code: Select all

return 
{
    on = 
    {
        timer = { 'every minute' }
    },

    logging = 
    {
        --level = domoticz.LOG_DEBUG, -- for debugging
        level = domoticz.LOG_INFO,
        marker = "WP vermogen per uur"
    },

    execute = function (dz)
        local heatPump = dz.devices(64) -- target: define as electric + counter
        local WpPower = dz.devices(70).WhActual -- source
        local newTotal = heatPump.WhTotal +  WpPower / 60 

        dz.log ('WpPower = '  ..  WpPower)
        dz.log ('newTotal = '  ..  newTotal)
        heatPump.updateElectricity ( WpPower, newTotal )

    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
MeAlbert
Posts: 65
Joined: Friday 06 December 2019 14:23
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Contact:

Re: how to get value in KwH device?

Post by MeAlbert »

@waaren
It works but I do not understand how it can work.
You use a local newTotal but when I read the manual I have to use persistant variables to keep the value between two script executions.
How does it work? Or is this because you use function?
I have seen more examples of functions but see it nowhere in the manual. Is there another manual I can read?
But thanks for the solution.
Last edited by MeAlbert on Tuesday 18 February 2020 20:41, edited 1 time in total.
MeAlbert
Posts: 65
Joined: Friday 06 December 2019 14:23
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Contact:

Re: how to get value in KwH device?

Post by MeAlbert »

The device I use in this case is the following.
Dummy type General. kWh.Type return. Energy read from device.
Are this the correct settings?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: how to get value in KwH device?

Post by waaren »

MeAlbert wrote: Tuesday 18 February 2020 20:16 @waaren
It works but I do not understand how it can work.
You use a local newTotal but when I read the manual I have to use persistant variables to keep the value between two script executions.
How does it work? Or is this because you use function?
I have seen more examples of functions but see it nowhere in the manual. Is there another manual I can read?
But thanks for the solution.
I don't use persistent variables here. I read the value from the device itself where I stored it in the previous execution of the script.
Functions are just a named section of a program that performs a specific task. Lua (dzVents) and almost every other programming language have the option to use it.
A good place to start reading about Lua functions is lua.org
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
MeAlbert
Posts: 65
Joined: Friday 06 December 2019 14:23
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Contact:

Re: how to get value in KwH device?

Post by MeAlbert »

@waaren
You are going to fast for me. I understand what you are doing but not how you manage to store the newTotal field in betweenscript executions.
But up till now I only used the dzVents wiki which is quite superficial. But I will start to read the lua wiki.
Are the device settings ok?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: how to get value in KwH device?

Post by waaren »

MeAlbert wrote: Tuesday 18 February 2020 22:20 I understand what you are doing but not how you manage to store the newTotal field in betweenscript executions.
The newTotal is a calculated value.

I take the total value from the virtual device

Code: Select all

heatPump.WhTotal
and add the energy from the last minute and store this in newTotal.

Code: Select all

local newTotal = heatPump.WhTotal +  WpPower / 60 

After that I write this calculated value newTotal back to the virtual device together with the original power.

Code: Select all

 heatPump.updateElectricity ( WpPower, newTotal )
Are the device settings ok?
Yes device is ok
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
MeAlbert
Posts: 65
Joined: Friday 06 December 2019 14:23
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Contact:

Re: how to get value in KwH device?

Post by MeAlbert »

It is not working correctly. The power generated is ok but the energy generated is 0. In the log screen I can see the calculation is correct.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: how to get value in KwH device?

Post by waaren »

MeAlbert wrote: Tuesday 18 February 2020 23:57 It is not working correctly. The power generated is ok but the energy generated is 0. In the log screen I can see the calculation is correct.
I run the the script myself on a test system and there it does work as expected. Will show some screenshots tomorrow so we can identify the differences between our systems.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
MeAlbert
Posts: 65
Joined: Friday 06 December 2019 14:23
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Contact:

Re: how to get value in KwH device?

Post by MeAlbert »

I have to come back to my previous statement. It works as on the end of the day it gives the day total in the days overview. I expected also the same thing in the hourly overview and that stays at 0. I have a script which reads the power produces by my solar panels and that does also show hourly values. I expected the same here as it is the same device. But daily is good enough for me.

I also understand the script you made completely now. Learned that it can be easier than you think. But I will probbaly come back again with some other trivial problem.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: how to get value in KwH device?

Post by waaren »

MeAlbert wrote: Wednesday 19 February 2020 10:34 .. It works as on the end of the day it gives the day total in the days overview..
Good to read its working.

If you go to log page of the target device you will see the graphs and have the option to run a report (top right corner). Sometimes you need to refresh the page first (CTRL r in Chrome) before the graphs / report shows.
Heatpump graph.png
Heatpump graph.png (215.89 KiB) Viewed 3213 times
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
MeAlbert
Posts: 65
Joined: Friday 06 December 2019 14:23
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Contact:

Re: how to get value in KwH device?  [Solved]

Post by MeAlbert »

I do not know how to put images in this forum. Then I could show you how my graph looks like. In the day view you show here between 12.00 and 16.00 you see some blue bars. I have the dotted yellow line (power generated) but the blue bars are all zero. In the week view Wednesday shows the accumulated value correctly. After some time it started to appear again. So problem solved. Thanks for that
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest