BUG ! Computed kWh device cannot be updated with initial svalue (workaround found)

Python and python framework

Moderator: leecollings

Post Reply
willemd
Posts: 649
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

BUG ! Computed kWh device cannot be updated with initial svalue (workaround found)

Post by willemd »

Below is a small piece of code from a Python Plugin that I am developing using the DomoticzEx framework.
After each piece of code I have pasted the log output. It all seems to work fine except for the final update.
You can see the sValue is not updated onto the device. sValue remains ''" instead of "7;1". Why?
I do not understand why the last update does not work. Someone here does?

The device is a kWh device with the kWh value to be computed from the Watts input (not from device). I have checked the definition of the device and that shows up correctly. According to the Wiki the nValue should be zero and the sValue should be in the format "123;123456" and the second value will be ignored.

Code: Select all

Unit=DEVSLIST[Dev][0]
DeviceID="{:04x}{:04x}".format(self.Hwid,Unit)
Domoticz.Log(responseJson["stats"][Dev])
output is: 2025-06-25 09:22:28.900 Xtend: 7

fieldValue=responseJson["stats"][Dev]
Domoticz.Log(Devices[DeviceID].Units[Unit])
output is: 2025-06-25 09:22:28.900 Xtend: Unit: 43, Name: 'XTEND: HP energy usage', nValue: 7, sValue: '', LastUpdate: 2025-06-25 09:21:17

Devices[DeviceID].Units[Unit].nValue=0
Devices[DeviceID].Units[Unit].sValue=str(fieldValue)+";1" # watts are supplied, kwh are calculated by Domoticz.
Domoticz.Log(Devices[DeviceID].Units[Unit].sValue)
output is: 2025-06-25 09:22:28.900 Xtend: 7;1

Devices[DeviceID].Units[Unit].Update()
Domoticz.Log(Devices[DeviceID].Units[Unit])
output is: 2025-06-25 09:22:28.914 Xtend: Unit: 43, Name: 'XTEND: HP energy usage', nValue: 0, sValue: '', LastUpdate: 2025-06-25 09:22:28
Last edited by willemd on Wednesday 25 June 2025 11:47, edited 3 times in total.
willemd
Posts: 649
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: Python plugin does not update kWh device. Why?

Post by willemd »

I see an issue 6194 on Github which also describes the problem, although the description states that the device cannot be created. I can create the device I just cannot populate it with data.

There does not seem to be any action or solution to that issue?
willemd
Posts: 649
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: Python plugin does not update kWh device. Why?

Post by willemd »

I found that if you put an initial svalue on the device (with sqlite3, directly on the devicestatus table) then after that the update code works correctly.

So it is a bug when the device is created. It should initialize it with an initial svalue.

Or alternatively the update statement should work when there is no initial value.

But how to work around this in a Python plugin?
willemd
Posts: 649
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: Python plugin does not update kWh device. Why?

Post by willemd »

More info: also updating the device with a JSON statement does not work when there is no initial svalue present.
So the only workaround now is to add a svalue in the devicestatus table first directly in the dbase.

Note there is no problem updating other device types, only the computed kwh device.
willemd
Posts: 649
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: BUG ! Computed kWh device cannot be updated with initial svalue

Post by willemd »

I think I found where the problem is. It is in line 5371-5397 in the SQLhelper.cpp file.

If the initial svalue does not consists of two elements then it just restores the initial value. So if the initial value is empty (which it always is after the device is created) then the svalue remains empty. Here is the offending code.

Code: Select all

std::vector<std::string> powerAndEnergyBeforeUpdate;
			StringSplit(sValueBeforeUpdate, ";", powerAndEnergyBeforeUpdate);
			if (powerAndEnergyBeforeUpdate.size() == 2)
			{
				//we need to use atof here because some users seem to have a illegal sValue in the database that causes std::stof to crash
				double powerDuringInterval = atof(powerAndEnergyBeforeUpdate[0].c_str());
				double energyUpToInterval = atof(powerAndEnergyBeforeUpdate[1].c_str());
				double energyDuringInterval = powerDuringInterval * intervalSeconds / 3600;
				double energyAfterInterval = energyUpToInterval + energyDuringInterval;
				std::vector<std::string> powerAndEnergyUpdate;
				StringSplit(sValue, ";", powerAndEnergyUpdate);
				if (!powerAndEnergyUpdate.empty())
				{
					const char* powerUpdate = powerAndEnergyUpdate[0].c_str();
                    char sValueUpdate[100];
                    sprintf(sValueUpdate, "%s;%.4f", powerUpdate, energyAfterInterval);
					sValue = sValueUpdate;
				}
				else
				{
                    sValue = sValueBeforeUpdate.c_str();
				}
			}
			else
            {
                sValue = sValueBeforeUpdate.c_str();
            }
willemd
Posts: 649
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: BUG ! Computed kWh device cannot be updated with initial svalue

Post by willemd »

I found a workaround

1) First I create the kWh device using the plugin with the default "from device" setting (leave the options field empty when creating the device).
2) Immediately after creation, I update the device svalue to "0;0" using the plugin.
3) and then immediately I update the options field to 'EnergyMeterMode': '1' which changes the device to "computed" instead of "from device".

Note in step 3 the update statement needs to be run with the UpdateOptions=True flag like this
Devices[DeviceID].Units[Unit].Update(UpdateOptions=True)

Now it has an initial value and future updates will work.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest