Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

747 lines
21 KiB

7 years ago
7 years ago
7 years ago
7 years ago
  1. /*
  2. RELAY MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #include <EEPROM.h>
  6. #include <Ticker.h>
  7. #include <ArduinoJson.h>
  8. #include <vector>
  9. #include <functional>
  10. typedef struct {
  11. // Configuration variables
  12. unsigned char pin; // GPIO pin for the relay
  13. unsigned char type; // RELAY_TYPE_NORMAL, RELAY_TYPE_INVERSE or RELAY_TYPE_LATCHED
  14. unsigned char reset_pin; // GPIO to reset the relay if RELAY_TYPE_LATCHED
  15. unsigned char pulse; // RELAY_PULSE_NONE, RELAY_PULSE_OFF or RELAY_PULSE_ON
  16. unsigned long pulse_ms; // Pulse length in millis
  17. unsigned long delay_on; // Delay to turn relay ON
  18. unsigned long delay_off; // Delay to turn relay OFF
  19. // Status variables
  20. bool current_status; // Holds the current (physical) status of the relay
  21. bool target_status; // Holds the target status
  22. unsigned long fw_start; // Flood window start time
  23. unsigned char fw_count; // Number of changes within the current flood window
  24. unsigned long change_time; // Scheduled time to change
  25. bool report; // Whether to report to own topic
  26. bool group_report; // Whether to report to group topic
  27. // Helping objects
  28. Ticker pulseTicker; // Holds the pulse back timer
  29. } relay_t;
  30. std::vector<relay_t> _relays;
  31. bool _relayRecursive = false;
  32. Ticker _relaySaveTicker;
  33. // -----------------------------------------------------------------------------
  34. // RELAY PROVIDERS
  35. // -----------------------------------------------------------------------------
  36. void _relayProviderStatus(unsigned char id, bool status) {
  37. // Check relay ID
  38. if (id >= _relays.size()) return;
  39. // Store new current status
  40. _relays[id].current_status = status;
  41. #if RELAY_PROVIDER == RELAY_PROVIDER_RFBRIDGE
  42. rfbStatus(id, status);
  43. #endif
  44. #if RELAY_PROVIDER == RELAY_PROVIDER_DUAL
  45. // Calculate mask
  46. unsigned char mask=0;
  47. for (unsigned char i=0; i<_relays.size(); i++) {
  48. if (_relays[i].current_status) mask = mask + (1 << i);
  49. }
  50. // Send it to F330
  51. Serial.flush();
  52. Serial.write(0xA0);
  53. Serial.write(0x04);
  54. Serial.write(mask);
  55. Serial.write(0xA1);
  56. Serial.flush();
  57. #endif
  58. #if RELAY_PROVIDER == RELAY_PROVIDER_LIGHT
  59. lightState(status);
  60. lightUpdate(true, true);
  61. #endif
  62. #if RELAY_PROVIDER == RELAY_PROVIDER_RELAY
  63. if (_relays[id].type == RELAY_TYPE_NORMAL) {
  64. digitalWrite(_relays[id].pin, status);
  65. } else if (_relays[id].type == RELAY_TYPE_INVERSE) {
  66. digitalWrite(_relays[id].pin, !status);
  67. } else if (_relays[id].type == RELAY_TYPE_LATCHED) {
  68. digitalWrite(_relays[id].pin, LOW);
  69. digitalWrite(_relays[id].reset_pin, LOW);
  70. if (status) {
  71. digitalWrite(_relays[id].pin, HIGH);
  72. } else {
  73. digitalWrite(_relays[id].reset_pin, HIGH);
  74. }
  75. delay(RELAY_LATCHING_PULSE);
  76. digitalWrite(_relays[id].pin, LOW);
  77. digitalWrite(_relays[id].reset_pin, LOW);
  78. }
  79. #endif
  80. }
  81. // -----------------------------------------------------------------------------
  82. // RELAY
  83. // -----------------------------------------------------------------------------
  84. void relayPulse(unsigned char id) {
  85. byte mode = _relays[id].pulse;
  86. if (mode == RELAY_PULSE_NONE) return;
  87. unsigned long ms = _relays[id].pulse_ms;
  88. if (ms == 0) return;
  89. bool status = relayStatus(id);
  90. bool pulseStatus = (mode == RELAY_PULSE_ON);
  91. if (pulseStatus == status) {
  92. _relays[id].pulseTicker.detach();
  93. } else {
  94. _relays[id].pulseTicker.once_ms(ms, relayToggle, id);
  95. }
  96. }
  97. bool relayStatus(unsigned char id, bool status, bool report, bool group_report) {
  98. if (id >= _relays.size()) return false;
  99. bool changed = false;
  100. if (_relays[id].current_status == status) {
  101. if (_relays[id].target_status != status) {
  102. DEBUG_MSG_P(PSTR("[RELAY] #%d scheduled change cancelled\n"), id);
  103. _relays[id].target_status = status;
  104. _relays[id].report = false;
  105. _relays[id].group_report = false;
  106. changed = true;
  107. }
  108. // For RFBridge, keep sending the message even if the status is already the required
  109. #if RELAY_PROVIDER == RELAY_PROVIDER_RFBRIDGE
  110. rfbStatus(id, status);
  111. #endif
  112. } else {
  113. unsigned int current_time = millis();
  114. unsigned int fw_end = _relays[id].fw_start + 1000 * RELAY_FLOOD_WINDOW;
  115. unsigned long delay = status ? _relays[id].delay_on : _relays[id].delay_off;
  116. _relays[id].fw_count++;
  117. _relays[id].change_time = current_time + delay;
  118. // If current_time is off-limits the floodWindow...
  119. if (current_time < _relays[id].fw_start || fw_end <= current_time) {
  120. // We reset the floodWindow
  121. _relays[id].fw_start = current_time;
  122. _relays[id].fw_count = 1;
  123. // If current_time is in the floodWindow and there have been too many requests...
  124. } else if (_relays[id].fw_count >= RELAY_FLOOD_CHANGES) {
  125. // We schedule the changes to the end of the floodWindow
  126. // unless it's already delayed beyond that point
  127. if (fw_end - delay > current_time) {
  128. _relays[id].change_time = fw_end;
  129. }
  130. }
  131. _relays[id].target_status = status;
  132. if (report) _relays[id].report = true;
  133. if (group_report) _relays[id].group_report = true;
  134. relaySync(id);
  135. DEBUG_MSG_P(PSTR("[RELAY] #%d scheduled %s in %u ms\n"),
  136. id, status ? "ON" : "OFF",
  137. (_relays[id].change_time - current_time));
  138. changed = true;
  139. }
  140. return changed;
  141. }
  142. bool relayStatus(unsigned char id, bool status) {
  143. return relayStatus(id, status, true, true);
  144. }
  145. bool relayStatus(unsigned char id) {
  146. // Check relay ID
  147. if (id >= _relays.size()) return false;
  148. // Get status from storage
  149. return _relays[id].current_status;
  150. }
  151. void relaySync(unsigned char id) {
  152. // No sync if none or only one relay
  153. if (_relays.size() < 2) return;
  154. // Do not go on if we are comming from a previous sync
  155. if (_relayRecursive) return;
  156. // Flag sync mode
  157. _relayRecursive = true;
  158. byte relaySync = getSetting("relaySync", RELAY_SYNC).toInt();
  159. bool status = _relays[id].target_status;
  160. // If RELAY_SYNC_SAME all relays should have the same state
  161. if (relaySync == RELAY_SYNC_SAME) {
  162. for (unsigned short i=0; i<_relays.size(); i++) {
  163. if (i != id) relayStatus(i, status);
  164. }
  165. // If NONE_OR_ONE or ONE and setting ON we should set OFF all the others
  166. } else if (status) {
  167. if (relaySync != RELAY_SYNC_ANY) {
  168. for (unsigned short i=0; i<_relays.size(); i++) {
  169. if (i != id) relayStatus(i, false);
  170. }
  171. }
  172. // If ONLY_ONE and setting OFF we should set ON the other one
  173. } else {
  174. if (relaySync == RELAY_SYNC_ONE) {
  175. unsigned char i = (id + 1) % _relays.size();
  176. relayStatus(i, true);
  177. }
  178. }
  179. // Unflag sync mode
  180. _relayRecursive = false;
  181. }
  182. void relaySave() {
  183. unsigned char bit = 1;
  184. unsigned char mask = 0;
  185. for (unsigned int i=0; i < _relays.size(); i++) {
  186. if (relayStatus(i)) mask += bit;
  187. bit += bit;
  188. }
  189. EEPROM.write(EEPROM_RELAY_STATUS, mask);
  190. DEBUG_MSG_P(PSTR("[RELAY] Saving mask: %d\n"), mask);
  191. EEPROM.commit();
  192. }
  193. void relayToggle(unsigned char id, bool report, bool group_report) {
  194. if (id >= _relays.size()) return;
  195. relayStatus(id, !relayStatus(id), report, group_report);
  196. }
  197. void relayToggle(unsigned char id) {
  198. relayToggle(id, true, true);
  199. }
  200. unsigned char relayCount() {
  201. return _relays.size();
  202. }
  203. unsigned char relayParsePayload(const char * payload) {
  204. // Payload could be "OFF", "ON", "TOGGLE"
  205. // or its number equivalents: 0, 1 or 2
  206. // trim payload
  207. char * p = ltrim((char *)payload);
  208. // to lower
  209. for (unsigned char i=0; i<strlen(p); i++) {
  210. p[i] = tolower(p[i]);
  211. }
  212. unsigned int value;
  213. if (strcmp(p, "off") == 0) {
  214. value = 0;
  215. } else if (strcmp(p, "on") == 0) {
  216. value = 1;
  217. } else if (strcmp(p, "toggle") == 0) {
  218. value = 2;
  219. } else if (strcmp(p, "query") == 0) {
  220. value = 3;
  221. } else {
  222. value = p[0] - '0';
  223. }
  224. if (0 <= value && value <=3) return value;
  225. return 0xFF;
  226. }
  227. // BACKWARDS COMPATIBILITY
  228. void _relayBackwards() {
  229. byte relayMode = getSetting("relayMode", RELAY_BOOT_MODE).toInt();
  230. byte relayPulseMode = getSetting("relayPulseMode", RELAY_PULSE_MODE).toInt();
  231. float relayPulseTime = getSetting("relayPulseTime", RELAY_PULSE_TIME).toFloat();
  232. if (relayPulseMode == RELAY_PULSE_NONE) relayPulseTime = 0;
  233. for (unsigned int i=0; i<_relays.size(); i++) {
  234. if (!hasSetting("relayBoot", i)) setSetting("relayBoot", i, relayMode);
  235. if (!hasSetting("relayPulse", i)) setSetting("relayPulse", i, relayPulseMode);
  236. if (!hasSetting("relayTime", i)) setSetting("relayTime", i, relayPulseTime);
  237. }
  238. delSetting("relayMode");
  239. delSetting("relayPulseMode");
  240. delSetting("relayPulseTime");
  241. }
  242. void _relayBoot() {
  243. _relayRecursive = true;
  244. unsigned char bit = 1;
  245. bool trigger_save = false;
  246. // Get last statuses from EEPROM
  247. unsigned char mask = EEPROM.read(EEPROM_RELAY_STATUS);
  248. DEBUG_MSG_P(PSTR("[RELAY] Retrieving mask: %d\n"), mask);
  249. // Walk the relays
  250. for (unsigned int i=0; i<_relays.size(); i++) {
  251. _relays[i].current_status = false;
  252. _relays[i].target_status = false;
  253. unsigned char boot_mode = getSetting("relayBoot", i, RELAY_BOOT_MODE).toInt();
  254. switch (boot_mode) {
  255. case RELAY_BOOT_OFF:
  256. relayStatus(i, false);
  257. break;
  258. case RELAY_BOOT_ON:
  259. relayStatus(i, true);
  260. break;
  261. case RELAY_BOOT_SAME:
  262. relayStatus(i, (mask & bit) == bit);
  263. break;
  264. case RELAY_BOOT_TOOGLE:
  265. relayStatus(i, (mask & bit) != bit);
  266. mask ^= bit;
  267. trigger_save = true;
  268. break;
  269. }
  270. bit <<= 1;
  271. }
  272. // Save if there is any relay in the RELAY_BOOT_TOOGLE mode
  273. if (trigger_save) {
  274. EEPROM.write(EEPROM_RELAY_STATUS, mask);
  275. EEPROM.commit();
  276. }
  277. _relayRecursive = false;
  278. }
  279. //------------------------------------------------------------------------------
  280. // WEBSOCKETS
  281. //------------------------------------------------------------------------------
  282. #if WEB_SUPPORT
  283. void _relayWebSocketUpdate(JsonObject& root) {
  284. JsonArray& relay = root.createNestedArray("relayStatus");
  285. for (unsigned char i=0; i<relayCount(); i++) {
  286. relay.add(_relays[i].target_status);
  287. }
  288. }
  289. void _relayWebSocketOnStart(JsonObject& root) {
  290. if (relayCount() == 0) return;
  291. // Statuses
  292. _relayWebSocketUpdate(root);
  293. // Configuration
  294. JsonArray& config = root.createNestedArray("relayConfig");
  295. for (unsigned char i=0; i<relayCount(); i++) {
  296. JsonObject& line = config.createNestedObject();
  297. line["gpio"] = _relays[i].pin;
  298. line["type"] = _relays[i].type;
  299. line["reset"] = _relays[i].reset_pin;
  300. line["boot"] = getSetting("relayBoot", i, RELAY_BOOT_MODE).toInt();
  301. line["pulse"] = _relays[i].pulse;
  302. line["pulse_ms"] = _relays[i].pulse_ms / 1000.0;
  303. #if MQTT_SUPPORT
  304. line["group"] = getSetting("mqttGroup", i, "");
  305. line["group_inv"] = getSetting("mqttGroupInv", i, 0).toInt();
  306. #endif
  307. }
  308. if (relayCount() > 1) {
  309. root["multirelayVisible"] = 1;
  310. root["relaySync"] = getSetting("relaySync", RELAY_SYNC);
  311. }
  312. root["relayVisible"] = 1;
  313. }
  314. void _relayWebSocketOnAction(const char * action, JsonObject& data) {
  315. if (strcmp(action, "relay") != 0) return;
  316. if (data.containsKey("status")) {
  317. unsigned char value = relayParsePayload(data["status"]);
  318. if (value == 3) {
  319. wsSend(_relayWebSocketUpdate);
  320. } else if (value < 3) {
  321. unsigned int relayID = 0;
  322. if (data.containsKey("id")) {
  323. String value = data["id"];
  324. relayID = value.toInt();
  325. }
  326. // Action to perform
  327. if (value == 0) {
  328. relayStatus(relayID, false);
  329. } else if (value == 1) {
  330. relayStatus(relayID, true);
  331. } else if (value == 2) {
  332. relayToggle(relayID);
  333. }
  334. }
  335. }
  336. }
  337. void _relayConfigure() {
  338. for (unsigned int i=0; i<_relays.size(); i++) {
  339. pinMode(_relays[i].pin, OUTPUT);
  340. if (_relays[i].type == RELAY_TYPE_LATCHED) pinMode(_relays[i].reset_pin, OUTPUT);
  341. _relays[i].pulse = getSetting("relayPulse", i, RELAY_PULSE_MODE).toInt();
  342. _relays[i].pulse_ms = 1000 * getSetting("relayTime", i, RELAY_PULSE_MODE).toFloat();
  343. }
  344. }
  345. void relaySetupWS() {
  346. wsOnSendRegister(_relayWebSocketOnStart);
  347. wsOnActionRegister(_relayWebSocketOnAction);
  348. wsOnAfterParseRegister(_relayConfigure);
  349. }
  350. #endif // WEB_SUPPORT
  351. //------------------------------------------------------------------------------
  352. // REST API
  353. //------------------------------------------------------------------------------
  354. #if WEB_SUPPORT
  355. void relaySetupAPI() {
  356. // API entry points (protected with apikey)
  357. for (unsigned int relayID=0; relayID<relayCount(); relayID++) {
  358. char key[15];
  359. snprintf_P(key, sizeof(key), PSTR("%s/%d"), MQTT_TOPIC_RELAY, relayID);
  360. apiRegister(key,
  361. [relayID](char * buffer, size_t len) {
  362. snprintf_P(buffer, len, PSTR("%d"), relayStatus(relayID) ? 1 : 0);
  363. },
  364. [relayID](const char * payload) {
  365. unsigned char value = relayParsePayload(payload);
  366. if (value == 0xFF) {
  367. DEBUG_MSG_P(PSTR("[RELAY] Wrong payload (%s)\n"), payload);
  368. return;
  369. }
  370. if (value == 0) {
  371. relayStatus(relayID, false);
  372. } else if (value == 1) {
  373. relayStatus(relayID, true);
  374. } else if (value == 2) {
  375. relayToggle(relayID);
  376. }
  377. }
  378. );
  379. }
  380. }
  381. #endif // WEB_SUPPORT
  382. //------------------------------------------------------------------------------
  383. // MQTT
  384. //------------------------------------------------------------------------------
  385. #if MQTT_SUPPORT
  386. void relayMQTT(unsigned char id) {
  387. if (id >= _relays.size()) return;
  388. // Send state topic
  389. if (_relays[id].report) {
  390. _relays[id].report = false;
  391. mqttSend(MQTT_TOPIC_RELAY, id, _relays[id].current_status ? "1" : "0");
  392. }
  393. // Check group topic
  394. if (_relays[id].group_report) {
  395. _relays[id].group_report = false;
  396. String t = getSetting("mqttGroup", id, "");
  397. if (t.length() > 0) {
  398. bool status = relayStatus(id);
  399. if (getSetting("mqttGroupInv", id, 0).toInt() == 1) status = !status;
  400. mqttSendRaw(t.c_str(), status ? "1" : "0");
  401. }
  402. }
  403. }
  404. void relayMQTT() {
  405. for (unsigned int id=0; id < _relays.size(); id++) {
  406. mqttSend(MQTT_TOPIC_RELAY, id, _relays[id].current_status ? "1" : "0");
  407. }
  408. }
  409. void relayStatusWrap(unsigned char id, unsigned char value, bool is_group_topic) {
  410. // Action to perform
  411. if (value == 0) {
  412. relayStatus(id, false, mqttForward(), !is_group_topic);
  413. } else if (value == 1) {
  414. relayStatus(id, true, mqttForward(), !is_group_topic);
  415. } else if (value == 2) {
  416. relayToggle(id, true, true);
  417. }
  418. }
  419. void relayMQTTCallback(unsigned int type, const char * topic, const char * payload) {
  420. if (type == MQTT_CONNECT_EVENT) {
  421. // Send status on connect
  422. #if not HEARTBEAT_REPORT_RELAY
  423. relayMQTT();
  424. #endif
  425. // Subscribe to own /set topic
  426. char buffer[strlen(MQTT_TOPIC_RELAY) + 3];
  427. snprintf_P(buffer, sizeof(buffer), PSTR("%s/+"), MQTT_TOPIC_RELAY);
  428. mqttSubscribe(buffer);
  429. // Subscribe to group topics
  430. for (unsigned int i=0; i < _relays.size(); i++) {
  431. String t = getSetting("mqttGroup", i, "");
  432. if (t.length() > 0) mqttSubscribeRaw(t.c_str());
  433. }
  434. }
  435. if (type == MQTT_MESSAGE_EVENT) {
  436. // Get value
  437. unsigned char value = relayParsePayload(payload);
  438. if (value == 0xFF) {
  439. DEBUG_MSG_P(PSTR("[RELAY] Wrong payload (%s)\n"), payload);
  440. return;
  441. }
  442. // Check group topics
  443. bool found = false;
  444. for (unsigned int i=0; i < _relays.size(); i++) {
  445. String t = getSetting("mqttGroup", i, "");
  446. if (t.equals(topic)) {
  447. unsigned char local_value = value;
  448. if (getSetting("mqttGroupInv", i, 0).toInt() == 1) {
  449. if (local_value < 2) local_value = 1 - local_value;
  450. }
  451. found = true;
  452. DEBUG_MSG_P(PSTR("[RELAY] Matched group topic for relayID %d\n"), i);
  453. relayStatusWrap(i, local_value, true);
  454. }
  455. }
  456. // If found as group topic quit
  457. if (found) return;
  458. // Else, try to match topic
  459. String t = mqttSubtopic((char *) topic);
  460. if (!t.startsWith(MQTT_TOPIC_RELAY)) return;
  461. // Get relay ID
  462. unsigned int id = t.substring(strlen(MQTT_TOPIC_RELAY)+1).toInt();
  463. if (id >= relayCount()) {
  464. DEBUG_MSG_P(PSTR("[RELAY] Wrong relayID (%d)\n"), id);
  465. return;
  466. }
  467. relayStatusWrap(id, value, false);
  468. }
  469. }
  470. void relaySetupMQTT() {
  471. mqttRegister(relayMQTTCallback);
  472. }
  473. #endif
  474. //------------------------------------------------------------------------------
  475. // InfluxDB
  476. //------------------------------------------------------------------------------
  477. #if INFLUXDB_SUPPORT
  478. void relayInfluxDB(unsigned char id) {
  479. if (id >= _relays.size()) return;
  480. idbSend(MQTT_TOPIC_RELAY, id, relayStatus(id) ? "1" : "0");
  481. }
  482. #endif
  483. //------------------------------------------------------------------------------
  484. // Setup
  485. //------------------------------------------------------------------------------
  486. void relaySetup() {
  487. // Dummy relays for AI Light, Magic Home LED Controller, H801,
  488. // Sonoff Dual and Sonoff RF Bridge
  489. #ifdef DUMMY_RELAY_COUNT
  490. for (unsigned char i=0; i < DUMMY_RELAY_COUNT; i++) {
  491. _relays.push_back((relay_t) {0, RELAY_TYPE_NORMAL});
  492. }
  493. #else
  494. #ifdef RELAY1_PIN
  495. _relays.push_back((relay_t) { RELAY1_PIN, RELAY1_TYPE, RELAY1_RESET_PIN, RELAY1_DELAY_ON, RELAY1_DELAY_OFF });
  496. #endif
  497. #ifdef RELAY2_PIN
  498. _relays.push_back((relay_t) { RELAY2_PIN, RELAY2_TYPE, RELAY2_RESET_PIN, RELAY2_DELAY_ON, RELAY2_DELAY_OFF });
  499. #endif
  500. #ifdef RELAY3_PIN
  501. _relays.push_back((relay_t) { RELAY3_PIN, RELAY3_TYPE, RELAY3_RESET_PIN, RELAY3_DELAY_ON, RELAY3_DELAY_OFF });
  502. #endif
  503. #ifdef RELAY4_PIN
  504. _relays.push_back((relay_t) { RELAY4_PIN, RELAY4_TYPE, RELAY4_RESET_PIN, RELAY4_DELAY_ON, RELAY4_DELAY_OFF });
  505. #endif
  506. #ifdef RELAY5_PIN
  507. _relays.push_back((relay_t) { RELAY5_PIN, RELAY5_TYPE, RELAY5_RESET_PIN, RELAY5_DELAY_ON, RELAY5_DELAY_OFF });
  508. #endif
  509. #ifdef RELAY6_PIN
  510. _relays.push_back((relay_t) { RELAY6_PIN, RELAY6_TYPE, RELAY6_RESET_PIN, RELAY6_DELAY_ON, RELAY6_DELAY_OFF });
  511. #endif
  512. #ifdef RELAY7_PIN
  513. _relays.push_back((relay_t) { RELAY7_PIN, RELAY7_TYPE, RELAY7_RESET_PIN, RELAY7_DELAY_ON, RELAY7_DELAY_OFF });
  514. #endif
  515. #ifdef RELAY8_PIN
  516. _relays.push_back((relay_t) { RELAY8_PIN, RELAY8_TYPE, RELAY8_RESET_PIN, RELAY8_DELAY_ON, RELAY8_DELAY_OFF });
  517. #endif
  518. #endif
  519. _relayBackwards();
  520. _relayConfigure();
  521. _relayBoot();
  522. relayLoop();
  523. #if WEB_SUPPORT
  524. relaySetupAPI();
  525. relaySetupWS();
  526. #endif
  527. #if MQTT_SUPPORT
  528. relaySetupMQTT();
  529. #endif
  530. DEBUG_MSG_P(PSTR("[RELAY] Number of relays: %d\n"), _relays.size());
  531. }
  532. void relayLoop(void) {
  533. unsigned char id;
  534. for (id = 0; id < _relays.size(); id++) {
  535. unsigned int current_time = millis();
  536. bool status = _relays[id].target_status;
  537. if ((_relays[id].current_status != status)
  538. && (current_time >= _relays[id].change_time)) {
  539. DEBUG_MSG_P(PSTR("[RELAY] #%d set to %s\n"), id, status ? "ON" : "OFF");
  540. // Call the provider to perform the action
  541. _relayProviderStatus(id, status);
  542. // Send MQTT
  543. #if MQTT_SUPPORT
  544. relayMQTT(id);
  545. #endif
  546. if (!_relayRecursive) {
  547. relayPulse(id);
  548. _relaySaveTicker.once_ms(RELAY_SAVE_DELAY, relaySave);
  549. #if WEB_SUPPORT
  550. wsSend(_relayWebSocketUpdate);
  551. #endif
  552. }
  553. #if DOMOTICZ_SUPPORT
  554. domoticzSendRelay(id);
  555. #endif
  556. #if INFLUXDB_SUPPORT
  557. relayInfluxDB(id);
  558. #endif
  559. _relays[id].report = false;
  560. _relays[id].group_report = false;
  561. }
  562. }
  563. }