Page 1 of 1

verify if inside an alert.text device there are some words

Posted: Friday 07 February 2020 11:47
by megamarco83
hi i have an alert device that every day change its text like this example:

Code: Select all

2020-02-04 12:01:24	martedì 4 febbraio 2020 - 12:01 : DB backup finished succesfully on raspberrypi
2020-02-04 11:58:31	martedì 4 febbraio 2020 - 11:58 : DB backup Starting .. on raspberrypi
2020-02-04 04:01:51	martedì 4 febbraio 2020 - 04:01 : DB backup lftp failed on raspberrypi
2020-02-04 04:01:01	martedì 4 febbraio 2020 - 04:01 : DB backup Starting .. on raspberrypi
i would like to run a script that if at 8:50 o'clock the text of the alert device contain : "DB backup lftp failed on raspberrypi"
trigger an os comand.

this script has two purposes:
1) lunch an os command if i press dummy switch named "ftpbackup" and this works :-)
2) every day at 8:50 check if the text inside alert device named "alert_ftpbackup" contain sentence like "DB backup finished succesfully on raspberrypi"
if yes lounch a backup, otherwise do nothing.
how i can verify if inside thext of device there are some king of words?
thanks

Code: Select all

return 
{
    on = 
    { 
        devices = { 'ftpbackup' }, 
        timer   = { 'everyday at 8:50'  },
    },

    logging     =   { 
                        level   =  domoticz.LOG_DEBUG,   
                        marker  =  "FTP BACKUP" 
                    },  


       execute = function(dz, item)

local backupAlert = dz.devices('alert_ftpbackup')    


dz.log('extract text from device =============>  '..backupAlert.text , dz.LOG_DEBUG)
            if backupAlert.text  == "DB backup finished succesfully on raspberrypi" then
                os.execute ('sudo /var/www/MyScripts/ftpbackup.sh &')  
                dz.log('NOW INSIDE IF, YEAH!!!!!!!!!!!!!!!!!!!', dz.LOG_DEBUG)
            end

    

            if item.active then
                os.execute ('sudo /var/www/MyScripts/ftpbackup.sh &')
              
                
           end
      end
}

Re: verify if inside an alert.text device there are some words

Posted: Saturday 08 February 2020 19:18
by waaren
megamarco83 wrote: Friday 07 February 2020 11:47 how i can verify if inside thext of device there are some king of words?
You can use match for that.

like

Code: Select all

return 
{
    on = 
    { 
        devices = { 'ftpbackup' }, 
        timer   = { 'everyday at 8:50'  },
    },

    logging     =   { 
                        level   =  domoticz.LOG_DEBUG,   
                        marker  =  "FTP BACKUP" 
                    },  


    execute = function(dz, item)

            local backupAlertText = dz.devices('alert_ftpbackup').text    
            dz.log('extract text from device =============>  '..backupAlertText , dz.LOG_DEBUG)
            if backupAlertText:match("DB backup finished succesfully on raspberrypi") then
                os.execute ('sudo /var/www/MyScripts/ftpbackup.sh &')  
                dz.log('NOW INSIDE IF, YEAH!!!!!!!!!!!!!!!!!!!', dz.LOG_DEBUG)
            end
      end
}

Re: verify if inside an alert.text device there are some words

Posted: Saturday 08 February 2020 19:43
by megamarco83
Hi, thanks for reply, i used a workaround, because i notice that i can also see the difference about colour propriety:

Code: Select all

dz.log('estraggo la scritta =============>  '..backupAlert.text , dz.LOG_DEBUG)
            if backupAlert.color == dz.ALERTLEVEL_RED then
                  os.execute ('sudo /var/www/MyScripts/ftpbackup.sh &')
                dz.log('NOW INSIDE IF, try to make another backup before sending error message', dz.LOG_DEBUG)
            end
but your suggestion is most precious becase i can use match for many other stuff, i will try, thanks so much!

Re: verify if inside an alert.text device there are some words

Posted: Monday 10 February 2020 11:51
by megamarco83
waaren wrote: Saturday 08 February 2020 19:18
You can use match for that.

i try this:

Code: Select all

return 
{
    on = 
    { 

        timer   = { 'everyday at 11:49'  },


local backupAlert = dz.devices('alert_ftpbackup') 
dz.log('estraggo la scritta =============>  '..backupAlert.text , dz.LOG_DEBUG)

                 if backupAlert.text.match("DB backup finished succesfully on raspberrypi") then
            
end
but i receive:

2020-02-10 11:49:00.709 Status: dzVents: Info: test test test test test test: ------ Start internal script: TEST:, trigger: everyday at 11:49
2020-02-10 11:49:00.711 Status: dzVents: Debug: test test test test test test: Processing device-adapter for alert_ftpbackup: Alert sensor adapter
2020-02-10 11:49:00.712 Status: dzVents: Debug: test test test test test test: estraggo la scritta =============> lunedì10 febbraio 2020 - 04:03 :
2020-02-10 11:49:00.712 DB backup finished succesfully on raspberrypi
2020-02-10 11:49:00.712 Status: dzVents: Error (2.4.19): test test test test test test: An error occured when calling event handler TEST
2020-02-10 11:49:00.712 Status: dzVents: Error (2.4.19): test test test test test test: ...e/pi/domoticz/scripts/dzVents/generated_scripts/TEST.lua:25: bad argument #2 to 'match' (string expected, got no value)
2020-02-10 11:49:00.712 Status: dzVents: Info: test test test test test test: ------ Finished TEST


line25 is this one:
if backupAlert.text.match("DB backup finished succesfully on raspberrypi") then

Re: verify if inside an alert.text device there are some words

Posted: Monday 10 February 2020 11:56
by waaren
megamarco83 wrote: Monday 10 February 2020 11:51

Code: Select all

               if backupAlert.text.match("DB backup finished succesfully on raspberrypi") then
match is a string method
You should use

Code: Select all

            if backupAlert.text:match("DB backup finished succesfully on raspberrypi") then

Re: verify if inside an alert.text device there are some words

Posted: Monday 10 February 2020 12:02
by megamarco83
yes, correct.
now is working, thanks!