script is keepon running

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

Moderator: leecollings

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

script is keepon running

Post by pvklink »

Hi, I have a script that does not stop...
The switches are off...
Even my other rfxcom devices dont work well i think because of this script.
When i stop the script , my other rfxcom devices work!

This is my logging:

Code: Select all

2019-04-09 16:00:14.467 (rfxcom2) Lighting 2 (Tuinirrigatie_border_rechts)
2019-04-09 16:00:14.772 (rfxcom2) Lighting 2 (Tuinirrigatie_border_rechts)
2019-04-09 16:00:14.387 Status: dzVents: Info: Tuinirrigatie_borders: ------ Start internal script: DZ_Irrigatie: Device: "Tuinirrigatie_border_rechts (rfxcom2)", Index: 973
2019-04-09 16:00:14.394 Status: dzVents: Info: Tuinirrigatie_borders: Script: DZ_Irrigatie Device uitgezet....
2019-04-09 16:00:14.394 Status: dzVents: Info: Tuinirrigatie_borders: ------ Finished DZ_Irrigatie
2019-04-09 16:00:14.402 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2019-04-09 16:00:14.702 Status: dzVents: Info: Tuinirrigatie_borders: ------ Start internal script: DZ_Irrigatie: Device: "Tuinirrigatie_border_rechts (rfxcom2)", Index: 973
2019-04-09 16:00:14.712 Status: dzVents: Info: Tuinirrigatie_borders: Script: DZ_Irrigatie Device uitgezet....
2019-04-09 16:00:14.712 Status: dzVents: Info: Tuinirrigatie_borders: ------ Finished DZ_Irrigatie
2019-04-09 16:00:14.716 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2019-04-09 16:00:15.087 (rfxcom2) Lighting 2 (Tuinirrigatie_border_rechts)
2019-04-09 16:00:15.363 (zwavepluspvk) General/kWh (wasmachine_kwh)
2019-04-09 16:00:15.387 (rfxcom2) Lighting 2 (Tuinirrigatie_border_rechts)
2019-04-09 16:00:15.401 (zwavepluspvk) Usage (Unknown)
2019-04-09 16:00:15.416 (zwavepluspvk) General/kWh (wasmachine_kwh)
2019-04-09 16:00:15.971 (rfxcom2) Lighting 2 (Tuinirrigatie_border_rechts)
2019-04-09 16:00:15.061 Status: dzVents: Info: Tuinirrigatie_borders: ------ Start internal script: DZ_Irrigatie: Device: "Tuinirrigatie_border_rechts (rfxcom2)", Index: 973
2019-04-09 16:00:15.072 Status: dzVents: Info: Tuinirrigatie_borders: Script: DZ_Irrigatie Device uitgezet....
2019-04-09 16:00:15.072 Status: dzVents: Info: Tuinirrigatie_borders: ------ Finished DZ_Irrigatie
2019-04-09 16:00:15.075 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2019-04-09 16:00:15.327 Status: dzVents: Info: Tuinirrigatie_borders: ------ Start internal script: DZ_Irrigatie: Device: "Tuinirrigatie_border_rechts (rfxcom2)", Index: 973
2019-04-09 16:00:15.337 Status: dzVents: Info: Tuinirrigatie_borders: Script: DZ_Irrigatie Device uitgezet....
2019-04-09 16:00:15.337 Status: dzVents: Info: Tuinirrigatie_borders: ------ Finished DZ_Irrigatie
2019-04-09 16:00:15.340 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2019-04-09 16:00:15.932 Status: dzVents: Info: Tuinirrigatie_borders: ------ Start internal script: DZ_Irrigatie: Device: "Tuinirrigatie_border_rechts (rfxcom2)", Index: 973
2019-04-09 16:00:15.942 Status: dzVents: Info: Tuinirrigatie_borders: Script: DZ_Irrigatie Device uitgezet....
2019-04-09 16:00:15.942 Status: dzVents: Info: Tuinirrigatie_borders: ------ Finished DZ_Irrigatie
2019-04-09 16:00:15.952 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2019-04-09 16:00:16.230 (rfxcom2) Lighting 2 (Tuinirrigatie_border_rechts)
2019-04-09 16:00:16.567 (rfxcom2) Lighting 2 (Tuinirrigatie_border_rechts)
2019-04-09 16:00:16.780 (rfxcom2) Lighting 2 (Tuinirrigatie_border_rechts)
2019-04-09 16:00:16.196 Status: dzVents: Info: Tuinirrigatie_borders: ------ Start internal script: DZ_Irrigatie: Device: "Tuinirrigatie_border_rechts (rfxcom2)", Index: 973
2019-04-09 16:00:16.205 Status: dzVents: Info: Tuinirrigatie_borders: Script: DZ_Irrigatie Device uitgezet....
2019-04-09 16:00:16.205 Status: dzVents: Info: Tuinirrigatie_borders: ------ Finished DZ_Irrigatie
2019-04-09 16:00:16.208 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2019-04-09 16:00:16.511 Status: dzVents: Info: Tuinirrigatie_borders: ------ Start internal script: DZ_Irrigatie: Device: "Tuinirrigatie_border_rechts (rfxcom2)", Index: 973
2019-04-09 16:00:16.520 Status: dzVents: Info: Tuinirrigatie_borders: Script: DZ_Irrigatie Device uitgezet....
2019-04-09 16:00:16.520 Status: dzVents: Info: Tuinirrigatie_borders: ------ Finished DZ_Irrigatie
2019-04-09 16:00:16.523 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2019-04-09 16:00:16.752 Status: dzVents: Info: Tuinirrigatie_borders: ------ Start internal script: DZ_Irrigatie: Device: "Tuinirrigatie_border_rechts (rfxcom2)", Index: 973
2019-04-09 16:00:16.760 Status: dzVents: Info: Tuinirrigatie_borders: Script: DZ_Irrigatie Device uitgezet....
2019-04-09 16:00:16.760 Status: dzVents: Info: Tuinirrigatie_borders: ------ Finished DZ_Irrigatie
2019-04-09 16:00:16.762 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2019-04-09 16:00:17.078 (rfxcom2) Lighting 2 (Tuinirrigatie_border_rechts)
2019-04-09 16:00:17.389 (rfxcom2) Lighting 2 (Tuinirrigatie_border_rechts)
2019-04-09 16:00:17.720 (rfxcom2) Lighting 2 (Tuinirrigatie_border_rechts)
2019-04-09 16:00:17.992 (rfxcom2) Lighting 2 (Tuinirrigatie_border_rechts)
2019-04-09 16:00:17.013 Status: dzVents: Info: Tuinirrigatie_borders: ------ Start internal script: DZ_Irrigatie: Device: "Tuinirrigatie_border_rechts (rfxcom2)", Index: 973
2019-04-09 16:00:17.024 Status: dzVents: Info: Tuinirrigatie_borders: Script: DZ_Irrigatie Device uitgezet....
2019-04-09 16:00:17.024 Status: dzVents: Info: Tuinirrigatie_borders: ------ Finished DZ_Irrigatie
2019-04-09 16:00:17.028 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2019-04-09 16:00:17.323 Status: dzVents: Info: Tuinirrigatie_borders: ------ Start internal script: DZ_Irrigatie: Device: "Tuinirrigatie_border_rechts (rfxcom2)", Index: 973
2019-04-09 16:00:17.334 Status: dzVents: Info: Tuinirrigatie_borders: Script: DZ_Irrigatie Device uitgezet....
2019-04-09 16:00:17.334 Status: dzVents: Info: Tuinirrigatie_borders: ------ Finished DZ_Irrigatie
2019-04-09 16:00:17.340 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2019-04-09 16:00:17.648 Status: dzVents: Info: Tuinirrigatie_borders: ------ Start internal script: DZ_Irrigatie: Device: "Tuinirrigatie_border_rechts (rfxcom2)", Index: 973
2019-04-09 16:00:17.669 Status: dzVents: Info: Tuinirrigatie_borders: Script: DZ_Irrigatie Device uitgezet....
2019-04-09 16:00:17.670 Status: dzVents: Info: Tuinirrigatie_borders: ------ Finished DZ_Irrigatie
2019-04-09 16:00:17.674 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2019-04-09 16:00:17.951 Status: dzVents: Info: Tuinirrigatie_borders: ------ Start internal script: DZ_Irrigatie: Device: "Tuinirrigatie_border_rechts (rfxcom2)", Index: 973
2019-04-09 16:00:17.959 Status: dzVents: Info: Tuinirrigatie_borders: Script: DZ_Irrigatie Device uitgezet....
2019-04-09 16:00:17.959 Status: dzVents: Info: Tuinirrigatie_borders: ------ Finished DZ_Irrigatie

return {
on = {devices = {'Tuinirrigatie_gazon','Tuinirrigatie_border_links','Tuinirrigatie_planten','Tuinirrigatie_border_rechts'}},

logging = {
level = domoticz.LOG_INFO,
marker = "Tuinirrigatie_borders",
},

execute = function(dz, device, info)
local waarde1 = dz.devices('Tuinirrigatie_gazon_waarde').level
local waarde2 = dz.devices('Tuinirrigatie_border_links_waarde').level
local waarde3 = dz.devices('Tuinirrigatie_planten_waarde').level
local waarde4 = dz.devices('Tuinirrigatie_border_rechts_waarde').level

if device.name == 'Tuinirrigatie_gazon' then
waarde = waarde1
elseif device.name == 'Tuinirrigatie_border_links' then
waarde = waarde2
elseif device.name == 'Tuinirrigatie_planten' then
waarde = waarde3
elseif device.name == 'Tuinirrigatie_border_rechts' then
waarde = waarde4
end

if device.state == 'Off' then
device.switchOff()
dz.log("Script: " .. info.scriptName .. " Device uitgezet....", dz.LOG_INFO)
else
device.switchOn().silent()
device.switchOff().afterMin(waarde).silent()

dz.log("Script: " .. info.scriptName .. " Device " .. device.name .. " voor " .. waarde .. " minuten aangezet....", dz.LOG_INFO)
end

end
}
this is my script
Last edited by pvklink on Tuesday 09 April 2019 17:23, edited 1 time in total.
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: script is keepon running

Post by pvklink »

I am trying some changes with the switch properties like forMin, silent and checkfirst


if device.state == 'Off' then
device.switchOff().silent()
dz.log("Script: " .. info.scriptName .. " Device uitgezet....", dz.LOG_INFO)
else
device.switchOn().checkFirst().forMin(waarde).silent()

dz.log("Script: " .. info.scriptName .. " Device " .. device.name .. " voor " .. waarde .. " minuten aangezet....", dz.LOG_INFO)
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: script is keepon running

Post by pvklink »

OK, the last change did not put the switch off after 10min. I remove checkfirst and then try again
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: script is keepon running

Post by pvklink »

Ok, i will try a solution with a home made timer
no errors, but the timer does not work! i need @waaren :lol:

Code: Select all

return {
    on = {devices = {'Tuinirrigatie_gazon','Tuinirrigatie_border_links','Tuinirrigatie_planten','Tuinirrigatie_border_rechts'}},

    logging = { 
                    level   = domoticz.LOG_INFO,
                    marker  = "Tuinirrigatie_borders",
              },
    data = { sensors              = { initial  = {} } },
 
    execute = function(dz, device, info)
    local now     = os.time(os.date('*t'))                      
    local waarde1 = dz.devices('Tuinirrigatie_gazon_waarde').level
    local waarde2 = dz.devices('Tuinirrigatie_border_links_waarde').level
    local waarde3 = dz.devices('Tuinirrigatie_planten_waarde').level
    local waarde4 = dz.devices('Tuinirrigatie_border_rechts_waarde').level

    if dz.data.sensors == nil then 
        dz.data.sensors = '0'
    end

    if device.name == 'Tuinirrigatie_gazon' then
        waarde = waarde1*60
    elseif device.name == 'Tuinirrigatie_border_links' then
        waarde = waarde2*60
    elseif device.name == 'Tuinirrigatie_planten' then
        waarde = waarde3*60
    elseif device.name == 'Tuinirrigatie_border_rechts' then
        waarde = waarde4*60
    end

    if device.state == 'Off' then
        device.switchOff().silent()
        dz.log("Script: " .. info.scriptName .. " Device uitgezet....", dz.LOG_INFO)    
        dz.data.sensors  = '0'
    else 

        if tonumber(dz.data.sensors) == 0 then                        
	        dz.data.sensors = tostring(now + waarde)
            dz.log("00 Notificatie.eind tijd............= " .. os.date("%X",dz.data.sensors))
            device.switchOn().checkFirst()
            dz.log("Script: " .. info.scriptName .. " Device " .. device.name .. " voor " .. waarde/60 .. " minuten aangezet....", dz.LOG_INFO)    
		end   

        if tonumber(now) < tonumber(dz.data.sensors) then 
                dz.log("Script: " .. info.scriptName .. " Device " .. device.name .. " timer: " .. waarde/60 .. " loopt....", dz.LOG_INFO)    
        else
            device.switchOff().checkFirst()
            dz.log("Script: " .. info.scriptName .. " Device " .. device.name .. " is na " .. waarde/60 .. " minuten uitgezet....", dz.LOG_INFO)   
            dz.data.sensors  = '0'
        end
    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: script is keepon running

Post by waaren »

pvklink wrote: Tuesday 09 April 2019 18:03 Ok, i will try a solution with a home made timer
no errors, but the timer does not work!
Can you describe what you try to achieve ?

Code: Select all

    if device.state == 'Off' then
        device.switchOff().silent()
I do not understand why you do this.
If the the device is Off you send another Off command ?
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: script is keepon running

Post by pvklink »

OK, i have four garden switches which i can put on/off. Each garden switch also has a corresponding switch (with levels 10, 20, 30-60 minutes)
So when activate a garden switch it uses the data of the corresponding switch(with levels).

All this functioned well. Till i stop a switch before the .aftermin was finished! My system went crazy...

I created a new script (alternative for the aftermin function) but that did not work (thats for later to learn from it)
So i get back to the original script i include in this post.

I added: device.cancelQueuedCommands() after device.switchOff()

Now everything seems to work well !, i can even stop my switch with pending timers (aftermin) without problems.
Does my actions make sense ?

i added a screendump (part of my dashticz)

Code: Select all

return {
    on = {devices = {'Tuinirrigatie_gazon','Tuinirrigatie_border_links','Tuinirrigatie_planten','Tuinirrigatie_border_rechts'}},

    logging = { 
                    level   = domoticz.LOG_INFO,
                    marker  = "Tuinirrigatie_borders",
              },

    execute = function(dz, device, info)
    local now     = os.time(os.date('*t'))                      

    if device.name == 'Tuinirrigatie_gazon' then
        waarde = tonumber(dz.devices('Tuinirrigatie_gazon_waarde').level)
    elseif device.name == 'Tuinirrigatie_border_links' then
        waarde = tonumber(dz.devices('Tuinirrigatie_border_links_waarde').level)
    elseif device.name == 'Tuinirrigatie_planten' then
        waarde = tonumber(dz.devices('Tuinirrigatie_planten_waarde').level)
    elseif device.name == 'Tuinirrigatie_border_rechts' then
        waarde = tonumber(dz.devices('Tuinirrigatie_border_rechts_waarde').level)
    end

    if device.state == 'Off' then
        device.switchOff()
        device.cancelQueuedCommands()
        dz.log("Script: " .. info.scriptName .. " Device uitgezet....", dz.LOG_INFO)    
    else 
        device.switchOn().silent()
        device.switchOff().afterMin(waarde).silent()
        
        dz.log("Script: " .. info.scriptName .. " Device " .. device.name .. " voor " .. waarde .. " minuten aangezet....", dz.LOG_INFO)    
    end

end
}
Attachments
domo.png
domo.png (56.92 KiB) Viewed 821 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: script is keepon running

Post by waaren »

pvklink wrote: Tuesday 09 April 2019 19:45 OK, i have four garden switches which i can put on/off. Each garden switch also has a corresponding switch (with levels 10, 20, 30-60 minutes)
So when activate a garden switch it uses the data of the corresponding switch(with levels).
All this functioned well. Till i stop a switch before the .aftermin was finished! My system went crazy...
I created a new script (alternative for the aftermin function) but that did not work (thats for later to learn from it)
So i get back to the original script i include in this post.
I added: device.cancelQueuedCommands() after device.switchOff()
Now everything seems to work well !, i can even stop my switch with pending timers (aftermin) without problems.
OK. Understand now what you do but still puzzled about the need for the device.switchOff() command at line 23.
Can you put -- before the command and check if the script still behaves as expected ? I think it should !
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: script is keepon running

Post by pvklink »

Ok, and yes it still works!

I thought that when you use a script for example an on/off device, you actually have to put the on and off command for that device also in the script, and that the script takes over the complete functionality of the switch.
So, when i push OFF on one off the garden switches, it does not need an OFF command in the script to go OFF?
If that is true, why do i need an ON command at line 27?
I disabled line 27 also with -- and it still works..... :D

To get the timervalue for a device i use:

if device.name == 'Tuinirrigatie_gazon' then
waarde = tonumber(dz.devices('Tuinirrigatie_gazon_waarde').level)
elseif device.name == 'Tuinirrigatie_border_links' then
waarde = tonumber(dz.devices('Tuinirrigatie_border_links_waarde').level)
elseif device.name == 'Tuinirrigatie_planten' then
waarde = tonumber(dz.devices('Tuinirrigatie_planten_waarde').level)
elseif device.name == 'Tuinirrigatie_border_rechts' then
waarde = tonumber(dz.devices('Tuinirrigatie_border_rechts_waarde').level)
end

Is this a better solution:
waarde = tonumber(dz.devices(device.name .. '_waarde').level)

Script is getting smaller:

Code: Select all

return {
    on = {devices = {'Tuinirrigatie_gazon','Tuinirrigatie_border_links','Tuinirrigatie_planten','Tuinirrigatie_border_rechts'}},

    logging = { 
                    level   = domoticz.LOG_INFO,
                    marker  = "Tuinirrigatie",
              },

    execute = function(dz, device, info)
    local now     = os.time(os.date('*t'))                      

    waarde = tonumber(dz.devices(device.name .. '_waarde').level)

    if device.state == 'Off' then
        device.cancelQueuedCommands()
        dz.log("Script: " .. info.scriptName .. " Device " .. device.name .. " Uitgezet....", dz.LOG_INFO)    
    else 
        device.switchOff().afterMin(waarde).silent()
        dz.log("Script: " .. info.scriptName .. " Device " .. device.name .. " Aangezet, om:" .. os.date("%X", now) .. " voor " .. waarde .. " minuten....", dz.LOG_INFO)    
    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: script is keepon running

Post by waaren »

pvklink wrote: Wednesday 10 April 2019 8:13 I thought that when you use a script for example an on/off device, you actually have to put the on and off command for that device also in the script, and that the script takes over the complete functionality of the switch.
So, when i push OFF on one off the garden switches, it does not need an OFF command in the script to go OFF?
That is indeed not needed. In fact the script is triggered by the Off command which triggers (in domoticz terms) an "event"
Is this a better solution:
waarde = tonumber(dz.devices(device.name .. '_waarde').level)
"better" is subjective but yes. It makes your script easier to maintain because now you can add an new irrigation section in the on = part without modifying other parts in the script.
Script is getting smaller:
If you like small scripts...

Code: Select all

return {
    on = {devices = {'Tuinirrigatie_gazon','Tuinirrigatie_border_links','Tuinirrigatie_planten','Tuinirrigatie_border_rechts'}},

    logging = { 
                    level   = domoticz.LOG_INFO,
                    marker  = "Tuinirrigatie",
              },

    execute = function(dz, item, info)
  
        local waarde = dz.devices(item.name .. '_waarde').level or 0 -- when device is just created and not set, level is undefined 

        if item.active then
            item.cancelQueuedCommands()
            item.switchOff().afterMin(waarde).silent()
            dz.log("Script: " .. info.scriptName .. ": Device " .. item.name .. " Aangezet, om: " .. dz.time.rawTime .. " voor " .. waarde .. " minuten....", dz.LOG_INFO)    
        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: script is keepon running

Post by pvklink »

Yes, i do!
You latest version adress OFF and ON with the same commands ?

@waaren: Can you look at my alternative script without function .aftermin and using a timestamp as global.
I dont want to use this script, but want to learn why things work, and in my case often why it wont :lol:

Code: Select all

-- script does not work, 
--    does not turn off automatically after devtimer
--    second problem is that 4 devices uses the same endtime variable and they need separte timers i think because they can execute together

return {
    on = {devices = {'Tuinirrigatie_gazon','Tuinirrigatie_border_links','Tuinirrigatie_planten','Tuinirrigatie_border_rechts'}},

    logging = { 
                    level   = domoticz.LOG_INFO,
                    marker  = "Tuinirrigatie",
              },
    data = { endtime = { initial  = {} } },
 
    execute = function(dz, device, info)
    local now     = os.time(os.date('*t'))                      

    if dz.data.endtime == nil then 
        dz.data.endtime = 0
    end

    devtimer = tonumber(dz.devices(device.name .. '_waarde').level) * 60  -- timer ophalen. timer heeft altijd de naam van het device met _waarde erachter

    if device.state == 'Off' then
        device.cancelQueuedCommands()
        dz.log("Script: " .. info.scriptName .. " Device " .. device.name .. " Uitgezet....", dz.LOG_INFO)    
        dz.data.endtime  = 0
    else 
        if tonumber(dz.data.endtime) == 0 then                        
	        dz.data.endtime = now + devtimer
            dz.log("00 Notificatie.eind tijd............= " .. os.date("%X",dz.data.sensors))
            dz.log("Script: " .. info.scriptName .. " Device " .. device.name .. " voor " .. devtimer/60 .. " minuten aangezet....", dz.LOG_INFO)    
		end   

        if tonumber(now) < tonumber(dz.data.endtime) then 
                dz.log("Script: " .. info.scriptName .. " Device " .. device.name .. " timer: " .. devtimer/60 .. " loopt....", dz.LOG_INFO)    
        else
            device.switchOff().silent()
            dz.log("Script: " .. info.scriptName .. " Device " .. device.name .. " is na " .. devtimer/60 .. " minuten uitgezet....", dz.LOG_INFO)   
            dz.data.endtime  = 0
        end
    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: script is keepon running

Post by waaren »

pvklink wrote: Wednesday 10 April 2019 10:05 You latest version adress OFF and ON with the same commands ?
Yes. The 'Off' event is just ignored by the script. No action is necessary.
Can you look at my alternative script without function .aftermin and using a timestamp as global.
I dont want to use this script, but want to learn why things work.
Will have a look but can take some time.
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: script is keepon running

Post by pvklink »

No problem, watering my garden works perfect!
Even with my self made rpi google home i can give voice commands to water sections of my garden
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: script is keepon running

Post by waaren »

pvklink wrote: Wednesday 10 April 2019 10:51 No problem, watering my garden works perfect!
Even with my self made rpi google home i can give voice commands to water sections of my garden
Had a look at your alternative but trying to get this to work without afterXXX requires at least another activation of the script by means of an
on = timer.
Furthermore; you do initialize endtime as a table but overwrite that with an integer. My preference would still be to use the afterXXX approach but below script will also work.

Code: Select all

return {
			on = 	{	devices = {'Tuinirrigatie_*'},
						timer 	= {'every minute'},
					},

    logging = { 
                    level   = domoticz.LOG_DEBUG,
                    marker  = "Tuinirrigatie",
              },

	data = { irrigates = { initial  = {} } },  -- initialize dz.data.irrigates as an empty table
 
    execute = function(dz, item)

        if item.isDevice and item.state == "On" and item.levelNames == nil then  -- ignore changes to selector devices
			local oppositeState = item.state == "Off" and "On" or "Off"          -- when state = On oppositeState = Off and vice versa
			if not dz.data.irrigates[item.name] then 							 -- tables not yet initialized
				dz.data.irrigates[item.name] = {} 								 -- initialize dz.data.irrigates[item.name] as an empty table
				dz.data.irrigates[item.name].On = {}
				dz.data.irrigates[item.name].Off = {}
			end	
			dz.data.irrigates[item.name].On.raw = dz.time.raw  -- store current date / time string 
			dz.data.irrigates[item.name].Off.dDate = dz.time.dDate +  dz.devices(item.name .. "_waarde").level -- store ( current date / time + delay) as seconds since EPOCH
        elseif item.isTimer then 
			local Time = require('Time')
			for switch, value in pairs(dz.data.irrigates) do                      -- All switches
				for state, value in pairs(dz.data.irrigates[switch]) do           -- Both states
					dz.log( switch .. " was set to " .. state .. " " ..
							dz.time.compare(Time(dz.data.irrigates[switch][state].raw)).seconds .. 
							" seconds ago",dz.LOG_DEBUG)
					if dz.data.irrigates[switch].Off.dDate < dz.time.dDate then   -- set time passed ? 
						if dz.devices(switch).state == "On" then
							dz.devices(switch).switchOff().silent()
							dz.data.irrigates[switch].Off.raw = dz.time.raw
						end
					end
				end
			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
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: script is keepon running

Post by pvklink »

@waaren thanks!, And yes i will use the .afterXXX approach!
This is more for learning

The irrigation is working perfect! I am now starting with watersensors in case it is raining, the irrigation dont start/stops
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
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest