   String topicString = String(topic);
   int relayNumber = topicString.substring(18, 20).toInt();
   int posInArray = relayNumber - 1;
+  char* switchState = (char)payload[0];
 
-  switchRelay(payload, posInArray);
+  if (switchState == '2') {
+    if (relayStates[posInArray] == HIGH){
+      switchState = '1';
+    }
+    else {
+      switchState = '0';
+    }
+  }
+
+  if (isMutuallyExclude){
+   mutuallyExcludePair(switchState, posInArray);
+  }
+  switchRelay(switchState, posInArray);
 
 }
 


}

void switchRelay(byte* payload, int pos) {
  // Switch on the LED if an 1 was received as first character
  if ((char)payload[0] == '0') {
    digitalWrite(relayPins[pos], HIGH);   
    relayStates[pos] = HIGH;
    EEPROM.update(pos, relayStates[pos]);    // Write state to EEPROM
  } else if ((char)payload[0] == '1') {
    digitalWrite(relayPins[pos], LOW);  
    relayStates[pos] = LOW;
    EEPROM.update(pos, relayStates[pos]);    // Write state to EEPROM
  } else if ((char)payload[0] == '2') {
    relayStates[pos] = !relayStates[pos];
    digitalWrite(relayPins[pos], relayStates[pos]); 
    EEPROM.update(pos, relayStates[pos]);    // Write state to EEPROM
  }
}

void callback(char* topic, byte* payload, unsigned int length) {

char msg[50];
int value = 0;

//MutuallyExclude on state between pairs of relays (for shutter to prevent up and down at the same time)
//ask R1 to turn on while R2 = on -> turns off R2 first (and broadcasts 'domogik/in/relay/r2 0')
//ask R2 to turn on while R1 = on -> turns off R1 first (and broadcasts 'domogik/in/relay/r1 0')
bool isMutuallyExclude = true;

const char* outTopic = "domogik/relayClient";
const char* inTopic = "domogik/in/relay/#";
const char* outRelayTopic = "domogik/in/relay/r";

bool relayStates[] = {
  HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH
