Page 1 of 1

Stop dzvents script when esp is not online

Posted: Saturday 27 April 2019 22:23
by hoeby
I hope somebody can help me.
Did a search on the forum, but couldn't find it. Maybe i am not searching with the right words.

Last week domiticz crashed 2 times. Working with it for 2 years without crash. Needed to search why and found a dzvents script that want toggle an espeasy switch. But the espeasy device was broken, but the script stayed running. And made domiticz crash after a few hours running.

I use this type of command in dzvents http://192.168.xxx.xxx/control?cmd=GPIO,14,0 but need something which checks the espeasy and stops the script when the device is not online.

Somebody has an idea how?

Re: Stop dzvents script when esp is not online

Posted: Saturday 27 April 2019 22:40
by waaren
hoeby wrote: Saturday 27 April 2019 22:23 Last week domiticz crashed 2 times. Working with it for 2 years without crash. Needed to search why and found a dzvents script that want toggle an espeasy switch. But the espeasy device was broken, but the script stayed running. And made domiticz crash after a few hours running.
Did you see any error message before the crash ?
Can you share the script that crashed domotizc ?
Did the espEasy react to a ping ?

Re: Stop dzvents script when esp is not online

Posted: Sunday 28 April 2019 7:55
by hoeby
I don't had log actieve, which writes a logfile (domoticz.sh, there was an # in front of the log row).
When looking in log of the domoticz dashboard, i couldn't look there. Domoticz was offline. After service stop and start the log was empty.
Don't know if there was an error message.

The espEasy was not reaction to a ping. To power supply was dead and therefor offline.

Code: Select all

return {
	on = {
		devices = {'Backlight Dashboard', 
		           'Dashboard Screen Status',
		           'Dashboard Homepage Trigger'}
		},
		
	execute = function(domoticz, device)
	    if(
	        domoticz.devices('Backlight Dashboard').state == 'On' and
	        domoticz.devices('Dashboard Screen Status').state == 'On' and
	        domoticz.time.matchesRule('on mon,tue,wed,thu,fri,sat,sun') and
	        (domoticz.time.hour >= 23 or domoticz.time.hour < 7)) then
	            domoticz.openURL({url = 'http://192.168.xxx.xxx/control?cmd=gpio,13,1'}).afterSec(150)
                    domoticz.openURL({url = 'http://192.168.xxx.xxx/control?cmd=gpio,13,0'}).afterSec(151)
        elseif(
            domoticz.devices('Backlight Dashboard').state == 'Off' and
	        domoticz.devices('Dashboard Screen Status').state == 'On' and
	        domoticz.time.matchesRule('on mon,tue,wed,thu,fri,sat,sun') and
	        (domoticz.time.hour >= 23 or domoticz.time.hour < 7)) then
	            domoticz.devices('Dashboard Screen Status').switchOff().silent() 
	    elseif(
            domoticz.devices('Backlight Dashboard').state == 'On' and
	        domoticz.devices('Dashboard Screen Status').state == 'Off' and
	        domoticz.time.matchesRule('on mon,tue,wed,thu,fri,sat,sun') and
	        (domoticz.time.hour >= 23 or domoticz.time.hour < 7)) then
	            domoticz.openURL({url = 'http://192.168.xxx.xxx/control?cmd=gpio,13,1'}).afterSec(150)
                    domoticz.openURL({url = 'http://192.168.xxx.xxx/control?cmd=gpio,13,0'}).afterSec(151)
	    elseif(
	        domoticz.devices('Backlight Dashboard').state == 'On' and 
	        domoticz.devices('Dashboard Screen Status').state == 'Off' and
	        domoticz.time.matchesRule('on mon,tue,wed,thu,fri,sat,sun') and
	        domoticz.time.matchesRule('between 07:00 and 23:00')) then
	            domoticz.devices('Dashboard Screen Status').switchOn().silent()
	    elseif(
	        domoticz.devices('Backlight Dashboard').state == 'Off' and
	        domoticz.devices('Dashboard Screen Status').state == 'Off' and
	        domoticz.time.matchesRule('on thu,fri,sat,sun') and
	        domoticz.time.matchesRule('between 07:00 and 23:00')) then
	            domoticz.openURL({url = 'http://192.168.xxx.xxx/control?cmd=gpio,13,1'})
                    domoticz.openURL({url = 'http://192.168.xxx.xxx/control?cmd=gpio,13,0'}).afterSec(1)
        elseif(
	        domoticz.devices('Backlight Dashboard').state == 'Off' and
	        domoticz.devices('Dashboard Screen Status').state == 'On' and
	        domoticz.time.matchesRule('on mon,tue,wed,thu,fri,sat,sun') and
	        domoticz.time.matchesRule('between 07:00 and 23:00')) then
	            domoticz.devices('Dashboard Screen Status').switchOff().silent() 
	    elseif(
	        domoticz.devices('Dashboard Homepage Trigger').state == 'On' and
	        domoticz.devices('Backlight Dashboard').state == 'Off' and
	        domoticz.devices('Dashboard Screen Status').state == 'Off') then
	            domoticz.openURL({url = 'http://192.168.xxx.xxx/control?cmd=gpio,13,1'})
                    domoticz.openURL({url = 'http://192.168.xxx.xxx/control?cmd=gpio,13,0'}).afterSec(1)
        elseif(
	        domoticz.devices('Dashboard Homepage Trigger').state == 'On') then
	            domoticz.devices('Dashboard Homepage Trigger').switchOff().silent().afterSec(20)         
	end
end
}

Re: Stop dzvents script when esp is not online

Posted: Sunday 28 April 2019 8:54
by SweetPants
hoeby wrote: Saturday 27 April 2019 22:23 I use this type of command in dzvents http://192.168.xxx.xxx/control?cmd=GPIO,14,0 but need something which checks the espeasy and stops the script when the device is not online.
This is the main reason I use MQTT with retained messages. ESP modules tend to go offline sometimes, either by it self or when you boot your WiFi router, they loose connection for some time. Using MQTT, Domoticz is always able to send the message to the broker, even if the ESP is offline. When it comes online again, the broker sends the last retained message to the ESP module again. I am using this for years now and it works great. Have around 20 ESP devices running now on MQTT without any major problems.

Re: Stop dzvents script when esp is not online

Posted: Sunday 28 April 2019 10:56
by waaren
hoeby wrote: Sunday 28 April 2019 7:55 I don't had log actieve, which writes a logfile (domoticz.sh, there was an # in front of the log row).
When looking in log of the domoticz dashboard, i couldn't look there. Domoticz was offline. After service stop and start the log was empty.
Don't know if there was an error message.

The espEasy was not reaction to a ping. To power supply was dead and therefor offline.
I agree with @SweetPants about his preference for MQTT. It is a more robust solution.
Do you have a reason to believe domoticz crashed because of the http calls to a dead system ?

Below script checks a valid response to ping before it does anything else. Can you please check and revert ? Thanks

Code: Select all

return {
    on = {
        devices = {'Backlight Dashboard', 
                   'Dashboard Screen Status',
                   'Dashboard Homepage Trigger'}
        },
        
    execute = function(dz)
        local espEasyIP = '192.168.xxx.xxx' -- change this to IP of your espEasy

        local backLight = dz.devices('Backlight Dashboard')
        local backLightOn = backLight.state == 'On'
        local backLightOff = not backLightOn
        
        local screenStatus = dz.devices('Dashboard Screen Status')
        local screenStatusOn = screenStatus.state == 'On'
        local screenStatusOff = not screenStatusOn
        
        local homePageTrigger = dz.devices('Dashboard Homepage Trigger')
        local homePageTriggerOn = homePageTrigger.state == 'On'
        local homePageTriggerOff = not homePageTriggerOn
        
        local nightTime = dz.time.matchesRule('at 23:00-07:00')
        local dayTime = not nightTime
        
        local function sendToEasy(delay)
            url = 'http://' .. espEasyIP .. '/control?cmd=gpio,13,'
             dz.openURL(url .. '1').afterSec(delay)
             dz.openURL(url .. '0').afterSec(delay + 1)
        end
        
        local function osCommand(cmd)
            local fileHandle     = assert(io.popen(cmd, 'r'))
            local commandOutput  = assert(fileHandle:read('*a'))
            local returnTable    = {fileHandle:close()}
            dz.log('\nCommand:       ' .. cmd ..
                   '\nReturnCode:    ' .. returnTable[3] ..
                   '\ncommandOutput: ' .. commandOutput)
            return commandOutput,returnTable[3]            -- rc[3] contains returnCode
        end
        
        local commandOutput, returnCode = osCommand('sudo ping -c1 -w0 ' .. espEasyIP)
        if returnCode ~= 0 then
            dz.notify('espEasy check', 'ping failed', dz.PRIORITY_NORMAL)
        elseif backLightOn and screenStatusOn and nightTime then
            sendToEasy(150)
        elseif backLightOff and screenStatusOn and nightTime then
            screenStatus.switchOff().silent() 
        elseif backLightOn and screenStatusOff and nightTime then
            sendToEasy(150)
        elseif backLightOn and screenStatusOff and dayTime then
            screenStatus.switchOn().silent()
        elseif backLightOff and screenStatusOff and dayTime then
            sendToEasy(0)
        elseif backLightOff and screenStatusOn and dayTime then
            screenStatus.switchOff().silent()
        elseif homePageTriggerOn and backLightOff and screenStatusOff then
            sendToEasy(0)
        elseif homePageTriggerOn then
            homePageTrigger.switchOff().silent().afterSec(20)
        end
    end
}

Re: Stop dzvents script when esp is not online

Posted: Sunday 28 April 2019 12:33
by hoeby
I am for sure that domoticz crashed because calling a dead system.
When not crashed i saw in the log that it tried to send something to espeasy, but got a red row back.
This kept going on. When trying to switch a light at that moment, the icon on the domoticz dashboard went off, but the light itself kept on.
Then disabled the script and services restarted, everything worked OK.
Then let it run for 1 week without problems. Enabled the script and in a few hours the problem was back.

Thanks Waaren, i will look at the script.
But maybe it is better to go working with MQTT, only need to search for documentation how to setup.

Do you maybe have some hints?
Don't know where to start with MQTT and how to send the messages to espeasy from domoticz

Re: Stop dzvents script when esp is not online

Posted: Sunday 28 April 2019 12:56
by waaren
hoeby wrote: Sunday 28 April 2019 12:33 Need to search for MQTT documentation how to setup.
I am not an expert on MQTT but this information got me started
setup MQTT in domoticz and MQTT in espEasy. There are also already a lot of forum members using this so when you need help for a specific question you can just ask here.

Re: Stop dzvents script when esp is not online

Posted: Sunday 28 April 2019 18:57
by SweetPants
hoeby wrote: Sunday 28 April 2019 12:33 Then let it run for 1 week without problems. Enabled the script and in a few hours the problem was back.
Hmmm, if your ESP module(s) get offline this easy (several hours), maybe you also should look at your WiFi signals. Mine run for days/weeks without loosing connection to the Access Point. I'm not sure if ESPEasy supports a watchdog or something like that. Because i write my own software for the ESP, i have added a watchdog timer that restarts the ESP module when there is no traffic to/from the Access Point for more then 60 sec.

Re: Stop dzvents script when esp is not online

Posted: Sunday 28 April 2019 19:16
by hoeby
The wifi signal is not the problem. A dead power supply was the reason that my espeasy was offline.
But i wanted to find out if that was the reason why domoticz stopped working, when the espeasy is offline. So did a test while keeping the espeasy offline and see how domoticz respons when trying to switch the esp.

@sweetpants, do you have any idea where to look for information about mqtt and esp?
All information is welcome

Re: Stop dzvents script when esp is not online

Posted: Sunday 28 April 2019 19:31
by SweetPants
hoeby wrote: Sunday 28 April 2019 19:16 @sweetpants, do you have any idea where to look for information about mqtt and esp?
You might take a look at https://github.com/arendst/Sonoff-Tasmota/wiki/Domoticz it supports out of the box domoticz MQTT. You need to install a MQTT broker and add MQTT hardware in domoticz

Re: Stop dzvents script when esp is not online

Posted: Sunday 28 April 2019 21:15
by hoeby
Got it working with MQTT.
But do i Always need to make a dummy device, to get the switch functional?
Or is it possible to send only codes?
From what i found i Always need a device, because i need to fill in an idx number