Calculation of Tuya Energy Consumption for a given time frame

Platform information:
Hardware: Raspberry PI4
OS: Openhabian
Java Runtime Environment: openjdk 17.0.13
openHAB version: OH4.3

Hi All
Iam using Smarthome J Tuya Binding with my energy meter. Now my challenge is that i have observed that Binding is not providing power consumed at all but the it seems to provide current usage in watts. The figure is generally provided every 5seconds. However sometimes every hour. I would like to calculate the power consumed using the figure coming through every 5 seconds. My challenge is that rule is not working as i want. The calculation gives 0E-8 which makes it impossible to calculate usage for the period.

rule "Calculate HP Power Used"
when Item switch_with_metering_cur_power received update
then
logInfo("HeatingManagementKWH","Update recieved")
if(power_last_update ===null)
	{
	
	var sw_lastUpdate = switch_with_metering_cur_power.lastUpdate
	power_last_update= sw_lastUpdate
	if(power_last_update ===null)
		{
		power_last_update =now
		}
	}
if (switch_with_metering_cur_power.state >0)
	{
	var timesince = (Duration.between(power_last_update,ZonedDateTime.now()).toSeconds)/5
	if (timesince==0){timesince=1}
	
	logInfo("HeatingManagementKWH","5sec Ticks: " + timesince + String::format(" PowerConsumed: %1$.3fw", ((switch_with_metering_cur_power.state as Number /10)*(timesince/720))) )
	logInfo("HeatingManagementKWH",String::format("Current val: %1$.3fw", (switch_with_metering_cur_power.state as Number /10)))
	kWh = kWh + ((switch_with_metering_cur_power.state as Number /10)*(timesince/720))/1000
	logInfo("HeatingManagementKWH","Current Raw: " + kWh)
	hp_tuya_power_consumed_today.postUpdate(kWh)
	logInfo("HeatingManagementKWH",String::format("Current: %1$.3f kW", kWh))
	}
power_last_update =now
end

the output from log is

2025-01-25 22:20:52.526 [INFO ] [re.model.script.HeatingManagementKWH] - Update recieved
2025-01-25 22:20:52.529 [INFO ] [re.model.script.HeatingManagementKWH] - 5sec Ticks: 1 PowerConsumed: 0.000w
2025-01-25 22:20:52.530 [INFO ] [re.model.script.HeatingManagementKWH] - Current val: 3.600w
2025-01-25 22:20:52.532 [INFO ] [re.model.script.HeatingManagementKWH] - Current Raw: 0E-8
2025-01-25 22:20:52.534 [INFO ] [re.model.script.HeatingManagementKWH] - Current: 0.000 kW

Break up your calculation and log out each part individually. You’ve got so much going on on each log line it’s impossible to tell if the other is the calculations, the formatting, both, it neither.

But if we assume the state of the Item is 36.

(36 / 10)(1 / 720) = 0.005
0.005 / 1000 = 0.000005

These are really small numbers and rounding them in the string format to 3 sectional places is going to lead to 0. You need all those decimal places to see what’s going on with small numbers like this.

You also didn’t indicate what type of Item it is and where it has units or not and what those units are.

Note, assuming you’re kwh variable it’s a global, it will get reset to 0 on every reload of that .rules file.

thanks Rich, and apologies. i forgot to update. i figured it out, i followed exactly your layout

rule "Calculate HP Power Used"
when Item switch_with_metering_cur_power received update 

then

if(power_last_update ===null)
	{
	
	var sw_lastUpdate = switch_with_metering_cur_power.lastUpdate
	power_last_update= sw_lastUpdate
	if(power_last_update ===null)
		{
		power_last_update =now
		}
	}
if (switch_with_metering_cur_power.state >0)
	{
	logInfo("KWH", "Calculating " + kWh + " meter: " + switch_with_metering_cur_power.state.toString)
	var ticks = (Duration.between(power_last_update,ZonedDateTime.now()).toSeconds)
	if (ticks>=0)
		{
		logInfo("HeatingManagementKWH","5sec Ticks: " + ticks + String::format(" PowerConsumed: %1$.3fw", ((switch_with_metering_cur_power.state as Number /10)*ticks/3600)) )
		logInfo("HeatingManagementKWH",String::format("Power : %1$.3fw", (switch_with_metering_cur_power.state as Number /10)))
		kWh = Float::parseFloat(String::format("%s",hp_tuya_power_consumed_today.state)) + (((switch_with_metering_cur_power.state as Number /10)* ticks/3600)/1000)
		logInfo("HeatingManagementKWH","Raw Consump: " + kWh)
		logInfo("HeatingManagementKWH",String::format("Form Consump: %1$.3f kW", kWh))
		hp_tuya_power_consumed_today.postUpdate(kWh)
		power_last_update =now
		}
	}
end
1 Like

and the output from my logging, now i am just monitoring to determine the accuracy of this

2025-01-26 20:41:36.450 [INFO ] [org.openhab.core.model.script.KWH   ] - Calculating 1.40410442 meter: 36.0
2025-01-26 20:41:36.455 [INFO ] [re.model.script.HeatingManagementKWH] - 5sec Ticks: 3602 PowerConsumed: 3.602w
2025-01-26 20:41:36.458 [INFO ] [re.model.script.HeatingManagementKWH] - Power : 3.600w
2025-01-26 20:41:36.461 [INFO ] [re.model.script.HeatingManagementKWH] - Raw Consump: 1.40770650
2025-01-26 20:41:36.463 [INFO ] [re.model.script.HeatingManagementKWH] - Form Consump: 1.408 kW

This is exactly what I need right now. Thanks for creating this discussion, and thanks Rich @rlkoshak for providing the guidance

So far, it’s working great except for a minor setback. The SmartmeterJTuya addon I’m using isn’t capturing minor fluctuations in power consumption. It records large jumps, like from 0 to 800W, but if the increase is less than 200W, the usage stays at 800W. As a result, my end-of-day usage of around 2.8 kWh is usually off by about 400W, whereas my recorded usage on Tuya’s official app is around 3.2 kWh.