House consumption script help needed

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

Moderator: leecollings

Post Reply
fjuppe
Posts: 42
Joined: Thursday 14 September 2023 19:32
Target OS: Raspberry Pi / ODroid
Domoticz version: 16341
Location: Stockholm
Contact:

House consumption script help needed

Post by fjuppe »

Hello Community

After 2 days of trial (Yes, I have read the wiki and looked at other scripts in this forum but still can't figure out how to do) with dzVents I give up to make a script for the following:

I have 3 devices, Solar (SE) and Electric meter over MQTT for usage and sold electricity Currently Used (CU) and Currently Returned (CR)).
I want to calculate and display in a virtual device Husets Förbrukning (HF) the value of SE+CU-CR which will give me the total consumption for my house.
With my trials, I either get nothing or "nil value" in the log. I have tried the direct reading of device values and also by the API without success.
The script should be triggred by the update of device CU and this I have get working without problem. But how to get the values from the three meters and update the HF?

Thanks a lot for your help

This is my meters:
Attachments
3.png
3.png (46.8 KiB) Viewed 572 times
1.png
1.png (18.05 KiB) Viewed 572 times
User avatar
jvdz
Posts: 2332
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: House consumption script help needed

Post by jvdz »

Can you show the code you tried with the devicenames etc in it, so it is much easier to assist?
SHow a version that is using the dzvents functions, ot the API pls.
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
lost
Posts: 660
Joined: Thursday 10 November 2016 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: House consumption script help needed

Post by lost »

fjuppe wrote: Friday 15 March 2024 8:53 The script should be triggred by the update of device CU and this I have get working without problem. But how to get the values from the three meters and update the HF?
IMO, all 3 real meters should update the virtual HF as this'll impact it's compute formula. But, when device script targeting those 3 triggers, current real meter value will be in 'devicechanged' table for the one that triggered and 'otherdevices' for others/unchanged values. So this'll need 3 cases to take care of this.

Cannot help in dzVent, never managed to cope with it's syntax & always used Lua, but this looks feasible.

The main problem I see for devices whose values may change a lot in the Domoticz 5mn sampling that'll make their way to DB records is you'll not be able to keep data that really make sense (but all value changes in widgets will be reported if you look at them).

This way of sampling data is not an issue for those than don't change a lot in 5mn (like temperature) but that's clearly the case for such meters.

=> IMO, your may need to add another virtual meter doing discrete integration of raw computed values virtual one over a time slice over 5mn to compute a mean value over this time slice. Otherwise data stored in DB will not represent an accurate picture of your power usage.

This can be done using 2 user variables, pwrLast (storing previous device script trigger power figure) and pwrSum (summing/integrating pwrLast*'time since last update' until target time slice, 10mn in my case so over 5mn, is reached to divide by last update of virtual mean power meter to get the integrated sum mean value), here is a Lua snippet (with devices/user var needed definition & role in comment) from a more complex script to make it clear:

devSmartMeterPwr='SmartMeter W' -- Real PWR consumtion measurement device name.
devMeanPwrVrtSen='Puissance MOY' -- Virtual sensor type "usage/electricity" name.
varPwrSum='pwrSum' -- User variable, decimal/float type, storing current time slice power ~integral.
varPwrLast='pwrLast' -- User variable, decimal/float type, storing last pwr watt value.
maxLastUpdMean=600 -- Virtual sensor update target rate.

Code: Select all

    -----------------------
    -- Update Mean power --
    -----------------------

    tLastMean = timeLastUpdate(devMeanPwrVrtSen, false)

    -- DEBUG :
    --print(devSmartMeterPwr..': P='..curPwr..'W ; Last : '..tLastMean..'s')

    pwrLast   = uservariables[varPwrLast]
    pwrSum    = uservariables[varPwrSum]
    sumDeltaT = timeLastUpdate(varPwrSum, true)

    -- Calc pwr sum + store. Set current mean pwr if time slice exceeded & reset user vars.
    if (tLastMean > maxLastUpdMean) then
       meanPwr = (pwrSum + pwrLast*sumDeltaT) / tLastMean

       --Log update :
       print(string.format('%s : New time slice (after %ds) => Record Pmean=%.2fW', devMeanPwrVrtSen,  tLastMean, meanPwr))

       updateNum(devMeanPwrVrtSen, meanPwr, true)
       commandArray['Variable:'..varPwrSum]  = '0'
       commandArray['Variable:'..varPwrLast] = tostring(curPwr)
    else
       pwrSum = pwrSum + pwrLast*sumDeltaT

       -- DEBUG :
       --print('Update current Psum='..pwrSum..'Ws ; deltaT='..sumDeltaT..'s')

       commandArray['Variable:'..varPwrLast] = tostring(curPwr)
       commandArray['Variable:'..varPwrSum]  = tostring(pwrSum)
    end
fjuppe
Posts: 42
Joined: Thursday 14 September 2023 19:32
Target OS: Raspberry Pi / ODroid
Domoticz version: 16341
Location: Stockholm
Contact:

Re: House consumption script help needed

Post by fjuppe »

This is my latest dzVents code but it gives error

Code: Select all

template: .../scripts/dzVents/generated_scripts/House Consumption.lua:17: attempt to perform arithmetic on a nil value (local 'CU')"


Also, please understand that I am a beginner of writing any language coding scripts... :D :D

Code: Select all

return {
	on = {
		devices = {
			'Currently Used' -- just an example to trigger the request
		},
	},
	logging = {
		level = domoticz.LOG_DEBUG,
		marker = 'template',
	},
	execute = function(domoticz, item)

		if (item.isDevice) then
			local CU = domoticz.devices('Currently Used').value
			local CR = domoticz.devices('Currently Returned').value
			local SE = domoticz.devices('SE: Tot kWh').value
			local HF = (CU + CR - SE)
                    
					-- update some device in Domoticz
					domoticz.devices('Husets Förbrukning').updateEnergy(HF)
					domoticz.log('Förbrukning:' ..HF, domoticz.LOG_INFO)
				end

	end
}
Kedi
Posts: 577
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: House consumption script help needed

Post by Kedi »

This domoticz.devices('Currently Used').value is the problem, and I suppose the next 2 lines also.
You have a General/kWh device, see https://www.domoticz.com/wiki/DzVents:_ ... counter.29
So the 'value' is NOT '.value' but .actualWatt or .usage
Logic will get you from A to B. Imagination will take you everywhere.
fjuppe
Posts: 42
Joined: Thursday 14 September 2023 19:32
Target OS: Raspberry Pi / ODroid
Domoticz version: 16341
Location: Stockholm
Contact:

Re: House consumption script help needed

Post by fjuppe »

So, I have got the script technically working. But the fact (as user "lost" pointed out), there is a problem in that the solar generated values are only updated every 5th minute while the electricity in/out are updated in real time, i.e. every second. If I let the electric meter trigger the script, I will sometimes get a negative house consumption when selling power to the grid and real house consumption is lower than solar generated power. It's the same with the energy value, it will sometimes decrease but will adjust correctly every 5th minute when solar energy is updated. But, at the end of the day, when solar power is nil, the house consumption will be OK. I think this functionality is OK for me,

Thanks for your inputs and hints!
This is my current script:

Code: Select all

return {
	on = {
		devices = {
			'Currently Used' -- device to trigger calculation and update
		},
	},
	logging = {
		level = domoticz.LOG_DEBUG,
		marker = 'template',
	},
	execute = function(domoticz, item)

		if (item.isDevice) then
			local CU = domoticz.devices('Currently Used').usage         -- Get current inbound power
			local CUk = domoticz.devices('Currently Used').WhToday      -- Get today inbound energy
			local CR = domoticz.devices('Currently Returned').usage     -- Get current outbound power
			local CRk = domoticz.devices('Currently Returned').WhToday  -- Get today outbound energy
			local SE = domoticz.devices('SE: Tot kWh').usage            --Get current Solar power but only updated every 5th minute
			local SEk = domoticz.devices('SE: Tot kWh').WhToday         --Get today Solar energy but only updated every 5th minute
			local HF = (SE - CR + CU)                                   -- Calculate current house Power
			local HFk = (CUk + SEk - CRk)                               -- Calculate today house consumption
			
                    
					-- update some device in Domoticz and write to log
					domoticz.devices('Husets Förbrukning').updateElectricity(HF, HFk)
					domoticz.log('Förbrukning:' ..HF, domoticz.LOG_INFO)
				end

	end
}
User avatar
jvdz
Posts: 2332
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: House consumption script help needed

Post by jvdz »

I assume you have a solar edge inverter? I have an solaredge and am using an batchscript which will get the currrent status from the inverter each 15 seconds using modbus over ip and feed that into domoticz to get a better approx value of the house usage. It is still not 100% as the value is never exactly in sync with yhe P1 update but much better than using the 5 min update from solaredge.
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
fjuppe
Posts: 42
Joined: Thursday 14 September 2023 19:32
Target OS: Raspberry Pi / ODroid
Domoticz version: 16341
Location: Stockholm
Contact:

Re: House consumption script help needed

Post by fjuppe »

Yes, it is a Solar Edge inverter and I know about the MODUS functionality. However, to enable MODBUS over IP I know you have to enter the settings in the inverter and, since this is a friends house and inverter, I am not brave enough to try to touch the settings if something goes wrong.
Maybe you can persuade me to enable the MODBUS....? :-)
lost
Posts: 660
Joined: Thursday 10 November 2016 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: House consumption script help needed

Post by lost »

fjuppe wrote: Friday 15 March 2024 14:07 But the fact (as user "lost" pointed out), there is a problem in that the solar generated values are only updated every 5th minute
Maybe I was no clear enough, but that's not the issue I pointed out: I said if the update frequency from any meter/sensor is under 5minutes, only raw values at the exact time they are sampled will make their way in domoticz DB (even if widgets are updated ~real time).

I have a power meter measuring my global home consumption & I 1st noticed the problem when my home was suddenly shut-down because my consumption became much higher, for several minutes, than supplier contract value: True day max value that caused the shutdown recorded by supplier meter was 5kW higher than the last value that made it's way in DB & could be seen seen in graphs for my own meter.

=> You have 1 problem as all devices are far from being updated in sync, but remains domoticz own way to sample unprocessed data: As I said not an issue for data like temperatures that does not change a lot over 5mn, but anything like power that can show huge changes in this time slice will most likely store figures that does not represent accurately what was your consumption over those 5 minutes.

My power consumption can change by several kW in a few seconds. IMO same for solar production on a windy/partly cloudy day.

That's why I setup 2 virtual power meters in fact: Mean I talked about (the better representation of my real power usage IMO) and another storing true max (and able to trigger software driven offloading of heaters & heavy consumers under Domoticz control), all for time slices targeting 10mn (that's device scripts, so cannot ensure exact time) so over domoticz DB values sampling rate. This way, no value missed.

If your solar production is already averaged over 5mn, just make the same on virtuals for other meters that are updated real-time & build the one that does your compute on those 5minute averaged meters. That's IMO the only way to get almost correct figure considering the way domoticz works.

Already talked about this with graph prints showing what domoticz store vs my own virtual average/max script driven virtual meters:
https://www.domoticz.com/forum/viewtopi ... 79#p312079
fjuppe
Posts: 42
Joined: Thursday 14 September 2023 19:32
Target OS: Raspberry Pi / ODroid
Domoticz version: 16341
Location: Stockholm
Contact:

Re: House consumption script help needed

Post by fjuppe »

OK, now I understand the problem that you shared with me. Thanks a lot, I have to think if I would implement something like you describe but as long as I only am interested in the daily figures of consumption right now and not triggering anything based on momentary values, I think it is a bit of overkill for me to implement your solution.

Thanks again
User avatar
RonkA
Posts: 115
Joined: Tuesday 14 June 2022 12:57
Target OS: NAS (Synology & others)
Domoticz version: 2025.1
Location: Harlingen
Contact:

Re: House consumption script help needed

Post by RonkA »

MODbus is the way to get useful data..
see: https://www.domoticz.com/forum/viewtopi ... 81#p290581
SolarEdge ModbusTCP - Kaku - Synology NAS - Watermeter - ESPEasy - DS18b20
Work in progress = Life in general..
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest