device enables after being shutdown  [Solved]

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

Moderator: leecollings

Post Reply
broker
Posts: 19
Joined: Friday 08 January 2016 14:57
Target OS: Linux
Domoticz version: 2020.2
Contact:

device enables after being shutdown

Post by broker »

Hello,

I have created a script to automatically turn on my waterpump when one of the watering zones has been enabled.
next to that it will run for a certain period as defined as "sproeitijd"

the functions are, enable to pump alone, no zone
enable a zone -> pump needs to enable too
extra zone -> pump needs to continue
disable zone -> check if other zone is still enabled -> if yes, leave pump on (do not reset timer)
if no, turn pump off

so, wha tis the issue, if the zone or pump is disabled manually, and not by timer, the pump will enable after as it seems the timer has ended.
it looks and I could be wrong that there is a state change and that is picked up and interpreted that the system needs to do somehting.

can some one confirm this, and help how to cercomvent this?

Code: Select all

return {
	on = {
		devices = {
			'Waterpomp',
			'Sproeier Voor',
			'Sproeier Achter',
			'Druppelslang'
		},
	},
	data = {
	    voor_aan    = {initial='false'},
	    achter_aan  = {initial='false'},
	    druppel_aan = {initial='false'}
	},
    
    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'Waterpomp',
    },
	
	execute = function(domoticz, device)
	    --declaratie van devices
	    local pomp          = domoticz.devices('Waterpomp')
	    local sachter       = domoticz.devices('Sproeier Achter')
	    local svoor         = domoticz.devices('Sproeier Voor')
	    local druppel       = domoticz.devices('Druppelslang')
	    --Hoelang moet er gesproeid worden
	    local sproeitijd    = 60

	    --help functie
	    local function schakelPomp(pomp,sproeitijd)
	        if pomp.active == false
	           then 
	               pomp.switchOn().forMin(sproeitijd-1)
	               print ("Pomp gaat aan via functie")
	       elseif pomp.active == true and domoticz.data.voor_aan == 'false' and domoticz.data.achter_aan == 'false' and domoticz.data.druppel_aan == 'false'
	           then 
	                pomp.switchOff()
	                print("pomp gaat uit via functie")
            end
            return self --om te testen dat het is gelukt
        end
        
	    --pomp alleen aan, let op de druk van de pomp!
	    if 
	        device.name == 'Waterpomp' and domoticz.data.voor_aan == 'false' and domoticz.data.achter_aan == 'false' and domoticz.data.druppel_aan == 'false'
    	    then  
    	        device.switchOff().afterMin(sproeitijd)
    	       print("pomp aan via schakelaar")
        --Sproeier in voortuin aan
        elseif 
            device.name == 'Sproeier Voor'
            then
                if device.active
                    then
                        svoor.switchOff().afterMin(sproeitijd)
                        domoticz.data.voor_aan = 'true'
                        print('Voor aan')
                        schakelPomp(pomp,sproeitijd)
                        
                    else
                        domoticz.data.voor_aan = 'false'
                        schakelPomp(pomp,sproeitijd)
                        print("Voor uit")
                end
        --Sproeier in achtertuin aan        
        elseif 
            device.name == 'Sproeier Achter'
	        then 
	            if device.active
	                then
                        sachter.switchOff().afterMin(sproeitijd)
                        domoticz.data.achter_aan = 'true'
                        schakelPomp(pomp,sproeitijd)
                        print("Achter aan")
                    else
                        domoticz.data.achter_aan = 'false'
                        schakelPomp(pomp,sproeitijd)
                        print("achter uit")
                end
        --Druppelslang voor en achter
        elseif 
            device.name == 'Druppelslang'
            then 
                if device.active
                    then
                        druppel.switchOff().afterMin(sproeitijd)
                        domoticz.data.druppel_aan = 'true'
                        schakelPomp(pomp,sproeitijd)
                        print("Druppel aan")
                    else
                        domoticz.data.druppel_aan = 'false'
                        schakelPomp(pomp,sproeitijd)
                        print("Druppel uit")
                    end
                end

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

Re: device enables after being shutdown

Post by waaren »

broker wrote: Thursday 01 April 2021 17:05 can some one confirm this, and help how to circumvent this?
I have to admit I have not fully studied the script and your description but before doing that maybe you can try and change the

Code: Select all

pomp.switchOn().forMin(sproeitijd-1)
to

Code: Select all

pomp.switchOn()
pomp.switchOff().afterMin(sproeitijd - 1)
and test again ?
I am not a big fan of forMin() because of the unpredictability. (Described in the wiki as:
Important note when using forAAA(): Let’s say you have a light that is triggered by a motion detector. Currently the light is Off and you do this: light.switchOn().forMin(5). What happens inside Domoticz is this: at t0 Domoticz issues the switchOn() command and schedules a command to restore the current state at t5 which is Off. So at t5 it will switch the light off.

If, however, before the scheduled switchOff() happens at t5, new motion is detected and you send this command again at t2 then something unpredictable may seem to happen: the light is never turned off! This is what happens:

At t2 Domoticz receives the switchOn().forMin(5) command again. It sees a scheduled command at t5 and deletes that command (it is within the new interval). Then Domoticz performs the (unnecessary, it’s already on) switchOn() command. Then it checks the current state of the light which is On!! and schedules a command to return to that state at t2+5=t7. So, at t7 the light is switched on again. And there you have it: the light is not switched off and never will be because future commands will always be checked against the current on-state.

That’s just how it works and you will have to deal with it in your script. So, instead of simply re-issuing switchOn().forMin(5) you have to check the switch’s state first:

Code: Select all

if (light.active) then
    light.switchOff().afterMin(5)
else
    light.switchOn().forMin(5)
end 
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
broker
Posts: 19
Joined: Friday 08 January 2016 14:57
Target OS: Linux
Domoticz version: 2020.2
Contact:

Re: device enables after being shutdown

Post by broker »

Thanks Waaren, I just made the change in the code, i'll test it tomorrow.
broker
Posts: 19
Joined: Friday 08 January 2016 14:57
Target OS: Linux
Domoticz version: 2020.2
Contact:

Re: device enables after being shutdown

Post by broker »

it seems that the script will break at the moment I make that change, when I enable one of the watering groups the pump will not enable.

so, after this trail I analyzed the debug, and what I see is that in the scenario of forMin(sproeitijd) it works, however when I use the:
pomp.switchOn()
pomp.switchOff().afterMin(sproeitijd)

I think the system sees the second one as a state change and will issue the script again, then it thinks that it has been toggled manually and will handle it as such (switch off the pump only)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: device enables after being shutdown

Post by waaren »

broker wrote: Friday 02 April 2021 10:05 it seems that the script will break at the moment I make that change, when I enable one of the watering groups the pump will not enable.

I think the system sees the second one as a state change and will issue the script again, then it thinks that it has been toggled manually and will handle it as such (switch off the pump only)
What if you make these actions silent (= no event will be triggered for this action)?

Code: Select all

pomp.switchOn().silent()
pomp.switchOff().afterMin(sproeitijd).silent()

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
broker
Posts: 19
Joined: Friday 08 January 2016 14:57
Target OS: Linux
Domoticz version: 2020.2
Contact:

Re: device enables after being shutdown

Post by broker »

waaren wrote: Friday 02 April 2021 11:14
broker wrote: Friday 02 April 2021 10:05 it seems that the script will break at the moment I make that change, when I enable one of the watering groups the pump will not enable.

I think the system sees the second one as a state change and will issue the script again, then it thinks that it has been toggled manually and will handle it as such (switch off the pump only)
What if you make these actions silent (= no event will be triggered for this action)?

Code: Select all

pomp.switchOn().silent()
pomp.switchOff().afterMin(sproeitijd).silent()

still not giving the result I would have expected, I actually though that would do the trick, here is the output in the log screen:

Code: Select all

2021-04-02 12:07:55.716 Status: dzVents: Info: Handling events for: "Druppelslang", value: "On"
2021-04-02 12:07:55.717 Status: dzVents: Info: Waterpomp: ------ Start internal script: pomp_lua: Device: "Druppelslang (Z-wave stick)", Index: 16
2021-04-02 12:07:55.719 Status: dzVents: Debug: Waterpomp: Processing device-adapter for Waterpomp: Switch device adapter
2021-04-02 12:07:55.721 Status: dzVents: Debug: Waterpomp: Processing device-adapter for Sproeier Achter: Switch device adapter
2021-04-02 12:07:55.722 Status: dzVents: Debug: Waterpomp: Processing device-adapter for Sproeier Voor: Switch device adapter
2021-04-02 12:07:55.724 Status: dzVents: Debug: Waterpomp: Constructed timed-command: Off
2021-04-02 12:07:55.725 Status: dzVents: Debug: Waterpomp: Constructed timed-command: Off AFTER 60 SECONDS
2021-04-02 12:07:55.726 Status: dzVents: Debug: Waterpomp: Constructed timed-command: On
2021-04-02 12:07:55.727 Status: dzVents: Debug: Waterpomp: Constructed timed-command: On NOTRIGGER
2021-04-02 12:07:55.728 Status: dzVents: Debug: Waterpomp: Constructed timed-command: Off
2021-04-02 12:07:55.729 Status: dzVents: Debug: Waterpomp: Constructed timed-command: Off AFTER 0 SECONDS
2021-04-02 12:07:55.730 Status: dzVents: Debug: Waterpomp: Constructed timed-command: Off AFTER 0 SECONDS NOTRIGGER
2021-04-02 12:07:55.731 Status: dzVents: Pomp gaat aan via functie
2021-04-02 12:07:55.732 Status: dzVents: Druppel aan
2021-04-02 12:07:55.733 Status: dzVents: Info: Waterpomp: ------ Finished pomp_lua
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: device enables after being shutdown

Post by waaren »

broker wrote: Friday 02 April 2021 12:11 still not giving the result I would have expected, I actually though that would do the trick, here is the output in the log screen:
Please ignore the double / triple Debug messages. Only the last constructed timed-commands per device are send trough to domoticz main program.
I don't see any next event scripts triggered so don't understand. What is now not working as expected?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
broker
Posts: 19
Joined: Friday 08 January 2016 14:57
Target OS: Linux
Domoticz version: 2020.2
Contact:

Re: device enables after being shutdown  [Solved]

Post by broker »

waaren wrote: Friday 02 April 2021 12:18 Please ignore the double / triple Debug messages. Only the last constructed timed-commands per device are send trough to domoticz main program.
I don't see any next event scripts triggered so don't understand. What is now not working as expected?
Yes, I found it, my mistake, as I was testing with a timer of 1 minute and the pump was set to run one minute less then the timer (sproeitijd - 1) the timer of the pump was 0. hence the pump showed the same behavoir as it did without the .silent() that triggered me that it was still not working.

it is working and indeed the .silent() did the trick.

Thanks!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest