ha-myenergi icon indicating copy to clipboard operation
ha-myenergi copied to clipboard

Support libbi export to grid

Open plord12 opened this issue 1 year ago • 68 comments

Is your feature request related to a problem? Please describe.

Since libbi 5.408 ( https://support.myenergi.com/hc/en-gb/articles/19276846239377-libbi-Firmware-Version-5-408 ) libbi supports export to grid. This is useful if you wanted to use your libbi to charge by cheap electricity (or solar) and export when export prices are high.

Describe the solution you'd like

HA entity to turn on and off export to grid.

Describe alternatives you've considered

none

Additional context

Also raised https://github.com/CJNE/pymyenergi/issues/18 for the underlying library

plord12 avatar Oct 26 '23 12:10 plord12

Looks like the endpoints are...

to turn on https://s18.myenergi.net/cgi-libbi-mode-L24047041-5

and to turn off https://s18.myenergi.net/cgi-libbi-mode-L24047041-1

It's not a very consistent API is it!

videojedi avatar Oct 26 '23 12:10 videojedi

I've got it working locally; will be posting it here soon.

The API is sort of consistent. Looking at the app code there are a number of modes that are defined, but not used and cannot be set. (capture, charge, match) The new mode (drain) was simply added to the list and that's why it became 5.

trizmark avatar Oct 29 '23 06:10 trizmark

Charge target is implemented as well, but it's super-broken in the libbi firmware. 😞

trizmark avatar Oct 29 '23 06:10 trizmark

Charge target is implemented as well, but it's super-broken in the libbi firmware. 😞

Nice :-) I'll try it with the app tonight.

BTW do you have 5.414 installed on your libbi ? ( see https://myenergi.info/libbi-no-longer-fully-charging-with-cheap-rate-ove-t11328.html )

plord12 avatar Oct 29 '23 07:10 plord12

I had 5.408 on until this morning. Overnight, instead of my set 2.5h charge, the libbi only charged for an hour and then it sort of held the SoC. I installed 5.414, which now completely broke the charge from grid. Grrrr!

trizmark avatar Oct 29 '23 07:10 trizmark

For those who are foolish and reckless.... I present you the latest bleeding edge version of the integration. New operation mode for your libbi: 'Export'. Let me know if you have any issues with it. myenergi-20231029.tgz

I am adding the service to set the charge target. New version probably tomorrow (depending on my schedule).

trizmark avatar Oct 29 '23 20:10 trizmark

Great work. I'm running 5.414.

Can confirm export works!

videojedi avatar Oct 29 '23 22:10 videojedi

My charge target remains the same with toggling the charge from grid. Does yours change?

trizmark avatar Oct 29 '23 22:10 trizmark

Yes mine seems to default to zero... I reset it in the app to 100%, and toggle it again in home assistant, but it returns to zero

videojedi avatar Oct 29 '23 22:10 videojedi

Same behaviour in the app too...... toggle it and it returns to zero.

videojedi avatar Oct 29 '23 22:10 videojedi

aha.... this is another shared account issue. If I login into app with primary account details, then it remembers the chargetarget between toggles...

videojedi avatar Oct 29 '23 22:10 videojedi

If it's the same issue in the app, then it's a myenergi issue.

trizmark avatar Oct 29 '23 22:10 trizmark

I have tested it with my secondary account and it is indeed a myenergi backend issue. 🤦

trizmark avatar Oct 30 '23 10:10 trizmark

For those who are foolish and reckless.... I present you the latest bleeding edge version of the integration. New operation mode for your libbi: 'Export'. Let me know if you have any issues with it. myenergi-20231029.tgz

FYI, didn't work for me, plugin failed to startup. I see in the logs -

2023-10-30 13:09:29.625 ERROR (MainThread) [homeassistant.config_entries] Error setting up entry Home for myenergi
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 399, in async_setup
    result = await component.async_setup_entry(hass, self)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/myenergi/__init__.py", line 53, in async_setup_entry
    conn = await hass.async_add_executor_job(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/myenergi/pymyenergi/connection.py", line 40, in __init__
    self.oauth.authenticate(password=self.app_password)
  File "/usr/local/lib/python3.11/site-packages/pycognito/__init__.py", line 478, in authenticate
    tokens = aws.authenticate_user(client_metadata=client_metadata)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pycognito/aws_srp.py", line 269, in authenticate_user
    response = boto_client.initiate_auth(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/botocore/client.py", line 535, in _api_call
    return self._make_api_call(operation_name, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/botocore/client.py", line 936, in _make_api_call
    request_dict = self._convert_to_request_dict(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/botocore/client.py", line 1007, in _convert_to_request_dict
    request_dict = self._serializer.serialize_to_request(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/botocore/validate.py", line 381, in serialize_to_request
    raise ParamValidationError(report=report.generate_report())
botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid type for parameter AuthParameters.USERNAME, value: None, type: <class 'NoneType'>, valid types: <class 'str'>

Rolled back to official version and it started up.

plord12 avatar Oct 30 '23 13:10 plord12

As this version adds the OAuth endpoints you need to remove your integration and re-add it as it requires both API key/password and App email/app password parameters.

trizmark avatar Oct 30 '23 13:10 trizmark

As this version adds the OAuth endpoints you need to remove your integration and re-add it as it requires both API key/password and App email/app password parameters.

Ah, got it, thanks. After re-adding ( and entering username/password ) I was able to enable export :-)

My grid charge percentage stayed at 60% FYI.

plord12 avatar Oct 30 '23 14:10 plord12

Great to hear! The charge target is only an issue if you're using a secondary account to access your main account. People who signed up to a myenergi account using google/apple/facebook need a secondary account as currently we only support OAuth with email/password combo.

I am nearly ready with the charge target setting functionality, but as this is the first time I'm implementing a HA service, it is a lot of trial and error.

trizmark avatar Oct 30 '23 15:10 trizmark

Thanks for making this version. Is there a plan for getting a 0.0.25 made with this in? (Sorry, I'm not sure how that side is managed) Thanks

whittyone avatar Dec 30 '23 21:12 whittyone

@whittyone Yes, there is. I am updating the pymyenergi library currently. Once that's done and out, I can push my modifications to the HA component.

trizmark avatar Jan 09 '24 10:01 trizmark

I see this didn't make 0.0.25

plord12 avatar Feb 19 '24 10:02 plord12

@plord12 The 0.1.0 version of pymyenergi (released yesterday) has all the necessary features in there, so once I rebase my local dev copy to 0.0.25, this will be in 0.0.26.

trizmark avatar Feb 19 '24 10:02 trizmark

@plord12 The 0.1.0 version of pymyenergi (released yesterday) has all the necessary features in there, so once I rebase my local dev copy to 0.0.25, this will be in 0.0.26.

Brilliant! Many thanks.

plord12 avatar Feb 19 '24 10:02 plord12

I'm making really good use of this to dump excess power in the evenings and during Octopus saving sessions, so good to know that I shouldn't upgrade just yet. Thanks!

MaximumFish avatar Feb 19 '24 10:02 MaximumFish

@plord12 The 0.1.0 version of pymyenergi (released yesterday) has all the necessary features in there, so once I rebase my local dev copy to 0.0.25, this will be in 0.0.26.

Did this get added to 0.0.26 or 0.0.27? I don't see it in the changelogs.

MaximumFish avatar Mar 14 '24 11:03 MaximumFish

I'm still working on getting the PR ready. Unfortunately work has been eating into my spare time, so no ETA at the moment.

trizmark avatar Mar 14 '24 11:03 trizmark

No worries. 0.0.25 is still working fine for me so I'm in no rush to update. 😄

MaximumFish avatar Mar 14 '24 11:03 MaximumFish

I hate to pester, given so many give their time for projects like this, but any news ?

Once this gets in I hope to look at the libbi dual tariff issue (eg sync libbi schedule from octopus) ... but I don't really want to get going until this is in.

Cheers :-)

plord12 avatar Apr 14 '24 10:04 plord12

I can see the light at the end of the tunnel! I've been playing catch-up with the changes that happened in this repo vs what I've added. Hoping to submit the PR this weekend. 🤞

trizmark avatar Apr 26 '24 08:04 trizmark

@trizmark would you be able to look at issue #516 when you do your update? Currently, Libbi operating mode is not working in 0.0.27 but the solution is described.

Thanks,

MalcolmSpencer avatar Apr 27 '24 07:04 MalcolmSpencer

I found that issue when I brought my fork up to 0.0.27 and it's already fixed.

trizmark avatar Apr 27 '24 08:04 trizmark