Skip to content

Commit

Permalink
Add Hass sensors Battery voltage and Current
Browse files Browse the repository at this point in the history
  • Loading branch information
helgeerbe committed Feb 7, 2023
1 parent 4f0a45c commit 997023e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
1 change: 1 addition & 0 deletions include/MqttHandleVedirectHass.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class MqttHandleVedirectHassClass {
private:
void publish(const String& subtopic, const String& payload);
void publishBinarySensor(const char* caption, const char* subTopic, const char* payload_on, const char* payload_off);
void publishSensor(const char* caption, const char* subTopic, const char* deviceClass = NULL, const char* stateClass = NULL, const char* unitOfMeasurement = NULL);
void createDeviceInfo(JsonObject& object);

bool _wasConnected = false;
Expand Down
51 changes: 47 additions & 4 deletions src/MqttHandlVedirectHass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,56 @@ void MqttHandleVedirectHassClass::publishConfig()
return;
}

publishBinarySensor("Load output state", "victron/LOAD", "ON", "OFF");
publishBinarySensor("Load output state", "LOAD", "ON", "OFF");

// battery info
publishSensor("Battery voltage", "V", "voltage", "measurement", "mV");
publishSensor("Battery current", "I", "current", "measurement", "mA");

yield();
}

void MqttHandleVedirectHassClass::publishSensor(const char* caption, const char* subTopic, const char* deviceClass, const char* stateClass, const char* unitOfMeasurement )
{
String serial = VeDirect.veMap["SER"];

String sensorId = caption;
sensorId.replace(" ", "_");
sensorId.toLowerCase();

String configTopic = "sensor/dtu_victron_" + serial
+ "/" + sensorId
+ "/config";

String statTopic = MqttSettings.getPrefix() + "victron/" + VeDirect.veMap["SER"] + "/" + subTopic;

DynamicJsonDocument root(1024);
root[F("name")] = caption;
root[F("stat_t")] = statTopic;
root[F("uniq_id")] = serial + "_" + sensorId;

if (unitOfMeasurement != NULL) {
root[F("unit_of_meas")] = unitOfMeasurement;
}

JsonObject deviceObj = root.createNestedObject("dev");
createDeviceInfo(deviceObj);

if (Configuration.get().Mqtt_Hass_Expire) {
root[F("exp_aft")] = Configuration.get().Mqtt_PublishInterval * 3;
}
if (deviceClass != NULL) {
root[F("dev_cla")] = deviceClass;
}
if (stateClass != NULL) {
root[F("stat_cla")] = stateClass;
}

char buffer[512];
serializeJson(root, buffer);
publish(configTopic, buffer);

}
void MqttHandleVedirectHassClass::publishBinarySensor(const char* caption, const char* subTopic, const char* payload_on, const char* payload_off)
{
String serial = VeDirect.veMap["SER"];
Expand All @@ -69,11 +113,10 @@ void MqttHandleVedirectHassClass::publishBinarySensor(const char* caption, const
+ "/" + sensorId
+ "/config";

// String statTopic = MqttSettings.getPrefix() + serial + "/" + subTopic; // TODO extend with serial
String statTopic = MqttSettings.getPrefix() + subTopic;
String statTopic = MqttSettings.getPrefix() + "victron/" + VeDirect.veMap["SER"] + "/" + subTopic;

DynamicJsonDocument root(1024);
root[F("name")] = String("Victron ") + caption;
root[F("name")] = caption;
root[F("uniq_id")] = serial + "_" + sensorId;
root[F("stat_t")] = statTopic;
root[F("pl_on")] = payload_on;
Expand Down

0 comments on commit 997023e

Please sign in to comment.