PiotrMachowski / Home-Assistant-custom-components-Tauron-AMIplus

This sensor uses unofficial API to get energy usage and generation data from https://elicznik.tauron-dystrybucja.pl.
MIT License
138 stars 34 forks source link

secondary energy meter #54

Closed acuszka closed 1 year ago

acuszka commented 2 years ago

Hi Piotr,

First of all, let me say thank you for writing this integration. It worked great when I had only one energy meter. There's now a secondary energy meter assigned to my profile in eLicznik. I want to display the data only for the secondary meter. I modified config.yaml with the new energy_meter_id, and restarted HA, but it still pulls the information for the primary meter.

PiotrMachowski commented 2 years ago

Do you have the latest version of the integration installed?

acuszka commented 2 years ago

Yes, I have the newest version.

image

grest commented 2 years ago

I have the same problem, or I'm using the wrong id.... I copy pasted the value from dropdown, which is the 18 digits number. Still, the values for the first meter are extracted

grest commented 2 years ago

I solved it! I've opened the developer tools, and copy pasted the value ofthe 'valute' attribute within the

PiotrMachowski commented 2 years ago

Maybe your solution is the same? @tacuszka

acuszka commented 2 years ago

I solved it! I've opened the developer tools, and copy pasted the value ofthe 'valute' attribute within the html tag.

I don't see 'valute' attribute in dev tools. Did you mean the 'value' attribute? The one with the underscores on the image below? (<option value = "5xxx_6xxxx_6xxxx)

image

PiotrMachowski commented 2 years ago

It is visible in "network" tab, in parameters of call to "charts" address

acuszka commented 2 years ago

It is visible in "network" tab, in parameters of call to "charts" address

Can you give me more details on how to find it? I clicked through all the entries in the network tab, but I don't see anything I could use

PiotrMachowski commented 2 years ago

image

Open dev tools and refresh the page. A call to "charts" should be visible. The value you need is passed in dane[smartNr]

acuszka commented 2 years ago

Open dev tools and refresh the page. A call to "charts" should be visible. The value you need is passed in dane[smartNr]

We're talking about https://elicznik.tauron-dystrybucja.pl/ correct?

image

PiotrMachowski commented 2 years ago

Yup. Do you have a chart visible?

acuszka commented 2 years ago

Ok, got it. It shows up only when you open the day/month/year tab for the first time. It gives me the same value as on my first dev tools screenshot (5xxx_6xxxx_6xxxx). I already tried passing it to configuration.yaml without luck. I'll remove the Tauron integration from HA and try again.

image

PiotrMachowski commented 2 years ago

You can also try to reinstall it in HACS

acuszka commented 2 years ago

You can also try to reinstall it in HACS

Removed Tauron-AMIplus integration using HACS. I deleted all the orphaned entities. Restarted HA. Installed the integration again. No matter what I put in the energy meter ID (value visible on the page or the danepsmartNr] value), it always downloads the first energy meter data.

@grest, how did you do it? :D

acuszka commented 2 years ago

@grest ?

lkuznicki commented 2 years ago

Hi I have got the same issue I tried all numbers: Płatnik, Punkt poboru, nr licznika, smart_id całe i pojedyncze człony oddzielone podkreślnikami...and nothing. @tacuszka but I have found workaround - if you disable your first meter in settings (on the eliczniki web page) then HA reads the second meter. I personally do not like because my parents needs access to their data but maybe it works for you. I will be trying more but later ... Tauron has blocked my account because of too many logins ;)

acuszka commented 2 years ago

Hi I have got the same issue I tried all numbers: Płatnik, Punkt poboru, nr licznika, smart_id całe i pojedyncze człony oddzielone podkreślnikami...and nothing. @tacuszka but I have found workaround - if you disable your first meter in settings (on the eliczniki web page) then HA reads the second meter. I personally do not like because my parents needs access to their data but maybe it works for you. I will be trying more but later ... Tauron has blocked my account because of too many logins ;)

Unfortunately, I need to keep both meters available on the Tauron web portal. I need only the second one in the HA tho.

mariusz-schimke commented 1 year ago

@tacuszka, czy mógłbym Cię prosić o wsparcie? Dodałem obsługę odczytu całkowitego poboru energii (stanu licznika), ale zastanawiam się, czy to będzie działać dla wielu liczników. Czy mógłbym Cię prosić o dostarczenie mi odpowiedzi na zapytanie o stan licznika – tak, jak to opisałem tutaj? Będę wdzięczny, jeśli to nie problem. Zobaczę wtedy, jaki jest format odpowiedzi przy wielu licznikach. Dziękuję!! :)

grest commented 1 year ago

@grest ?

I solved it! I've opened the developer tools, and copy pasted the value ofthe 'valute' attribute within the html tag.

I don't see 'valute' attribute in dev tools. Did you mean the 'value' attribute? The one with the underscores on the image below? (<option value = "5xxx_6xxxx_6xxxx)

image

Of course, it's value attribute. Exactly as on your screenshot.

mariusz-schimke commented 1 year ago

Sorry for mixing topics here, but since you both have multiple meters, maybe you could help me find out how the data is structured when requesting readings for a specific date, @grest? I described here what I would need to handle readings of the meter that is specified in the configuration of the add on. Will be grateful, thanks!

grest commented 1 year ago

Sorry for mixing topics here, but since you both have multiple meters, maybe you could help me find out how the data is structured when requesting readings for a specific date, @grest? I described here what I would need to handle readings of the meter that is specified in the configuration of the add on. Will be grateful, thanks!

<div class="clear"></div>Pobór:<br /><span class="name"> <b></b> 02.11.2022 (23:59:59)</span> <span original-title="Nr licznika: N000000000000" class="value tipsyOnTop"> 002040</span><span class="unit">kWh </span><br /><div class="clear"></div><br />Oddanie:<br /><span class="name"> <b></b> 02.11.2022 (23:59:59)</span> <span original-title="Nr licznika: N000000000000" class="value tipsyOnTop"> 003040</span><span class="unit">kWh </span><br />

mariusz-schimke commented 1 year ago

Many thanks! This reading includes only one meter, though. I thought it would be a list of generations and consumptions for multiple meters. Is the meter somehow preselected on the website?

I also noticed that the meter number may contain letters, which I didn't take into consideration when formulating a regular expression.

Thanks!

grest commented 1 year ago

There is a dropdown on the page, which sets the page context. After value changed, the whole page is reloaded. I thought the meter id will be later on a part of a request, but unfortunately I cannot find it anywhere. I'll try again.

mariusz-schimke commented 1 year ago

Isn't it specified in the session cookie perchance?

acuszka commented 1 year ago

@tacuszka, czy mógłbym Cię prosić o wsparcie? Dodałem obsługę odczytu całkowitego poboru energii (stanu licznika), ale zastanawiam się, czy to będzie działać dla wielu liczników. Czy mógłbym Cię prosić o dostarczenie mi odpowiedzi na zapytanie o stan licznika – tak, jak to opisałem tutaj? Będę wdzięczny, jeśli to nie problem. Zobaczę wtedy, jaki jest format odpowiedzi przy wielu licznikach. Dziękuję!! :)

Do you still need this, or is @grest answer enough? I can do some quick checks if you need anything else.

mariusz-schimke commented 1 year ago

@tacuszka, thanks for asking. What I'm trying to test is whether you will always get total values from the correct meter specified in the add-on's configuration. But if the meter context (PPE?) is selected globally, then maybe there is no problem (I can't test that though).

I don't want to bother you, but if you like, you can download the version from here, and follow the steps from here. If you then get total readings from the meter you specified in the configuration, then everything probably works fine.

But no pressure, I'm pretty sure it should be working correctly. Thanks!

acuszka commented 1 year ago

@tacuszka, thanks for asking. What I'm trying to test is whether you will always get total values from the correct meter specified in the add-on's configuration. But if the meter context (PPE?) is selected globally, then maybe there is no problem (I can't test that though).

I don't want to bother you, but if you like, you can download the version from here, and follow the steps from here. If you then get total readings from the meter you specified in the configuration, then everything probably works fine.

But no pressure, I'm pretty sure it should be working correctly. Thanks!

Unfortunately, I can't install the new version manually at the moment. Can you publish it as a beta in HACS? Also, if you need to see how the Tauron portal behaves with two energy meters, I'm open for call and screen-sharing sessions.

mariusz-schimke commented 1 year ago

Oh, I'm not sure if I can do that not being an owner of this repository (I have never done that, I don't use HACS). Maybe let us wait for @PiotrMachowski to publish it when he has time to do so?

When it comes to multiple energy meters, since they are not returned all when you go to readings, but when you change the context, then you get only the readings of one associated meter, I'm pretty sure the add-on will work as expected, because you provide a PPE number in its configuration (which, I assume, represents the context). Unless you suspect it's not that simple?

PiotrMachowski commented 1 year ago

@mariusz-schimke yes, there's a request to change a context (here), unfortunately I'm not able to test it on my account

mariusz-schimke commented 1 year ago

@PiotrMachowski, thanks for your input! So it seems that we can confirm this should work as it is. Thanks for your screen sharing session offer, though, @acuszka!!

grest commented 1 year ago

@PiotrMachowski , I'd like to sent you privately a HAR file // I'm not sure how to anonymise it correctly and removing important debug data // with a complete request and response for ustaw_punkt operation. How to sort this out? Any network share?

PiotrMachowski commented 1 year ago

@grest HAR file should contain a JSON, you can edit it in a text editor to remove sensitive data. You can send it to piotr.machowski.dev [at] gmail.com

grest commented 1 year ago

Sent. I also exported this particular call into the node.js call format and copy pasted below. 12345678_01234567_1234567 is my meter ID, which can be found in the value attribute of the dropdown.

fetch("https://elicznik.tauron-dystrybucja.pl/ustaw_punkt", {
  "headers": {
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "accept-language": "pl-PL,pl;q=0.9,en-GB;q=0.8,en;q=0.7,en-US;q=0.6,ru;q=0.5",
    "cache-control": "max-age=0",
    "content-type": "application/x-www-form-urlencoded",
    "sec-ch-ua": "\"Not_A Brand\";v=\"99\", \"Google Chrome\";v=\"109\", \"Chromium\";v=\"109\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "navigate",
    "sec-fetch-site": "same-origin",
    "upgrade-insecure-requests": "1",
    "cookie": <removed>
    "Referer": "https://elicznik.tauron-dystrybucja.pl/",
    "Referrer-Policy": "strict-origin-when-cross-origin"
  },
  "body": "site%5Bclient%5D=12345678_01234567_1234567",
  "method": "POST"
});
PiotrMachowski commented 1 year ago

@grest thank you!

grest commented 1 year ago

@grest thank you!

Is it enough information to fix this issue?

PiotrMachowski commented 1 year ago

@grest I hope so

PiotrMachowski commented 1 year ago

@grest Can you try applying following change and checking if it works?

file to change

modification:

        )
-       # session.request("POST", CONF_URL_SERVICE, data={"smart": self.meter_id}, headers=CONST_REQUEST_HEADERS)
-       # https://elicznik.tauron-dystrybucja.pl/ustaw_punkt # TODO
+       payload_select_meter = {"site[client]", self.meter_id}
+       session.request("POST", "https://elicznik.tauron-dystrybucja.pl/ustaw_punkt", data=payload_select_meter, headers=CONST_REQUEST_HEADERS)
        self.session = session
grest commented 1 year ago

I had errors in my logs - expecting binary not a str (unfortunately I did not save it). I replaced ',' with ':' in payload_select_meter initiation and reorganized code a little bit. Following implementation works fine:

    def login(self):
        payload_login = {
            "username": self.username,
            "password": self.password,
            "service": CONST_URL_SERVICE,
        }
        payload_select_meter = {
                "site[client]": self.meter_id,
        }
        session = requests.session()
        session.mount("https://", TLSAdapter())
        session.request(
            "POST",
            CONST_URL_LOGIN,
            data=payload_login,
            headers=CONST_REQUEST_HEADERS,
        )
        session.request(
            "POST",
            CONST_URL_LOGIN,
            data=payload_login,
            headers=CONST_REQUEST_HEADERS,
        )
        session.request(
            "POST", 
            "https://elicznik.tauron-dystrybucja.pl/ustaw_punkt", 
            data=payload_select_meter, 
            headers=CONST_REQUEST_HEADERS,
        )
        self.session = session
PiotrMachowski commented 1 year ago

I replaced ',' with ':' in payload_select_meter initiation

Whoops, that was a typo.

Thank you for testing! Do you have 2 meters available for the same account?

grest commented 1 year ago

Yes, I do. It works fine for both meters. Thanks!

PiotrMachowski commented 1 year ago

Thank you! I will release it today (alongside support for hourly data in energy dashboard)

grest commented 1 year ago

No problem. Last question - is it normal that HA displays the data from the day before yesterday (16.01) not from yesterday (17.01)? On the e-licznik website, I see the data from yesterday already.

PiotrMachowski commented 1 year ago

This integration is refreshed every 8h, you probably just have to wait

PiotrMachowski commented 1 year ago

Feature added in v2.3.0

acuszka commented 1 year ago

Thank you! I will release it today (alongside support for hourly data in energy dashboard)

ok, I started fresh today - deleted Tauron integration from HACS, deleted all orphaned entities, and related config.yaml entries. I installed v2.3.0 using HACS. Added Tauron integration using Settings > Devices and Services > Add integration. Typed in the user, password and energy meter ID in the popup. Unfortunately, it still pulled the data for the other energy meter.

I logged in to eLicznik to triple-check the energy meter ID. I noticed a new, modern interface (Tauron probably updated it recently?).

image

It turned out that now in the eLicznik settings, there's an option to disable chosen energy meter from showing up. I did that. I restarted HA, and voila, now it pulls the data for the energy meter I need.

image