Hello all,
Started with HA a few days ago and hence still a complete Noob, whereas i’m trying to integrate my BMS (Battery Management System) with the HA system. Eventhough i followed different instructions, tried mutiple methods i’m still unable to read a simple value. What i can’t find it how to debug jSon related requests as i can’t even see if the call is formatted correct and if there is a response.
in C# i have already a working configuration which logs to a SQL engine, whereas i want to get rid of this as HA has all features needed.
So we are calling: http://192.168.0.21/JsonHandle
Posting: {“type”:“dashboard”}
content-type: application/json
then the response is:
[
{
“type”: “dashboard”,
“status”: {
“current”: " -7.5",
“event”: “Restarted “,
“SoC”: 47,
“PackV”: “51.46”,
“MaxC”: " 3.68”,
“MinC”: " 3.65”
},
“TempProbes”: {
“Die Temp”: 22,
“MCU Temp”: 18,
“Aux_1”: 14,
“Aux_2”: 15,
“Aux_3”: 15,
“Aux_4”: 15,
“Aux_5”: 15
},
“IO_States”: {
“IN1”: 0,
“IN2”: 0,
“DO1”: 0,
“DO2”: 0,
“CHG”: 1,
“DSC”: 1
}
}
]
in the configuration.yaml i inserted:
rest:
resource: http://192.168.0.21/JsonHandle
method: POST
payload: ‘{“type”:“cellstates”}’
scan_interval: 60
sensor:
- name: “Moonitor21 Current”
value_template: “{{ value_json[0].status.current }}”
device_class: current
unit_of_measurement: “A”
- name: “Moonitor21 Voltage”
value_template: “{{ value_json[0].status.PackV }}”
device_class: voltage
unit_of_measurement: “V”
though there is no response. Below is the C# code that i’m using to pull this json out of the Moonitor16S BMS. If someone has an idea as i already parced the json in jsonpathfinder to structure the value_template but that didn’t help. I see the details in the sensor overview but no values.
foreach (string strBms in lBmsIPs)
{
CellData cellData = new CellData();string URL = "http://" + strBms + "/JsonHandle"; string response = GetWebResponse(URL, "{\"type\":\"dashboard\"}"); // https://app.quicktype.io/ Console.WriteLine(response); List<Dashboard> l = Dashboard.FromJson(response); if (null == l) { _log.WriteLogLine("Unable to connect to BMS", CandeUtils.eMessageLevel.Warning); continue; } cellData.DieTemp = l[0].TempProbes.DieTemp; cellData.McuTemp = l[0].TempProbes.McuTemp; cellData.PackVoltage = l[0].StatusDashboard.PackV; cellData.Current = l[0].StatusDashboard.Current; cellData.SOC = l[0].StatusDashboard.SoC; URL = "http://" + strBms + "/JsonHandle"; response = GetWebResponse(URL, "{\"type\":\"cellStates\"}"); Console.WriteLine(response); List<CellStates> c = CellStates.FromJson(response); if (null == c) { _log.WriteLogLine("Unable to connect to BMS", CandeUtils.eMessageLevel.Warning); continue; } cellData.Cell1 = c[0].Cells.Cell1; cellData.Cell2 = c[0].Cells.Cell2; cellData.Cell3 = c[0].Cells.Cell3; cellData.Cell4 = c[0].Cells.Cell4; cellData.Cell5 = c[0].Cells.Cell5; cellData.Cell6 = c[0].Cells.Cell6; cellData.Cell7 = c[0].Cells.Cell7; cellData.Cell8 = c[0].Cells.Cell8; cellData.Cell9 = c[0].Cells.Cell9; cellData.Cell10 = c[0].Cells.Cell10; cellData.Cell11 = c[0].Cells.Cell11; cellData.Cell12 = c[0].Cells.Cell12; cellData.Cell13 = c[0].Cells.Cell13; cellData.Cell14 = c[0].Cells.Cell14; cellData.Cell15 = c[0].Cells.Cell15; cellData.Cell16 = c[0].Cells.Cell16; cellData.Current = -c[0].statusCellStates.Current; if (!_sql.AddCellData(cellData)) { _log.WriteLogLine("Failed to add record to DB.", CandeUtils.eMessageLevel.Warning); Console.WriteLine("Failed to add record to DB."); } else { _log.WriteLogLine("Record added to DB.", CandeUtils.eMessageLevel.Notice); Console.WriteLine("Record added to DB."); } URL = "http://" + strBms + "/JsonHandle"; response = GetWebResponse(URL, "{\"type\":\"readSettings\"}"); Console.WriteLine(response); List<Settings> s = Settings.FromJson(response); if (null == s) { _log.WriteLogLine("Unable to connect to BMS", CandeUtils.eMessageLevel.Warning); continue; } } }
private string GetWebResponse(string URL, string jSon)
{
string serverAddress = GetServerAddressFromUrl(URL);
string result = string.Empty;
var httpWebRequest = (HttpWebRequest)WebRequest.Create(URL);
httpWebRequest.Method = “POST”;
httpWebRequest.ContentType = “application/json”;
httpWebRequest.UserAgent = “python-requests/2.22.0”;
httpWebRequest.Accept = “.”;
httpWebRequest.ContentLength = jSon.Length;
httpWebRequest.Headers.Add(“Accept-Encoding”, “gzip,deflate”); //<-- added 29 aug 2019
httpWebRequest.ProtocolVersion = HttpVersion.Version10;
httpWebRequest.ContinueTimeout = 1000;
httpWebRequest.Proxy = null;WebResponse httpResponse = null; StreamReader streamReader = null; StreamWriter streamWriter = null; Stream rStream = null; try { rStream = httpWebRequest.GetRequestStream(); streamWriter = new StreamWriter(rStream); jSon = jSon.Replace("\r\n", ""); streamWriter.Write(jSon); streamWriter.Flush(); httpResponse = httpWebRequest.GetResponse(); streamReader = new StreamReader(httpResponse.GetResponseStream()); result = streamReader.ReadToEnd(); streamWriter.Close(); rStream.Close(); streamReader.Close(); httpResponse.Close(); } catch (Exception ex) { if (null != streamWriter) streamWriter.Close(); if (null != rStream) rStream.Close(); if (null != streamReader) streamReader.Close(); if (null != httpResponse) httpResponse.Close(); string errorMsg = "(" + serverAddress + ") GetWebResponse(" + URL + ", " + jSon + "): " + ex.Message; return string.Empty; } _log.WriteLogLine(result, CandeUtils.eMessageLevel.Informational); return result; }