need a loop for reading messages for logging  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

need a loop for reading messages for logging

Post by pvklink »

Hi, i made one script for all my timers and groups with dzvents, so no domoticz timers and groups..
after a manual event or a timed event groups of switches are executed and switches for my dayly cycle (sun op, wake up breakf, etc are put to on-off.. works very nice!

i also made my own logging, not for errors and sensor info, only clear results..

With these rules i can log to domoticz and to my own logging...

for each group of devices: msgtxt = 'LAATSTE SCENES gereset ...'

and at the end of my script:
dz.log('Script: ' .. info.scriptName .. ', device: ' .. item.name .. ' ' .. msgtxt, dz.LOG_INFO)
timertekst = dz.time.rawDate .. ' ' .. dz.time.rawTime .. ' : ' .. msgtxt .. '<br>' .. dz.devices('timerlog').text
dz.devices('timerlog').updateText(timertekst)

Question
In the script below there can be executed more then one scenes/groups.. so my msgtxt is overwritten and only the last one is executed in the log
So i need:
- an array for setting msgtxt
- and a construct that reads all defined msgtxt(1-n) (see the end of my script)

Can somebody give a hand? Script is working more than fine! almost finished

Code: Select all

-- dit script draait bij avond slapen, zon op, ontbijten, van huis, thuis en opstaan, uit huis thuis 

local OnTimer1      = '15 minutes before sunset on mon,tue,wed,thu,fri,sat,sun'         -- HUIS SFEERVERLICHTING AAN
local OnTimer2      = '15 minutes after sunset on mon,tue,wed,thu,fri,sat,sun'          -- TUIN SFEERVERLICHTING AAN

local OffTimer1     = 'at 23:00 on mon,tue,wed,thu,sun'                                 -- EINDE AVOND ALLES UIT DOOR DE WEEK
local OffTimer2     = 'at 23:45 on fri,sat'                                             -- EINDE AVOND ALLES UIT IN HET WEEKEND

local OnTimer3      = 'at 23:05 on mon,tue,wed,thu,sun'                                 -- SLAPEN START door de week
local OnTimer4      = 'at 23:50 on fri,sat'                                             -- SLAPEN START in het weekend
local OffTimer3     = 'at 06:08 on mon,tue,thu,fri'                                     -- EINDE SLAPEN peter
local OffTimer4     = 'at 07:08 on mon,tue,wed,thu,fri'                                 -- EINDE SLAPEN MO

local OnTimer5      = 'at 06:10 on mon,tue,thu,fri'                                     -- OPSTAAN peter
local OnTimer6      = 'at 07:10 on mon,tue,wed,thu,fri'                                 -- OPSTAAN mo
local OffTimer5     = 'at 06:15 on mon,tue,thu,fri'                                     -- EINDE OPSTAAN peter  
local OffTimer6     = 'at 07:25 on mon,tue,wed,thu,fri'                                 -- EINDE OPSTAAN mo

local OnTimer7      = '2 minutes after sunrise on mon,tue,wed,thu,fri,sat,sun'          -- ZON OP 
local OffTimer7     = '2 minutes after sunset on mon,tue,wed,thu,fri,sat,sun'           -- ZON ONDER

local OnTimer8      = 'at 06:13 on mon,tue,thu,fri'                                     -- ONTBIJTEN AAN PETER
local OnTimer9      = 'at 07:20 on mon,tue,wed,thu,fri'                                 -- ONTBIJTEN AAN MO
local OnTimer10     = 'at 09:20 on sat,sun'                                             -- ONTBIJTEN AAN WEEKEND
local OffTimer8     = 'at 06:44 on mon,tue,thu,fri'                                     -- ONTBIJTEN UIT PETER
local OffTimer9     = 'at 08:00 on mon,tue,wed,thu,fri'                                 -- ONTBIJTEN UIT MO
local OffTimer10    = 'at 11:00 on sat,sun'                                             -- ONTBIJTEN UIT WEEKEND

return {
    on = {  devices = {'Schakelgroepen','Jablotronenabled'},     
            timer = {OnTimer1, OnTimer2, OnTimer3, OnTimer4, OnTimer5, OnTimer6, OnTimer7, OnTimer8, OnTimer9, OnTimer10, OffTimer1, OffTimer2, OffTimer3, OffTimer4, OffTimer5, OffTimer6, OffTimer7, OffTimer8, OffTimer9, OffTimer10}
        },
        
    logging =   { level   = domoticz.LOG_DEBUG,                 
                  marker  = "Timers"},
              
    execute = function(dz,item,info)

    if (
        (item.isTimer and (item.trigger == OnTimer1))                                           -- START AVOND HUIS sfeerverlichting binnen
        or (item.isDevice and item.name == 'Schakelgroepen' and (item.state == 'Aan' or item.state == 'Sfeerverlichting binnen')) 
        ) then                                                                          
 
            --if (item.isTimer) then  dz.devices('Schakelgroepen').switchSelector(10)  end              -- weet niet of deze nodig is n keer remarken
            dz.devices('Opstaan').switchOff()
            dz.devices('Ontbijten').switchOff()
            dz.devices('Slapen').switchOff()
            dz.devices('Avond').switchOn()

            dz.devices('usb lamp').switchSelector(40)
            dz.devices('lantaarn').switchOn()
            dz.devices('Lamp dressoir links').switchOn()
            dz.devices('Lamp dressoir rechts').switchOn()
            dz.devices('Lamp tv links').switchOn()
            dz.devices('Lamp tv rechts').switchOn()

            msgtxt = 'Verlichting huis wordt AAN gezet...'

    end

    if (
        (item.isTimer and (item.trigger == OnTimer2))                                           -- START AVOND TUIN sfeerverlichting buiten
        or (item.isDevice and item.name == 'Schakelgroepen'  and (item.state == 'Aan' or item.state == 'Sfeerverlichting tuin')) 
        ) then                                                                          
 
            --if (item.isTimer) then  dz.devices('Schakelgroepen').switchSelector(20)  end           -- weet niet of deze nodig is
            dz.devices('Opstaan').switchOff()
            dz.devices('Ontbijten').switchOff()
            dz.devices('Slapen').switchOff()
            dz.devices('Avond').switchOn()

            dz.devices('Achtertuin huis terraslamp').switchOn()
            dz.devices('Borderverlichting').switchOn()
            dz.devices('Achtertuin schuur bewegingslamp').switchOn()
            dz.devices('Voordeurlamp').switchOn()
            dz.devices('Buiten_led_kerstlantaarn').switchOn()

            msgtxt = 'Verlichting tuin wordt AAN gezet'

    end

    if (
        (item.isTimer and dz.devices('Visite_disable_timers').state == 'Off' and (item.trigger == OffTimer1 or item.trigger == OffTimer2 ))    -- EINDE AVOND via timer of handmatig alles uit
        or (item.isDevice and item.name == 'Schakelgroepen'  and item.state == 'Uit')
        ) then                                                                          

            --if (item.isTimer) then dz.devices('Schakelgroepen').switchSelector(0)   end   -- weet niet of deze nodig is
            dz.devices('Avond').switchOff()

            dz.devices('Achtertuin huis terraslamp').switchOff()
            dz.devices('Borderverlichting').switchOff()
            dz.devices('Achtertuin schuur bewegingslamp').switchOff()
            dz.devices('Voordeurlamp').switchOff()
            dz.devices('Buiten_led_kerstlantaarn').switchOff()

            dz.devices('usb lamp').switchSelector(0)
            dz.devices('lantaarn').switchOff()
            dz.devices('Lamp dressoir links').switchOff()
            dz.devices('Lamp dressoir rechts').switchOff()
            dz.devices('Lamp tv links').switchOff()
            dz.devices('Lamp tv rechts').switchOff()

            msgtxt = 'ALLE verlichting huis wordt UIT gezet'

    end

    if (
        (item.isTimer and (item.trigger == OnTimer3 or item.trigger == OnTimer4))               -- START SLAPEN
        or (item.isDevice and item.name == 'Schakelgroepen'  and (item.state == 'Aan' or item.state == 'Slapen')) 
        ) then                                                                          
 
            --if (item.isTimer) then  dz.devices('Schakelgroepen').switchSelector(30)  end        -- weet niet of deze nodig is
            dz.devices('Opstaan').switchOff()
            dz.devices('Ontbijten').switchOff()
            dz.devices('Avond').switchOff()
            dz.devices('Slapen').switchOn()

            dz.devices('Achtertuin schuur bewegingslamp').switchOn()
            dz.devices('Voordeurlamp').switchOn()
            dz.devices('Buiten_led_kerstlantaarn').switchOn()

            if item.isTimer and item.trigger == OnTimer3 then 
                dz.devices('Bewegingsservice').switchOn().checkFirst().afterMin(70) 
            elseif item.isTimer and item.trigger == OnTimer4 then 
                dz.devices('Bewegingsservice').switchOn().checkFirst().afterMin(30) 
            elseif item.isDevice and item.state == 'Slapen' then 
                dz.devices('Bewegingsservice').switchOn().checkFirst()
            end

            msgtxt = 'SLAPEN, nachtverlichting en bewegingsservice wordt AAN gezet...'

    end

    if (
        (item.isTimer and (item.trigger == OffTimer3 or item.trigger == OffTimer4))    -- EINDE SLAPEN
        or (item.isDevice and item.name == 'Schakelgroepen'  and item.state == 'Uit')
            ) then                                                                          

        dz.devices('Slapen').switchOff()

        msgtxt = 'SLAPEN voorbij ...'

    end

    if (
        (item.isTimer and (item.trigger == OnTimer7))                                       -- ZON OP
        or (item.isDevice and item.name == 'Schakelgroepen'  and item.state == 'Zon op')                                       -- handmatig Zon op
        ) then                                                                          
 
        --if (item.isTimer) then  dz.devices('Schakelgroepen').switchSelector(30)  end      -- niet nodig
            dz.devices('Zon op').switchOn()                                                
            dz.devices('Zon onder').switchOff()                                              

            dz.devices('Achtertuin schuur bewegingslamp').switchOff()
            dz.devices('Voordeurlamp').switchOff()
            dz.devices('Buiten_led_kerstlantaarn').switchOff()
            -- ONTBIJT DEVICES Kunnen al aan staan evt uitzetten !!!
            dz.devices('Achtertuin huis terraslamp').switchOff()
            dz.devices('Lamp dressoir links').switchOff()

            msgtxt = 'ZON OP....'

    end

    if (
        (item.isTimer and (item.trigger == OffTimer7))                                       -- ZON ONDER
        or (item.isDevice and item.name == 'Schakelgroepen'  and item.state == 'Zon onder')                                     -- handmatig Zon onder
        ) then                                                                          
 
        --if (item.isTimer) then  dz.devices('Schakelgroepen').switchSelector(30)  end      -- niet nodig
            dz.devices('Zon op').switchOff()                                                
            dz.devices('Zon onder').switchOn()                                              

            msgtxt = 'ZON ONDER....'

    end

    if (
        (item.isTimer and (item.trigger == OnTimer5 or item.trigger == OnTimer6))           -- START OPSTAAN
        or (item.isDevice and item.name == 'Schakelgroepen'  and (item.state == 'Aan'  or item.state == 'Opstaan')) -- is nog niet gemaakt
        ) then                                                                          
 
        --if (item.isTimer) then  dz.devices('Schakelgroepen').switchSelector(40)  end                 -- weet niet of deze nodig is
        dz.devices('Opstaan').switchOn()

	    if  dz.devices('Jablotronenabled').state == 'Unlocked' then                        -- huis is van alarm, er is iemand thuis

            dz.devices('Nachtlamp').switchOn()
            dz.devices('Bewegingsservice').switchOff().checkFirst()

            msgtxt = 'OPSTAAN wordt uitgevoerd, want er is iemand thuis....'

        elseif  dz.devices('Jablotronenabled').state == 'Locked' then                                -- huis op alarm, iedereen is weg, dus niets aanzetten

            msgtxt = 'OPSTAAN wordt niet uitgevoerd, want er is niemand thuis....'
	    end
    end

    if (
        (item.isTimer and (item.trigger == OffTimer5 or item.trigger == OffTimer6))         -- EINDE OPSTAAN
        or (item.isDevice and item.name == 'Schakelgroepen'  and item.state == 'Uit')
    ) then                                                                          
 
        --if (item.isTimer) then  dz.devices('Schakelgroepen').switchSelector(30)  end               -- niet nodig er is geen uit knop
            dz.devices('Opstaan').switchOff()

            dz.devices('Nachtlamp').switchOff()

            msgtxt = 'OPSTAAN voorbij....'

    end

    if (
        (item.isDevice and item.name == 'Schakelgroepen' and item.state == 'Uit huis')                                  -- UIT HUIS
        or (item.isDevice and item.name == 'Jablotronenabled' and item.state == 'Locked')                               -- op alarm
        ) then                                                                          

            dz.devices('Opstaan').switchOff()                                           -- basisvoorziening UIT want er is niemand thuis
            dz.devices('Ontbijten').switchOff()                                         -- basisvoorziening UIT want er is niemand thuis
            dz.devices('Thuis').switchOff()                                             -- basisvoorziening UIT want er is niemand thuis
            dz.devices('Uit huis').switchOn()                                           -- basisvoorziening UIT want er is niemand thuis
            dz.devices('Bewegingsservice').switchOn()

            msgtxt = 'UIT HUIS, basisvoorzieningen uit, bewegingservice AAN...'

    end

    if (
        (item.isDevice and item.name == 'Schakelgroepen' and item.state == 'Thuis')                                     -- THUIS
        or (item.isDevice and item.name == 'Jablotronenabled' and item.state == 'Unlocked')                                -- van alarm af 
        ) then                                                                          

            dz.devices('Uit huis').switchOff()                                          -- basisvoorziening UIT want er is niemand thuis
            dz.devices('Thuis').switchOn()                                              -- basisvoorziening UIT want er is niemand thuis
            dz.devices('Bewegingsservice').switchOff()

            msgtxt = 'THUIS, basisvoorzieningen aan, bewegingservice UIT...'

    end

    if (
        (item.isTimer and (item.trigger == OnTimer5 or item.trigger == OnTimer6))       -- BASISVOORZIENING AAN  bij timer OPSTAAN
        or (item.isDevice and item.name == 'Schakelgroepen'  and (item.state == 'Basisvoorziening' or item.state == 'Opstaan' or item.state == 'Thuis'))      -- AANZETTEN bij handmatig BASISVOORZIENING, OPSTAAN en THUIS            
        ) then                                                                          

            dz.devices('boiler').switchOn()
            dz.devices('printer').switchOn()                                             
            
            msgtxt = 'BASISVOORZIENING AAN...'

    end

    if (
        (item.isTimer and (item.trigger == OnTimer1 or item.trigger == OnTimer2))       -- BASISVOORZIENING UIT bij einde avond
        or (item.isDevice and item.name == 'Schakelgroepen'  and (item.state == 'Uit' or item.state == 'Slapen' or item.state == 'Uit huis'))                  -- ACTIVEREN bij handmatig Uit, Slapen en Uit huis
        ) then                                                                          

            dz.devices('boiler').switchOff()
            dz.devices('printer').switchOff()                                             
            
    end

    if (
        (item.isTimer and (item.trigger == OnTimer8 or item.trigger == OnTimer9 or item.trigger == OnTimer10))  -- START ONTBIJTEN
        or (item.isDevice and item.name == 'Schakelgroepen'  and (item.state == 'Aan'  or item.state == 'Ontbijten'))
        ) then                                                                          
 
        dz.devices('Ontbijten').switchOn()

	    if  dz.devices('Jablotronenabled').state == 'Unlocked' then                          -- huis is van alarm, er is iemand thuis


        	if dz.devices('Zon onder').state == 'On' then -- als de zon nog niet op is

                dz.devices('Achtertuin huis terraslamp').switchOn()
                dz.devices('Lamp dressoir links').switchOn()

                msgtxt = 'ONTBIJTEN wordt uitgevoerd, want er is iemand thuis en het is nog donker...'
            else
                msgtxt = 'ONTBIJTEN wordt niet uitgevoerd, want de zon is op ....'

            end

        elseif  dz.devices('Jablotronenabled').state == 'Locked' then                                -- huis op alarm, iedereen is weg, dus niets aanzetten

            msgtxt = 'ONTBIJTEN wordt niet uitgevoerd, want er is niemand thuis....'
	    end
    end

    if (
        (item.isTimer and (item.trigger == OffTimer8 or item.trigger == OffTimer9 or item.trigger == OffTimer10))    -- EINDE ONTBIJTEN
        or (item.isDevice and item.name == 'Schakelgroepen'  and item.state == 'Uit')
            ) then                                                                          

        dz.devices('Ontbijten').switchOff()

        dz.devices('Achtertuin huis terraslamp').switchOff()
        dz.devices('Lamp dressoir links').switchOff()

        msgtxt = 'ONTBIJTEN is afgelopen ...'

    end

    if (
        (item.isDevice  and item.name == 'Schakelgroepen' and item.state == 'Uit')                                                                     -- RESET VAN DE LAATSTE SCENES BIJ handmatig UIT, rest is al gereset
            ) then                                                                          

	    if  dz.devices('Jablotronenabled').state == 'Unlocked' then                          -- huis is van alarm, er is iemand thuis
            dz.devices('Uit huis').switchOff()                                                  
            dz.devices('Thuis').switchOn()                                              
	    else
            dz.devices('Uit huis').switchOn()                                          
            dz.devices('Thuis').switchOff()                                             
	    end

        if dz.time.isNightTime then                                                             -- als de zon nog niet op is
            dz.devices('Zon op').switchOff()                                                
            dz.devices('Zon onder').switchOn()                                              
        else
            dz.devices('Zon op').switchOn()                                                
            dz.devices('Zon onder').switchOff()                                              
        end

        msgtxt = 'LAATSTE SCENES gereset ...'

    end

    dz.log('Script: ' .. info.scriptName .. ', device: ' .. item.name .. ' ' .. msgtxt, dz.LOG_INFO)
    timertekst = dz.time.rawDate .. '  ' .. dz.time.rawTime .. ' : ' .. msgtxt .. '<br>' ..  dz.devices('timerlog').text 
    dz.devices('timerlog').updateText(timertekst)

end
}
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: need a loop for reading messages for logging

Post by pvklink »

Is this a better way to do it, see pvklog part of the global lua
dz.helpers.pvklog(dz, item, info, "dit is de booschap", 3) ?

only problem is that the scripts where i want to call this function use:
execute = function(dz,item,info)
or execute = function(dz,device,info)

and these scripts are trigger by webhooks, variables, devices, timers and combinations of this...
Is this gonna work, i think the global helper needs all the info from the calling script, timer or device, or item etc.

Code: Select all

return 
{
    data = 
    {
        myGlobalVar = { initial = 12 }
    },

    helpers = 
    {
        alertLidl = function(dz, action, period)
            local controlVar = dz.variables("controlVar")
            local value = controlVar.value
            if controlVar.value ~= action then
                controlVar.set(action) 
                if period then
                    controlVar.set(value).afterSec(period)
                    dz.log("alert requested; controlVar set to " .. action .. ". It will return to ".. value .. " after " .. period .. " seconds." ,dz.LOG_FORCE)
                else
                    dz.log("alert requested; controlVar set to " .. action  ,dz.LOG_FORCE)
                end
            else
                dz.log("No changed needed; controlVar is already set to " .. value,dz.LOG_DEBUG)
            end
        end,
        pvklog = function(dz, item, info, msgtxt, loglevel)
        	if loglevel == 1 or loglevel > 2 then
		    dz.log('Script: ' .. info.scriptName .. ', device: ' .. item.name .. ' ' .. msgtxt, dz.LOG_INFO)
		
		elseif loglevel == 2 or loglevel > 2 then
		    timertekst = dz.time.rawDate .. '  ' .. dz.time.rawTime .. ' : ' .. msgtxt .. '<br>' ..  dz.devices('timerlog').text 
		    dz.devices('timerlog').updateText(timertekst)

            	end
        end
    }
}
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: need a loop for reading messages for logging

Post by waaren »

pvklink wrote: Friday 28 June 2019 18:33 In the script below there can be executed more then one scenes/groups.. so my msgtxt is overwritten and only the last one is executed in the log
So i need:
- an array for setting msgtxt
- and a construct that reads all defined msgtxt(1-n) (see the end of my script)
Can somebody give a hand? Script is working more than fine! almost finished
Please have a look at this one.

Code: Select all

-- dit script draait bij avond slapen, zon op, ontbijten, van huis, thuis en opstaan, uit huis thuis 

local OnTimer1      = '15 minutes before sunset on mon,tue,wed,thu,fri,sat,sun'         -- HUIS SFEERVERLICHTING AAN
local OnTimer2      = '15 minutes after sunset on mon,tue,wed,thu,fri,sat,sun'          -- TUIN SFEERVERLICHTING AAN

local OffTimer1     = 'at 23:00 on mon,tue,wed,thu,sun'                                 -- EINDE AVOND ALLES UIT DOOR DE WEEK
local OffTimer2     = 'at 23:45 on fri,sat'                                             -- EINDE AVOND ALLES UIT IN HET WEEKEND

local OnTimer3      = 'at 23:05 on mon,tue,wed,thu,sun'                                 -- SLAPEN START door de week
local OnTimer4      = 'at 23:50 on fri,sat'                                             -- SLAPEN START in het weekend
local OffTimer3     = 'at 06:08 on mon,tue,thu,fri'                                     -- EINDE SLAPEN peter
local OffTimer4     = 'at 07:08 on mon,tue,wed,thu,fri'                                 -- EINDE SLAPEN MO

local OnTimer5      = 'at 06:10 on mon,tue,thu,fri'                                     -- OPSTAAN peter
local OnTimer6      = 'at 07:10 on mon,tue,wed,thu,fri'                                 -- OPSTAAN mo
local OffTimer5     = 'at 06:15 on mon,tue,thu,fri'                                     -- EINDE OPSTAAN peter  
local OffTimer6     = 'at 07:25 on mon,tue,wed,thu,fri'                                 -- EINDE OPSTAAN mo

local OnTimer7      = '2 minutes after sunrise on mon,tue,wed,thu,fri,sat,sun'          -- ZON OP 
local OffTimer7     = '2 minutes after sunset on mon,tue,wed,thu,fri,sat,sun'           -- ZON ONDER

local OnTimer8      = 'at 06:13 on mon,tue,thu,fri'                                     -- ONTBIJTEN AAN PETER
local OnTimer9      = 'at 07:20 on mon,tue,wed,thu,fri'                                 -- ONTBIJTEN AAN MO
local OnTimer10     = 'at 09:20 on sat,sun'                                             -- ONTBIJTEN AAN WEEKEND
local OffTimer8     = 'at 06:44 on mon,tue,thu,fri'                                     -- ONTBIJTEN UIT PETER
local OffTimer9     = 'at 08:00 on mon,tue,wed,thu,fri'                                 -- ONTBIJTEN UIT MO
local OffTimer10    = 'at 11:00 on sat,sun'                                             -- ONTBIJTEN UIT WEEKEND

return {
    on = {  devices = {'Schakelgroepen','Jablotronenabled'},     
            timer = {OnTimer1, OnTimer2, OnTimer3, OnTimer4, OnTimer5, OnTimer6, OnTimer7, OnTimer8, OnTimer9, OnTimer10, OffTimer1, OffTimer2, OffTimer3, OffTimer4, OffTimer5, OffTimer6, OffTimer7, OffTimer8, OffTimer9, OffTimer10}
        },
        
    logging =   { level   = domoticz.LOG_DEBUG,                 
                  marker  = "Timers"},
              
    execute = function(dz,item,info)

        local messages = {}
        
        local function addMessage(message)
            local trigger =  item.isDevice and (", Device: " .. item.name ) or (", Timer: " .. item.trigger)
            messages[#messages + 1] = {}
            messages[#messages].log  = 'scriptname: ' .. info.scriptName .. trigger .. ' ' .. message
            messages[#messages].text = dz.time.rawDate .. '  ' .. dz.time.rawTime .. ' : ' .. message 
        end

        local function dumpMessages()
            local textDevice = dz.devices('timerlog')
            local currentText = textDevice.text
            local newText = currentText
            
            for i, record in ipairs(messages) do
                dz.log(record.log, dz.LOG_INFO) 
                newText = record.text .. '<br>' .. newText
            end
            
            if newText ~= currentText then
               textDevice.updateText(newText) 
            end    
        end
    
    if (
        (item.isTimer and (item.trigger == OnTimer1))                                           -- START AVOND HUIS sfeerverlichting binnen
        or (item.isDevice and item.name == 'Schakelgroepen' and (item.state == 'Aan' or item.state == 'Sfeerverlichting binnen')) 
        ) then                                                                          
 
            --if (item.isTimer) then  dz.devices('Schakelgroepen').switchSelector(10)  end              -- weet niet of deze nodig is n keer remarken
            dz.devices('Opstaan').switchOff()
            dz.devices('Ontbijten').switchOff()
            dz.devices('Slapen').switchOff()
            dz.devices('Avond').switchOn()

            dz.devices('usb lamp').switchSelector(40)
            dz.devices('lantaarn').switchOn()
            dz.devices('Lamp dressoir links').switchOn()
            dz.devices('Lamp dressoir rechts').switchOn()
            dz.devices('Lamp tv links').switchOn()
            dz.devices('Lamp tv rechts').switchOn()

            addMessage('Verlichting huis wordt AAN gezet...')

    end

    if (
        (item.isTimer and (item.trigger == OnTimer2))                                           -- START AVOND TUIN sfeerverlichting buiten
        or (item.isDevice and item.name == 'Schakelgroepen'  and (item.state == 'Aan' or item.state == 'Sfeerverlichting tuin')) 
        ) then                                                                          
 
            --if (item.isTimer) then  dz.devices('Schakelgroepen').switchSelector(20)  end           -- weet niet of deze nodig is
            dz.devices('Opstaan').switchOff()
            dz.devices('Ontbijten').switchOff()
            dz.devices('Slapen').switchOff()
            dz.devices('Avond').switchOn()

            dz.devices('Achtertuin huis terraslamp').switchOn()
            dz.devices('Borderverlichting').switchOn()
            dz.devices('Achtertuin schuur bewegingslamp').switchOn()
            dz.devices('Voordeurlamp').switchOn()
            dz.devices('Buiten_led_kerstlantaarn').switchOn()

            addMessage('Verlichting tuin wordt AAN gezet')

    end

    if (
        (item.isTimer and dz.devices('Visite_disable_timers').state == 'Off' and (item.trigger == OffTimer1 or item.trigger == OffTimer2 ))    -- EINDE AVOND via timer of handmatig alles uit
        or (item.isDevice and item.name == 'Schakelgroepen'  and item.state == 'Uit')
        ) then                                                                          

            --if (item.isTimer) then dz.devices('Schakelgroepen').switchSelector(0)   end   -- weet niet of deze nodig is
            dz.devices('Avond').switchOff()

            dz.devices('Achtertuin huis terraslamp').switchOff()
            dz.devices('Borderverlichting').switchOff()
            dz.devices('Achtertuin schuur bewegingslamp').switchOff()
            dz.devices('Voordeurlamp').switchOff()
            dz.devices('Buiten_led_kerstlantaarn').switchOff()

            dz.devices('usb lamp').switchSelector(0)
            dz.devices('lantaarn').switchOff()
            dz.devices('Lamp dressoir links').switchOff()
            dz.devices('Lamp dressoir rechts').switchOff()
            dz.devices('Lamp tv links').switchOff()
            dz.devices('Lamp tv rechts').switchOff()

            addMessage('ALLE verlichting huis wordt UIT gezet')

    end

    if (
        (item.isTimer and (item.trigger == OnTimer3 or item.trigger == OnTimer4))               -- START SLAPEN
        or (item.isDevice and item.name == 'Schakelgroepen'  and (item.state == 'Aan' or item.state == 'Slapen')) 
        ) then                                                                          
 
            --if (item.isTimer) then  dz.devices('Schakelgroepen').switchSelector(30)  end        -- weet niet of deze nodig is
            dz.devices('Opstaan').switchOff()
            dz.devices('Ontbijten').switchOff()
            dz.devices('Avond').switchOff()
            dz.devices('Slapen').switchOn()

            dz.devices('Achtertuin schuur bewegingslamp').switchOn()
            dz.devices('Voordeurlamp').switchOn()
            dz.devices('Buiten_led_kerstlantaarn').switchOn()

            if item.isTimer and item.trigger == OnTimer3 then 
                dz.devices('Bewegingsservice').switchOn().checkFirst().afterMin(70) 
            elseif item.isTimer and item.trigger == OnTimer4 then 
                dz.devices('Bewegingsservice').switchOn().checkFirst().afterMin(30) 
            elseif item.isDevice and item.state == 'Slapen' then 
                dz.devices('Bewegingsservice').switchOn().checkFirst()
            end

            addMessage('SLAPEN, nachtverlichting en bewegingsservice wordt AAN gezet...')

    end

    if (
        (item.isTimer and (item.trigger == OffTimer3 or item.trigger == OffTimer4))    -- EINDE SLAPEN
        or (item.isDevice and item.name == 'Schakelgroepen'  and item.state == 'Uit')
            ) then                                                                          

        dz.devices('Slapen').switchOff()

        addMessage('SLAPEN voorbij ...')

    end

    if (
        (item.isTimer and (item.trigger == OnTimer7))                                       -- ZON OP
        or (item.isDevice and item.name == 'Schakelgroepen'  and item.state == 'Zon op')                                       -- handmatig Zon op
        ) then                                                                          
 
        --if (item.isTimer) then  dz.devices('Schakelgroepen').switchSelector(30)  end      -- niet nodig
            dz.devices('Zon op').switchOn()                                                
            dz.devices('Zon onder').switchOff()                                              

            dz.devices('Achtertuin schuur bewegingslamp').switchOff()
            dz.devices('Voordeurlamp').switchOff()
            dz.devices('Buiten_led_kerstlantaarn').switchOff()
            -- ONTBIJT DEVICES Kunnen al aan staan evt uitzetten !!!
            dz.devices('Achtertuin huis terraslamp').switchOff()
            dz.devices('Lamp dressoir links').switchOff()

            addMessage('ZON OP....')

    end

    if (
        (item.isTimer and (item.trigger == OffTimer7))                                       -- ZON ONDER
        or (item.isDevice and item.name == 'Schakelgroepen'  and item.state == 'Zon onder')                                     -- handmatig Zon onder
        ) then                                                                          
 
        --if (item.isTimer) then  dz.devices('Schakelgroepen').switchSelector(30)  end      -- niet nodig
            dz.devices('Zon op').switchOff()                                                
            dz.devices('Zon onder').switchOn()                                              

            addMessage('ZON ONDER....')

    end

    if (
        (item.isTimer and (item.trigger == OnTimer5 or item.trigger == OnTimer6))           -- START OPSTAAN
        or (item.isDevice and item.name == 'Schakelgroepen'  and (item.state == 'Aan'  or item.state == 'Opstaan')) -- is nog niet gemaakt
        ) then                                                                          
 
        --if (item.isTimer) then  dz.devices('Schakelgroepen').switchSelector(40)  end                 -- weet niet of deze nodig is
        dz.devices('Opstaan').switchOn()

        if  dz.devices('Jablotronenabled').state == 'Unlocked' then                        -- huis is van alarm, er is iemand thuis

            dz.devices('Nachtlamp').switchOn()
            dz.devices('Bewegingsservice').switchOff().checkFirst()

            addMessage('OPSTAAN wordt uitgevoerd, want er is iemand thuis....')

        elseif  dz.devices('Jablotronenabled').state == 'Locked' then                                -- huis op alarm, iedereen is weg, dus niets aanzetten

            addMessage('OPSTAAN wordt niet uitgevoerd, want er is niemand thuis....')
        end
    end

    if (
        (item.isTimer and (item.trigger == OffTimer5 or item.trigger == OffTimer6))         -- EINDE OPSTAAN
        or (item.isDevice and item.name == 'Schakelgroepen'  and item.state == 'Uit')
    ) then                                                                          
 
        --if (item.isTimer) then  dz.devices('Schakelgroepen').switchSelector(30)  end               -- niet nodig er is geen uit knop
            dz.devices('Opstaan').switchOff()

            dz.devices('Nachtlamp').switchOff()

            addMessage('OPSTAAN voorbij....')

    end

    if (
        (item.isDevice and item.name == 'Schakelgroepen' and item.state == 'Uit huis')                                  -- UIT HUIS
        or (item.isDevice and item.name == 'Jablotronenabled' and item.state == 'Locked')                               -- op alarm
        ) then                                                                          

            dz.devices('Opstaan').switchOff()                                           -- basisvoorziening UIT want er is niemand thuis
            dz.devices('Ontbijten').switchOff()                                         -- basisvoorziening UIT want er is niemand thuis
            dz.devices('Thuis').switchOff()                                             -- basisvoorziening UIT want er is niemand thuis
            dz.devices('Uit huis').switchOn()                                           -- basisvoorziening UIT want er is niemand thuis
            dz.devices('Bewegingsservice').switchOn()

            addMessage('UIT HUIS, basisvoorzieningen uit, bewegingservice AAN...')

    end

    if (
        (item.isDevice and item.name == 'Schakelgroepen' and item.state == 'Thuis')                                     -- THUIS
        or (item.isDevice and item.name == 'Jablotronenabled' and item.state == 'Unlocked')                                -- van alarm af 
        ) then                                                                          

            dz.devices('Uit huis').switchOff()                                          -- basisvoorziening UIT want er is niemand thuis
            dz.devices('Thuis').switchOn()                                              -- basisvoorziening UIT want er is niemand thuis
            dz.devices('Bewegingsservice').switchOff()

            addMessage('THUIS, basisvoorzieningen aan, bewegingservice UIT...')

    end

    if (
        (item.isTimer and (item.trigger == OnTimer5 or item.trigger == OnTimer6))       -- BASISVOORZIENING AAN  bij timer OPSTAAN
        or (item.isDevice and item.name == 'Schakelgroepen'  and (item.state == 'Basisvoorziening' or item.state == 'Opstaan' or item.state == 'Thuis'))      -- AANZETTEN bij handmatig BASISVOORZIENING, OPSTAAN en THUIS            
        ) then                                                                          

            dz.devices('boiler').switchOn()
            dz.devices('printer').switchOn()                                             
            
            addMessage('BASISVOORZIENING AAN...')

    end

    if (
        (item.isTimer and (item.trigger == OnTimer1 or item.trigger == OnTimer2))       -- BASISVOORZIENING UIT bij einde avond
        or (item.isDevice and item.name == 'Schakelgroepen'  and (item.state == 'Uit' or item.state == 'Slapen' or item.state == 'Uit huis'))                  -- ACTIVEREN bij handmatig Uit, Slapen en Uit huis
        ) then                                                                          

            dz.devices('boiler').switchOff()
            dz.devices('printer').switchOff()                                             
            
    end

    if (
        (item.isTimer and (item.trigger == OnTimer8 or item.trigger == OnTimer9 or item.trigger == OnTimer10))  -- START ONTBIJTEN
        or (item.isDevice and item.name == 'Schakelgroepen'  and (item.state == 'Aan'  or item.state == 'Ontbijten'))
        ) then                                                                          
 
        dz.devices('Ontbijten').switchOn()

        if  dz.devices('Jablotronenabled').state == 'Unlocked' then                          -- huis is van alarm, er is iemand thuis


            if dz.devices('Zon onder').state == 'On' then -- als de zon nog niet op is

                dz.devices('Achtertuin huis terraslamp').switchOn()
                dz.devices('Lamp dressoir links').switchOn()

                addMessage('ONTBIJTEN wordt uitgevoerd, want er is iemand thuis en het is nog donker...')
            else
                addMessage('ONTBIJTEN wordt niet uitgevoerd, want de zon is op ....')

            end

        elseif  dz.devices('Jablotronenabled').state == 'Locked' then                                -- huis op alarm, iedereen is weg, dus niets aanzetten

            addMessage('ONTBIJTEN wordt niet uitgevoerd, want er is niemand thuis....')
        end
    end

    if (
        (item.isTimer and (item.trigger == OffTimer8 or item.trigger == OffTimer9 or item.trigger == OffTimer10))    -- EINDE ONTBIJTEN
        or (item.isDevice and item.name == 'Schakelgroepen'  and item.state == 'Uit')
            ) then                                                                          

        dz.devices('Ontbijten').switchOff()

        dz.devices('Achtertuin huis terraslamp').switchOff()
        dz.devices('Lamp dressoir links').switchOff()

        addMessage('ONTBIJTEN is afgelopen ...')

    end

    if (
        (item.isDevice  and item.name == 'Schakelgroepen' and item.state == 'Uit')                                                                     -- RESET VAN DE LAATSTE SCENES BIJ handmatig UIT, rest is al gereset
            ) then                                                                          

        if  dz.devices('Jablotronenabled').state == 'Unlocked' then                          -- huis is van alarm, er is iemand thuis
            dz.devices('Uit huis').switchOff()                                                  
            dz.devices('Thuis').switchOn()                                              
        else
            dz.devices('Uit huis').switchOn()                                          
            dz.devices('Thuis').switchOff()                                             
        end

        if dz.time.isNightTime then                                                             -- als de zon nog niet op is
            dz.devices('Zon op').switchOff()                                                
            dz.devices('Zon onder').switchOn()                                              
        else
            dz.devices('Zon op').switchOn()                                                
            dz.devices('Zon onder').switchOff()                                              
        end

        addMessage('LAATSTE SCENES gereset ...')

    end

    dumpMessages()
end
}
Please note that you need some kind of housekeeping to prevent your textdevice from overflowing.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: need a loop for reading messages for logging

Post by waaren »

pvklink wrote: Friday 28 June 2019 20:50 Is this a better way to do it, see pvklog part of the global lua
Only if you will be using the same method of logging from multiple scripts. Then it will be best to construct your complete messages in the original script and only send constructed message (and maybe target textdevice) to helper function.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: need a loop for reading messages for logging

Post by pvklink »

ok!
learning more and more!
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: need a loop for reading messages for logging

Post by pvklink »

I gonna try all your tips..
i changed my global, and only use dz, msgtxt and loglevel
how do i seperate the helpers? by {} or bu , or ; ?
it does not work
dz.helpers.pvklog(dz, "dit is de booschap", 3) ....

Code: Select all

return 
{
    data = 
    {
        myGlobalVar = { initial = 12 }
    },

    helpers = 
    {
        alertLidl = function(dz, action, period)
            local controlVar = dz.variables("controlVar")
            local value = controlVar.value
            if controlVar.value ~= action then
                controlVar.set(action) 
                if period then
                    controlVar.set(value).afterSec(period)
                    dz.log("alert requested; controlVar set to " .. action .. ". It will return to ".. value .. " after " .. period .. " seconds." ,dz.LOG_FORCE)
                else
                    dz.log("alert requested; controlVar set to " .. action  ,dz.LOG_FORCE)
                end
            else
                dz.log("No changed needed; controlVar is already set to " .. value,dz.LOG_DEBUG)
            end
        end
     }
	{
        pvklog = function(dz,msgtxt, loglevel)
        	if loglevel == 1 or loglevel > 2 then
		    dz.log(msgtxt, dz.LOG_INFO)
		
		elseif loglevel == 2 or loglevel > 2 then
		    timertekst = dz.time.rawDate .. '  ' .. dz.time.rawTime .. ' : ' .. msgtxt .. '<br>' ..  dz.devices('timerlog').text 
		    dz.devices('timerlog').updateText(timertekst)

            	end
        end
    }
}
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: need a loop for reading messages for logging

Post by waaren »

pvklink wrote: Friday 28 June 2019 21:56 i changed my global, and only use dz, msgtxt and loglevel
how do i separate the helpers? by {} or by , or ; ?
dz.helpers is a Lua table and a Lua table can contain values, variables, functions and ... tables
The contents of a Lua table can be separated by a , or a ;
so in an example global_data.lua

Code: Select all

return 
{
    data = { myGlobalVar = { initial = 100 }},

    helpers = 
    {
       MY_CONSTANT_STRING = "text"; -- call with dz.helpers.MY_CONSTANT_STRING  
       MY_CONSTANT_NUMBER = 12; -- call with dz.helpers.MY_CONSTANT_NUMBER
       MY_CONSTANT_BOOLEAN = true, -- call with dz.helpers.MY_CONSTANT_BOOLEAN
       myVar = dz.globalData.myGlobalVar, -- call with dz.helpers.myVar

       myFunction1 = function(msg)  -- call with dz.helpers.myFunction1("test message") ==>> print test message
           print( tostring(msg) )
      end,
     
      myFunction2 = function(dz, myDeviceName)  -- call with dz.helpers.myFunction2(dz, item) ==>> returns true or false
           return dz.devices(myDeviceName).state == "On" 
      end,
     
      t = {1,2,3,99,"a","b" , key = "master" }, -- call with dz.helpers.t[4] => returns 99,  dz.helpers.t[5] => returns a , dz.helpers.t.key => returns master
    }
}
it does not work
You have to be a bit more descriptive than that :)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: need a loop for reading messages for logging

Post by pvklink »

Your other solution works perfect! i also made a function to delete the log in the textfile..
Now i am trying to get the global_data solution to work...
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: need a loop for reading messages for logging

Post by pvklink »

Ok, i will be more specific !
I get an error
2019-06-29 07:37:13.211 Error: dzVents: Error: (2.4.23) An error occurred when calling event handler test
2019-06-29 07:37:13.211 Error: dzVents: Error: (2.4.23) ...e/pi/domoticz/scripts/dzVents/generated_scripts/test.lua:16: attempt to call field 'addMessages' (a nil value)
2

i like to use this concept in every script, so

1. put the message functions in global_data

first, trying myself, something like this:

Code: Select all

return 
{
    data = 
    {
        myGlobalVar = { initial = 12 }
    },

    helpers = 
    {
        alertLidl = function(dz, action, period)
            local controlVar = dz.variables("controlVar")
            local value = controlVar.value
            if controlVar.value ~= action then
                controlVar.set(action) 
                if period then
                    controlVar.set(value).afterSec(period)
                    dz.log("alert requested; controlVar set to " .. action .. ". It will return to ".. value .. " after " .. period .. " seconds." ,dz.LOG_FORCE)
                else
                    dz.log("alert requested; controlVar set to " .. action  ,dz.LOG_FORCE)
                end
            else
                dz.log("No changed needed; controlVar is already set to " .. value,dz.LOG_DEBUG)
            end
        end,
    addMessage = function(dz,item,info,message)
            local trigger =  item.isDevice and (", Device: " .. item.name ) or (", Timer: " .. item.trigger)
            messages[#messages + 1] = {}
            messages[#messages].log  = 'scriptname: ' .. info.scriptName .. trigger .. ' ' .. message
            messages[#messages].text = dz.time.rawDate .. '  ' .. dz.time.rawTime .. ' : ' .. message 
		return messages
    	end,
    dumpMessages = function(dz,info,messages)
            local textDevice = dz.devices('timerlog')
            local currentText = textDevice.text
            local newText = currentText
            
            for i, record in ipairs(messages) do
                dz.log(record.log, dz.LOG_INFO) 
                newText = record.text .. '<br>' .. newText
            end
            
            if newText ~= currentText then
               textDevice.updateText(newText) 
            end    
    	end
    }
}
2. and in the callng scripts something like this:

Code: Select all

return {
	on = {
		devices = {
			'test'
		}
	},
	execute = function(dz, item, info)
    local messages = {}


        if item.state == 'On' then
            message = 'dit is een test1'
            messages = dz.helpers.addMessages(dz,item, info,'test1')
        else
            message = 'dit is een test2'
            messages = dz.helpers.addMessages(dz,item,info,'test2')
        end
        dz.helpers.dumpMessages(dz,info,messages)
end
}
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: need a loop for reading messages for logging

Post by waaren »

pvklink wrote: Saturday 29 June 2019 7:02 2019-06-29 07:37:13.211 Error: dzVents: Error: (2.4.23) ...e/pi/domoticz/scripts/dzVents/generated_scripts/test.lua:16: attempt to call field 'addMessages' (a nil value)
Please have a look at this.

global_data.lua

Code: Select all

return 
{
    data = 
    {
        myGlobalVar = { initial = 12 }
    },

    helpers = 
    {
        alertLidl = function(dz, action, period)
            local controlVar = dz.variables("controlVar")
            local value = controlVar.value
            if controlVar.value ~= action then
                controlVar.set(action) 
                if period then
                    controlVar.set(value).afterSec(period)
                    dz.log("alert requested; controlVar set to " .. action .. ". It will return to ".. value .. " after " .. period .. " seconds." ,dz.LOG_FORCE)
                else
                    dz.log("alert requested; controlVar set to " .. action  ,dz.LOG_FORCE)
                end
            else
                dz.log("No changed needed; controlVar is already set to " .. value,dz.LOG_DEBUG)
            end
    end,
    
    addMessage = 
        function(dz, item, info, message, messages)
            local trigger =  item.isDevice and (", Device: " .. item.name ) or (", Timer: " .. item.trigger)
            messages[#messages + 1] = {}
            messages[#messages].log  = 'scriptname: ' .. info.scriptName .. trigger .. ' ' .. message
            messages[#messages].text = dz.time.rawDate .. '  ' .. dz.time.rawTime .. ' : ' .. message 
            return messages
        end,

    dumpMessages = 
        function(dz, messages)
            local textDevice = dz.devices('timerlog')
            local currentText = textDevice.text
            local newText = currentText
            local lf = '<br>'
            local maxLines = 8

            for i, record in ipairs(messages) do
                dz.log(record.log, dz.LOG_INFO) 
                newText = record.text .. lf .. newText
            end

            if newText ~= currentText then
                lines = dz.utils.stringSplit(newText, lf)  -- create lines table from newText string
                newText = table.concat(lines, lf, 1, maxLines) -- recreate newText string from first maxLines 
                textDevice.updateText(newText) 
            end
        end,
    }
}
testScript

Code: Select all

return {
    on = { devices = { 'test' }},
    
    execute = function(dz, item, info)

        local messageTable = {}
        local add = 'add'
        
        local function globalMessage(action, message )
            if action == add then
                messageTable = dz.helpers.addMessage(dz, item, info, message, messageTable )
            else
                dz.helpers.dumpMessages(dz, messageTable)
            end
        end
        
        message = item.name .. ' switched ' .. item.state
        globalMessage(add, message .. ' test-1') 
        globalMessage(add, message .. ' test-2')
        globalMessage(add, message .. ' test-3')
        
        globalMessage() -- dump
end
}
code]
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: need a loop for reading messages for logging

Post by pvklink »

YESSSS , that works great!
Only my rules are not on one line and some leters are missing?
Naamloos.png
Naamloos.png (35.41 KiB) Viewed 2442 times

Code: Select all

-- dit script draait bij avond slapen, zon op, ontbijten, van huis, thuis en opstaan, uit huis thuis 

local OnTimer1      = '15 minutes before sunset on mon,tue,wed,thu,fri,sat,sun'         -- HUIS SFEERVERLICHTING AAN
local OnTimer2      = '15 minutes after sunset on mon,tue,wed,thu,fri,sat,sun'          -- TUIN SFEERVERLICHTING AAN

local OffTimer1     = 'at 23:00 on mon,tue,wed,thu,sun'                                 -- EINDE AVOND ALLES UIT DOOR DE WEEK
local OffTimer2     = 'at 23:45 on fri,sat'                                             -- EINDE AVOND ALLES UIT IN HET WEEKEND

local OnTimer3      = 'at 23:05 on mon,tue,wed,thu,sun'                                 -- SLAPEN START door de week
local OnTimer4      = 'at 23:50 on fri,sat'                                             -- SLAPEN START in het weekend
local OffTimer3     = 'at 06:08 on mon,tue,thu,fri'                                     -- EINDE SLAPEN peter
local OffTimer4     = 'at 07:08 on mon,tue,wed,thu,fri'                                 -- EINDE SLAPEN MO

local OnTimer5      = 'at 06:10 on mon,tue,thu,fri'                                     -- OPSTAAN peter
--local OnTimer5      = 'at 23:05 on mon,tue,wed,thu,fri,sat,sun'         -- test
local OnTimer6      = 'at 07:10 on mon,tue,wed,thu,fri'                                 -- OPSTAAN mo
local OffTimer5     = 'at 06:15 on mon,tue,thu,fri'                                     -- EINDE OPSTAAN peter  
local OffTimer6     = 'at 07:25 on mon,tue,wed,thu,fri'                                 -- EINDE OPSTAAN mo

local OnTimer7      = '2 minutes after sunrise on mon,tue,wed,thu,fri,sat,sun'          -- ZON OP 
local OffTimer7     = '2 minutes after sunset on mon,tue,wed,thu,fri,sat,sun'           -- ZON ONDER

local OnTimer8      = 'at 06:13 on mon,tue,thu,fri'                                     -- ONTBIJTEN AAN PETER
local OnTimer9      = 'at 07:20 on mon,tue,wed,thu,fri'                                 -- ONTBIJTEN AAN MO
local OnTimer10     = 'at 09:20 on sat,sun'                                             -- ONTBIJTEN AAN WEEKEND
local OffTimer8     = 'at 06:44 on mon,tue,thu,fri'                                     -- ONTBIJTEN UIT PETER
local OffTimer9     = 'at 08:00 on mon,tue,wed,thu,fri'                                 -- ONTBIJTEN UIT MO
local OffTimer10    = 'at 11:00 on sat,sun'                                             -- ONTBIJTEN UIT WEEKEND

return {
    on = {  devices = {'Schakelgroepen','Jablotronenabled'},     
            timer = {OnTimer1, OnTimer2, OnTimer3, OnTimer4, OnTimer5, OnTimer6, OnTimer7, OnTimer8, OnTimer9, OnTimer10, OffTimer1, OffTimer2, OffTimer3, OffTimer4, OffTimer5, OffTimer6, OffTimer7, OffTimer8, OffTimer9, OffTimer10}
        },
        
    logging =   { level   = domoticz.LOG_DEBUG,                 
                  marker  = "Timers"},
              
    execute = function(dz,item,info)

        local messageTable = {}
        local add = 'add'
        
        local function globalMessage(action, message )
            if action == add then
                messageTable = dz.helpers.addMessage(dz, item, info, message, messageTable )
            else
                dz.helpers.dumpMessages(dz, messageTable)
            end
        end
        
    if (
        (item.isTimer and (item.trigger == OnTimer1))                                           -- START AVOND HUIS sfeerverlichting binnen
        or (item.isDevice and item.name == 'Schakelgroepen' and (item.state == 'Aan' or item.state == 'Sfeerverlichting binnen')) 
        ) then                                                                          
 
            --if (item.isTimer) then  dz.devices('Schakelgroepen').switchSelector(10)  end              -- weet niet of deze nodig is n keer remarken
            dz.devices('Opstaan').switchOff()
            dz.devices('Ontbijten').switchOff()
            dz.devices('Slapen').switchOff()
            dz.devices('Avond').switchOn()

            dz.devices('usb lamp').switchSelector(40)
            dz.devices('lantaarn').switchOn()
            dz.devices('Lamp dressoir links').switchOn()
            dz.devices('Lamp dressoir rechts').switchOn()
            dz.devices('Lamp tv links').switchOn()
            dz.devices('Lamp tv rechts').switchOn()

            message = item.name .. ' switched ' .. item.state
            globalMessage(add, message .. 'Verlichting huis wordt AAN gezet...') 

    end

    if (
        (item.isTimer and (item.trigger == OnTimer2))                                           -- START AVOND TUIN sfeerverlichting buiten
        or (item.isDevice and item.name == 'Schakelgroepen'  and (item.state == 'Aan' or item.state == 'Sfeerverlichting tuin')) 
        ) then                                                                          
 
            --if (item.isTimer) then  dz.devices('Schakelgroepen').switchSelector(20)  end           -- weet niet of deze nodig is
            dz.devices('Opstaan').switchOff()
            dz.devices('Ontbijten').switchOff()
            dz.devices('Slapen').switchOff()
            dz.devices('Avond').switchOn()

            dz.devices('Achtertuin huis terraslamp').switchOn()
            dz.devices('Borderverlichting').switchOn()
            dz.devices('Achtertuin schuur bewegingslamp').switchOn()
            dz.devices('Voordeurlamp').switchOn()
            dz.devices('Buiten_led_kerstlantaarn').switchOn()

            message = item.name .. ' switched ' .. item.state
            globalMessage(add, message .. 'Verlichting tuin wordt AAN gezet') 

    end

    if (
        (item.isTimer and dz.devices('Visite_disable_timers').state == 'Off' and (item.trigger == OffTimer1 or item.trigger == OffTimer2 ))    -- EINDE AVOND via timer of handmatig alles uit
        or (item.isDevice and item.name == 'Schakelgroepen'  and item.state == 'Uit')
        ) then                                                                          

            --if (item.isTimer) then dz.devices('Schakelgroepen').switchSelector(0)   end   -- weet niet of deze nodig is
            dz.devices('Avond').switchOff()
            message = item.name .. ' switched ' .. item.state
            globalMessage(add, message .. 'ALLE verlichting huis wordt UIT gezet') 


            dz.devices('Achtertuin huis terraslamp').switchOff()
            dz.devices('Borderverlichting').switchOff()
            dz.devices('Achtertuin schuur bewegingslamp').switchOff()
            dz.devices('Voordeurlamp').switchOff()
            dz.devices('Buiten_led_kerstlantaarn').switchOff()

            dz.devices('usb lamp').switchSelector(0)
            dz.devices('lantaarn').switchOff()
            dz.devices('Lamp dressoir links').switchOff()
            dz.devices('Lamp dressoir rechts').switchOff()
            dz.devices('Lamp tv links').switchOff()
            dz.devices('Lamp tv rechts').switchOff()

    end

    if (
        (item.isTimer and (item.trigger == OnTimer3 or item.trigger == OnTimer4))               -- START SLAPEN
        or (item.isDevice and item.name == 'Schakelgroepen'  and (item.state == 'Aan' or item.state == 'Slapen')) 
        ) then                                                                          
 
            --if (item.isTimer) then  dz.devices('Schakelgroepen').switchSelector(30)  end        -- weet niet of deze nodig is
            dz.devices('Opstaan').switchOff()
            dz.devices('Ontbijten').switchOff()
            dz.devices('Avond').switchOff()
            dz.devices('Slapen').switchOn()

            dz.devices('Achtertuin schuur bewegingslamp').switchOn()
            dz.devices('Voordeurlamp').switchOn()
            dz.devices('Buiten_led_kerstlantaarn').switchOn()

            if item.isTimer and item.trigger == OnTimer3 then 
                dz.devices('Bewegingsservice').switchOn().checkFirst().afterMin(70) 
            elseif item.isTimer and item.trigger == OnTimer4 then 
                dz.devices('Bewegingsservice').switchOn().checkFirst().afterMin(30) 
            elseif item.isDevice and item.state == 'Slapen' then 
                dz.devices('Bewegingsservice').switchOn().checkFirst()
            end

            message = item.name .. ' switched ' .. item.state
            globalMessage(add, message .. 'SLAPEN, nachtverlichting en bewegingsservice wordt AAN gezet...') 

    end

    if (
        (item.isTimer and (item.trigger == OffTimer3 or item.trigger == OffTimer4))    -- EINDE SLAPEN
        or (item.isDevice and item.name == 'Schakelgroepen'  and item.state == 'Uit')
            ) then                                                                          

        dz.devices('Slapen').switchOff()

            message = item.name .. ' switched ' .. item.state
            globalMessage(add, message .. 'SLAPEN voorbij ...') 

    end

    if (
        (item.isTimer and (item.trigger == OnTimer7))                                       -- ZON OP
        or (item.isDevice and item.name == 'Schakelgroepen'  and item.state == 'Zon op')                                       -- handmatig Zon op
        ) then                                                                          
 
        --if (item.isTimer) then  dz.devices('Schakelgroepen').switchSelector(30)  end      -- niet nodig
            dz.devices('Zon op').switchOn()                                                
            dz.devices('Zon onder').switchOff()                                              

            dz.devices('Achtertuin schuur bewegingslamp').switchOff()
            dz.devices('Voordeurlamp').switchOff()
            dz.devices('Buiten_led_kerstlantaarn').switchOff()
            -- ONTBIJT DEVICES Kunnen al aan staan evt uitzetten !!!
            dz.devices('Achtertuin huis terraslamp').switchOff()
            dz.devices('Lamp dressoir links').switchOff()

            message = item.name .. ' switched ' .. item.state
            globalMessage(add, message .. 'ZON OP....') 

    end

    if (
        (item.isTimer and (item.trigger == OffTimer7))                                       -- ZON ONDER
        or (item.isDevice and item.name == 'Schakelgroepen'  and item.state == 'Zon onder')                                     -- handmatig Zon onder
        ) then                                                                          
 
        --if (item.isTimer) then  dz.devices('Schakelgroepen').switchSelector(30)  end      -- niet nodig
            dz.devices('Zon op').switchOff()                                                
            dz.devices('Zon onder').switchOn()                                              

            message = item.name .. ' switched ' .. item.state
            globalMessage(add, message .. 'ZON ONDER....') 

    end

    if (
        (item.isTimer and (item.trigger == OnTimer5 or item.trigger == OnTimer6))           -- START OPSTAAN
        or (item.isDevice and item.name == 'Schakelgroepen'  and (item.state == 'Aan'  or item.state == 'Opstaan')) -- is nog niet gemaakt
        ) then                                                                          
 
        --if (item.isTimer) then  dz.devices('Schakelgroepen').switchSelector(40)  end                 -- weet niet of deze nodig is
        dz.devices('Opstaan').switchOn()

	    if  dz.devices('Jablotronenabled').state == 'Unlocked' then                        -- huis is van alarm, er is iemand thuis

            dz.devices('Nachtlamp').switchOn()
            dz.devices('Bewegingsservice').switchOff().checkFirst()

            message = item.name .. ' switched ' .. item.state
            globalMessage(add, message .. 'OPSTAAN wordt uitgevoerd, want er is iemand thuis....') 

        elseif  dz.devices('Jablotronenabled').state == 'Locked' then                                -- huis op alarm, iedereen is weg, dus niets aanzetten

            message = item.name .. ' switched ' .. item.state
            globalMessage(add, message .. 'OPSTAAN wordt niet uitgevoerd, want er is niemand thuis....') 

	    end
    end

    if (
        (item.isTimer and (item.trigger == OffTimer5 or item.trigger == OffTimer6))         -- EINDE OPSTAAN
        or (item.isDevice and item.name == 'Schakelgroepen'  and item.state == 'Uit')
    ) then                                                                          
 
        --if (item.isTimer) then  dz.devices('Schakelgroepen').switchSelector(30)  end               -- niet nodig er is geen uit knop
            dz.devices('Opstaan').switchOff()

            dz.devices('Nachtlamp').switchOff()

            message = item.name .. ' switched ' .. item.state
            globalMessage(add, message .. 'OPSTAAN voorbij....') 

    end

    if (
        (item.isDevice and item.name == 'Schakelgroepen' and item.state == 'Uit huis')                                  -- UIT HUIS
        or (item.isDevice and item.name == 'Jablotronenabled' and item.state == 'Locked')                               -- op alarm
        ) then                                                                          

            dz.devices('Opstaan').switchOff()                                           -- basisvoorziening UIT want er is niemand thuis
            dz.devices('Ontbijten').switchOff()                                         -- basisvoorziening UIT want er is niemand thuis
            dz.devices('Thuis').switchOff()                                             -- basisvoorziening UIT want er is niemand thuis
            dz.devices('Uit huis').switchOn()                                           -- basisvoorziening UIT want er is niemand thuis
            dz.devices('Bewegingsservice').switchOn()

            message = item.name .. ' switched ' .. item.state
            globalMessage(add, message .. 'UIT HUIS, basisvoorzieningen uit, bewegingservice AAN...') 

    end

    if (
        (item.isDevice and item.name == 'Schakelgroepen' and item.state == 'Thuis')                                     -- THUIS
        or (item.isDevice and item.name == 'Jablotronenabled' and item.state == 'Unlocked')                                -- van alarm af 
        ) then                                                                          

            dz.devices('Uit huis').switchOff()                                          -- basisvoorziening UIT want er is niemand thuis
            dz.devices('Thuis').switchOn()                                              -- basisvoorziening UIT want er is niemand thuis
            dz.devices('Bewegingsservice').switchOff()

            message = item.name .. ' switched ' .. item.state
            globalMessage(add, message .. 'THUIS, basisvoorzieningen aan, bewegingservice UIT...') 

    end

    if (
        (item.isTimer and (item.trigger == OnTimer5 or item.trigger == OnTimer6))       -- BASISVOORZIENING AAN  bij timer OPSTAAN
        or (item.isDevice and item.name == 'Schakelgroepen'  and (item.state == 'Basisvoorziening' or item.state == 'Opstaan' or item.state == 'Thuis'))      -- AANZETTEN bij handmatig BASISVOORZIENING, OPSTAAN en THUIS            
        ) then                                                                          

            dz.devices('boiler').switchOn()
            dz.devices('printer').switchOn()                                             
            
            message = item.name .. ' switched ' .. item.state
            globalMessage(add, message .. 'BASISVOORZIENING AAN...') 

    end

    if (
        (item.isTimer and (item.trigger == OnTimer1 or item.trigger == OnTimer2))       -- BASISVOORZIENING UIT bij einde avond
        or (item.isDevice and item.name == 'Schakelgroepen'  and (item.state == 'Uit' or item.state == 'Slapen' or item.state == 'Uit huis'))                  -- ACTIVEREN bij handmatig Uit, Slapen en Uit huis
        ) then                                                                          

            dz.devices('boiler').switchOff()
            dz.devices('printer').switchOff()                                             

            message = item.name .. ' switched ' .. item.state
            globalMessage(add, message .. 'BASISVOORZIENING UIT...') 
            
    end

    if (
        (item.isTimer and (item.trigger == OnTimer8 or item.trigger == OnTimer9 or item.trigger == OnTimer10))  -- START ONTBIJTEN
        or (item.isDevice and item.name == 'Schakelgroepen'  and (item.state == 'Aan'  or item.state == 'Ontbijten'))
        ) then                                                                          
 
        dz.devices('Ontbijten').switchOn()

	    if  dz.devices('Jablotronenabled').state == 'Unlocked' then                          -- huis is van alarm, er is iemand thuis


        	if dz.devices('Zon onder').state == 'On' then -- als de zon nog niet op is

                dz.devices('Achtertuin huis terraslamp').switchOn()
                dz.devices('Lamp dressoir links').switchOn()

                message = item.name .. ' switched ' .. item.state
                globalMessage(add, message .. 'ONTBIJTEN wordt uitgevoerd, want er is iemand thuis en het is nog donker...') 

            else
                message = item.name .. ' switched ' .. item.state
                globalMessage(add, message .. 'ONTBIJTEN wordt niet uitgevoerd, want de zon is op ....') 

            end

        elseif  dz.devices('Jablotronenabled').state == 'Locked' then                                -- huis op alarm, iedereen is weg, dus niets aanzetten

            message = item.name .. ' switched ' .. item.state
            globalMessage(add, message .. 'ONTBIJTEN wordt niet uitgevoerd, want er is niemand thuis....') 

        end
    end

    if (
        (item.isTimer and (item.trigger == OffTimer8 or item.trigger == OffTimer9 or item.trigger == OffTimer10))    -- EINDE ONTBIJTEN
        or (item.isDevice and item.name == 'Schakelgroepen'  and item.state == 'Uit')
            ) then                                                                          

        dz.devices('Ontbijten').switchOff()

        dz.devices('Achtertuin huis terraslamp').switchOff()
        dz.devices('Lamp dressoir links').switchOff()

        message = item.name .. ' switched ' .. item.state
        globalMessage(add, message .. 'ONTBIJTEN is afgelopen ...') 

    end

    if (
        (item.isDevice  and item.name == 'Schakelgroepen' and item.state == 'Uit')                                                                     -- RESET VAN DE LAATSTE SCENES BIJ handmatig UIT, rest is al gereset
            ) then                                                                          

	    if  dz.devices('Jablotronenabled').state == 'Unlocked' then                          -- huis is van alarm, er is iemand thuis
            dz.devices('Uit huis').switchOff()                                                  
            dz.devices('Thuis').switchOn()                                              
	    else
            dz.devices('Uit huis').switchOn()                                          
            dz.devices('Thuis').switchOff()                                             
	    end

        if dz.time.isNightTime then                                                             -- als de zon nog niet op is
            dz.devices('Zon op').switchOff()                                                
            dz.devices('Zon onder').switchOn()                                              
        else
            dz.devices('Zon op').switchOn()                                                
            dz.devices('Zon onder').switchOff()                                              
        end

        message = item.name .. ' switched ' .. item.state
        globalMessage(add, message .. 'LAATSTE SCENES gereset ...') 

    end

     globalMessage() -- dump

end
}
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: need a loop for reading messages for logging

Post by waaren »

pvklink wrote: Saturday 29 June 2019 12:33 YESSSS , that works great!
Only my rules are not on one line and some leters are missing?
OK please try and change

Code: Select all

local lf = '<br>'
to

Code: Select all

local lf = '\n'
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: need a loop for reading messages for logging

Post by pvklink »

that gives:
019-06-29 14:41:37.991 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2019-06-29 14:41:37.990 Error: dzVents: Error: (2.4.23) Timers: An error occurred when calling event handler DZ_schakelgroepen
2019-06-29 14:41:37.990 Error: dzVents: Error: (2.4.23) Timers: /home/pi/domoticz/scripts/dzVents/scripts/global_data.lua:51: invalid value (nil) at index 3 in table for 'concat'
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: need a loop for reading messages for logging

Post by pvklink »

When i remark those 2 lines in global_data.lua, the result is looking better..

if newText ~= currentText then
--lines = dz.utils.stringSplit(newText, lf) -- create lines table from newText string
--newText = table.concat(lines, lf, 1, maxLines) -- recreate newText string from first maxLines
textDevice.updateText(newText)
end
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: need a loop for reading messages for logging

Post by waaren »

pvklink wrote: Saturday 29 June 2019 14:44 that gives:
019-06-29 14:41:37.991 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2019-06-29 14:41:37.990 Error: dzVents: Error: (2.4.23) Timers: An error occurred when calling event handler DZ_schakelgroepen
2019-06-29 14:41:37.990 Error: dzVents: Error: (2.4.23) Timers: /home/pi/domoticz/scripts/dzVents/scripts/global_data.lua:51: invalid value (nil) at index 3 in table for 'concat'
can you try this one ?

Code: Select all

return 
{
    data = 
    {
        myGlobalVar = { initial = 12 }
    },

    helpers = 
    {
        alertLidl = function(dz, action, period)
            local controlVar = dz.variables("controlVar")
            local value = controlVar.value
            if controlVar.value ~= action then
                controlVar.set(action) 
                if period then
                    controlVar.set(value).afterSec(period)
                    dz.log("alert requested; controlVar set to " .. action .. ". It will return to ".. value .. " after " .. period .. " seconds." ,dz.LOG_FORCE)
                else
                    dz.log("alert requested; controlVar set to " .. action  ,dz.LOG_FORCE)
                end
            else
                dz.log("No changed needed; controlVar is already set to " .. value,dz.LOG_DEBUG)
            end
    end,
    
    addMessage = 
        function(dz, item, info, message, messages)
            local trigger =  item.isDevice and (", Device: " .. item.name ) or (", Timer: " .. item.trigger)
            messages[#messages + 1] = {}
            messages[#messages].log  = 'scriptname: ' .. info.scriptName .. trigger .. ' ' .. message
            messages[#messages].text = dz.time.rawDate .. '  ' .. dz.time.rawTime .. ' : ' .. message 
            return messages
        end,
    
    dumpMessages = 
        function(dz, messages)
            local textDevice = dz.devices('timerlog')
            local currentText = textDevice.text
            local newText = currentText
            local lf = "\r\n"
            local maxLines = 8
            
            for i, record in ipairs(messages) do
                dz.log(record.log, dz.LOG_INFO) 
                newText = record.text .. lf .. newText
            end
            
            dz.log(newText, dz.LOG_INFO)
            
            if newText ~= currentText then
                lines = dz.utils.stringSplit(newText, lf)  -- create lines table from newText string
                newText = table.concat(lines, lf, 1, (math.min(maxLines,#lines))) -- recreate newText string from first maxLines 
                textDevice.updateText(newText) 
            end    
        end,       
    }
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: need a loop for reading messages for logging

Post by pvklink »

dit one is much better, uses the whole textswitch. Only an enter missing after a rule....
Attachments
Naamloos.png
Naamloos.png (68.51 KiB) Viewed 2409 times
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: need a loop for reading messages for logging

Post by waaren »

pvklink wrote: Sunday 30 June 2019 17:28 This one is much better, uses the whole textswitch. Only an enter missing after a rule....
I cannot reproduce this behavior. The helper function does add a CR/LF between the messages. What is the user interface theme where you see this ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: need a loop for reading messages for logging

Post by pvklink »

the default theme in dashtics
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: need a loop for reading messages for logging

Post by pvklink »

browser chrome
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: need a loop for reading messages for logging

Post by waaren »

pvklink wrote: Sunday 30 June 2019 18:55 the default theme in dashtics
And what do you see in the device in the domoticz Gui ?
Could it be that dashticz behaves different on a LineFeed then domoticz does ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest