esp switch+relay <-> domoticz no stop!

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
hestia
Normal user
Posts: 90
Joined: 06 Mar 2021, 08:27
Location: Paris

esp switch+relay <-> domoticz no stop!

#1 Post by hestia » 20 Mar 2021, 14:24

Hello dear community
My first post… I new in this forum, new with espeasy, new with esp…
I’d like to use a Sonoff 4CHR3 to open and close 4 valves, driven by Domoticz.
So far I think I’ve succeeded in flashing the Sonoff. To be checked and for the record (for other beginners like me, because it was difficult for me to find information that are obvious for most of the community), I did:
what is described at the beginning of this video https://notenoughtech.com/sonoff/flashi ... usel-23132
with info how to put the FTD1232 (USB to TTL Serial Adapter) on 3.3V (and not 5V) with the jumper https://wombat3.kozo.ch/j/smarthome/985 ... -operation
I took this release mega-20210223 https://github.com/letscontrolit/ESPEas ... a-20210223
and the normal esp8285 1M bin
Screenshot 2021-03-20 101026.png
Screenshot 2021-03-20 101026.png (144.38 KiB) Viewed 6577 times
Did I took the right version, it seems to be pre-release, is there a release?
I didn’t succeeded with the postflash action, but in the end I could connect the Sonoff 4CHR3

I did 4 buttons and 4 relays like this
Screenshot 2021-03-20 101407.png
Screenshot 2021-03-20 101407.png (134.92 KiB) Viewed 6577 times
And rules like this

Code: Select all

on Button1#State do
  if [Relay1#State]=0
    gpio,12,1
  else
    gpio,12,0
endif
endon
Some relays were on (up?) at the start the sonoff so I put this in the boot

Code: Select all

on System#Boot do
   gpio,12,0
   gpio,5,0
   gpio,4,0
   gpio,15,0
endon
But after this, some buttons seemed to be activated at the boot (even though I don’t even have rules on them!)
So I added a flag to avoid it

Code: Select all

on System#Boot do
   Let, 1, 0
   Let, 2, 0
   Let, 3, 0
   Let, 4, 0
   gpio,12,0
   gpio,5,0
   gpio,4,0
   gpio,15,0
Endon

Code: Select all

on Button1#State do
LogEntry,'*** Button1 ? ***'
if [int#1]!=0
LogEntry,'*** Button1 OK ***'
  if [Relay1#State]=0
    gpio,12,1
  else
    gpio,12,0
  endif
endif
Let, 1, 1
Endon
Is it normal that gpio went to 1 at the boot? (I read, it is for gpio 0, but the others)
What is the best practice to handle this?
After I added a communication with Domoticz with mqtt to update user variables like this

Code: Select all

on Button1#State do
LogEntry,'*** Button1 ? ***'
if [int#1]!=0 // gpio 0 is at 0 on boot
LogEntry,'*** Button1 OK ***'
  if [Relay1#State]=0
    gpio,12,1
    Publish domoticz/in,'{"command": "setuservariable", "idx": 13, "value": 30 }'
  else
    gpio,12,0
    Publish domoticz/in,'{"command": "setuservariable", "idx": 13, "value": 0 }'
  endif
endif
Let, 1, 1
Endon
(the value is the time to let the valves open, to add rules after...)
This part seemed to be ok
With the other direction troubles arrived :-(
I used the Generic - MQTT Import to receive value from Domoticz
Screenshot 2021-03-20 102821.png
Screenshot 2021-03-20 102821.png (46.6 KiB) Viewed 6577 times
And a script in Domoticz triggered by the user variable update

Code: Select all

   on =        {
                variables = VARIABLES
  	            },
[…]
mosquitto_pub -t domoticz/out/RSDz1 -m 77
At that point the update back in the sonoff is done but Button1 is triggered in the Sonoff and also all other Buttons !
Log extract

Code: Select all

24310: inputswitchstate is deprecatedinputSwitchState,4,0.00
24319: inputswitchstate is deprecatedinputSwitchState,5,0.00
24327: inputswitchstate is deprecatedinputSwitchState,6,0.00
24337: inputswitchstate is deprecatedinputSwitchState,7,0.00
24364: IMPT : [RelaysSecondsInDz#RelaySecondsInDz1] : 77.00
24371: EVENT: Button1#State=0
24378: ACT : LogEntry,'*** Button1 ? ***'
24380: *** Button1 ? ***
24389: ACT : LogEntry,'*** Button1 OK ***'
24391: *** Button1 OK ***
24419: ACT : gpio,12,0
24422: GPIO : port#12: set to 0
24429: ACT : Publish domoticz/in,'{'command': 'setuservariable', 'idx': 13, 'value': 0 }'
24448: ACT : Let, 1, 1
24678: EVENT: Button2#State=0
25406: *** Button4 OK ***
25423: ACT : gpio,15,1
25425: GPIO : port#15: set to 1
25431: ACT : Publish domoticz/in,'{'command': 'setuservariable', 'idx': 16, 'value': 30 }'
25463: ACT : Let, 4, 1
What I don’t understand is why something is triggered when "MQTT import" receives something because there are no rules on it.
After the system never stops and all relays are switched on and off ; I had to stop the script on Domoticz to stop it!
What is wrong in my understanding?
What are the best practice to avoid this?
Last edited by hestia on 22 Mar 2021, 00:30, edited 1 time in total.

hestia
Normal user
Posts: 90
Joined: 06 Mar 2021, 08:27
Location: Paris

esp switch+relay <-> domoticz no stop!

#2 Post by hestia » 22 Mar 2021, 00:18

Hi,
I did the same test with a Wemos, and same issue
What is strange is that it is the same even though the Generic - MQTT Import is disabled
wemos.png
wemos.png (155.87 KiB) Viewed 6560 times
and more strange (for me) also when the Generic - MQTT Import is deleted

Code: Select all

240500 : Info   : SW  : GPIO=13 State=0 Output value=0
240512 : Info   : EVENT: Button1#State=0
240523 : Info   : ACT  : LogEntry,'*** Button1 OK ***'
240526 : Info   : *** Button1 OK ***
240543 : Info   : ACT  : gpio,5,1
240546 : Info   : GPIO : port#5: set to 1
240553 : Info   : ACT  : Publish domoticz/in,'{"command": "setuservariable", "idx": 13, "value": 30 }'
240582 : Info   : ACT  : LogEntry,'*** Relay1 0 -> 1 ***'
240584 : Info   : *** Relay1 0 -> 1 ***
240604 : Info   : ACT  : Let,1,1
240731 : Info   : SW  : GPIO=5 State=1 Output value=1
240743 : Info   : EVENT: Relay1#State=1
240999 : Error  : inputswitchstate is deprecatedinputSwitchState,0,0.00
241010 : Error  : inputswitchstate is deprecatedinputSwitchState,1,0.00
241022 : Error  : inputswitchstate is deprecatedinputSwitchState,2,0.00
241034 : Error  : inputswitchstate is deprecatedinputSwitchState,3,0.00
241046 : Error  : inputswitchstate is deprecatedinputSwitchState,4,0.00
241057 : Error  : inputswitchstate is deprecatedinputSwitchState,5,0.00
241068 : Error  : inputswitchstate is deprecatedinputSwitchState,6,0.00
241079 : Error  : inputswitchstate is deprecatedinputSwitchState,7,0.00
241091 : Info   : EVENT: Button1#State=0
241102 : Info   : ACT  : LogEntry,'*** Button1 OK ***'
241105 : Info   : *** Button1 OK ***
241138 : Info   : ACT  : gpio,5,0
241141 : Info   : GPIO : port#5: set to 0
241149 : Info   : ACT  : Publish domoticz/in,'{"command": "setuservariable", "idx": 13, "value": 0 }'
241179 : Info   : ACT  : LogEntry,'*** Relay1 1 -> 0 ***'
241181 : Info   : *** Relay1 1 -> 0 ***
241188 : Info   : ACT  : Let,1,1
241308 : Info   : SW  : GPIO=5 State=0 Output value=0
241319 : Info   : EVENT: Button2#State=1
241358 : Info   : ACT  : LogEntry,'*** Button2 OK ***'
241361 : Info   : *** Button2 OK ***
241391 : Info   : ACT  : gpio,16,0
241394 : Info   : GPIO : port#16: set to 0
241400 : Info   : ACT  : Publish domoticz/in,'{"command": "setuservariable", "idx": 14, "value": 0 }'
241430 : Info   : ACT  : LogEntry,'*** Relay2 1 -> 0 ***'
241432 : Info   : *** Relay2 1 -> 0 ***
241439 : Info   : ACT  : Let,2,1
241528 : Info   : SW  : GPIO=16 State=0 Output value=0
241538 : Info   : EVENT: Button3#State=1
241609 : Info   : ACT  : LogEntry,'*** Button3 OK ***'
241612 : Info   : *** Button3 OK ***
241643 : Info   : ACT  : gpio,12,0
241645 : Info   : GPIO : port#12: set to 0
241651 : Info   : ACT  : Publish domoticz/in,'{"command": "setuservariable", "idx": 15, "value": 0 }'
241681 : Info   : ACT  : LogEntry,'*** Relay3 1 -> 0 ***'
241683 : Info   : *** Relay3 1 -> 0 ***
241691 : Info   : ACT  : Let,3,1
241752 : Info   : SW  : GPIO=12 State=0 Output value=0
241762 : Info   : EVENT: Button4#State=1
241857 : Info   : ACT  : LogEntry,'*** Button4 OK ***'
241862 : Info   : *** Button4 OK ***
241888 : Info   : ACT  : gpio,0,0
241891 : Info   : GPIO : port#0: set to 0
241895 : Info   : ACT  : LogEntry,'*** Relay4 1 -> 0 ***'
241897 : Info   : *** Relay4 1 -> 0 ***
241904 : Info   : ACT  : Let,4,1
241947 : Info   : SW  : GPIO=0 State=0 Output value=0
241957 : Info   : EVENT: Relay1#State=1
242105 : Info   : EVENT: Relay2#State=1
242255 : Info   : EVENT: Relay3#State=1
242401 : Info   : EVENT: Relay4#State=1
242541 : Info   : EVENT: Relay1#State=0
242679 : Info   : WD   : Uptime 4 ConnectFailures 0 FreeMem 19248 WiFiStatus WL_CONNECTED ESPeasy internal wifi status: Conn. IP Init
242691 : Info   : EVENT: Relay2#State=0
242828 : Error  : inputswitchstate is deprecatedinputSwitchState,0,0.00
242839 : Error  : inputswitchstate is deprecatedinputSwitchState,1,0.00
242851 : Error  : inputswitchstate is deprecatedinputSwitchState,2,0.00
242862 : Error  : inputswitchstate is deprecatedinputSwitchState,3,0.00
242873 : Error  : inputswitchstate is deprecatedinputSwitchState,4,0.00
242885 : Error  : inputswitchstate is deprecatedinputSwitchState,5,0.00
242897 : Error  : inputswitchstate is deprecatedinputSwitchState,6,0.00
242908 : Error  : inputswitchstate is deprecatedinputSwitchState,7,0.00
One more test, it is the same with 1 switch and 1 relay !

Code: Select all

20209 : Info   : SW  : GPIO=13 State=0 Output value=0
20219 : Info   : EVENT: Button1#State=0
20232 : Info   : ACT  : LogEntry,'*** Button1 OK ***'
20240 : Info   : *** Button1 OK ***
20261 : Info   : ACT  : gpio,5,1
20263 : Info   : GPIO : port#5: set to 1
20270 : Info   : ACT  : Publish domoticz/in,'{"command": "setuservariable", "idx": 13, "value": 30 }'
20302 : Info   : ACT  : LogEntry,'*** Relay1 0 -> 1 ***'
20305 : Info   : *** Relay1 0 -> 1 ***
20325 : Info   : ACT  : Let,1,1
20451 : Info   : SW  : GPIO=5 State=1 Output value=1
20460 : Info   : EVENT: Relay1#State=1
32408 : Info   : WD   : Uptime 1 ConnectFailures 0 FreeMem 19960 WiFiStatus WL_CONNECTED ESPeasy internal wifi status: Conn. IP Init
36010 : Info   : SW  : GPIO=13 State=0 Output value=1
36019 : Info   : EVENT: Button1#State=1
36030 : Info   : ACT  : LogEntry,'*** Button1 OK ***'
36032 : Info   : *** Button1 OK ***
36063 : Info   : ACT  : gpio,5,0
36066 : Info   : GPIO : port#5: set to 0
36072 : Info   : ACT  : Publish domoticz/in,'{"command": "setuservariable", "idx": 13, "value": 0 }'
36102 : Info   : ACT  : LogEntry,'*** Relay1 1 -> 0 ***'
36104 : Info   : *** Relay1 1 -> 0 ***
36111 : Info   : ACT  : Let,1,1
36230 : Info   : SW  : GPIO=5 State=0 Output value=0
36239 : Info   : EVENT: Relay1#State=0
36762 : Error  : inputswitchstate is deprecatedinputSwitchState,0,0.00
36774 : Error  : inputswitchstate is deprecatedinputSwitchState,4,0.00
36811 : Info   : EVENT: Button1#State=1
36823 : Info   : ACT  : LogEntry,'*** Button1 OK ***'
36825 : Info   : *** Button1 OK ***
36840 : Info   : ACT  : gpio,5,1
36843 : Info   : GPIO : port#5: set to 1
36851 : Info   : ACT  : Publish domoticz/in,'{"command": "setuservariable", "idx": 13, "value": 30 }'
36881 : Info   : ACT  : LogEntry,'*** Relay1 0 -> 1 ***'
36883 : Info   : *** Relay1 0 -> 1 ***
36904 : Info   : ACT  : Let,1,1
37028 : Info   : SW  : GPIO=5 State=1 Output value=1
37038 : Info   : EVENT: Relay1#State=0
37181 : Info   : EVENT: Relay1#State=1
37549 : Error  : inputswitchstate is deprecatedinputSwitchState,0,0.00
37560 : Error  : inputswitchstate is deprecatedinputSwitchState,4,0.00
I'll change the title if possible

User avatar
Ath
Normal user
Posts: 3417
Joined: 10 Jun 2018, 12:06
Location: NL

Re: esp switch+relay <-> domoticz no stop!

#3 Post by Ath » 22 Mar 2021, 08:08

If you are using push buttons on the default Switch settings, then please be aware that the event you handle here: (and: please share your Switch settings)

Code: Select all

on Button1#State do
  if [Relay1#State]=0
    gpio,12,1
  else
    gpio,12,0
  endif
endon
is fired twice, once when depressing the switch, and again when releasing the switch.
When using a 'normal' switch this of course shouldn't happen (though it may if you don't use any de-bouncing).
To avoid 'double-firing' an event you could change that to:

Code: Select all

on Button1#State=0 do
And simplifying the IF would end you up with:

Code: Select all

on Button1#State=0 do
  gpio,12,=![Relay1#State] // Invert relay state
endon
(I put in the = to avoid confusion when reading the log as it completes the expression parsing)
/Ton (PayPal.me)

hestia
Normal user
Posts: 90
Joined: 06 Mar 2021, 08:27
Location: Paris

Re: esp switch+relay <-> domoticz no stop!

#4 Post by hestia » 22 Mar 2021, 09:08

Thanks Ath for your reply
my switch is Push Button Active Low
Screenshot 2021-03-22 090355.png
Screenshot 2021-03-22 090355.png (37.75 KiB) Viewed 6541 times
I've tried your suggestion,

Code: Select all

on Button1#State=0 do
, but with '=0' the switch works only once of 2 times, and it didn't solve the trigger back from domoticz

Code: Select all

34823 : Error  : inputswitchstate is deprecatedinputSwitchState,0,0.00
34834 : Error  : inputswitchstate is deprecatedinputSwitchState,4,0.00
I don't know why it is deprecated because it is not present in domoticz
As a workaround,, I added a flag with a timer, but I think it is dirty

Code: Select all

on Button1#State do 
  LogEntry,'*** Button1 ?? ***'
  if [int#1]!=0 and [int#5]!=0 // gpio 0 is at 0 on boot
    LogEntry,'*** Button1 OK ***'
    if [Relay1#State]=0
      gpio,5,1
      Publish domoticz/in,'{"command": "setuservariable", "idx": 13, "value": 30 }'
      LogEntry,'*** Relay1 0 -> 1 ***'
    else
      gpio,5,0
      Publish domoticz/in,'{"command": "setuservariable", "idx": 13, "value": 0 }'
      LogEntry,'*** Relay1 1 -> 0 ***'
    endif
    Let,5,0
    TimerSet,1,3
  endif
  Let,1,1
endon

on Rules#Timer=1 do
   Let,5,1
endon
I kept the test on the relay state because I need it to choose what to send to domoticz

User avatar
Ath
Normal user
Posts: 3417
Joined: 10 Jun 2018, 12:06
Location: NL

Re: esp switch+relay <-> domoticz no stop!

#5 Post by Ath » 22 Mar 2021, 09:21

hestia wrote: 22 Mar 2021, 09:08 I've tried your suggestion,

Code: Select all

on Button1#State=0 do
, but with '=0' the switch works only once of 2 times,
Ah, yes, for a push button config it does that.
And I see you've got the De-bounce covered.

There is a similar discussion going on in the Github issues area: https://github.com/letscontrolit/ESPEasy/issues/3544
You could monitor that to see what you can learn there, repeating the same information here doesn't seem helpful.
/Ton (PayPal.me)

hestia
Normal user
Posts: 90
Joined: 06 Mar 2021, 08:27
Location: Paris

Re: esp switch+relay <-> domoticz no stop!

#6 Post by hestia » 22 Mar 2021, 09:45

thanks
I'll read it better to night...
It is relative, but it is not the same "scope"
scope: switch on/off the light using wall switch and use domoticz as well to control the status of the light and switch on/off also the light from domoticz.
My scope is "send a value to a user variable in domoticz, which is not changed by domoticz and in reverse receiving another value"

hestia
Normal user
Posts: 90
Joined: 06 Mar 2021, 08:27
Location: Paris

Re: esp switch+relay <-> domoticz no stop!

#7 Post by hestia » 23 Mar 2021, 14:18

I've read the discussion going on in the Github issues area: https://github.com/letscontrolit/ESPEasy/issues/3544
and didn't find answer to my problem which is get data from domoticz with mqtt when having switch input (connected or not with domoticz) without interfere with thoses switches

hestia
Normal user
Posts: 90
Joined: 06 Mar 2021, 08:27
Location: Paris

Re: esp switch+relay <-> domoticz no stop!

#8 Post by hestia » 24 Mar 2021, 07:54

Hi again
I did it with an "asynevent"
domoticz:

Code: Select all

http://esptest/control?cmd=asyncevent,DzTimeSec1=90
esp:

Code: Select all

on DzTimeSec1 do
instead of "Generic - MQTT Import"
and I've used your tip for
simplifying the IF

Code: Select all

on Button1#State do
if [int#1]!=0
    gpio,5,=![Relay1#State]
    Let, 5, [Relay1#State]-1
    Publish domoticz/in,'{"command": "setuservariable", "idx": 13, "value": [VAR#5]}'
    TaskValueSet 9,1,[VAR#5]
    Let, 5,abs([VAR#5])*60
    TimerSet,1,[VAR#5]
  endif
  Let,1,1
endon

on DzTimeSec1 do
  TaskValueSet 9,1,%eventvalue%
  if [Relay1#State]=0 and %eventvalue% !=0
    Publish domoticz/in,'{"command": "setuservariable", "idx": 13, "value": %eventvalue%}'
    gpio,5,1
    TimerSet,1,60
  endif
  if [Relay1#State]=1 and %eventvalue% =0
    gpio,5,0
    TimerSet,1,0
  endif
endon 
Another question, even it is not directly relative to this topic...
As I have 3 switches / relays, is there a way to avoid to triplicate the previous code?
Screenshot 2021-03-24 075137.png
Screenshot 2021-03-24 075137.png (129.01 KiB) Viewed 6398 times

User avatar
Ath
Normal user
Posts: 3417
Joined: 10 Jun 2018, 12:06
Location: NL

Re: esp switch+relay <-> domoticz no stop!

#9 Post by Ath » 24 Mar 2021, 08:26

hestia wrote: 24 Mar 2021, 07:54 Another question, even it is not directly relative to this topic...
As I have 3 switches / relays, is there a way to avoid to triplicate the previous code?
As you have seen, you can use eventhandlers as 'subroutines', by using the 'AsyncEvent' (or 'Event' but that's not advised anymore) command. This command can accept up to 4 arguments, %eventvalue1% through %eventvalue4%.

You do have a couple of variable parameters, the GPIO pin, Relay number and Domoticz IDX value, and the rules allow constructs like [VAR#%eventvalue1%] (untested for now, it is known to work with [VAR#%v50%], so might need to assign %eventvalue1% to a variable first), so combining all that you can then use code like this:

Code: Select all

on Button1#State do
  if [int#1]!=0
    AsyncEvent,handleButton=5,1,13
  endif
  Let,1,1
endon

on Button2#State do
  if [int#2]!=0
    AsyncEvent,handleButton=16,2,14 // IDX was guessed
  endif
  Let,2,1
endon

// Usage: AsyncEvent,handleButton=<gpio>,<relay>,<idx> // Fill in the correct values, uses timers 1..4 !
on HandleButton do
    gpio,%eventvalue1%,=![Relay%eventvalue2%#State]
    Let, %eventvalue1%, [Relay%eventvalue2%#State]-1
    Publish domoticz/in,'{"command": "setuservariable", "idx": %eventvalue3%, "value": [VAR#%eventvalue1%]}'
    TaskValueSet 9,%eventvalue2%,[VAR#%eventvalue1%] // Supports max. 4 relays!
    Let, %eventvalue1%,abs([VAR#%eventvalue1%])*60
    TimerSet,%eventvalue2%,[VAR#%eventvalue1%]
endon

on Rules#Timer=1 do
// Turn off GPIO?
endon
NB: All 'air-code' (untested)
/Ton (PayPal.me)

hestia
Normal user
Posts: 90
Joined: 06 Mar 2021, 08:27
Location: Paris

Re: esp switch+relay <-> domoticz no stop!

#10 Post by hestia » 25 Mar 2021, 20:12

eventhandlers as 'subroutines', by using the 'AsyncEvent'
I did some testing and it is clear now
Thanks Ath :-)

User avatar
Ath
Normal user
Posts: 3417
Joined: 10 Jun 2018, 12:06
Location: NL

Re: esp switch+relay <-> domoticz no stop!

#11 Post by Ath » 25 Mar 2021, 21:25

You're welcome :)
/Ton (PayPal.me)

Post Reply

Who is online

Users browsing this forum: No registered users and 31 guests