Describe the bug
I want to use te powergeneration read from the device tot calculate and upload to pvoutput.org
In the calculation I get the following error: "attempt to perform arithmetic on global 'PowerGeneration' (a string value)".
After adding the command "print(" ----- PowerConsumption = " .. PowerConsumption .. " W"); I found out that to the value a W is added. This makes the value a string value.
Can you remove the W?
To Reproduce
Install following script:
`` - /home/pi/domoticz/scripts/lua/script_device_calculate_consumption.lua
-- This script collects the values below from Domoticz
-- * The Power and energy values (import and export) from a smartmeter
-- * The Power and energy values from a Solar power inverter
-- It then calculates the consumed power and energy from the values above with the formula's
-- * EnergyConsumption = EnergyGeneration + EnergyImport - EnergyExport
-- * PowerConsumption = PowerGeneration + PowerImport - PowerExport
-- It then updates a virtual device which displays the consumed power and energy in Domoticz
-- Domoticz IDX and names of the needed devices
local GenerationDeviceName = "GoodWe solar - Power" -- Device name of the Generated energy
local EnergyDeviceName = "..................." -- Name of the energy device that shows imported and exported energy
local ConsumptionIDX = .... -- IDX of the energy device that shows calculated Consumption
local ConsumptionDeviceName = "Calculation" -- Name of the energy device that shows calculated Consumption
-- Require parameters
local http = require("socket.http")
-- Script parameters
PowerGeneration = 0 -- in Watts
EnergyGeneration = 0 -- in Watt hours
PowerImport = 0 -- in Watts
EnergyImportLow = 0 -- in Watt hours
EnergyImportHigh = 0 -- in Watt hours
EnergyImport = 0 -- in Watt hours
PowerExport = 0 -- in Watts (toegevoegd)
EnergyExportLow = 0 -- in Watt hours
EnergyExportHigh = 0 -- in Watt hours
EnergyExport = 0 -- in Watt hours
PowerConsumption = 0 -- in Watts
EnergyConsumption = 0 -- in Watt hours
Debug = "YES" -- Turn debugging on ("YES") or off ("NO")
-- Lua Functions
function update(device, id, power, energy, index)
commandArray[index] = {['UpdateDevice'] = id .. "|0|" .. power .. ";" .. energy}
end
-- CommandArray
commandArray = {}
-- Generated
PowerGeneration, EnergyGeneration = otherdevices_svalues[GenerationDeviceName]:match("([^;]+);([^;]+)")
if Debug=="YES" then
print(" ----- ---- ---- ---- ---- ---- ---- ---- ---- ");
print(" ----- PowerGeneration = " .. PowerGeneration .. " W");
print(" ----- EnergyGeneration = " .. EnergyGeneration .. " Wh");
print(" ----- ---- ---- ---- ---- ---- ---- ---- ---- ");
end
-- Imported/Exported
EnergyImportLow, EnergyImportHigh, EnergyExportLow, EnergyExportHigh, PowerImport, PowerExport = otherdevices_svalues[EnergyDeviceName]:match("([^;]+);([^;]+);([^$
EnergyImport = EnergyImportLow + EnergyImportHigh
EnergyExport = EnergyExportLow + EnergyExportHigh
if Debug=="YES" then
print(" ----- PowerImport = " .. PowerImport .. " W");
print(" ----- EnergyImport = " .. EnergyImport .. " Wh");
print(" ----- EnergyImportLow = " .. EnergyImportLow .. " Wh");
print(" ----- EnergyImportHigh = " .. EnergyImportHigh .. " Wh");
print(" ----- PowerExport = " .. PowerExport .. " W");
print(" ----- EnergyExport = " .. EnergyExport .. " Wh");
print(" ----- EnergyExportLow = " .. EnergyExportLow .. " Wh");
print(" ----- EnergyExportHigh = " .. EnergyExportHigh .. " Wh");
end
-- Calculate consumption
PowerConsumption = PowerGeneration + PowerImport - PowerExport
EnergyConsumption = EnergyGeneration + EnergyImport - EnergyExport
if Debug=="YES" then
print(" ----- PowerConsumption = " .. PowerConsumption .. " W");
print(" ----- EnergyConsumption = " .. EnergyConsumption .. " Wh");
end
-- Update consumption device in Domoticz
PowerConsumption = PowerGeneration + PowerImport - PowerExport
EnergyConsumption = EnergyGeneration + EnergyImport - EnergyExport
if Debug=="YES" then
print(" ----- PowerConsumption = " .. PowerConsumption .. " W");
print(" ----- EnergyConsumption = " .. EnergyConsumption .. " Wh");
end
-- Update consumption device in Domoticz
if devicechanged[EnergyDeviceName] then
update(ConsumptionDeviceName, ConsumptionIDX, PowerConsumption, EnergyConsumption, 1)
end
return commandArray
``
After running you will find
Expected behavior
You would expect to get the following result:
2019-10-12 22:35:37.731 Status: LUA: ----- ---- ---- ---- ---- ---- ---- ---- ----
2019-10-12 22:35:37.731 Status: LUA: ----- PowerGeneration = 1 W
2019-10-12 22:35:37.731 Status: LUA: ----- EnergyGeneration = 2426100.0 Wh
2019-10-12 22:35:37.731 Status: LUA: ----- ---- ---- ---- ---- ---- ---- ---- ----
But instead I get:
2019-10-12 22:35:37.731 Status: LUA: ----- ---- ---- ---- ---- ---- ---- ---- ----
2019-10-12 22:35:37.731 Status: LUA: ----- PowerGeneration = 1W W
2019-10-12 22:35:37.731 Status: LUA: ----- EnergyGeneration = 2426100.0 Wh
2019-10-12 22:35:37.731 Status: LUA: ----- ---- ---- ---- ---- ---- ---- ---- ----
Screenshots
If applicable, add screenshots to help explain your problem.
Domoticz environment (please complete the following information):
- Domoticz Version: [e.g. 4.10717]
- Python Version: Python 2.7.13 (default, Sep 26 2018, 18:42:22)
Additional context
Add any other context about the problem here.