Logic Machine Forum
grp.create() - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: grp.create() (/showthread.php?tid=5229)



grp.create() - tomnord - 02.02.2024

I'm trying to figure out how to generate a series of viritual adresses. example:

room_nr = {name=, datatype=, }
functions =
 
etc...

I want GA's to be named something like this: "room_nr""function"

i guess I need a for loop, based on room_nr, but how do I separate the different roomnumbers in the table? 
It's most likely easy, brain is in weekend mode Smile


RE: grp.create() - admin - 02.02.2024

Use this as a starting point:
Code:
for i = 1, 10 do
  name = 'room ' .. i
  log(name)
end



RE: grp.create() - tomnord - 12.02.2024

What is the best way, when using grp.create, to check if the GA is allready created? I'm thinking of making a dynamic script, so that when I add new GA's and tag them correctly the script will generate new virtual GA's.


RE: grp.create() - admin - 12.02.2024

You can use grp.find().


RE: grp.create() - tomnord - 12.02.2024

Regarding this, and the funcion string.gsub() my pattern is '_PV' but i also have some GA's named '_PV3'. is there any way to make this function to apply only with _PV and not _PV3?


RE: grp.create() - admin - 12.02.2024

If your string ends with _PV then you can use _PV$ regular expression. See this for more info: https://www.lua.org/manual/5.1/manual.html#5.4.1


RE: grp.create() - fleeceable - 12.02.2024

(12.02.2024, 08:20)tomnord Wrote: What is the best way, when using grp.create, to check if the GA is allready created? I'm thinking of making a dynamic script, so that when I add new GA's and tag them correctly the script will generate new virtual GA's.

I use function block like this when automatically generating addresses in my programs...

Code:
--**********CREATE GROUP ADDRESSES******************
function createga(groupaddress,datatype,name)
  exist = grp.alias(groupaddress)
  if exist == nil then
    address = grp.create({
    datatype = datatype,
    address = groupaddress,
    name = name,
    comment = 'Automatically created object',
    units = '',
    tags = { },
    })
  end
end
On some systems I use user script where I put all needed group addresses (so they all are on the same file and it's quite easy to change them)..

User script (HEATING_config):
Code:
Main_GA = '33'

group_address_for_frontend_update = '/0/0'        --If any modifications on values or chart must be updated, this variable is used.
group_address_for_backend_update = '/0/1'        --If any modifications on values or data file must be updated, this variable is used.

Scheduled/Resident/Event script:
Code:
require('user.HEATING_config')
createga( Main_GA .. group_address_for_backend_update, dt.bool,'HEATING - Update backend')
createga( Main_GA .. group_address_for_frontend_update, dt.bool,'HEATING - Update frontend')

--DO SOMETHING


Hope this helps...


RE: grp.create() - tomnord - 12.02.2024

Thanks guys. I'll do some testing


RE: grp.create() - victor.back - 12.04.2024

Hi I have a function in a script for creating the necessarily groupadresses. But in this script it´s not creating the adresses, can I ask if someone can see what can be the issue? 

Code:
-- Skapa nödvändiga gruppadresser
local function skapaGruppadresser()
    local maxZoner = 25  -- Antagande att det finns max 25 zoner
    for i = 100, 121 do
        -- Adresser och namn för zonerna
        local varaktighetAddress = '40/2/' .. i
        local dagligVaraktighetAddress = '40/4/' .. i
        local varaktighetName = 'Varaktighet Zon ' .. i
        local dagligVaraktighetName = 'Daglig Varaktighet Zon ' .. i

        -- Skapa adress för varaktighet om den inte redan finns
        if not grp.find(varaktighetAddress) then
            grp.create({datatype = 'dt.text', address = varaktighetAddress, name = varaktighetName})
        end

        -- Skapa adress för daglig varaktighet om den inte redan finns
        if not grp.find(dagligVaraktighetAddress) then
            grp.create({datatype = 'dt.text', address = dagligVaraktighetAddress, name = dagligVaraktighetName})
        end
    end

    -- Skapa återställningsflagga om den inte redan finns
    local resetAddress = '40/3/0'
    if not grp.find(resetAddress) then
        grp.create({datatype = 'dt.bool', address = resetAddress, name = 'Återställningsflagga'})
    end
end

skapaGruppadresser()



RE: grp.create() - admin - 13.04.2024

datatype field should be either dt.text (without quotes) or 'text'


RE: grp.create() - victor.back - 13.04.2024

(13.04.2024, 08:44)admin Wrote: datatype field should be either dt.text (without quotes) or 'text'

Thanks! That solved it... Big Grin