Page 1 of 1

DZvents power get negative at 00:00

Posted: Thursday 02 May 2024 9:41
by rolfie23
I have the following script running with succes for calculate total usage for few seprated led strips. I use the script for different uses, but there is one problem I already play with a long time but didn't find any solution yet.

My scripting is not the best, but what i archieved already in domoticz i'm really proud on!

The script is:

Code: Select all

return {

--	on = {
--	    timer = { 'at 00:00' }
--	},
	
	on = {
		devices = {
			'$Aquarium verlichting Rood',
		}
	},
	 
	execute = function(dz, item)
	    
	    local RoodToday = dz.devices(451).WhToday
	    local GroenToday = dz.devices(454).WhToday
	    local BlauwToday = dz.devices(457).WhToday
	    local WitToday = dz.devices(460).WhToday
		local TotaalToday = RoodToday + GroenToday + BlauwToday + WitToday
			    
		local RoodPower = dz.devices(451).actualWatt
	    local GroenPower = dz.devices(454).actualWatt
	    local BlauwPower = dz.devices(457).actualWatt
	    local WitPower = dz.devices(460).actualWatt
		local Power = RoodPower + GroenPower + BlauwPower + WitPower

	    if dz.time.matchesRule('at 00:05-23:59') then	    
            dz.devices(1174).updateElectricity(Power,TotaalToday)
        end
        
--        if dz.time.matchesRule('at 00:00') then
--          dz.devices(1174).updateElectricity(0,0)	 
--        end
	end
	
}
You see, i already tried sending a 0 at midnight what doesn't work. Also different times around midnight, found that as solution but doesn't work.

In the picture below you see the total usage at midnight getting negative.
Naamloos.jpg
Naamloos.jpg (73.4 KiB) Viewed 969 times
Here some information about the switch where it's sending to:

Code: Select all

{
    "ActTime": 1714635427,
    "AstrTwilightEnd": "23:50",
    "AstrTwilightStart": "03:26",
    "CivTwilightEnd": "21:50",
    "CivTwilightStart": "05:27",
    "DayLength": "15:03",
    "NautTwilightEnd": "22:42",
    "NautTwilightStart": "04:35",
    "ServerTime": "2024-05-02 09:37:07",
    "SunAtSouth": "13:38",
    "Sunrise": "06:07",
    "Sunset": "21:10",
    "app_version": "2024.2",
    "result": [
        {
            "AddjMulti": 1,
            "AddjMulti2": 1,
            "AddjValue": 0,
            "AddjValue2": 0,
            "BatteryLevel": 255,
            "CounterToday": "0.005 kWh",
            "CustomImage": 101,
            "Data": "0.005 kWh",
            "Description": "",
            "EnergyMeterMode": "0",
            "Favorite": 1,
            "HardwareDisabled": false,
            "HardwareID": 6,
            "HardwareName": "Dummy",
            "HardwareType": "Dummy (Does nothing, use for virtual switches only)",
            "HardwareTypeVal": 15,
            "HaveTimeout": false,
            "ID": "00083174",
            "Image": "Aquarium",
            "LastUpdate": "2024-05-02 09:37:06",
            "Name": "Aquarium verlichting verbruik",
            "Notifications": "false",
            "PlanID": "0",
            "PlanIDs": [
                0
            ],
            "Protected": false,
            "ShowNotifications": true,
            "SignalLevel": "-",
            "SubType": "kWh",
            "SwitchTypeVal": 0,
            "Timers": "false",
            "Type": "General",
            "TypeImg": "current",
            "Unit": 1,
            "Usage": "0.46 Watt",
            "Used": 1,
            "XOffset": "0",
            "YOffset": "0",
            "idx": "1174"
        }
    ],
    "status": "OK",
    "title": "Devices"
}
Energy read is "from device" and not on "computed". It works on Computed, but the difference with the real calculation is for my purpose to big.

For now, i delete the value and the day value get's correct. But doing this every day is a little bit to mucht work so I hope someone can help me with the script to make it workable!

Re: DZvents power get negative at 00:00

Posted: Thursday 02 May 2024 10:52
by waltervl
You should add the current TotalWh of your total device. The Energy should not go zero every day but a runing increasing value (like your home energy meter)

So add in the begin of your script

Code: Select all

local TotaalWh = dz.devices(1174).TotalWh
Then modify your update line to

Code: Select all

dz.devices(1174).updateElectricity(Power,TotaalToday+TotaalWh)

Re: DZvents power get negative at 00:00

Posted: Thursday 02 May 2024 12:56
by FlyingDomotic
Electricity is counted as cumulative Wh (even if displayed as kWh).

Depending on values returned by your module, technic is different.

If values you get are cumulative but reset at some time (as midnight), you should write some code which:
- initialize saved value to zero
- read current value
- if current value is greater or equal to saved one, add the difference between current and saved to total
- else only add current value to total (as it has been reset to zero by device)

if values you get are instantaneous ones, you must first convert them to hourly values, multiplying them by (3600/<number of seconds since last read>), then adding them to total. Note in this case that for stable consumption, this may make it, but if values vary too much, or interval is too long, result will be not so good.

Re: DZvents power get negative at 00:00

Posted: Thursday 02 May 2024 13:40
by rolfie23
FlyingDomotic wrote: Thursday 02 May 2024 12:56 Electricity is counted as cumulative Wh (even if displayed as kWh).

Depending on values returned by your module, technic is different.

If values you get are cumulative but reset at some time (as midnight), you should write some code which:
- initialize saved value to zero
- read current value
- if current value is greater or equal to saved one, add the difference between current and saved to total
- else only add current value to total (as it has been reset to zero by device)

if values you get are instantaneous ones, you must first convert them to hourly values, multiplying them by (3600/<number of seconds since last read>), then adding them to total. Note in this case that for stable consumption, this may make it, but if values vary too much, or interval is too long, result will be not so good.
My scripting is not so good, but the values comes from Shelly devices. Maybe that can help for an solution?

Re: DZvents power get negative at 00:00

Posted: Thursday 02 May 2024 14:14
by FlyingDomotic
I don't known Shelly original firmware (I only use Shelly 1PM with my own code), so I can't tell how it works.

However, reading your code, I can imagine that devices 451, 454, 457 and 460 are electricity counters.

If that's the case, you may try replacing .WhToday by .WhTotal in your sketch, as device may already also return the incremental counter.

If this is not the case, I can provide an example which will implement my first hypothesis.

Re: DZvents power get negative at 00:00

Posted: Thursday 02 May 2024 15:18
by rolfie23
waltervl wrote: Thursday 02 May 2024 10:52 You should add the current TotalWh of your total device. The Energy should not go zero every day but a runing increasing value (like your home energy meter)

So add in the begin of your script

Code: Select all

local TotaalWh = dz.devices(1174).TotalWh
Then modify your update line to

Code: Select all

dz.devices(1174).updateElectricity(Power,TotaalToday+TotaalWh)
Hello Walter,

Didn't see your message on my phone at first.

If I do that, I get the following error:

Code: Select all

2024-05-02 15:16:58.505 Error: dzVents: Error: (3.1.8) ...ents/generated_scripts/Aquarium verlichting verbruik.lua:34: attempt to perform arithmetic on a nil value (local 'TotaalWh')

Re: DZvents power get negative at 00:00

Posted: Friday 03 May 2024 10:28
by rolfie23
Thanks waltervl found the solution thanks to you. Quite simple, and worked perfectly last night.
Maybe you have some adjustments on it, please let me know:

Code: Select all

return {

	on = {
	    timer = { 'every minute' }
	},
	
	execute = function(dz, item)
	    
	    local RoodToday = dz.devices(451).WhTotal
	    local GroenToday = dz.devices(454).WhTotal
	    local BlauwToday = dz.devices(457).WhTotal
	    local WitToday = dz.devices(460).WhTotal
	    local TotaalToday = RoodToday + GroenToday + BlauwToday + WitToday
			    
            local RoodPower = dz.devices(451).actualWatt
	    local GroenPower = dz.devices(454).actualWatt
	    local BlauwPower = dz.devices(457).actualWatt
	    local WitPower = dz.devices(460).actualWatt
	    local Power = RoodPower + GroenPower + BlauwPower + WitPower
		   
            dz.devices(1174).updateElectricity(Power,TotaalToday)
        
	end
	
}

Re: DZvents power get negative at 00:00

Posted: Friday 03 May 2024 16:11
by waltervl
Looks perfect! I see a made a mistake in my TotaalWh definition but your solution is perfectly fine.

Re: DZvents power get negative at 00:00

Posted: Monday 06 May 2024 19:03
by rolfie23
:)
waltervl wrote: Friday 03 May 2024 16:11 Looks perfect! I see a made a mistake in my TotaalWh definition but your solution is perfectly fine.
Great, thanks! Running few days now on multiple devices without any problem!