Add Hass sensors Battery voltage and Current
This commit is contained in:
parent
4f0a45c902
commit
997023e52f
@ -14,6 +14,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
void publish(const String& subtopic, const String& payload);
|
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 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);
|
void createDeviceInfo(JsonObject& object);
|
||||||
|
|
||||||
bool _wasConnected = false;
|
bool _wasConnected = false;
|
||||||
|
|||||||
@ -51,12 +51,56 @@ void MqttHandleVedirectHassClass::publishConfig()
|
|||||||
return;
|
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();
|
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)
|
void MqttHandleVedirectHassClass::publishBinarySensor(const char* caption, const char* subTopic, const char* payload_on, const char* payload_off)
|
||||||
{
|
{
|
||||||
String serial = VeDirect.veMap["SER"];
|
String serial = VeDirect.veMap["SER"];
|
||||||
@ -69,11 +113,10 @@ void MqttHandleVedirectHassClass::publishBinarySensor(const char* caption, const
|
|||||||
+ "/" + sensorId
|
+ "/" + sensorId
|
||||||
+ "/config";
|
+ "/config";
|
||||||
|
|
||||||
// String statTopic = MqttSettings.getPrefix() + serial + "/" + subTopic; // TODO extend with serial
|
String statTopic = MqttSettings.getPrefix() + "victron/" + VeDirect.veMap["SER"] + "/" + subTopic;
|
||||||
String statTopic = MqttSettings.getPrefix() + subTopic;
|
|
||||||
|
|
||||||
DynamicJsonDocument root(1024);
|
DynamicJsonDocument root(1024);
|
||||||
root[F("name")] = String("Victron ") + caption;
|
root[F("name")] = caption;
|
||||||
root[F("uniq_id")] = serial + "_" + sensorId;
|
root[F("uniq_id")] = serial + "_" + sensorId;
|
||||||
root[F("stat_t")] = statTopic;
|
root[F("stat_t")] = statTopic;
|
||||||
root[F("pl_on")] = payload_on;
|
root[F("pl_on")] = payload_on;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user