multiple textlines in textdevice

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

Moderator: leecollings

Post Reply
Gravityz
Posts: 587
Joined: Wednesday 16 December 2015 19:13
Target OS: NAS (Synology & others)
Domoticz version: 2022.2
Location: Netherlands
Contact:

multiple textlines in textdevice

Post by Gravityz »

Hello,

i have a textdevice which i use to display the last status.

is there a way to display multiple lines so i can use it as a log.

e.g. i use this comand at the moment

domoticz.devices('Status').updateText('Woonkamer lampen ingeschakeld')
which displays Woonkamer lampen ingeschakeld

now when i issue a second command like
domoticz.devices('Status').updateText('Alarm ingeschakeld')

i want to see the most actual one on top and the other one below

Alarm ingeschakeld
Woonkamer lampen ingeschakeld


i know this can be done in lua(but i do not know how) butdoes this work in dzvents
zygios
Posts: 15
Joined: Saturday 01 December 2018 6:42
Target OS: Linux
Domoticz version:
Contact:

Re: multiple textlines in textdevice

Post by zygios »

Hi,
I'm using this:

Code: Select all

 departimestext = departimestext .. ' '..departime ..';\r\n'
 
and result for text device is next:
Status: 08:21:24
08:23:30
08:29:00

You get check in my GIT:
https://github.com/zygios/Domoticz_scri ... nsport.lua
Gravityz
Posts: 587
Joined: Wednesday 16 December 2015 19:13
Target OS: NAS (Synology & others)
Domoticz version: 2022.2
Location: Netherlands
Contact:

Re: multiple textlines in textdevice

Post by Gravityz »

Thanks,
got is working(sort of)
the problem i have now i need to limit the amount of lines to max 5 i think
is there an easy way to do this


this is my code
currentstatus = domoticz.devices('Status').state ..'\r\n'
newstatus = 'Woonkamer lampen sfeervol ingeschakeld'
currentstatus = newstatus .. ';\r\n'..currentstatus ..'\r\n'
domoticz.devices('Status').updateText(currentstatus)
Knipsel.JPG
Knipsel.JPG (42.38 KiB) Viewed 1373 times
zygios
Posts: 15
Joined: Saturday 01 December 2018 6:42
Target OS: Linux
Domoticz version:
Contact:

Re: multiple textlines in textdevice

Post by zygios »

try this code:

Code: Select all

limit_max = 5

for m = 1, limit_max do
	currentstatus = domoticz.devices('Status').state ..'\r\n'
	newstatus = 'Woonkamer lampen sfeervol ingeschakeld'
	currentstatus = newstatus .. ';\r\n'..currentstatus ..'\r\n' 
	m = m + 1
end
domoticz.devices('Status').updateText(currentstatus)
Gravityz
Posts: 587
Joined: Wednesday 16 December 2015 19:13
Target OS: NAS (Synology & others)
Domoticz version: 2022.2
Location: Netherlands
Contact:

Re: multiple textlines in textdevice

Post by Gravityz »

hm. wonder if this works

this script is called everytime somebody comes/leaves or when a schedule kicks in.

m is not a global variable so i think every time the script is called m=1
in case it does remember after 5 times it will not refresh anymore
zygios
Posts: 15
Joined: Saturday 01 December 2018 6:42
Target OS: Linux
Domoticz version:
Contact:

Re: multiple textlines in textdevice

Post by zygios »

Try this:

Code: Select all

limit_max = 4
temp_stastus = ''
m = 1

currentstatus = domoticz.devices('Status').state

--removing 5-th status
for i in string.gmatch(currentstatus,  "[^\r\n]+") do
    print(i)
	if m <=4 then 
		temp_stastus = temp_stastus .. i.. "\r\n";
		m = m + 1
	end
end

newstatus = 'Woonkamer lampen sfeervol ingeschakeld'
currentstatus = newstatus .. ';\r\n'..temp_stastus
domoticz.devices('Status').updateText(currentstatus)
Gravityz
Posts: 587
Joined: Wednesday 16 December 2015 19:13
Target OS: NAS (Synology & others)
Domoticz version: 2022.2
Location: Netherlands
Contact:

Re: multiple textlines in textdevice

Post by Gravityz »

this indeed works with limiting the messages

however as far as i can see the top line is changing but the others are not.

the idea is that when things change the old messages shift down.

eg

a
b
c
d
e

new message f
f
a
b
c
d

new message h
h
f
a
b
c

i think i need to combine things from the old code and your new code but this is looking promising
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: multiple textlines in textdevice

Post by waaren »

Gravityz wrote: Thursday 11 April 2019 13:41 this indeed works with limiting the messages
however as far as i can see the top line is changing but the others are not.
the idea is that when things change the old messages shift down.
If I understand your requirement correct, the function addRollingTextLine in below script should do it.

Code: Select all

return {	
			on =    {
						timer = { 'every minute' },
                      		},
        
    logging =   { level   = domoticz.LOG_DEBUG ,                  
                  marker  = "multiLine text function"},
              
    execute = function(dz, item)
		
        local function addRollingTextLine(textDevice, line, maxLines)
        
            local function stringSplit(str)
                local lines = {}
                for s in str:gmatch("[^\r\n]+") do
                    table.insert(lines, s)
                end
                return lines
            end
            
            local function checkParms()
                if not textDevice or ( type(textDevice) ~= "string" and type(textDevice) ~= "table" ) then
                    dz.log("textDevice should be of type table or string; now: " .. (tostring(textDevice) or type(textDevice)),dz.LOG_ERROR)
                    return false
                elseif type(textDevice) == "string" then 
                    textDevice = dz.devices(textDevice) 
                end    
                if not line then
                    dz.log("No text supplied",dz.LOG_ERROR)
                    return false
                end
                if not maxLines then maxLines = 5 end          -- default
                return true
            end
                
            if checkParms() then
                local lines = textDevice.text or ""
                lines = stringSplit(lines) 
                lines[0] = line
                local newText = ""
                for i = 0, ( maxLines - 1) do 
                    newText = newText ..  ( lines[i] or "" ) .. "\r\n" 
                end
                textDevice.updateText(newText)    
            else
                return false
            end
        end 
        
        -- Test call 
        addRollingTextLine("multiLine", dz.time.raw, 3)
        
    end
} 
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Gravityz
Posts: 587
Joined: Wednesday 16 December 2015 19:13
Target OS: NAS (Synology & others)
Domoticz version: 2022.2
Location: Netherlands
Contact:

Re: multiple textlines in textdevice

Post by Gravityz »

Hi waaren.

that code works very nice
Knipsel2.JPG
Knipsel2.JPG (21.46 KiB) Viewed 1283 times

the only downfall is that i need to add it to every script that is updating the status device.

is it possible to put this into some kind of module or helper function.

eg declare it once and then call it from every script you like
Last edited by Gravityz on Saturday 13 April 2019 19:05, edited 1 time in total.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: multiple textlines in textdevice

Post by waaren »

Gravityz wrote: Saturday 13 April 2019 17:04 the only downfall is that i need to add it to every script that is updating the status device.
is it possible to put this into some kind of module or helper function.
eg declare it once and then call it from every script you like
Have a look here where exactly that is described in detail
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Gravityz
Posts: 587
Joined: Wednesday 16 December 2015 19:13
Target OS: NAS (Synology & others)
Domoticz version: 2022.2
Location: Netherlands
Contact:

Re: multiple textlines in textdevice

Post by Gravityz »

Thanks waaren, you made my day.
i have read that myhelper part multiple times over and over again and could not figur out how it was working, until now.

after reading MORE CAREFULLY i noticed what i was doing wrong

working perfectly now

i now can use the Status textdevice as a logging device lights on/of alarm on/off sunscreen in/out. al the things i normally wrote to the log are now visible
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest