arendst / Tasmota

Alternative firmware for ESP8266 and ESP32 based devices with easy configuration using webUI, OTA updates, automation using timers or rules, expandability and entirely local control over MQTT, HTTP, Serial or KNX. Full documentation at
https://tasmota.github.io/docs
GNU General Public License v3.0
22k stars 4.77k forks source link

[SOLVED] Rules - Inter-lock Sonoff 4CH Pro Channels #3204

Closed meingraham closed 6 years ago

meingraham commented 6 years ago

TL;DR

Help needed to set up a separate rule for each button on a 4CH Pro which when a button is pressed triggers the associated rule and then turns on or off a combination of the channel relays on the switch. I have not been able to figure out how to write the rules syntax to "see" the separate trigger events for each relay (PowerN) nor how to actually implement them in the switch.

The details...

I have several ceiling fans which I want to connect to my home automation hub (openHAB) and be able to control via the mobile app or via Google Assistant.

A Sonoff 4CH Pro is ideal for the four functions each of them has: the fan light and three fan speeds. I cannot use the 4CH Pro inter-locking mode (only one relay turns on at a time based on the button input) because some fans require more than one contact to be connected simultaneously for achieving some of the speed settings. Also, If I want to turn on the fan and also have the fan light on, the 4CH inter-locking mode would not allow those outputs on simultaneously. For these reasons, I am using the 4CH switches in self-locking mode... even when the particular brand of fan uses a single contact scheme for the fan speed.

Our ceiling fans are manufactured by three different companies: Hunter, Hampton Bay, & Harbor Breeze. These fans use two types of 4-position speed control switches and have different wiring for controlling the motor. The connections for different types of fans:

Hunter Low: Ch.1 Med: Ch.1 & Ch. 2 Hi: Ch.3 Light: Ch. 4

Harbor Breeze Low: Ch.1 Med: Ch. 2 Hi: Ch.3 Light: Ch. 4

I haven't cracked open my Hampton Bay wiring yet, but I'm sure the fan speed switch falls into one of the two categories - single-contact (e.g., Harbor Breeze) or multi-contact (e.g. Hunter).

I have the automation and inter-lock rules set up in openHAB with the appropriate rules to handle the different fan speed selections. Setting the fan speed via the openHAB mobile app or via Google Assistant openHAB integration allows me to set the channels explicitly and there is no fear of improperly setting the fan speed relays.

The reason I wanted to add rules in TASMOTA is that I am accounting for someone making a selection via the 4CH Pro itself (e.g., via an RF remote control) rather than via openHAB. I want to allow this because when we have guests, I don't expect them to know know the Google Assistant voice command "syntax" and they won't have access to openHAB via the GUI. With the RF remote, I've assigned each speed and the light to one of the four buttons on the remote.

I realize that the choice of what function the buttons perform is a personal preference. I am always frustrated by the pull-chain and never knowing how many pulls to make to figure out if I've selected the speed I want or if I've actually turned the fan off. Once the fan is spinning, it's sometimes difficult to know when the speed is changing to the desired speed or slowing to a stop altogether. That's why I decided to implement my automation buttons as a direct speed setting rather than having speed up or speed down buttons. Having both up and down speed control removes a bit of the hit or miss frustration... but I still prefer the direct setting of the speed (or off directly) as long as I have only four things to control and I have four buttons to do so. Again, personal preference.

The RF remote is essentially equivalent to pressing the buttons on the 4CH Pro itself. What happens is that it toggles the relay for the associated channel. That results in a status message being sent to openHAB which triggers the rule ensuring the appropriate inter-lock which results in turning on or off the speed as selected via the remote. For example: if fan is off and button C is pressed, set to high speed (ch. 3 - all done); if on high and button B is pressed, set to medium speed (ch.2 and ch. 3 are now on so turn off ch. 3 and turn on ch. 1 ); etc.

I have the automation functioning as desired. For whatever reason, it sometimes takes 3-4 seconds for openHAB to respond to the state change event and complete sending the appropriate commands to ensure the relays end up in the requested state for the speed requested on the remote control.

On the Hunter fan, I'm not as concerned because having multiple "wrong" relays on actually only sets a slightly different speed based on the "selection" of the capacitor circuitry. In the Hunter fan circuitry, this is actually allowed. I could even play with permutations of speeds beyond the normally available three. I'm not ;-) The speeds aren't that different and why add confusion; three speeds is all that is needed.

My real concern is on the Harbor Breeze fan where I don't have insights into how the windings and associated capacitors are wired. No amount of searching for the model number or even generic Harbor Breeze has resulted in a diagram or schematic. However, assuming that since the pull-chain switch does not turn on more than one circuit at a time, I want to ensure that I emulate it as much as possible. Having more than one circuit path completed/closed for 3-4 seconds is what concerns me. My intent with the TASMOTA rule in the switch itself will ensure near instantaneous switching of the relays to avoid having multiple relays latched for any significant amount of time.

I've investigated using ButtonTopic so that the button on the switch does not control the relay except by sending an MQTT to openHAB which then sends back the appropriate MQTT to actually set the relays. I am thoroughly baffled (even more so than rules) about how to set up TASMOTA to use ButtonTopic for this operating mode... plus, with using rules, the fan can operate "manually" even if openHAB is out of commission for any reason.

I would like to set up rules in the TASMOTA firmware to "prohibit" certain combinations of relays from being turned on simultaneously. The rules would vary slightly for each brand of fan connected. Hunter: inter-lock Ch.1, & Ch. 3 Harbor Breeze: inter-lock Ch.1, Ch.2, & Ch. 3

I have compiled the TASMOTA firmware uploaded to the 4CH with USE_RULES.

I have not been able to figure out how to write the rules syntax to "see" the separate trigger events for each relay (PowerN) nor how to actually implement them in the switch.

Thanks.

Mike

meingraham commented 6 years ago

I need a schematic of the internals of the fan (the internal speed capacitor circuits and how they are connected to the motor windings. The mains wiring is straightforward and most of these diagrams on the net are for that anyhow.

Regardless... I'm just want to add a failsafe via TASMOTA rules. But it doesn't seem anybody knows if it's possible to set up a 4CH in this manner nor how to do what I'm asking. Neither this post nor a similar one on the Sonoff Google Group have elicited any responses to my rules configuration question.

I am waiting to receive four new 4CH Pro switches. Once I get them I'll flash one of them and start trying this by brute force. Good luck to me :(

Mike

meingraham commented 6 years ago

Success! Fortunately, I only need to inter-lock three channels... for which there are just enough TASMOTA rule "slots" available. Each rule uses 241 characters of the available 511.

Here are the three rules. Each rule is a variation of the toggling rule in the TASMOTA Rules wiki page examples. This rule set is for the Hunter fan which has the need to turn on two channels for Medium (Rule 2) but only one channel for either low or high. For my Harbor Breeze and Hampton Bay fans, each fan speed needs at most one relay latched. That rule set is a pretty straightforward variation of this set of rules.

I broke the rule syntax up into multiple lines for readability. The rule needs to be entered in the TASMOTA Console as a single line. I have included the "single line" rules as well for proper reference.

Rule1 on BUTTON1#State do Event toggling1=%var1% endon on event#toggling1<1 do Backlog Power2 OFF ; Power3 OFF ; Power1 ON ; var1 1 ; var2 0 ; var3 0 endon on event#toggling1>0 do Backlog Power1 OFF ; Power2 OFF ; Power3 OFF ; var1 0 ; var2 0 ; var3 0 endon

Rule1 on BUTTON1#State do Event toggling1=%var1% endon on event#toggling1<1 do Backlog Power2 OFF; Power3 OFF; Power1 ON; var1 1; var2 0; var3 0 endon on event#toggling1>0 do Backlog Power1 OFF; Power2 OFF; Power3 OFF; var1 0; var2 0; var3 0 endon

Rule2 on BUTTON2#State do Event toggling2=%var2% endon on event#toggling2<1 do Backlog Power3 OFF ; Power1 ON ; Power2 ON ; var1 0 ; var2 1 ; var3 0 endon on event#toggling2>0 do Backlog Power1 OFF ; Power2 OFF ; Power3 OFF ; var1 0 ; var2 0 ; var3 0 endon

Rule2 on BUTTON2#State do Event toggling2=%var2% endon on event#toggling2<1 do Backlog Power3 OFF; Power1 ON; Power2 ON; var1 0; var2 1; var3 0 endon on event#toggling2>0 do Backlog Power1 OFF; Power2 OFF; Power3 OFF; var1 0; var2 0; var3 0 endon

Rule3 on BUTTON3#State do Event toggling3=%var3% endon on event#toggling3<1 do Backlog Power1 OFF ; Power2 OFF ; Power3 ON ; var1 0 ; var2 0 ; var3 1 endon on event#toggling3>0 do Backlog Power1 OFF ; Power2 OFF ; Power3 OFF ; var1 0 ; var2 0 ; var3 0 endon

Rule3 on BUTTON3#State do Event toggling3=%var3% endon on event#toggling3<1 do Backlog Power1 OFF; Power2 OFF; Power3 ON; var1 0; var2 0; var3 1 endon on event#toggling3>0 do Backlog Power1 OFF; Power2 OFF; Power3 OFF; var1 0; var2 0; var3 0 endon

And don't forget to turn the rules on: Backlog Rule1 1; Rule2 1; Rule3 1

Regards.

Mike

ascillato2 commented 6 years ago

@meingraham You can concatenate the rules. The 3 slots are there but each rule that start with ON is independent from each other. So you can add more triggers if you want.

meingraham commented 6 years ago

@ascillato2 OK, makes sense. I'll make a note of this... but since my three rule combined exceed 511 characters, I probably keep these three separate for "easier" management. But, if I ever need a slot, I now know that I can combine two of them to free up a rule slot. Of course, it's a good tip for future need. Thanks.

ascillato commented 6 years ago

You have 511 per set.

RULE1 has 511 + RULE2 has 511 + RULE3 has 511 = 1533 characters for rules :smile:

ascillato commented 6 years ago

the sets allow you to make some rules with the RULE_ONCE activated (in one set) and other rules without that (other sets).

meingraham commented 6 years ago

Oh, so it's not segmented to 511 separately? It's a combined 1533? The note that is returned when a rule is defined makes it seem like there's a 511 limit max on any one rule.

ascillato commented 6 years ago

it's not segmented to 511 separately?

no

It's a combined 1533?

yes :smile:

The note that is returned when a rule is defined makes it seem like there's a 511 limit max on any one rule.

That is remaining of each set. For RULE1, RULE2 or RULE3

Remember, if you use the command RULE, Tasmota will use RULE1.

meingraham commented 6 years ago

I tried this:

Enter a "short" rule. The response in the Console is:

MQT: stat/sonoff-keurig_toggle/RESULT = {"Rule1":"OFF","Once":"OFF","Free":274,"Rules":"BUTTON2#State do Event toggling2=%var2% endon on event#toggling2<1 do Backlog Power3 OFF; Power1 ON; Power2 ON; var1 0; var2 1; var3 0 endon on event#toggling2>0 do Backlog Power1 OFF; Power2 OFF; Power3 OFF; var1 0; var2 0; var3 0 endon"}

In other words, the rule is "echoed" back and the "Free" value is reflected.

However, if I enter the "long" rule

Rule on BUTTON1#State do Event toggling1=%var1% endon on event#toggling1<1 do Backlog Power2 OFF; Power3 OFF; Power1 ON; var1 1; var2 0; var3 0 endon on event#toggling1>0 do Backlog Power1 OFF; Power2 OFF; Power3 OFF; var1 0; var2 0; var3 0 endon on BUTTON2#State do Event toggling2=%var2% endon on event#toggling2<1 do Backlog Power3 OFF; Power1 ON; Power2 ON; var1 0; var2 1; var3 0 endon on event#toggling2>0 do Backlog Power1 OFF; Power2 OFF; Power3 OFF; var1 0; var2 0; var3 0 endon on BUTTON3#State do Event toggling3=%var3% endon on event#toggling3<1 do Backlog Power1 OFF; Power2 OFF; Power3 ON; var1 0; var2 0; var3 1 endon on event#toggling3>0 do Backlog Power1 OFF; Power2 OFF; Power3 OFF; var1 0; var2 0; var3 0 endon

the response is

MQT: stat/sonoff-keurig_toggle/RESULT = {"Rule1":"OFF","Once":"OFF","Free":511,"Rules":""}

as if thought it "threw away" my rule definition. Note that I specified "Rule" when I sent the long rule rather than "Rule1".

I checked all three rules - they are all "empty". This seems to imply that it doesn't like rules longer than 511.

ascillato commented 6 years ago

Each set is 511

ascillato commented 6 years ago

you can set up to 511 for each set. all rules inside all sets will be executed if the trigger is met

meingraham commented 6 years ago

Where is that 511 default limit set? I didn't see that in the config header.

ascillato commented 6 years ago

Is in https://github.com/arendst/Sonoff-Tasmota/blob/development/sonoff/sonoff.h#L50 but if you change it, you will brake all your config and can led to an unusable device.

meingraham commented 6 years ago

Right. Unless it's a user_config option, I would definitely want to stay away from that kind of change. So in essence I can only have rules that remain under the 511 limit each; yes? Sorry if I'm being dense. Just not clear how to define an individual rule that is longer than 511 without running into that limit.

ascillato commented 6 years ago

exactly

meingraham commented 6 years ago

OK. Net-net... I can "fill up" each rule slot with as many triggers and actions as will fit in each rule slot. As long as a contiguous On/EndOn fits, just keep filling up the slot. Tip noted. Thanks.

creativtransfer commented 4 years ago

How do I detect a long press on a sonoff 4ch?

Currently, I am using this rule to toggle all four relais at once, but I haven't figured out how to accomplish this with a long press, so that a short press of button1 will toggle power1 only!

Rule1 on BUTTON1#State do Event toggling1=%var1% endon on event#toggling1<1 do Backlog Power1 ON;Power2 ON; Power3 ON; Power4 ON; var1 1 endon on event#toggling1>0 do Backlog Power1 OFF; Power2 OFF; Power3 OFF; Power4 OFF; var1 0 endon

BUTTON1#State=3 does not work. I already tried that.

Is it possible at all to get a long press event on a sonoff 4ch?