         flashSuccess(false);
       }
 
+#ifdef JSON
+      subscribeToMQTTTopic(ARILUX_MQTT_JSON_COMMAND_TOPIC);
+#else
       subscribeToMQTTTopic(ARILUX_MQTT_STATE_COMMAND_TOPIC);
       subscribeToMQTTTopic(ARILUX_MQTT_BRIGHTNESS_COMMAND_TOPIC);
       subscribeToMQTTTopic(ARILUX_MQTT_COLOR_COMMAND_TOPIC);
 
-#if defined(RGBW) || defined (RGBWW)
-      subscribeToMQTTTopic(ARILUX_MQTT_WHITE_COMMAND_TOPIC);
-#endif
-
-#ifdef JSON
-      subscribeToMQTTTopic(ARILUX_MQTT_JSON_COMMAND_TOPIC);
+      #if defined(RGBW) || defined (RGBWW)
+            subscribeToMQTTTopic(ARILUX_MQTT_WHITE_COMMAND_TOPIC);
+      #endif
 #endif
 
       lastmqttreconnect = millis();

  }

  // Handle the MQTT topic of the received message
  if (String(ARILUX_MQTT_JSON_COMMAND_TOPIC).equals(p_topic)) {
    DynamicJsonBuffer incomingJsonPayload;
    JsonObject& root = incomingJsonPayload.parseObject(payload);
    if (!root.success()) {
      DEBUG_PRINTLN("parseObject() failed");
      return;
    }

    if (root.containsKey("color")) {
      int red_color = root["color"]["r"];
      int green_color = root["color"]["g"];
      int blue_color = root["color"]["b"];

      realRed = red_color;
      realGreen = green_color;
      realBlue = blue_color;
    } else {
      realRed = arilux.getRedValue();
      realGreen = arilux.getGreenValue();
      realBlue = arilux.getBlueValue();
    }

    startFade = true;
    inFade = false; // Kill the current fade

    if (root.containsKey("flash")) {
      flashLength = (int)root["flash"] * 1000;

      if (root.containsKey("brightness")) {
        flashBrightness = root["brightness"];
      } else {
        flashBrightness = arilux.getBrightness();
      }

      if (root.containsKey("color")) {
        flashRed = root["color"]["r"];
        flashGreen = root["color"]["g"];
        flashBlue = root["color"]["b"];
      } else {
        flashRed = arilux.getRedValue();
        flashGreen = arilux.getGreenValue();
        flashBlue = arilux.getBlueValue();
      }

      flashRed = map(flashRed, 0, 255, 0, flashBrightness);
      flashGreen = map(flashGreen, 0, 255, 0, flashBrightness);
      flashBlue = map(flashBlue, 0, 255, 0, flashBrightness);

      flash = true;
      startFlash = true;
    } else { // Not flashing
      flash = false;
      if (root.containsKey("state")) {
        if (strcmp(root["state"], "ON") == 0) {
          arilux.turnOn();
        } else if (strcmp(root["state"], "OFF") == 0) {
          arilux.turnOff();
        }
      }

      if (root.containsKey("transition")) {
        transitionTime = root["transition"];
      } else {
        transitionTime = 0;
      }

      if (root.containsKey("brightness")) {
        int brightness = root["brightness"];
        arilux.setBrightness(brightness);
        publishBrightnessChange();
      }

      if (root.containsKey("white_value")) {
        int white_value = root["white_value"];
        arilux.setWhite(white_value, white_value);
        publishWhiteChange();
      }
    }
    cmd = ARILUX_CMD_JSON;
  } else if (String(ARILUX_MQTT_STATE_COMMAND_TOPIC).equals(p_topic)) {
    if (payload.equals(String(MQTT_STATE_ON_PAYLOAD))) {
      if (arilux.turnOn())
        cmd = ARILUX_CMD_STATE_CHANGED;
    } else if (payload.equals(String(MQTT_STATE_OFF_PAYLOAD))) {
      if (arilux.turnOff())
        cmd = ARILUX_CMD_STATE_CHANGED;
    }
  } else if (String(ARILUX_MQTT_BRIGHTNESS_COMMAND_TOPIC).equals(p_topic)) {
    if (arilux.setBrightness(payload.toInt()))
      cmd = ARILUX_CMD_BRIGHTNESS_CHANGED;
  } else if (String(ARILUX_MQTT_COLOR_COMMAND_TOPIC).equals(p_topic)) {
    // Get the position of the first and second commas
    int commaIndex = payload.indexOf(',');
    //  Search for the next comma just after the first
    int secondCommaIndex = payload.indexOf(',', commaIndex + 1);
    String firstValue = payload.substring(0, commaIndex);
    String secondValue = payload.substring(commaIndex + 1, secondCommaIndex);
    String thirdValue = payload.substring(secondCommaIndex + 1); // To the end of the string
    int r = firstValue.toInt();
    int g = secondValue.toInt();
    int b = thirdValue.toInt();

    if (arilux.setColor(r, g, b))
      cmd = ARILUX_CMD_COLOR_CHANGED;
  } else if (String(ARILUX_MQTT_WHITE_COMMAND_TOPIC).equals(p_topic)) {
    uint8_t firstIndex = payload.indexOf(',');
    if (arilux.setWhite(payload.substring(0, firstIndex).toInt(), payload.substring(firstIndex + 1).toInt()))
      cmd = ARILUX_CMD_WHITE_CHANGED;
  }
}

/*

      root["color"]["b"] = arilux.getBlueValue();
      root.printTo(outgoingJsonBuffer);
      publishToMQTT(ARILUX_MQTT_JSON_STATE_TOPIC, outgoingJsonBuffer);
  };
  #else
    switch (cmd) {
      case ARILUX_CMD_NOT_DEFINED:
        break;
      case ARILUX_CMD_STATE_CHANGED:
        publishStateChange();
        break;
      case ARILUX_CMD_BRIGHTNESS_CHANGED:
        publishBrightnessChange();
        break;
      case ARILUX_CMD_COLOR_CHANGED:
        publishColorChange();
        break;
      case ARILUX_CMD_WHITE_CHANGED:
        publishWhiteChange();
        break;
      default:
        break;
    }
  #endif
  cmd = ARILUX_CMD_NOT_DEFINED;
}

#ifndef JSON
  void publishStateChange(void) {
    publishToMQTT(ARILUX_MQTT_STATE_STATE_TOPIC, (arilux.getState() ? MQTT_STATE_ON_PAYLOAD : MQTT_STATE_OFF_PAYLOAD));
  }

  void publishBrightnessChange(void) {
    snprintf(msgBuffer, sizeof(msgBuffer), "%d", arilux.getBrightness());
    publishToMQTT(ARILUX_MQTT_BRIGHTNESS_STATE_TOPIC, msgBuffer);
  }

  void publishColorChange(void) {
    snprintf(msgBuffer, sizeof(msgBuffer), "%d,%d,%d", arilux.getRedValue(), arilux.getGreenValue(), arilux.getBlueValue());
    publishToMQTT(ARILUX_MQTT_COLOR_STATE_TOPIC, msgBuffer);
  }

  void publishWhiteChange(void) {
    snprintf(msgBuffer, sizeof(msgBuffer), "%d,%d", arilux.getWhite1Value(), arilux.getWhite2Value());
    publishToMQTT(ARILUX_MQTT_WHITE_STATE_TOPIC, msgBuffer);
  }
#endif

///////////////////////////////////////////////////////////////////////////
//  WiFi
