diff --git a/src/current_shaper.cpp b/src/current_shaper.cpp index c1c5a3b0..e3b4af74 100644 --- a/src/current_shaper.cpp +++ b/src/current_shaper.cpp @@ -18,7 +18,7 @@ void CurrentShaperTask::setup() { } unsigned long CurrentShaperTask::loop(MicroTasks::WakeReason reason) { - if (_enabled && !_evse->clientHasClaim(EvseClient_OpenEVSE_Divert)) { + if (_enabled) { EvseProperties props; if (_changed) { props.setMaxCurrent(_max_cur); @@ -116,7 +116,19 @@ void CurrentShaperTask::setState(bool state) { void CurrentShaperTask::shapeCurrent() { _updated = true; - _max_cur = round(((_max_pwr - _live_pwr) / evse.getVoltage()) + (evse.getAmps())); + // adding self produced energy to total + int max_pwr = _max_pwr; + if (config_divert_enabled()) { + if (mqtt_solar != "") { + max_pwr += solar; + } + else if (mqtt_grid_ie != "" && (grid_ie <= 0)) { + max_pwr -= grid_ie; + } + } + _max_cur = round(((max_pwr - _live_pwr) / evse.getVoltage()) + (evse.getAmps())); + + _changed = true; } diff --git a/src/current_shaper.h b/src/current_shaper.h index 3e3bd115..1ea1b80f 100644 --- a/src/current_shaper.h +++ b/src/current_shaper.h @@ -17,6 +17,7 @@ #include "http_update.h" #include "input.h" #include "event.h" +#include "divert.h" class CurrentShaperTask: public MicroTasks::Task { @@ -34,14 +35,12 @@ class CurrentShaperTask: public MicroTasks::Task protected: void setup(); unsigned long loop(MicroTasks::WakeReason reason); - void shapeCurrent(); - public: CurrentShaperTask(); ~CurrentShaperTask(); void begin(EvseManager &evse); - + void shapeCurrent(); void setMaxPwr(int max_pwr); void setLivePwr(int live_pwr); void setState(bool state); diff --git a/src/mqtt.cpp b/src/mqtt.cpp index 7df25a8b..12069800 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -33,6 +33,7 @@ static bool connecting = false; static bool mqttRetained = false; uint8_t claimsVersion = 0; uint8_t overrideVersion = 0; +uint8_t scheduleVersion = 0; String lastWill = ""; @@ -64,12 +65,21 @@ void mqttmsg_callback(MongooseString topic, MongooseString payload) { solar = payload_str.toInt(); DBUGF("solar:%dW", solar); divert.update_state(); + //recalculate shaper + if (shaper.getState()) { + shaper.shapeCurrent(); + } + } else if (topic_string == mqtt_grid_ie) { grid_ie = payload_str.toInt(); DBUGF("grid:%dW", grid_ie); divert.update_state(); + //recalculate shaper + if (shaper.getState()) { + shaper.shapeCurrent(); + } } else if (topic_string == mqtt_live_pwr) { @@ -521,10 +531,16 @@ mqtt_loop() { } if (overrideVersion != manual.getVersion()) { - mqtt_publish_override; + mqtt_publish_override(); DBUGF("Override has changed, publishing to MQTT"); overrideVersion = manual.getVersion(); } + + if (scheduleVersion != scheduler.getVersion()) { + mqtt_publish_schedule(); + DBUGF("Schedule has changed, publishing to MQTT"); + scheduleVersion = scheduler.getVersion(); + } } Profile_End(mqtt_loop, 5); } diff --git a/src/web_server.cpp b/src/web_server.cpp index 25ecf13b..f43fade9 100644 --- a/src/web_server.cpp +++ b/src/web_server.cpp @@ -244,6 +244,7 @@ void buildStatus(DynamicJsonDocument &doc) { doc["charge_rate"] = divert.getChargeRate(); doc["divert_update"] = (millis() - divert.getLastUpdate()) / 1000; doc["divert_active"] = divert.isActive(); + doc["divertmode"] = (uint8_t)divert.getMode(); doc["shaper"] = shaper.getState()?1:0; doc["shaper_live_pwr"] = shaper.getLivePwr(); @@ -708,12 +709,20 @@ void handleStatusPost(MongooseHttpServerRequest *request, MongooseHttpServerResp solar = doc["solar"]; DBUGF("solar:%dW", solar); divert.update_state(); + // recalculate shaper + if (shaper.getState()) { + shaper.shapeCurrent(); + } send_event = false; // Divert sends the event so no need to send here } else if(doc.containsKey("grid_ie")) { grid_ie = doc["grid_ie"]; DBUGF("grid:%dW", grid_ie); divert.update_state(); + // recalculate shaper + if (shaper.getState()) { + shaper.shapeCurrent(); + } send_event = false; // Divert sends the event so no need to send here } if(doc.containsKey("battery_level")) {