pyTibber icon indicating copy to clipboard operation
pyTibber copied to clipboard

pyTibber always loses tibber connection after some time

Open christoph623 opened this issue 1 year ago • 7 comments

I have a simple script that connects to tibber server and print my energy consumption forever. Always after around 30 Minutes, sometimes more or less the script loses the connection and never succeeds in reconnecting again. When i kill the script and start it again it will again work for something like 30 Minutes. Any Ideas how i can improve the behaviour ?

Error: Watchdog: Connection is down, 2023-11-28 16:23:45.302878 Error in watchdog connect, retrying in 45 seconds, 1 Watchdog: Connection is down, 2023-11-28 16:24:35.578354 Error in watchdog connect, retrying in 51 seconds, 2 Traceback (most recent call last): File "/home/ado/.local/lib/python3.10/site-packages/tibber/realtime.py", line 157, in _watchdog await self._resubscribe_homes() File "/home/ado/.local/lib/python3.10/site-packages/tibber/realtime.py", line 178, in _resubscribe_homes await asyncio.gather(*[home.rt_resubscribe() for home in self._homes]) File "/home/ado/.local/lib/python3.10/site-packages/tibber/home.py", line 473, in rt_resubscribe await asyncio.gather( File "/home/ado/.local/lib/python3.10/site-packages/tibber/home.py", line 202, in update_info if data := await self._tibber_control.execute(UPDATE_INFO % self._home_id): File "/home/ado/.local/lib/python3.10/site-packages/tibber/__init__.py", line 104, in execute resp = await self.websession.post(API_ENDPOINT, **post_args) File "/home/ado/.local/lib/python3.10/site-packages/aiohttp/client.py", line 400, in _request raise RuntimeError("Session is closed") RuntimeError: Session is closed Watchdog: Connection is down, 2023-11-28 16:25:31.860971 Error in watchdog connect, retrying in 16 seconds, 3 Traceback (most recent call last): File "/home/ado/.local/lib/python3.10/site-packages/tibber/realtime.py", line 157, in _watchdog await self._resubscribe_homes() File "/home/ado/.local/lib/python3.10/site-packages/tibber/realtime.py", line 178, in _resubscribe_homes await asyncio.gather(*[home.rt_resubscribe() for home in self._homes]) File "/home/ado/.local/lib/python3.10/site-packages/tibber/home.py", line 473, in rt_resubscribe await asyncio.gather( File "/home/ado/.local/lib/python3.10/site-packages/tibber/home.py", line 202, in update_info if data := await self._tibber_control.execute(UPDATE_INFO % self._home_id): File "/home/ado/.local/lib/python3.10/site-packages/tibber/__init__.py", line 104, in execute resp = await self.websession.post(API_ENDPOINT, **post_args) File "/home/ado/.local/lib/python3.10/site-packages/aiohttp/client.py", line 400, in _request raise RuntimeError("Session is closed") RuntimeError: Session is closed ^CTraceback (most recent call last): File "/home/ado/tibb/./tib7.py", line 34, in <module> loop.run_until_complete(run()) File "/usr/lib/python3.10/asyncio/base_events.py", line 636, in run_until_complete self.run_forever() File "/usr/lib/python3.10/asyncio/base_events.py", line 603, in run_forever self._run_once() File "/usr/lib/python3.10/asyncio/base_events.py", line 1871, in _run_once event_list = self._selector.select(timeout) File "/usr/lib/python3.10/selectors.py", line 469, in select fd_event_list = self._selector.poll(timeout, max_ev) KeyboardInterrupt Script: `#!/usr/bin/python3

import tibber.const import tibber import asyncio import aiohttp import json import pprint

access_token = "xx"

ACCESS_TOKEN = access_token

def _callback(pkg): data = pkg.get("data") if data is None: return

print(data.get("liveMeasurement"))

power=data.get("liveMeasurement").get("power");
print(power)

async def run(): async with aiohttp.ClientSession() as session: tibber_connection = tibber.Tibber(ACCESS_TOKEN, websession=session, user_agent="change_this") await tibber_connection.update_info() home = tibber_connection.get_homes()[0] await home.rt_subscribe(_callback)

while True:
  await asyncio.sleep(10)

loop = asyncio.get_event_loop() loop.run_until_complete(run()) `

christoph623 avatar Nov 28 '23 15:11 christoph623

I have the same problem. Were you able to solve it?

flrnmllr avatar Dec 11 '23 10:12 flrnmllr

No, I did not solve it, no idea. I hope Daniel will have an idea what to do when he updates the code next time ..

christoph623 avatar Dec 11 '23 10:12 christoph623

I have the same problem as well.

uid01089 avatar Dec 28 '23 06:12 uid01089

On my side it seems the session was closed somehow and I do not find a recovery strategy in the code to handle that situation:

ERROR:tibber.realtime:Watchdog: Connection is down, 2024-01-01 08:38:26.302279
ERROR:tibber.realtime:Error in watchdog connect, retrying in 730 seconds, 28
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/tibber/realtime.py", line 157, in _watchdog
    await self._resubscribe_homes()
  File "/usr/local/lib/python3.10/site-packages/tibber/realtime.py", line 178, in _resubscribe_homes
    await asyncio.gather(*[home.rt_resubscribe() for home in self._homes])
  File "/usr/local/lib/python3.10/site-packages/tibber/home.py", line 473, in rt_resubscribe
    await asyncio.gather(
  File "/usr/local/lib/python3.10/site-packages/tibber/home.py", line 202, in update_info
    if data := await self._tibber_control.execute(UPDATE_INFO % self._home_id):
  File "/usr/local/lib/python3.10/site-packages/tibber/__init__.py", line 104, in execute
    resp = await self.websession.post(API_ENDPOINT, **post_args)
  File "/usr/local/lib/python3.10/site-packages/aiohttp/client.py", line 421, in _request
    raise RuntimeError("Session is closed")

uid01089 avatar Jan 01 '24 07:01 uid01089

The same problem here. After some time the connection closes and all internal retries will not work and it loops then with a retry connect with 1200s delay which never ends. Any help is appreciated!

michaeltraxler avatar Feb 17 '24 12:02 michaeltraxler

I am facing the same problem. The real-time web socket connection stops after running a few minutes. Is there a rest API to get the realtime data without the web socket?

FayzulSaimun avatar Mar 24 '24 12:03 FayzulSaimun

I rather wanted to "tweak" the tibber pulse to either report to my own MQTT broker to get direct access to live data or have my existing smartreader header send to tibber (I'm not a fan of using any internet services outside my house for home automation). But I gave up on that idea after a whole day trying. So I ended up here.

So, same problem pyTibber doesn't have a reconnect strategy it seems for live data. It seems one must handle this in their own code, sigh.

reinhard-brandstaedter avatar Apr 11 '24 07:04 reinhard-brandstaedter

Same problem here... as a workaround i automatically redeploy my containers, but i'd rather not have to do this. Did anyone find a solution?

See: https://github.com/Danielhiversen/pyTibber/issues/280

janolehuebner avatar May 07 '24 16:05 janolehuebner