Need Lua help here, no programer

Moderator: leecollings

Post Reply
Droll
Posts: 33
Joined: Sunday 28 December 2014 23:50
Target OS: Linux
Domoticz version: 2024.7
Location: Norway
Contact:

Need Lua help here, no programer

Post by Droll »

Got some ideas, but I don't know if it work, or if there is a better way..
Not new to Domoticz, but scrips are not my language :? .

Running linux, on a Pc, Ubuntu, SSD harddrive.
About Domoticz
Version: 2024.7 (build 16297)
Build Hash: 85e28226f
Compile Date: 2024-10-26 09:19:44
dzVents Version: 3.1.8
Python Version: 3.8.10 (default, Sep 11 2024, 16:02:53) [GCC 9.4.0]

This is a cut/paste schript, doesn't work.
Spoiler: show
-- script_device_text.lua
commandArray = {}

local virtualcounter_idx = "1887"
local MaxUse = 3000 -- adjuseble, might be a UserVariable ?--local MaxUse = toInteger(uservariables['MaxUse'])

AMS strøm forbruk = tonumber(uservariables['AMS strøm forbruk'])-- string ?, 3460.0;73711400 0/3460.0;73711400---3460

Effekt bruk varmtvann = tonumber(otherdevices_svalues['Effekt bruk varmtvann'])--string ?, 12.8;0.0;0.0 0---log "2024-10-22 14:45:00";0"2024-10-22 14:50:00";3057

Varmt Vann Bereder = tonumber(otherdevices_svalues['Varmt Vann Bereder'])--string ? , 77.1 0/77.1---77,1

Volt 1 = tonumber(otherdevices_svalues['AMS Spenning L1'])--string ?,237.30 0/237.30
Volt 2 = tonumber(otherdevices_svalues['AMS Spenning L2'])--string ?,237.60 0/237.60
Volt 3 = tonumber(otherdevices_svalues['AMS Spenning L3'])--string ?,236.70 0/236.70


Volt = (AMS Spenning L1 + AMS Spenning L2 + AMS Spenning L3) / 3 -- volt = 235 ?
Varmt Vann Bereder W = volt * Effekt bruk varmtvann -- Think it's Ampere, need watt.

MaxUseDevice = AMS strøm forbruk - Varmt Vann Bereder W -- exs 2100 = 5100 - 3000 or 3100 = 6100 - 3000, here 460 = 3460 -3000

If "MaxUseDevice" < "MaxUse" then commandArray['Power bereder']='Off'else commandArray['MyOtherDevice']='on'-- If MaxUsaDevice is higer than MaxUse then Do...
if Varmt Vann Bereder <= 50 then commandArray['MyOtherDevice']='on'-- ignore 'MaxUse', if 'Varmt Vann Bereder' is lower temp 50 than, leave on..

--Write to TextDevice, Dummy.
-- line 1, MaxUse
-- line 2.
-- line 3, status off "myOtherDevice" ON/OFF
-- line 4, AMS strøm forbruk


-- Update virtual counter device

ts = virtualcounter_idx .. "|0|" .. string.format("%.3f", tostring(countvalue)) -- copy/paste
commandArray['UpdateDevice'] = ts --copy/paste



return commandArray
It's a cut/paste script, not supposed to work, but you should get the picture on what I want to achieve :-)
Some parts do work, but not as I want.
The math parts is OK, I guess, not pretty..
But how to get the values from the different sensors, format them to numbers ?
And the there is the layout of the script itself, make it the RIGHT way..


Droll
HvdW
Posts: 504
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: Need Lua help here, no programer

Post by HvdW »

When you're on Edge use CoPilot AI (the sign in the upper right corner)
Ask your question in your own language, give some clues - the input you have - ask for the output you want and see what happens.
I suggest you ask CoPilot to write the script in dzVents, which is easier to handle and understand.
Bugs bug me.
Droll
Posts: 33
Joined: Sunday 28 December 2014 23:50
Target OS: Linux
Domoticz version: 2024.7
Location: Norway
Contact:

Re: Need Lua help here, no programer

Post by Droll »

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
Droll
Posts: 33
Joined: Sunday 28 December 2014 23:50
Target OS: Linux
Domoticz version: 2024.7
Location: Norway
Contact:

Re: Need Lua help here, no programer

Post by Droll »

There is something in the line below.
local AMS_strom_forbruk = (string.match(uservariables['AMS strøm forbruk'], "%d+%.?%d*")) -- extract and convert to number

1691 2024-10-27 18:43:37 AMS strom forbruk 3796.0;73721888 0/3796.0;73721888

I want the 3796,0 part, in number.

But it's somewhat a progress 8-)


Arne Kjetil
Droll
Posts: 33
Joined: Sunday 28 December 2014 23:50
Target OS: Linux
Domoticz version: 2024.7
Location: Norway
Contact:

Re: Need Lua help here, no programer

Post by Droll »

Got something I think will work, probably not the prettyest, but it does do the simple task I want it to do...

Still some bug in the programming....

local virtualMeterValue = otherdevices_svalues['AMS strom forbruk']
local extractedValue = tonumber(string.match(virtualMeterValue or "", "%d+%.?%d*"))


Works

local extractedValue = tonumber(string.match('AMS strom forbruk' or "", "%d+%.?%d*"))

In my mind, it shoud work, but doesnt..


And the distane between line on the TextDevice..
It's like:
Text 1

Text 2

Text 3

I want:
Text 1
Text 2
Text 3
https://domoticz.com/forum/download/fil ... w&id=33247
But this is one off my few codes, acceally made by CoPilot AI....With some inout from me

Code: Select all

commandArray = {}

--local virtualcounter_idx = "1887"
--local MaxUse = 3000 -- adjustable, might be a UserVariable
local MaxUse = tonumber(uservariables['MaxUse'])

local virtualMeterValue = otherdevices_svalues['AMS strom forbruk']
local extractedValue = tonumber(string.match(virtualMeterValue or "", "%d+%.?%d*"))

local Effekt_bruk_varmtvann = tonumber(string.match(otherdevices_svalues['Effekt bruk varmtvann'], "%d+%.?%d*"))
local Varmt_Vann_Bereder = tonumber(string.match(otherdevices_svalues['Varmt Vann Bereder'], "%d+%.?%d*"))

local Volt1 = tonumber(string.match(otherdevices_svalues['AMS Spenning L1'], "%d+%.?%d*"))
local Volt2 = tonumber(string.match(otherdevices_svalues['AMS Spenning L2'], "%d+%.?%d*"))
local Volt3 = tonumber(string.match(otherdevices_svalues['AMS Spenning L3'], "%d+%.?%d*"))

local Volt = (Volt1 + Volt2 + Volt3) / 3 -- average voltage
local Varmt_Vann_Bereder_W = Volt * Effekt_bruk_varmtvann -- calculate wattage

local MaxUseDevice = extractedValue - Varmt_Vann_Bereder_W -- example: 2100 = 5100 - 3000

-- Log values for debugging
print("extractedValue: " .. extractedValue)
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['Rele varmt vann virt'] = 'Off'
    Status = Off
    print("Power bereder set to Off")
else
    commandArray['Rele varmt vann virt'] = 'On'
    print("Power bereder set to On")
end

if Varmt_Vann_Bereder <= 50 then
    commandArray['Rele varmt vann virt'] = 'On'
    print("Rele varmt vann set to On due to Varmt Vann <= 50")
end


local Tekst1 = "<font color='Aqua'>Strom Inntak: </font><font color='Yellow'>" .. extractedValue .. "</font>\n"
local Tekst2 = "<font color='Aqua'>Effekt bruk varmtvann: </font><font color='Yellow'>" .. Varmt_Vann_Bereder_W .. "</font>\n"
local Tekst3 = "<font color='Aqua'>Status rele: </font><font color='Yellow'>" .. otherdevices['Rele varmt vann virt'] .. "</font>"




-- Combine the text values into one string
local displayText = Tekst1 .. "\n" .. Tekst2 .. "\n" .. Tekst3

-- Update the text device with the combined text
ttidx = otherdevices_idx['TextTest']
print('TextTest idx '..ttidx)

commandArray['UpdateDevice'] = ttidx..'|0|'..displayText --newtext


return commandArray
Arne Kjetil
Attachments
Skjermbilde 2024-10-28 232215.png
Skjermbilde 2024-10-28 232215.png (30.47 KiB) Viewed 465 times
HvdW
Posts: 504
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: Need Lua help here, no programer

Post by HvdW »

Beautiful!
Bugs bug me.
Droll
Posts: 33
Joined: Sunday 28 December 2014 23:50
Target OS: Linux
Domoticz version: 2024.7
Location: Norway
Contact:

Re: Need Lua help here, no programer

Post by Droll »

Started with some ideas, roughly put together, mostly wrong code...

Got some experience with Basic (Bascom), Lua (Domoticz) and basic Excel.
But nothing advance...n

Will let it run for some time, find some bugs :lol:, and eventually put together some hardware

Spoiler: show
commandArray = {}

local MaxUse, NedreVannTemp = tonumber(uservariables['MaxUse']), tonumber(uservariables['NedreVannTemp'])
local extractedValue = tonumber(string.match(otherdevices_svalues['AMS strom forbruk'] or "", "%d+%.?%d*"))

local Effekt_bruk_varmtvann = tonumber(string.match(otherdevices_svalues['Effekt bruk varmtvann'], "%d+%.?%d*"))
local Varmt_Vann_Bereder = tonumber(string.match(otherdevices_svalues['Varmt Vann Bereder'], "%d+%.?%d*"))
local Volt1 = tonumber(string.match(otherdevices_svalues['AMS Spenning L1'], "%d+%.?%d*"))
local Volt2 = tonumber(string.match(otherdevices_svalues['AMS Spenning L2'], "%d+%.?%d*"))
local Volt3 = tonumber(string.match(otherdevices_svalues['AMS Spenning L3'], "%d+%.?%d*"))

local Volt = (Volt1 + Volt2 + Volt3) / 3 -- average voltage, skkert n�ye nok med 235V ??
local Varmt_Vann_Bereder_W = Volt * Effekt_bruk_varmtvann -- calculate wattage

local MaxUseDevice = extractedValue - 3000 -- example: 2100 = 5100 - 3000

-- Log values for debugging
print("extractedValue: " .. extractedValue)
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['Rele varmt vann virt'] = 'on'
Status = ON
print("Power bereder set to On")
else
commandArray['Rele varmt vann virt'] = 'Off'
print("Power bereder set to Off")
end

if Varmt_Vann_Bereder <= 50 then
--if Varmt_Vann_Bereder <= Nedre VannTemp then
commandArray['Rele varmt vann virt'] = 'On'
print("Rele varmt vann set to On due to Varmt Vann <= 50")
end

local Tekst1 = "<font color='Aqua'>Strøm Inntak: </font><font color='Yellow'>" .. string.format("%.2f", extractedValue) .. "</font> W<br>"
local Tekst2 = "<font color='Aqua'>Effekt bruk varmtvann: </font><font color='Yellow'>" .. string.format("%.2f", Varmt_Vann_Bereder_W) .. "</font> W<br>"
local Tekst3 = "<font color='Aqua'>Status rele: </font><font color='Yellow'>" .. otherdevices['Rele varmt vann virt'] .. "</font><font color='Aqua'> " .. string.format("%.2f", MaxUseDevice) .. "</font> W"

-- Combine the text values into one string
--local displayText = Tekst1 .. "\n" .. Tekst2 .. "\n" .. Tekst3

local displayText = "<div style='margin-bottom:-4px;'>" .. Tekst1 .. "</div>" ..
"<div style='margin-bottom:-3px;'>" .. Tekst2 .. "</div>" ..
"<div style='margin-bottom:-4px;'>" .. Tekst3 .. "</div>"

-- Update the text device with the combined text
ttidx = otherdevices_idx['TextTest']
print('TextTest idx '..ttidx)

commandArray['UpdateDevice'] = ttidx..'|0|'..displayText --newtext

return commandArray
Arne Kjetil

https://domoticz.com/forum/download/fil ... w&id=33249
Attachments
Skjermbilde 2024-10-29 171805.png
Skjermbilde 2024-10-29 171805.png (29.32 KiB) Viewed 440 times
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest