Use an error in log as a trigger

Moderator: leecollings

User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Use an error in log as a trigger

Post by waaren »

Biiiiino wrote: Tuesday 20 April 2021 13:39 This is not test message. Its a button with not existing URL on action - thats what i want to monitor, if i turn on heating and device (esp8266) is not respondig, revert status of my switch back by call url directly to device in domoticz.. Maybe this is not the correct way, because now is searching for all erros in log.

Do you know any better way how to monitor and revert back if URL of switch is not accessible ?
I do not completely understand all the steps in your procedure but it seems to me that you try to notify dzVents that something happened via the domotiocz-log. Would it not be easier to notify dzVents directly if the device is updated or if that's not feasible, trigger a customEvent using the API from the On action?

Code: Select all

/json.htm?type=command&param=customevent&event=MyEvent&data=MyData
see for a detailed howto [url=https://www.domoticz.com/wiki/DzVents:_ ... _.7D_3.0.0\ the dzVents wiki [/url]
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Biiiiino
Posts: 18
Joined: Monday 19 April 2021 18:15
Target OS: Linux
Domoticz version:
Contact:

Re: Use an error in log as a trigger

Post by Biiiiino »

I have few esp8266 devices, which all i am using http commands instead of mqtt because if URL (esp is not running) is not accessible, domoticz write it to log and i know, the button is showing "on" in domoticz but in reality is "off" because it was not able during "switching on" process in domoticz... Now few critical switches like heating or irrigation when i am using out of my house, i always manually checking in domoticz log, if everythink is okay...and that is what i want to automate :-/
Biiiiino
Posts: 18
Joined: Monday 19 April 2021 18:15
Target OS: Linux
Domoticz version:
Contact:

Re: Use an error in log as a trigger

Post by Biiiiino »

For example:

When i switch in domoticz button "heating" to on, it calls url (on action in virtual switch):

http://192.168.1.241/control?cmd=GPIO,12,0

If that URL is not accesible from domoticz (esp8266 is not running) it will mark it to log as "Error opening url: http://http://192.168.1.241/control?cmd=GPIO,12,0

After that i want automaticaaly open another URL directly to switch button in domoticz back:

http://127.0.0.1/json.htm?type=command& ... itchcmd=on


When this happen, i see the real state of the heating unit in domoticz
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Use an error in log as a trigger

Post by waaren »

Biiiiino wrote: Tuesday 20 April 2021 15:42 If that URL is not accesible from domoticz (esp8266 is not running) it will mark it to log as "Error opening url: http://http://192.168.1.241/control?cmd=GPIO,12,0
Below solution seems to be simpler.
Please make sure you remove the "On action" from heating before testing

Code: Select all

local scriptVar = 'myheating'

return 
{
    on = 
    {
        devices = 
        {
            'heating',
        },
        httpResponses = 
        {
            scriptVar,
        }
    },
    logging = 
    {
        level = domoticz.LOG_DEBUG,
        marker = 'scriptVar',
    },
    
    execute = function(dz, item)

        if item.isDevice and item.active then
            dz.openURL(
            {
                url = 'http://192.168.1.241/control?cmd=GPIO,12,0',
                callback = scriptVar
            })
        elseif item.isHTTPResponse and item.ok then
            dz.log('Heating on ESP switched on', dz.LOG_DEBUG)
        elseif item.isHTTPResponse then
            dz.log('There was a problem with the ESP. Switching back the heating switch ', dz.LOG_ERROR)
            dz.log(item, dz.LOG_DEBUG)
            dz.devices('heating').switchOff().silent()
        end

    end
}

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Biiiiino
Posts: 18
Joined: Monday 19 April 2021 18:15
Target OS: Linux
Domoticz version:
Contact:

Re: Use an error in log as a trigger

Post by Biiiiino »

Waaren OMG!!! thank you so much, please last think from me. Could you add there also option, when this situation happen if i turn switch off ?

Its possible to have it in one script ? Revert to on ( when url not avalabile) revert to off ( when url not available) ?

again thank you a lot
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Use an error in log as a trigger

Post by waaren »

Biiiiino wrote: Tuesday 20 April 2021 16:57 Please last thing from me. Could you add there also option, when this situation happen if i turn switch off ?
What is the url to switch the heating to Off on the ESP ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Biiiiino
Posts: 18
Joined: Monday 19 April 2021 18:15
Target OS: Linux
Domoticz version:
Contact:

Re: Use an error in log as a trigger

Post by Biiiiino »

User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Use an error in log as a trigger

Post by waaren »

Biiiiino wrote: Tuesday 20 April 2021 16:57 please last thing from me. Could you add there also option, when this situation happen if i turn switch off ?
Could look like below

Code: Select all

local scriptVar = 'myheating'

return
{
    on =
    {
        devices =
        {
            'heating',
        },
        httpResponses =
        {
            scriptVar,
        }
    },
    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'scriptVar',
    },

    execute = function(dz, item)

        if item.isDevice then
            local action = 1
            if item.active then
                action = 0
            end

            dz.openURL(
            {
                url = 'http://192.168.1.241/control?cmd=GPIO,12,' .. action,
                callback = scriptVar,
            })

        elseif item.isHTTPResponse and item.ok then
            dz.log('Heating on ESP switched, dz.LOG_DEBUG)
        elseif item.isHTTPResponse then
            dz.log('There was a problem with the ESP. Switching back the heating switch.', dz.LOG_ERROR)
            dz.log(item, dz.LOG_DEBUG)
            dz.devices('heating').toggleSwitch().silent()
        end

    end
}

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Biiiiino
Posts: 18
Joined: Monday 19 April 2021 18:15
Target OS: Linux
Domoticz version:
Contact:

Re: Use an error in log as a trigger

Post by Biiiiino »

Looks great, its possible to split log message based on action?

Eg if “on” cant be switched - log message: heating cant be turned on is stiĺ off

And reverse on off action

Its that possible? Thaank you
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Use an error in log as a trigger

Post by waaren »

Biiiiino wrote: Tuesday 20 April 2021 20:11 Looks great, its possible to split log message based on action?
Its that possible? Thaank you
Yes that's possible. You can retrieve the current state of the device using dz.devices('heating').state and base your log message on that state.
Please try it yourself
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Biiiiino
Posts: 18
Joined: Monday 19 April 2021 18:15
Target OS: Linux
Domoticz version:
Contact:

Re: Use an error in log as a trigger

Post by Biiiiino »

Okkay :) thats totaly out of my skill you can trust me:) but again one more big THANK YOU waaren.
Biiiiino
Posts: 18
Joined: Monday 19 April 2021 18:15
Target OS: Linux
Domoticz version:
Contact:

Re: Use an error in log as a trigger

Post by Biiiiino »

but if you have a bit of free time... :)
Biiiiino
Posts: 18
Joined: Monday 19 April 2021 18:15
Target OS: Linux
Domoticz version:
Contact:

Re: Use an error in log as a trigger

Post by Biiiiino »

Im trying to do it like this:

Code: Select all

local scriptVar = 'myheating'
local kurenie = 'test_switch'


return
{
    on =
    {
        devices =
        {
            kurenie,
        },
        httpResponses =
        {
            scriptVar,
        }
    },
    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = scriptVar,
    },

    execute = function(dz, item)

        if item.isDevice then
            local action = 1
            if item.active then
                action = 0
            end

            dz.openURL(
            {
                url = 'http://192.168.1.800/control?cmd=GPIO,12,' .. action,
                callback = scriptVar,
            })

        elseif item.isHTTPResponse and item.ok then
          dz.log('Heating on ESP switched', dz.LOG_DEBUG)
        elseif item.isHTTPResponse then
            if dz.devices(kurenie).state == Off  then
                dz.log('Cant turn switch ON, stay turned OFF.', dz.LOG_ERROR)
                dz.log(item, dz.LOG_DEBUG)
                dz.devices(kurenie).toggleSwitch().silent()
            
        elseif item.isHTTPResponse then 
            if dz.devices(kurenie).state == On  then
                dz.log('Cant turn switch OFF, stay turned ON.', dz.LOG_ERROR)
                dz.log(item, dz.LOG_DEBUG)
                dz.devices(kurenie).toggleSwitch().silent()
            end    
         end
     end
end
}

But no success, what im I doing wrong please ???

Thank you in advance
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Use an error in log as a trigger

Post by waaren »

Biiiiino wrote: Thursday 22 April 2021 16:41 Im trying to do it like this:

Code: Select all

            if dz.devices(kurenie).state == On  then
But no success, what im I doing wrong please ???
You were quite close. States are strings

Code: Select all

local scriptVar = 'myheating'
local kurenie = 'test_switch'


return
{
    on =
    {
        devices =
        {
            kurenie,
        },
        httpResponses =
        {
            scriptVar,
        }
    },
    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = scriptVar,
    },

    execute = function(dz, item)

        if item.isDevice then
            local action = 1
            if item.active then
                action = 0
            end

            dz.openURL(
            {
                url = 'http://192.168.1.800/control?cmd=GPIO,12,' .. action,
                callback = scriptVar,
            })

        elseif item.isHTTPResponse and item.ok then
            dz.log('Heating on ESP switched', dz.LOG_DEBUG)
        elseif item.isHTTPResponse then
            if dz.devices(kurenie).state == 'Off'  then
                dz.log('Cant turn switch ON, stay turned OFF.', dz.LOG_ERROR)
            else
                dz.log('Cant turn switch OFF, stay turned ON.', dz.LOG_ERROR)
            end
            dz.devices(kurenie).toggleSwitch().silent()
         end
     end
end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Biiiiino
Posts: 18
Joined: Monday 19 April 2021 18:15
Target OS: Linux
Domoticz version:
Contact:

Re: Use an error in log as a trigger

Post by Biiiiino »

Aah okay, thank you one more time.
Zachtstaart2002
Posts: 3
Joined: Sunday 24 November 2019 16:06
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Use an error in log as a trigger

Post by Zachtstaart2002 »

Found this discusion because i want the same thing. Copied the script and edited to my need. Works as a charme. I have one question. Is it normaal that it logs in the error log of domoticz every time it finds a error message?

this is the script i have running every three minutes.

Code: Select all

-- WLED: No transport, write directive to 'JSONConn' ignored.
myHttpResponse = "getDomoticzErrorLog"

return {
    on      =   {   httpResponses   =   { myHttpResponse },
                    timer           =   { "every 3 minutes"},
                },

    logging =   {   level           =   domoticz.LOG_DEBUG,
                    marker          =   "getStr from log"    },

    data    =   {   lastlogtime     =   { initial = 0 }}, 

    execute = function(dz, trigger)
        
        local searchStr = "WLED: No transport, write directive to 'JSONConn' ignored."          -- Change to reflect the messages you want to catch

        local function askDomoticzForLogLines()
            local lastLogTime = dz.data.lastlogtime            -- get time last logrequest
            local logLevel    = 4                              -- loglevel (1=normal,2=Status,4=error,268435455=all
            local jsonString  = "/json.htm?type=command" .. 
                                "&param=getlog" ..
                                "&lastlogtime=" .. tostring(lastLogTime) ..
                                "&loglevel=" .. tostring(logLevel)
            
            local logURL      =  dz.settings["Domoticz url"] .. jsonString
            
            dz.openURL  ({ url      = logURL,
                           method   = "GET", 
                           callback = myHttpResponse 
                        })

            dz.data.lastlogtime = dz.time.dDate        -- store current Time as seconds from epoch  
        end

        local function findString()
           if trigger.json.result ~= nil then               -- Only when there are errormessages in the log
               local resultTable = trigger.json.result
               for i = #resultTable,1,-1 do                 -- traverse backwards to get latest violation first
                    local s = resultTable[i].message
                    local found = s:find(searchStr,1,true) -- searcg plain string starting at pos 1 (no pattern) 
                    local dzVents = s:find("dzVents")
                    if found and not(dzVents) then
                       return true
                    end
                end
            end
        end

        if trigger.isDevice or trigger.isTimer then
            askDomoticzForLogLines()                     -- get the relevant loglines
        elseif trigger.ok then
            dz.log("Show ALL: \n" .. trigger.data,dz.LOG_DEBUG)
            if findString() then
                dz.log("string " .. searchStr .. " found!!!!!!!!!! ",dz.LOG_DEBUG)
                if dz.devices('Vir_Openhaard').state == 'On' then
                    dz.log("Zou Openhaard aan moeten gaan",dz.LOG_DEBUG)
                    dz.devices('Openhaard').switchOff()
                -- else
                    -- dz.devices('Openhaard').switchOn()
                end
                -- dz.notify('Openhaard gereset vanwege WLED foutmelding',dz.PRIORITY_LOW)
                -- dz.notify("searchStr", searchStr, dz.PRIORITY_NORMAL, nil, nil, dz.NSS_PUSHOVER)
            else    
                dz.log("No (new) occurrences of string " .. searchStr .. " in the domoticz log ",dz.LOG_DEBUG)
            end    
        else
            dz.log("Access problem to log. Data found: " .. trigger.data,dz.LOG_ERROR)
       end
    end
}
In the error log of domoticz it looks like this:

2023-02-09 14:22:16.581 Error: Error parsing http request address: ::ffff:35.216.132.205
2023-02-09 14:22:16.591 Error: dvVents: Debug: getStr from log: Show ALL:
2023-02-09 14:22:16.591 {
2023-02-09 14:22:16.591 "LastLogTime" : "1675948936",
2023-02-09 14:22:16.591 "result" :
2023-02-09 14:22:16.591 [
2023-02-09 14:22:16.591 {
2023-02-09 14:22:16.591 "level" : 4,
2023-02-09 14:22:16.591 "message" : "2023-02-09 14:22:16.581 Error parsing http request address: ::ffff:35.216.132.205"
2023-02-09 14:22:16.591 }
2023-02-09 14:22:16.591 ],
2023-02-09 14:22:16.591 "status" : "OK",
2023-02-09 14:22:16.591 "title" : "GGetlog"
2023-02-09 14:22:16.591 }
2023-02-09 14:22:16.591

Any idea why this appears in the error log? Is it something to wory about?
mgugu
Posts: 208
Joined: Friday 04 November 2016 12:33
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: France
Contact:

Re: Use an error in log as a trigger

Post by mgugu »

I asked chatGpt for such a dzvents script, here its answer:

Code: Select all

return {
	on = {
		log = {
			types = { "error" },
			level = 4
		}
	},
	execute = function(domoticz, item)
		-- Code for action
		domoticz.notify("Error detected in log", "An error has been detected in the Domoticz log. Check the logs for details.", domoticz.PRIORITY_HIGH)
	end
}
Syntax is correct but it is not working because the on log trigger does not exist.
Stange, where does that come from ? Nothing from Google :?
User avatar
boum
Posts: 130
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Use an error in log as a trigger

Post by boum »

Any idea why this appears in the error log? Is it something to wory about?
Change the logging level to LOG_ERROR there:

Code: Select all

    logging =   {   level           =   domoticz.LOG_DEBUG,
                    marker          =   "getStr from log"    },
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests