custom-components / zaptec

zaptec charger custom component for home assistant
MIT License
66 stars 20 forks source link

Get power information from Zaptec Sense #123

Closed baunan closed 1 month ago

baunan commented 1 month ago

Could be a dublicate of this request which is closed, https://github.com/custom-components/zaptec/issues/121

I have a Zaptec Sense installed and the information from the Sense unit generates this figure. image

I see from the portal that the request for info goes to the api https://api.zaptec.com/api/installation/xxxxxxx-yyyy-dddd-cccc-ffffffff/energySensorData?from=2024-10-02T13:47:03.000Z&to=2024-10-04T13:57:25.716Z&resolution=day1 Payload: from=2024-10-03T08:44:49.000Z&to=2024-10-05T08:49:18.424Z&resolution=day1 From the request the following json formated information is provided see attachment. power-json.txt For my part it would the power reading that is of interest to get a sensor value on so that I can track the total power consumption for the house, but there could be additional information here of interest. Would it be possible to get this info?

sveinse commented 1 month ago

A couple of questions springs to mind:

The second question is important. Please be advised that HA doesn't really handle historic data coming from other systems such as Zaptec. It's built on sampling data here and now and any history you see in HA is based on the gathered data. So I would assume that we would only be interested in the latest measurements.

PS! I do not have Zaptec sense, so it will be hard to develop anything for it, since I am unable to test anything.

baunan commented 1 month ago

As mentioned what I was after is the power reading, so that I can use the Sense unit as a HAN sensor. So in node-red I have made a sensor which just grab the last value in the array. As from the attachement the data is updates about every second minute, so that is how frequent I update that sensor. The URL is the api link (https://api.zaptec.com/api/installation/xxxxxxx-yyyy-dddd-cccc-ffffffff/energySensorData?from=2024-10-02T13:47:03.000Z&to=2024-10-04T13:57:25.716), please note that it is the installation ID that is used (so replace x-y-d-c-f with that) , but the api token is the same. In node-red I just pull the data from the last hour

sveinse commented 1 month ago

As far as my understanding of HA, it's not possible to do a request with the data from the last hour and inject this into the history. It can only store the instantaneous value at the time it has been sampled. So any data in HAs sensors will only have the resolution equal to the poll rate.

Other components, such as nordpool, reads the electricity price for the next 24 hours and stores the values as hourly attributes. The user must then use some custom templates to extract the information for display and graphing. That's also possible here, but it will create some work to make a display as above.

baunan commented 1 month ago

The graph will easy enough to make once you collect the data, so not sure why you are so focus on that. As per my initialt request, if you have the Sense install you also get access to the house total power consumption. That informtion could be of important for somebody as like myself as then I don't have to install a separate HAN sensor for that. So perhaps start by grabbing that information to HA? As saif before via Node-red I grab this information by requesting the last hour and just grab the last value in the array and send it to the HA sensor.

sveinse commented 1 month ago

How do you suggest the development of this should be done? Let's ask the obvious question: Are you able to make a PR with the code changes? Please be advised: I am unfortunately very busy at the moment, and I've got limited time to support and develop this and cannot reliably guarantee when I can look at it. Help wanted. Since I don't have Zaptec Sense (and I don't plan to since I have another HAN), I won't be able to test any of the things if I'm the one doing the implementation. It's a tedious way of doing development.

Is the API for the Sense officially documented anywhere? I'd prefer using official APIs if possible, as reverse engineered ones tend to be changed or removed in time.

On the technical side, what happens if you call the API link without any additional parameters?https://api.zaptec.com/api/installation/xxxxxxx-yyyy-dddd-cccc-ffffffff/energySensorData What kind of response and data do you get?

I think the easiest would be to link this to the general Zaptec integration data poll rate that happens every 30 or 60 seconds. If that's too often, it probably would need a separate timer. I'm a little reluctant to make too much special logic into the integration, since this the HAN functionality is not the main intent for the zaptec integration.

baunan commented 1 month ago

I see, as per the API information for the Sense you should stay below 150 request per hour, so if you pull by every 30seconds you will go above. It is easy enough to get additional Sense data via the Node-red integration, so if somebody need help with that, then contact me. I will close the request with this. Thanks for your feedback on this case.

keviny86 commented 1 month ago

@baunan did you manage to get total power consumption for the house? If so, can you share your solution? Thanks!

baunan commented 1 month ago

Hi, I do it Node-red This is how it look like. image

Request is made every 5min in the injector

The send block looks like this

msg.headers = {
    'content-type':'application/x-www-form-urlencoded'};

msg.payload = {
    'username': 'your email',
    'password': 'your password',
    'grant_type': 'password'
}
return msg

request like this image

Token like this

var d = new Date();
//var IDAG = d;
var IDAG = d.toISOString();
msg.nowdate = IDAG
//var y = new Date(new Date().setDate(new Date().getDate() - 1));
var y = new Date(new Date().setHours(new Date().getHours() - 1));

var yesterday = y.toISOString();
msg.yesterdaydate = yesterday

var token = msg.payload.access_token
//var token = 123
var token1 = 'Bearer ' + token
msg.headers = {
    'Authorization': token1,
    'Accept': 'text/plain'
}
return msg;

Power node: https://api.zaptec.com/api/installation/your_installation_id/energySensorData?from={{{yesterdaydate}}}&to={{{nowdate}}} image

And the last one to get the power reading

let pe = msg.payload.Readings;
let e = pe.map(x => x.Power);
msg.payload = e;
var my_array = e;
var last_element = e[e.length - 1];
msg.power = last_element;
return msg;

Then it just to send msg.power to a HA via the sensor node

keviny86 commented 1 month ago

So by doing this api request you get real time house power consumption?

baunan commented 1 month ago

Yes, that is correct.

keviny86 commented 1 month ago

It would be nice if we could get this into this component :)

sveinse commented 1 month ago

What happens if the URL is called without the from= and to= arguments? What does it return?

baunan commented 1 month ago

@sveinse It doesn't return anything when I try it.

sveinse commented 1 month ago

@baunan ok thanks. So the API is dependent on a time range to return any data.