LogicMachine Forum
Script with Tag condition - Printable Version

+- LogicMachine 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: Script with Tag condition (/showthread.php?tid=6416)



Script with Tag condition - Domoticatorino - 05.05.2026

Code:
-- 1. MAPPATURA: "Tag del Sensore" -> "Tag dell'Abilitazione"
local mappa_settori = {
  ['sensore_area_1_settore_1'] = 'area_1_settore_1',
  ['sensore_area_1_settore_2'] = 'area_1_settore_2',
  ['sensore_area_1_settore_3'] = 'area_1_settore_3',
  ['sensore_area_2_settore_1'] = 'area_2_settore_1',
  ['sensore_area_2_settore_2'] = 'area_2_settore_2',
  ['sensore_area_2_settore_3'] = 'area_2_settore_3',
}

local valore_allarme = event.getvalue()
local addr_sorgente = event.dst

if valore_allarme == true then
    -- A. CONTROLLO ESCLUSIONE (5/2/x -> 5/5/100+x)
    local offset = tonumber(addr_sorgente:match("/([^/]+)$"))
    if offset then
        local addr_esclusione = "5/5/" .. (100 + offset)
        if grp.getvalue(addr_esclusione) == true then
            log('ESCLUSO: ' .. addr_sorgente)
            return
        end
    end

    -- B. RECUPERO OGGETTO E IDENTIFICAZIONE
    local oggetto_allarme = grp.find(addr_sorgente)
    local tags = (oggetto_allarme and oggetto_allarme.tags) or {}
   
    local tag_abilitazione_da_cercare = nil

    -- Cerchiamo quale dei tag del sensore è presente nella nostra mappa
    for _, tag in ipairs(tags) do
        local clean_tag = tag:gsub("%s+", ""):lower()
        if mappa_settori[clean_tag] then
            tag_abilitazione_da_cercare = mappa_settori[clean_tag]
            break
        end
    end

    -- C. VERIFICA STATO AREA
    if tag_abilitazione_da_cercare then
        -- Cerchiamo l'oggetto che ha il tag del settore (es. 'area_1_settore_1')
        local ogg_settore = grp.tag(tag_abilitazione_da_cercare)
       
        if #ogg_settore > 0 then
            local stato_area = grp.getvalue(ogg_settore[1].address)
           
            if stato_area == true then
                -- D. INVIO MAIL
                local mailer = require('mail')
                local nome_sensore = oggetto_allarme.name or addr_sorgente
                local res, err = mailer.send('tua_mail@esempio.it',
                    'ALLARME: ' .. nome_sensore,
                    'Allarme reale in ' .. tag_abilitazione_da_cercare)
                log('Mail inviata:', res)
            else
                log('Area disattivata: ' .. tag_abilitazione_da_cercare)
            end
        else
            log('ERRORE: Nessun oggetto di comando trovato con tag: ' .. tag_abilitazione_da_cercare)
        end
    else
        log('ERRORE: Sensore ' .. addr_sorgente .. ' non ha un tag "sensore_area_..." valido.')
    end
end

Hi everybody,
with this script I would like to send a notification (by mail) if an alarm sensor (tag alarm) goes to true but only if this sensor own to a sector (tag "area_x_sector_x) is armed.
The sensor has a second tag "sensore_area_x_settore_x) in order to verify if it owns to the sector armed.
It does not work because the tag value is empty.

How can be possible? Is it possible to use 2 tags for a grp address?

Thanks.


RE: Script with Tag condition - admin - 05.05.2026

Yes, you can assign multiple tags to a group address.

Your script does not work because grp.find() does not return assigned tags as a table. Use grp.gettags(alias) instead.

You should add some extra checks so that an email is only sent once, not on every detection.


RE: Script with Tag condition - Domoticatorino - 05.05.2026

(6 hours ago)admin Wrote: Yes, you can assign multiple tags to a group address.

Your script does not work because grp.find() does not return assigned tags as a table. Use grp.gettags(alias) instead.

You should add some extra checks so that an email is only sent once, not on every detection.

Thank you Admin! 

Do you mean to use the following command instead:

local oggetto_allarme = grp.gettags(addr_sorgente)

But  grp.gettags it should be blue. But it is not. It is correct?

Thanks.


RE: Script with Tag condition - admin - 05.05.2026

grp.gettags is missing from the highlighted function list but it does not affect the actual script.