Page 1 of 1

.switchOff() command switches switch on

Posted: Thursday 14 August 2025 12:45
by Gravityz
i have a dummy switch which i use to turn a an ESP relay ON or Off
i use the following on an off action commands

switchtype On/Off

On action:http://192.168.1.150/control?cmd=GPIO,5,1
Off action http://192.168.1.150/control?cmd=GPIO,50

this works and when clicking the switch in the gui the relay turns on and off

if i use the http command in a webexplorer(safari) it also works
even if i use the same command multiple times it only works once(eg 2 times off command still results in the relay being off)

now when i try to switch things from a dzvents script it also works BUT!!!!
it seems when i use the switchoff command while the switch is already off it turns on.
also when the switch is already on and i use the on command it switches off
so .switchOff() and .switchOn() on a dummy switch do not work like expected, it seems to toggle

here are the lines from my script
Brink Ventilator is the dummy switch with the action on/off commands
domoticz.devices('Brink Ventilator').switchOff()
domoticz.devices('Brink Ventilator').switchOn()

also adding checkfirst() does not help, the result is the same
if the switch is off and i use command domoticz.devices('Brink Ventilator').switchOff() it turns on

Re: .switchOff() command switches switch on

Posted: Thursday 14 August 2025 13:10
by waltervl
What version of Domoticz do you use? Your signature says 2022.2 so very old.
This should work...
domoticz.devices('Brink Ventilator').switchOff().checkFirst()
domoticz.devices('Brink Ventilator').switchOn().checkFirst()

Does the ESP sends its feedback back to Domoticz with API call?

Can you post a simple dzvents scripts that shows your issue?

Re: .switchOff() command switches switch on

Posted: Thursday 14 August 2025 18:53
by Gravityz
i am currently running 2025.1 build 16587
i already tried checkFirst() and that did not work either
so when dummy switch Brink Ventilator is switched on and the script kicks in the switch is turned off
however when dummy switch Brink Ventilator is switched Off and the script kicks in it is turned on.
it acts like a toggle

Code: Select all

return {
    active = true,
    on = {
        timer = {'on 01/05-09/09 at 09:30'},
 
    },
    execute = function(domoticz,event)
        if (event.isTimer) then
         domoticz.devices('Brink Ventilator').switchOff().checkFirst()
        end
end
}

Re: .switchOff() command switches switch on

Posted: Thursday 14 August 2025 19:08
by waltervl
Is there another device that is called Brink Ventilator? Dzvents cannot handle different devices with the same name.

Re: .switchOff() command switches switch on

Posted: Thursday 14 August 2025 20:00
by waltervl
Works fine for me.
I changed the script so it runs every minute.
So everytime I switch the switch On it will be switched off on the minute by the script. And no switching On by the script. You do not see switchlog entries on 19:51 and 19:52. So works as expected......
The only reason I can think off is a other device of the same name.
Schermafbeelding 2025-08-14 195433.png
Schermafbeelding 2025-08-14 195433.png (99.18 KiB) Viewed 854 times

Re: .switchOff() command switches switch on

Posted: Thursday 14 August 2025 20:05
by Gravityz
no, it is the only one with that name but you make me inspect the script again and i think i found the problem.
in the original script i have 2 triggers which only trigger at a specific timeframe.
however, once the script is triggered by the timer it does not check anymore what triggered the script and just execute the second if-then statement.

thanks for helping me out Walter and really stupid i did not see this earlier
one of the reasons i did not see it in the log it only recorded the on switch
if it showed off switch (while it was already off) and then an on switch i would have noticed earlier

Code: Select all

return {
    active = true,
    on = {
        timer = {'on 01/05-09/09 at 09:30'},
       devices = { ['Flair Aanvoer'] = {'on 01/05-09/09 at 21:15-06:00'} }
    
    },
    execute = function(domoticz,event)
        if (event.isTimer) then
         domoticz.devices('Brink Ventilator').switchOff()
        
        --zet Thermostaat-Current op UIT zodat de thermostaat in homekit dit herkent
        --domoticz.devices('Thermostaat Current').switchSelector(10)
        --domoticz.devices('Thermostaat-Target').switchSelector(10)
        end

    if ((domoticz.devices('Flair Aanvoer').temperature < domoticz.devices('Thermostaat Temperatuur').temperature) and domoticz.devices('Thermostaat Setpoint').temperature == 15) then
    domoticz.devices('Brink Ventilator').switchOn()
    
    --zet Thermostaat-Current op COOL zodat de thermostaat in homekit dit herkent
    --domoticz.devices('Thermostaat Current').switchSelector(20)
    --domoticz.devices('Thermostaat-Target').switchSelector(20)
    end

    end
}

Re: .switchOff() command switches switch on

Posted: Thursday 14 August 2025 20:11
by Gravityz
i added .isDevice to the check
hopefully this will solve it

if (event.isDevice) and ((domoticz.devices('Flair Aanvoer').temperature < domoticz.devices('Thermostaat Temperatuur').temperature) and domoticz.devices('Thermostaat Setpoint').temperature == 15) then
domoticz.devices('Brink Ventilator').switchOn()

Re: .switchOff() command switches switch on

Posted: Friday 15 August 2025 9:04
by azonneveld
On action:http://192.168.1.150/control?cmd=GPIO,5,1
Off action http://192.168.1.150/control?cmd=GPIO,50

this works and when clicking the switch in the gui the relay turns on and off
I think the off command should be: http://192.168.1.150/control?cmd=GPIO,5,0

Also, ".checkFirst()" is useless, the ESP does not returns any status by default (only if you programmed it yourself by using Rules, or using MQTT)

Re: .switchOff() command switches switch on

Posted: Friday 15 August 2025 9:25
by Gravityz
thanks. that was indeed a typo.

i switch the dummy switch(which uses the gpio command.
indeed the esp does not give any feedback but the dummy switch does.

i figur domoticz will check the status of the dummy switch right?

Re: .switchOff() command switches switch on

Posted: Friday 15 August 2025 9:30
by waltervl
Gravityz wrote: Friday 15 August 2025 9:25 i figur domoticz will check the status of the dummy switch right?
Yes, Domoticz checks the status of the Domoticz device....

Re: .switchOff() command switches switch on

Posted: Friday 15 August 2025 9:41
by Gravityz
tested and everything working.

Re: .switchOff() command switches switch on

Posted: Friday 15 August 2025 10:04
by waltervl
azonneveld wrote: Friday 15 August 2025 9:04
Also, ".checkFirst()" is useless, the ESP does not returns any status by default (only if you programmed it yourself by using Rules, or using MQTT)
Doing ".checkFirst()" makes perfectly sense if you want to prevent unnecessary switch commands by dzvents scripting.
And not sending back it's status by ESP should be a standard action to be implemented when activating the ESP device to connect to Domoticz.

Re: .switchOff() command switches switch on

Posted: Thursday 04 September 2025 12:13
by azonneveld
Gravityz wrote: Friday 15 August 2025 9:25 thanks. that was indeed a typo.

i switch the dummy switch(which uses the gpio command.
indeed the esp does not give any feedback but the dummy switch does.

i figur domoticz will check the status of the dummy switch right?
Correct, only the internal dummy switch is checked.
If for some reason the esp is rebooted, the GPIO resets to 0, the switch is not set again by domo due to the checkfirst() statement.

Re: .switchOff() command switches switch on

Posted: Tuesday 30 September 2025 21:01
by kimot
With ESPeasy I do it very simply with rules.

On the side of Domoticz traditional definition switch action:

Code: Select all

  
http://192.168.1.119/control?cmd=event,lamp_on
         or
http://192.168.1.119/control?cmd=event,lamp_off
On ESPeasy this rules:

Code: Select all

on lamp_on do
    gpio,12,1   // relay ON
    gpio,13,0  //  LED ON
endon

on lamp_off do
    gpio,12,0
    gpio,13,1
endon

on lamp_change do
  if [rele#Switch]=1        // if relay is on
    event,lamp_off           // switch it off
    SendToHTTP 192.168.1.253,8080,/json.htm?type=command&param=switchlight&idx=19&switchcmd=Off        // send actual status to Domoticz
  else
    event,lamp_on
    SendToHTTP 192.168.1.253,8080,/json.htm?type=command&param=switchlight&idx=19&switchcmd=On
  endif
endon

on SW01#Switch do          // if button pressed
  event,lamp_change        // change relay status
This ensures that I can still see the current status of the relay in Domoticz, whether it was turned on by a local switch or via Domoticz.

Re: .switchOff() command switches switch on

Posted: Wednesday 01 October 2025 18:21
by Gravityz
Nice solution. i do the same but have no switch in the esp, only a relay which is controlled by a switch in domoticz.

for me the right status of the relais is always domoticz.

when you reboot or switchoff the esp it looses it's status.

on esp reboot's i initiate a mqtt broadcast, domoticz is sending out the status of all switches.

i use the generic mqtt import function in the esp to filter out the status of the relay and set it again.

maybe there is an easier way to do it (checking the status of a domoticz switch from esp) but this is what i came up with a couple of years ago.

maybee the esp will survive a cold and hot boot now, not sure

Re: .switchOff() command switches switch on

Posted: Thursday 16 October 2025 23:17
by BartSr
I never tried but as far as I know you can select an esp device called output domoticz mqtt helper. Thats to synchronise domoticz device with switch on esp.
Maybe this might make sense for reporting from esp back to domoticz about esp status