First.. I have never used a MS browser, ecept to install another..
But it gave me a working code, sort of
Need to figure out what the output there is on "local AMS_strom_forbruk", it's a textdevice updated from MQTT from a HAN plug on my meter..
https://www.amsleser.no/
Code: Select all
-- script_device_text.lua
--Absolutely! Here's your script with the syntax fixed and the necessary conversions from strings to numbers:
-- script_device_text.lua
--To view the log in Domoticz from the code you pasted, you can add logging statements to your Lua script. This will help you track the values and flow of your script. Here's an updated version of your script with added logging:
--```lua
-- script_device_text.lua
commandArray = {}
local virtualcounter_idx = "1887"
local MaxUse = 3000 -- adjustable, might be a UserVariable
-- local MaxUse = tonumber(uservariables['MaxUse'])
-- Convert string inputs to numbers
local AMS_strom_forbruk = 222 --tonumber(string.match(uservariables['AMS strøm forbruk'], "%d+%.?%d*")) -- extract and convert to number
local Effekt_bruk_varmtvann = tonumber(string.match(otherdevices_svalues['Effekt bruk varmtvann'], "%d+%.?%d*")) -- extract and convert to number
local Varmt_Vann_Bereder = tonumber(string.match(otherdevices_svalues['Varmt Vann Bereder'], "%d+%.?%d*")) -- extract and convert to number
local Volt1 = tonumber(string.match(otherdevices_svalues['AMS Spenning L1'], "%d+%.?%d*")) -- extract and convert to number
local Volt2 = tonumber(string.match(otherdevices_svalues['AMS Spenning L2'], "%d+%.?%d*")) -- extract and convert to number
local Volt3 = tonumber(string.match(otherdevices_svalues['AMS Spenning L3'], "%d+%.?%d*")) -- extract and convert to number
local Volt = (Volt1 + Volt2 + Volt3) / 3 -- average voltage
local Varmt_Vann_Bereder_W = Volt * Effekt_bruk_varmtvann -- calculate wattage
local MaxUseDevice = AMS_strom_forbruk - Varmt_Vann_Bereder_W -- example: 2100 = 5100 - 3000
-- Log values for debugging
print("AMS strøm forbruk: " .. AMS_strom_forbruk)
print("Effekt bruk varmtvann: " .. Effekt_bruk_varmtvann)
print("Varmt Vann Bereder: " .. Varmt_Vann_Bereder)
print("Volt1: " .. Volt1)
print("Volt2: " .. Volt2)
print("Volt3: " .. Volt3)
print("Average Volt: " .. Volt)
print("Varmt Vann Bereder W: " .. Varmt_Vann_Bereder_W)
print("MaxUseDevice: " .. MaxUseDevice)
if MaxUseDevice < MaxUse then
commandArray['Power bereder'] = 'Off'
print("Power bereder set to Off")
else
commandArray['MyOtherDevice'] = 'On' -- if MaxUseDevice is higher than MaxUse then do...
print("MyOtherDevice set to On")
end
if Varmt_Vann_Bereder <= 50 then
commandArray['MyOtherDevice'] = 'On' -- ignore MaxUse if Varmt Vann Bereder is lower than 50
print("MyOtherDevice set to On due to Varmt Vann Bereder <= 50")
end
-- Write to TextDevice, Dummy.
-- line 1, MaxUse
-- line 2.
-- line 3, status of "MyOtherDevice" ON/OFF
-- line 4, AMS strøm forbruk
-- Update virtual counter device
local ts = virtualcounter_idx .. "|0|" .. string.format("%.3f", MaxUseDevice)
commandArray['UpdateDevice'] = ts
print("UpdateDevice: " .. ts)
return commandArray
--```
--With these `print` statements, you can view the log output in the Domoticz log to see the values of your variables and the flow of your script. This should help you debug and understand how your script is working. Let me know if you need any further assistance!
--This script should now be syntactically correct and include the necessary conversions from strings to numbers. Let me know if you need any further adjustments or explanations!
From the log in Domoticz..
2024-10-27 16:53:00.117 Status: LUA: Volt1: 235.6
2024-10-27 16:53:00.117 Status: LUA: Volt2: 234.5
2024-10-27 16:53:00.117 Status: LUA: Volt3: 233.6
2024-10-27 16:53:00.117 Status: LUA: Average Volt: 234.56666666667
2024-10-27 16:53:00.118 Status: LUA: Varmt Vann Bereder W: 0.0
2024-10-27 16:53:00.118 Status: LUA: MaxUseDevice: 222.0
My manuell input as this part didn't read the sensr right! AMS_strom_forbruk
2024-10-27 16:53:00.118 Status: LUA: Power bereder set to Off
2024-10-27 16:53:00.118 Status: LUA: UpdateDevice: 1887|0|222.000
2024-10-27 16:53:00.121 Status: EventSystem: Script event triggered: /home/domoticz/scripts/lua/script_time_text.lua
Some parts are working, maybe not in the correct script way or thinking..
Need to figgure out the missing parts
Arne Kjetil