void switch_relay(uint8_t id, uint8_t state, bool negation) {
  if (negation) {
    state = !state;
  }
  digitalWrite(id, state);
}

bool relay_state(uint8_t id, bool negation) {
  if (negation) {
    return !digitalRead(id);
  } else {
    return digitalRead(id);
  }
}

  void relay_setup() {
    pinMode(RELAY1, OUTPUT);
    pinMode(RELAY2, OUTPUT);
    pinMode(RELAY3, OUTPUT);
    pinMode(RELAY4, OUTPUT);

    switch_relay(RELAY1, LOW, pref.getBool("r1_neg", false));
    switch_relay(RELAY2, LOW, pref.getBool("r2_neg", false));
    switch_relay(RELAY3, LOW, pref.getBool("r3_neg", false));
    switch_relay(RELAY4, LOW, pref.getBool("r4_neg", false));
  }

  void relay_scheduler() {
    int hours = time_getHours();
    int minutes = time_getMinutes();
    // rele 1
    if (pref.getBool("r1_enable", true)) {
      if (hours == pref.getString("r1_on_h", "00").toInt() && minutes == pref.getString("r1_on_m", "00").toInt() && relay_state(RELAY1, pref.getBool("r1_neg", false)) == 0) {
        switch_relay(RELAY1, HIGH, pref.getBool("r1_neg", false));
        Serial.println("Relay 1 on (" + timeClient.getFormattedTime() + ")");
        Serial.println(timeClient.getFormattedTime());
      }
      if (hours == pref.getString("r1_off_h", "01").toInt() && minutes == pref.getString("r1_off_m", "00").toInt() && relay_state(RELAY1, pref.getBool("r1_neg", false)) == 1) {
        switch_relay(RELAY1, LOW, pref.getBool("r1_neg", false));
        Serial.println("Relay 1 off (" + timeClient.getFormattedTime() + ")");
        Serial.println(timeClient.getFormattedTime());
      }
    }
    // rele 2
    if (pref.getBool("r2_enable", true)) {
      if (hours == pref.getString("r2_on_h", "01").toInt() && minutes == pref.getString("r2_on_m", "00").toInt() && relay_state(RELAY2, pref.getBool("r2_neg", false)) == 0) {
        switch_relay(RELAY2, HIGH, pref.getBool("r2_neg", false));
        Serial.println("Relay 1 on (" + timeClient.getFormattedTime() + ")");
      }
      if (hours == pref.getString("r2_off_h", "02").toInt() && minutes == pref.getString("r2_off_m", "00").toInt() && relay_state(RELAY2, pref.getBool("r2_neg", false)) == 1) {
        switch_relay(RELAY2, LOW, pref.getBool("r2_neg", false));
        Serial.println("Relay 2 off (" + timeClient.getFormattedTime() + ")");
      }
    }
    // rele 3
    if (pref.getBool("r3_enable", true)) {
      if (hours == pref.getString("r3_on_h", "02").toInt() && minutes == pref.getString("r3_on_m", "00").toInt() && relay_state(RELAY3, pref.getBool("r3_neg", false)) == 0) {
        switch_relay(RELAY3, HIGH, pref.getBool("r3_neg", false));
        Serial.println("Relay 3 on (" + timeClient.getFormattedTime() + ")");
      }
      if (hours == pref.getString("r3_off_h", "03").toInt() && minutes == pref.getString("r3_off_m", "00").toInt() && relay_state(RELAY3, pref.getBool("r3_neg", false)) == 1) {
        switch_relay(RELAY3, LOW, pref.getBool("r3_neg", false));
        Serial.println("Relay 3 off (" + timeClient.getFormattedTime() + ")");
      }
    }
    // rele 4
    if (pref.getBool("r4_enable", true)) {
      if (hours == pref.getString("r4_on_h", "03").toInt() && minutes == pref.getString("r4_on_m", "00").toInt() && relay_state(RELAY4, pref.getBool("r4_neg", false)) == 0) {
        switch_relay(RELAY4, HIGH, pref.getBool("r4_neg", false));
        Serial.println("Relay 4 on (" + timeClient.getFormattedTime() + ")");
      }
      if (hours == pref.getString("r4_off_h", "04").toInt() && minutes == pref.getString("r4_off_m", "00").toInt() && relay_state(RELAY4, pref.getBool("r4_neg", false)) == 1) {
        switch_relay(RELAY4, LOW, pref.getBool("r4_neg", false));
        Serial.println("Relay 4 off (" + timeClient.getFormattedTime() + ")");
      }
    }
  }