Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for libbi #395

Closed
plord12 opened this issue Aug 12, 2023 · 45 comments · Fixed by #418
Closed

Add support for libbi #395

plord12 opened this issue Aug 12, 2023 · 45 comments · Fixed by #418

Comments

@plord12
Copy link

plord12 commented Aug 12, 2023

Is your feature request related to a problem? Please describe.
Add support for the libbi battery

Describe the solution you'd like
Full integration to libbi, inc charge status, charge from grid, charge from solar, integration into Energy dashboard etc

Describe alternatives you've considered
None

Additional context
I see some API info is available here https://myenergi.info/libbi-api-t9910.html

@Future-Surfer
Copy link

another vote for this feature please. Happy to be a guinea pig if required :)

@videojedi
Copy link
Contributor

Me too. Happy to test, libbi fitted yesterday.

@simonjgreen
Copy link

simonjgreen commented Sep 8, 2023

Because myenergi don't officially operate a public API this hampers development against new products they release. There is active discussion reverse engineering the libbi portion of the API on their forum . This add-on utilises the pymyenergi upstream library which is there you may want to redirect requests for libbi support.

(I would also love to have libbi in home assistant!)

reedy added a commit to reedy/ha-myenergi that referenced this issue Sep 8, 2023
* Fixes a typo
* Add a list of "common" sensors (firmware version, serial number etc)
* Adds a few more sensors etc that are exposed
* Mention that libbi isn't currently supported; CJNE#395
reedy added a commit to reedy/ha-myenergi that referenced this issue Sep 8, 2023
* Fixes a typo
* Add a list of "common" sensors (firmware version, serial number etc)
* Adds a few more sensors etc that are exposed
* Mention that libbi isn't currently supported; CJNE#395
reedy added a commit to reedy/ha-myenergi that referenced this issue Sep 8, 2023
* Fixes a typo
* Add a list of "common" sensors (firmware version, serial number etc)
* Adds a few more sensors etc that are exposed
* Mention that libbi isn't currently supported; CJNE#395
* Document how to change the API key; CJNE#400
reedy added a commit to reedy/ha-myenergi that referenced this issue Sep 8, 2023
* Fixes a typo
* Add a list of "common" sensors (firmware version, serial number etc)
* Adds a few more sensors etc that are exposed
* Mention that libbi isn't currently supported; CJNE#395
* Document how to change the API key; CJNE#400
@plord12
Copy link
Author

plord12 commented Sep 12, 2023

There is a pull request for pymyenergi here CJNE/pymyenergi#10 FYI

@videojedi
Copy link
Contributor

It's been accepted. :) I've not got my head around the integration with this repo and home assistant yet... maybe someone can have a go....?

pymyenergi can now read some basic values from the libbi including state of charge and solar PV ct values.
You can also set the mode (Stopped or normal) and set the priority.

However the ellusive 'charge from grid' command is proving tricky as it seems to talk to the front end server and not the back end like all the other commands....
Probably because the front end is actually turning the grid charging on and off according to the tariff schedule. I've tried guessing what the actual backend endpoint might be, but not luck yet.

@CJNE
Copy link
Owner

CJNE commented Sep 12, 2023

It's been accepted. :) I've not got my head around the integration with this repo and home assistant yet... maybe someone can have a go....?

pymyenergi can now read some basic values from the libbi including state of charge and solar PV ct values. You can also set the mode (Stopped or normal) and set the priority.

However the ellusive 'charge from grid' command is proving tricky as it seems to talk to the front end server and not the back end like all the other commands.... Probably because the front end is actually turning the grid charging on and off according to the tariff schedule. I've tried guessing what the actual backend endpoint might be, but not luck yet.

Getting the basic functionality into pymyenrgi is a very good start on this, the next step would be to add the entities needed for HA, if someone is up for it it should be pretty straightforward to mimic for example the Zappi entities and tweak accordingly to what the Libbi uses.
I can pick it up when I get some time but unfortunately I don't have much of that at the moment :)

@trizmark
Copy link
Contributor

I am having a stab at this (as I got my libbi installed yesterday)...
Screenshot 2023-09-15 at 10 32 52

@trizmark
Copy link
Contributor

@videojedi https://github.com/trizmark/ha-myenergi/tree/add-libbi-support
Added SoC, voltage, frequency and some of the CTs. Will do some more work later.

@videojedi
Copy link
Contributor

videojedi commented Sep 15, 2023

Amazing work!! Shows up nicely in the HACS Power Flow Card Plus

picture

@Future-Surfer
Copy link

apologies if this is a daft question, I had the myenergi custom integration already installed and its currently version 0.0.23.

Do I need to do anything in particular to get the libbi device/entities to show up?

@trizmark
Copy link
Contributor

@Future-Surfer Grab the updated code from my fork: https://github.com/trizmark/ha-myenergi/tree/add-libbi-support
Use the custom_components/myenergi directory and replace your current integration with it.
In case that's too complicated, I've attached a snapshot of the integration, which you can use to replace your current one.
myenergi.tgz

Once I've added all the entities and tested the updates, I'll submit a PR and once that's been merged the update will also show up in HACS.

@trizmark
Copy link
Contributor

Added more stuff to the integration. You can set the priority and the operating mode, though the mode selection seems to be bugged. You can issue a stop, but going back to 'normal' doesn't seem to work. The logbook entry is also missing about the state change. First time messing around with HA integrations, so bear with me....
As my libbi is my vHub there's a clash between the unique IDs generated for the FW version and Serial Nr, so they're missing from the libbi entity, but they're present for 'My Home' (which is the vHub).
Screenshot 2023-09-15 at 14 51 10

@videojedi
Copy link
Contributor

will check the mode stuff again, probably my fault!

@trizmark
Copy link
Contributor

trizmark commented Sep 15, 2023

will check the mode stuff again, probably my fault!

It works fine using pymyenergi directly (myenergi libbi mode Normal), so I'm fairly certain it's the integration code.

@trizmark
Copy link
Contributor

trizmark commented Sep 15, 2023

Ok, so I think it's to do with the various status mappings. We try to equate 0 to Stopped and 1 to Nomal (just how the cgi-libbi-mode endpoint expects it), but right now my libbi is returning 102.
I was wondering if we should use the 'lmo' property rather than the 'sta' property. lmo reads 'local mode of operation' to me and it changes between 'BALANCE' (which is on) and 'STOP' (which is off).

@Future-Surfer
Copy link

@Future-Surfer Grab the updated code from my fork: https://github.com/trizmark/ha-myenergi/tree/add-libbi-support Use the custom_components/myenergi directory and replace your current integration with it. In case that's too complicated, I've attached a snapshot of the integration, which you can use to replace your current one. myenergi.tgz

Once I've added all the entities and tested the updates, I'll submit a PR and once that's been merged the update will also show up in HACS.

Appreciate the handholding! Successfully got it updated and working. Will keep an eye on the thread and test out any other updates you guys make :)

@videojedi
Copy link
Contributor

videojedi commented Sep 15, 2023 via email

@trizmark
Copy link
Contributor

@videojedi Are you working on pymyenergi? I forked it as well, but let's not duplicate efforts.

@videojedi
Copy link
Contributor

videojedi commented Sep 16, 2023 via email

@simonjgreen
Copy link

@videojedi https://github.com/trizmark/ha-myenergi/tree/add-libbi-support Added SoC, voltage, frequency and some of the CTs. Will do some more work later.

I may be being blind, but was looking for where you added the CTs and can't see it in your commits. Mind sharing where that is? I have some time today so was going to test against mine.

@videojedi
Copy link
Contributor

I may be being blind, but was looking for where you added the CTs and can't see it in your commits. Mind sharing where that is? I have some time today so was going to test against mine.

have a look in ha-myenergi/custom-components/myenergi/sensor.py lines 492-568

@trizmark
Copy link
Contributor

@videojedi Would you mind having a look at this line? https://github.com/videojedi/pymyenergi/blob/69e6a19e0f3b17c2275eb5eb9472176db11c4a06/pymyenergi/libbi.py#L184
That throws an error for me.

I have reworked the mode selection and will start updating the HA component.

@G6EJD
Copy link

G6EJD commented Sep 17, 2023

.get() needs two parameters - missing

@videojedi
Copy link
Contributor

removed for now..... still working out energy calcs....
added battery in an out values to cli energy

myenergi libbi energy

generated: 2.39kWh
grid_import: 12.31kWh
grid_export: 0.15kWh
battery_in: 1.22kWh
battery_out: 2.33kWh
pv_total: 0.0kWh
device_boosted: 0.0kWh
device_green: 0.0kWh
device_total: 0.0kWh
ct_internal_load: 0.0kWh
ct_dcpv: 0.0kWh

although, pv_total is only provided on a minute by minute (/cgi-jday-), /cgi-jdayhour- does not include pvp1 values

@simonjgreen
Copy link

simonjgreen commented Sep 17, 2023

We appear to be following the same track @videojedi :D

% myenergi libbi energy
generated: 3.27kWh
grid_import: 17.26kWh
grid_export: 0.53kWh
device_boosted: 0.0kWh
device_green: 0.0kWh
device_total: 0.0kWh
device_charge: 12.87kWh
device_discharge: 10.91kWh
ct_internal_load: 0.0kWh
ct_grid: 0.0kWh
ct_dcpv: 0.0kWh

I went with charge/discharge rather than in/out though to disambiguate.

ive1 appears to be inverter energy, do you agree?

@videojedi
Copy link
Contributor

inverter export? as there is also ivi1 inverter import?

@simonjgreen
Copy link

simonjgreen commented Sep 17, 2023

I'm not sure the pvp1/ive1 values are especially useful. I've confirmed that gep matches my total solar generation, including solar though PV directly connected to libbi as well as solar monitored on two other inverters by CTs on eddis. The total generation is much more meaningful for home assistants context. That is also already exposed at the hub level as it has always been monitored. Having a value that represents the combined inverter output of battery and PV is not particularly helpful imo.

Adding in the bcp/bdp values eg

            "device_charge": round(energy_wh["bcp1"] / 1000, 2),
            "device_discharge": round(energy_wh["bdp1"] / 1000, 2)

and adding in the property values to libbi.py eg

    @property
    def energy_charge(self):
        """Device battery charged from history data"""
        return self.history_data.get("device_charge", 0)
    
    @property
    def energy_discharge(self):
        """Device battery discharged from history data"""
        return self.history_data.get("device_discharge", 0)

and then update ha-myenergi with two additional sensors eg

sensors.append(
    MyenergiSensor(
        coordinator,
        device,
        entry,
        create_energy_meta("Battery charged session", "energy_charge"),
    )
)
sensors.append(
    MyenergiSensor(
        coordinator,
        device,
        entry,
        create_energy_meta("Battery discharged session", "energy_discharge"),
    )
)

or similar...

is the next logical move.

@videojedi
Copy link
Contributor

looking at libbi in myenergi app. it would seem Libbi discharge is actually ive1 minus bdp1??

@simonjgreen
Copy link

simonjgreen commented Sep 17, 2023

Hmm I am unsure. That doesn't track with what I'm seeing, but also my bdp1 and app are not consistent. Compare these:

[trimmed for brevity]
generated: 4.34kWh
grid_import: 17.29kWh
grid_export: 0.59kWh
device_charge[bcp1]: 12.88kWh
device_discharge[bdp1]: 12.44kWh
device_ive1: 14.8kWh

image

(I have just about run out of charge, and it's raining, which is somewhat hampering my ability to test 😄 )

@videojedi
Copy link
Contributor

gray and cloudy here too...

yes inconsistent....

Pasted Graphic

generated: 6.3kWh
grid_import: 13.13kWh
grid_export: 0.36kWh
battery_charge: 1.58kWh
battery_discharge: 3.37kWh
inverter_export: 8.47kWh
inverter_import: 0.97kWh

@videojedi
Copy link
Contributor

So the request from the App is

https://s18.myenergi.net/cgi-jday-Lxxxxxxxx-2023-9-16-23-0-1440

where as the pymyenergi (when using the energy option) is

https://s18.myenergi.net/cgi-jdayhour-Lxxxxxxxx-2023-9-17-0-24

So the app (despite being set to Today) is requesting minute by minute for 24 hours starting 11pm yesterday?

We are asking for hour by hour for today.......

@videojedi
Copy link
Contributor

confirmed by forcing request to same values.....

image

@trizmark
Copy link
Contributor

Finally, finally got stuff working. I spent most of my afternoon trying to figure out how to use a local copy of my updated pymyenergi module.
Mode change is working (and logged by HA):
Screenshot 2023-09-17 at 18 48 14

Also got the actual status in a sensor:

Screenshot 2023-09-17 at 18 51 25

I am attaching the custom component if anybody wants to test it. (It's two repos cobbled together while we're working on the HA integration and the actual pymyenergi module)

myenergi.tgz

@videojedi
Copy link
Contributor

im struggling to work that out too!, how do you run update pymyenergi module iin home assistant?

@trizmark
Copy link
Contributor

trizmark commented Sep 17, 2023

If you look at the attached tgz you'll see that I included the pymyenergi under the myenergi dir.
I also modified the __init__.py of myenergi and added:

import sys
sys.path.insert(0, '/config/custom_components/myenergi')

before importing anything from pymyenergi.

@simonjgreen
Copy link

I'd been trying to figure out the same thing 😆. I'd still like to understand what becomes of the modules from requirements.txt in HA.

If you look at the attached tgz you'll see that I included the pymyenergi under the myenergi dir. I also modified the __init__.py of myenergi and added:

import sys
sys.path.insert(0, '/config/custom_components/myenergi')

before importing anything from pymyenergi.

Are you sure you've uploaded the right tgz? I don't see that in there.

@videojedi
Copy link
Contributor

yep mode select works..... :)
Do you want to merge that back into main branch? and I'll merge in the battery energy stuff from today.

@trizmark
Copy link
Contributor

I'd been trying to figure out the same thing 😆. I'd still like to understand what becomes of the modules from requirements.txt in HA.

They're installed via pip.

Are you sure you've uploaded the right tgz? I don't see that in there.

Attaching the file again. Here's the listing:

% tar tvzf myenergi.tgz
drwxr-xr-x  0 root   root        0 17 Sep 18:43 myenergi/
-rw-r--r--  0 root   root      341 17 Sep 18:18 myenergi/manifest.json
-rw-r--r--  0 root   root    23972 17 Sep 18:41 myenergi/sensor.py
drwxr-xr-x  0 root   root        0 17 Sep 18:43 myenergi/__pycache__/
-rw-r--r--  0 root   root     9939 17 Sep 18:43 myenergi/__pycache__/select.cpython-311.pyc
-rw-r--r--  0 root   root    23650 17 Sep 18:43 myenergi/__pycache__/sensor.cpython-311.pyc
-rw-r--r--  0 root   root     9494 17 Sep 18:43 myenergi/__pycache__/number.cpython-311.pyc
-rw-r--r--  0 root   root     7079 17 Sep 18:43 myenergi/__pycache__/config_flow.cpython-311.pyc
-rw-r--r--  0 root   root     6101 17 Sep 18:43 myenergi/__pycache__/entity.cpython-311.pyc
-rw-r--r--  0 root   root     3733 17 Sep 18:43 myenergi/__pycache__/binary_sensor.cpython-311.pyc
-rw-r--r--  0 root   root     1023 17 Sep 18:43 myenergi/__pycache__/const.cpython-311.pyc
-rw-r--r--  0 root   root     7349 17 Sep 18:43 myenergi/__pycache__/__init__.cpython-311.pyc
-rw-r--r--  0 root   root     3254 14 Sep 17:08 myenergi/entity.py
drwxr-xr-x  0 root   root        0 17 Sep 10:07 myenergi/pymyenergi/
-rw-r--r--  0 root   root     5583 17 Sep 18:22 myenergi/pymyenergi/libbi.py
-rw-r--r--  0 root   root     1505 16 Sep 13:35 myenergi/pymyenergi/harvi.py
-rw-r--r--  0 root   root     7727 16 Sep 13:35 myenergi/pymyenergi/base_device.py
drwxr-xr-x  0 root   root        0 17 Sep 18:43 myenergi/pymyenergi/__pycache__/
-rw-r--r--  0 root   root    19962 17 Sep 10:07 myenergi/pymyenergi/__pycache__/zappi.cpython-311.pyc
-rw-r--r--  0 root   root     2598 17 Sep 10:07 myenergi/pymyenergi/__pycache__/exceptions.cpython-311.pyc
-rw-r--r--  0 root   root    14777 17 Sep 10:07 myenergi/pymyenergi/__pycache__/cli.cpython-311.pyc
-rw-r--r--  0 root   root     3157 17 Sep 10:07 myenergi/pymyenergi/__pycache__/harvi.cpython-311.pyc
-rw-r--r--  0 root   root    18691 17 Sep 10:07 myenergi/pymyenergi/__pycache__/client.cpython-311.pyc
-rw-r--r--  0 root   root    13773 17 Sep 10:07 myenergi/pymyenergi/__pycache__/base_device.cpython-311.pyc
-rw-r--r--  0 root   root     5795 17 Sep 10:07 myenergi/pymyenergi/__pycache__/connection.cpython-311.pyc
-rw-r--r--  0 root   root    10389 17 Sep 18:43 myenergi/pymyenergi/__pycache__/libbi.cpython-311.pyc
-rw-r--r--  0 root   root      570 17 Sep 10:07 myenergi/pymyenergi/__pycache__/__init__.cpython-311.pyc
-rw-r--r--  0 root   root    12695 17 Sep 10:07 myenergi/pymyenergi/__pycache__/eddi.cpython-311.pyc
-rw-r--r--  0 root   root      309 16 Sep 13:35 myenergi/pymyenergi/ct.py
-rw-r--r--  0 root   root      312 16 Sep 13:35 myenergi/pymyenergi/__init__.py
-rw-r--r--  0 root   root        6 17 Sep 18:20 myenergi/pymyenergi/VERSION
-rw-r--r--  0 root   root     9718 16 Sep 13:35 myenergi/pymyenergi/cli.py
-rw-r--r--  0 root   root     3566 16 Sep 13:35 myenergi/pymyenergi/connection.py
-rw-r--r--  0 root   root     1652 16 Sep 13:35 myenergi/pymyenergi/exceptions.py
-rw-r--r--  0 root   root     6856 16 Sep 13:35 myenergi/pymyenergi/eddi.py
-rw-r--r--  0 root   root    11022 16 Sep 13:35 myenergi/pymyenergi/zappi.py
-rw-r--r--  0 root   root    12007 16 Sep 13:35 myenergi/pymyenergi/client.py
-rw-r--r--  0 root   root     5063 15 Sep 13:22 myenergi/number.py
-rw-r--r--  0 root   root     5796 17 Sep 18:26 myenergi/select.py
drwxr-xr-x  0 root   root        0 17 Sep 18:42 myenergi/translations/
-rw-r--r--  0 root   root      868 14 Sep 17:08 myenergi/translations/nb.json
-rw-r--r--  0 root   root      822 14 Sep 17:08 myenergi/translations/sv.json
-rw-r--r--  0 root   root      895 14 Sep 17:08 myenergi/translations/de.json
-rw-r--r--  0 root   root      874 14 Sep 17:08 myenergi/translations/fr.json
-rw-r--r--  0 root   root      797 14 Sep 17:08 myenergi/translations/en.json
-rw-r--r--  0 root   root     3949 17 Sep 18:17 myenergi/__init__.py
-rw-r--r--  0 root   root      890 14 Sep 17:08 myenergi/const.py
-rw-r--r--  0 root   root     4234 14 Sep 17:08 myenergi/config_flow.py
-rw-r--r--  0 root   root     1256 14 Sep 17:08 myenergi/services.yaml
-rw-r--r--  0 root   root     2776 14 Sep 17:08 myenergi/binary_sensor.py

myenergi.tgz

@trizmark
Copy link
Contributor

yep mode select works..... :) Do you want to merge that back into main branch? and I'll merge in the battery energy stuff from today.

Merged.
I'll have a look at the energy data once you merge it and expose it in the HA component.

@trizmark
Copy link
Contributor

I've got the energy data exposed now. Also moved some of the entities from diagnostic to sensors.
Screenshot 2023-09-18 at 11 12 57

The updated component + python package for those who want to test:
myenergi.tgz

@videojedi
Copy link
Contributor

videojedi commented Sep 18, 2023 via email

@trizmark
Copy link
Contributor

Yeah, all that stuff is under the Diagnostic section.
image

I am fairly happy with it, so once I've done some more testing I'll issue a PR for pymyenergi first, then the HA component.
I've got plans to add the various calls to enable/disable battery discharge to eddi/zappi. Also enable/disable grid charging (once the bearer token stuff is worked out).
But I think it's in a good shape to call this version as 'support for libbi monitoring'. Feel free to disagree!

@videojedi
Copy link
Contributor

videojedi commented Sep 18, 2023

Amazing work! All good for me.
There was some inconsistency between the daily charge totals of home assistant and the app. But pretty sure this was down to the app measuring 24hours from 11pm on the previous day, rather than midnight on the day requested.

Very happy with it. Please issue the PR when you're happy too.

My end goal is to automate the overnight charging option when the solar prediction is showing poor weather for the following day. There must be a backend endpoint for switching grid charging on and off we just haven't found. But I guess only myenergi know what that is...... :)

@plord12
Copy link
Author

plord12 commented Sep 28, 2023

Well, I raised this issue ... today my libbi arrived, I installed this update and it worked first time !

Great work !

@CJNE CJNE closed this as completed in #418 Oct 4, 2023
@plord12
Copy link
Author

plord12 commented Oct 4, 2023

I am attaching the custom component if anybody wants to test it. (It's two repos cobbled together while we're working on the HA integration and the actual pymyenergi module)

myenergi.tgz

I used this version for a while ( file size 77932 bytes ) but now the changes have merged, I've just switched to main.

I noticed that sensor.myenergi_libbi_home_consumption and sensor.myenergi_libbi_power_charging have gone.

Not really a problem, since I think both of these can be calculated in home assistant, but just wanted to check that this was intentional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants