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
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.
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.
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.