diff --git a/usermods/multi_relay/readme.md b/usermods/multi_relay/readme.md index e6b4adac..ebf2056a 100644 --- a/usermods/multi_relay/readme.md +++ b/usermods/multi_relay/readme.md @@ -19,7 +19,7 @@ Examples ## JSON API You can switch relay state using the following JSON object transmitted to: `http://[device-ip]/json` -Switch relay 0 on: `{MultiRelay:{relay:0,on:true}}` +Switch relay 0 on: `{"MultiRelay":{"relay":0,"on":true}}` Switch relay4 3 & 4 off: `{"MultiRelay":[{"relay":2,"on":false},{"relay":3,"on":false}]}` ## MQTT API diff --git a/usermods/multi_relay/usermod_multi_relay.h b/usermods/multi_relay/usermod_multi_relay.h index 4172d2a8..786461ab 100644 --- a/usermods/multi_relay/usermod_multi_relay.h +++ b/usermods/multi_relay/usermod_multi_relay.h @@ -429,8 +429,25 @@ class MultiRelay : public Usermod { * addToJsonState() can be used to add custom entries to the /json/state part of the JSON API (state object). * Values in the state object may be modified by connected clients */ - //void addToJsonState(JsonObject &root) { - //} + void addToJsonState(JsonObject &root) { + if (!initDone || !enabled) return; // prevent crash on boot applyPreset() + JsonObject multiRelay = root[FPSTR(_name)]; + if (multiRelay.isNull()) { + multiRelay = root.createNestedObject(FPSTR(_name)); + } + #if MULTI_RELAY_MAX_RELAYS > 1 + JsonArray rel_arr = multiRelay.createNestedArray(F("relays")); + for (uint8_t i=0; i