zaubererty / homeassistant-4heat

Integration for 4 heat wifi module of Tiemme
MIT License
12 stars 7 forks source link

Loosing connection all the time #2

Open ruialmeidinha opened 1 year ago

ruialmeidinha commented 1 year ago

Hi I have installed this add on 3 days ago. First of all it worked Immediately as i Have placed the ip address of the 4 heat module of my stove.

i got immediately all the readings

However, it only works randomly as the connection “comes and goes” This is the image of “right now”:

457D7A1B-F79E-4EAA-BF6D-AF966AC4F2AF

Should i place a fixed ip address for the 4heat module in stove in the router ?

I have everything with the latest software 4heat module Home assistant Integration of 4heat

thanks in advance !!! 👌🙌

ruialmeidinha commented 1 year ago

Hi, Any news ?

leoshusar commented 1 year ago

Same problem here, due to this, my graphs look like this: image

image

I even have fixed IP for it.

EDIT: tried to raise socket timeout from 10 seconds to 30, huge improvement, only failing once in a while. I'm now trying 55 seconds and will let know. It's because I noticed that sometimes the WiFi module is busy with something (?) and it responds even after such a long time. So, bigger timeout and perhaps retry mechanism would help.

I also tried to spam it every 5 seconds for a while - responded immediately everytime!

I also found in the Android app code that they are using only 1 second timeout for the query request: image and they are sending it more often so it doesn't go to sleep, because it fails for me with 1s timeout and 1 minute delay between requests: image with 5 seconds delay no problem again, everything goes on the first try... Here is what I captured from the Android app:

13:27:59.335835 ethertype IPv4, IP Galaxy-S22-Ultra.52672 > 4Heat: Flags [P.], seq 0:11, ack 1, win 8760, length 11: HTTP
E..3..@.@....../
......P.'c...0)P."8....["SEL","0"]
.
.
.
13:28:05.352900 ethertype IPv4, IP Galaxy-S22-Ultra.52676 > 4Heat: Flags [P.], seq 0:11, ack 1, win 8760, length 11: HTTP
E..3.v@.@.}l.../
......P.q....0|P."8.X..["SEL","0"]
.
.
.
13:28:11.352412 ethertype IPv4, IP Galaxy-S22-Ultra.52678 > 4Heat: Flags [P.], seq 0:11, ack 1, win 8760, length 11: HTTP
E..3f.@.@..A.../
......P..|S..0.P."8....["SEL","0"]

so they are too spamming it every 5 seconds :)

You can try yourself if you want:

import socket
from time import sleep

# retry method
def with_retry(func, *args, **kwargs):
    max_retries = 3
    for _ in range(max_retries):
        try:
            return func(*args, **kwargs)
        except OSError as e:
            sleep(5) # delay between retries, probably not important
    raise Exception(f"Failed after {max_retries} retries")

def query():
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(1) # socket timeout
        s.connect(('10.0.2.11', 80))

        s.send(b'["SEL","0"]')
        response = s.recv(1024)
        decoded = response.decode()
        print(decoded)

    finally:
        try:
            s.close()
        except:
            pass

for i in range(100):
    with_retry(query)

    print(i)
    sleep(5) # delay between requests

Anyway, so far with 55 seconds timeout no problem for me: image

Now the real question is: what do we want to do? Even 15 seconds between requests fail for me, we would need to lower down to 10 (10 looks like is enough) and that's IMO pointless to spam it every 10 seconds 24/7. So, let it sleep and set 1 minute timeout for wake up?

And one more thing - in this piece of code:

try:
    async with timeout(10):
        d = await self.hass.async_add_executor_job(_update_data)
        return d
except Exception as error:
    raise UpdateFailed(f"Invalid response from API: {error}") from error

~~if the socket throws any error, it's being caught and then it returns empty dict to HASS. This sets values as unavailable. But if HASS gets asyncio.TimeoutError, it's logged as timeout and doesn't replace values with unavailable:~~ nevermind, it shows unknown on empty dict and unavailable on timeout. I don't know how why it was working before. EDIT: Maybe I know now. Either d had saved value from previous update (great Python scopes) or HASS doesn't update anything if you don't return anything.

However I don't know how HASS integration works, if we have 1 minute update interval, can we have longer timeout? Will HASS wait or send another update before this one finishes?

leoshusar commented 1 year ago

@ruialmeidinha you can try my fork. I've set higher timeouts and changed error handling:

Sometimes it still timeouts, but for now less than 10 times per day, hence the last valid value logic: image

You can just replace const.py and coordinator.py using VS Code addon in custom_components/4heat. These changes should eventually get overwritten once new "official" version comes out, I'll let it run for some more time and then create PR here - maybe I'll find some more things in that time.

ruialmeidinha commented 1 year ago

Hi

@leoshusar Many thanks for taking a look and trying to help

I have copied both const.py and coordinator.py files from your fork, to my custom componets folder.

The behaviour is exactly the same....

Did I do what was expected, or did I do something wrong ?

Thanks in advance,

leoshusar commented 1 year ago

No problem!

You have to replace existing files and then restart Home Assistant so it actually loads them. Did you do this?

ruialmeidinha commented 1 year ago

Yes, I did ! It is slightly mosre cosntant. I have ben on the pC for 1 hour ans it has benn offline already 3 times

leoshusar commented 1 year ago

Hmm, what is in your logs?

ruialmeidinha commented 1 year ago

4HEAT - Salamandra Pellets Combustion power changed to 120 14:39:36 - 13 minutes ago 4HEAT - Salamandra Pellets Combustion power changed to 7 14:33:37 - 19 minutes ago 4HEAT - Salamandra Pellets State changed to Standby 14:33:37 - 19 minutes ago 4HEAT - Salamandra Pellets Combustion power changed to 9 14:32:36 - 20 minutes ago 4HEAT - Salamandra Pellets State changed to Modulation 14:32:36 - 20 minutes ago 4HEAT - Salamandra Pellets Error turned off 14:27:58 - 25 minutes ago 4HEAT - Salamandra Pellets State turned on 14:27:58 - 25 minutes ago 4HEAT - Salamandra Pellets UN 20375 changed to 65 14:27:58 - 25 minutes ago 4HEAT - Salamandra Pellets UN 20374 changed to 20 14:27:58 - 25 minutes ago 4HEAT - Salamandra Pellets UN 20179 changed to 75 14:27:58 - 25 minutes ago 4HEAT - Salamandra Pellets UN 20178 changed to 40 14:27:58 - 25 minutes ago 4HEAT - Salamandra Pellets UN 20366 changed to 75 14:27:58 - 25 minutes ago 4HEAT - Salamandra Pellets UN 20365 changed to 60 14:27:58 - 25 minutes ago 4HEAT - Salamandra Pellets UN 20570 changed to 1 14:27:58 - 25 minutes ago 4HEAT - Salamandra Pellets UN 20369 changed to 6 14:27:58 - 25 minutes ago 4HEAT - Salamandra Pellets UN 20225 changed to 55 14:27:58 - 25 minutes ago 4HEAT - Salamandra Pellets UN 20230 changed to 65 14:27:58 - 25 minutes ago

ruialmeidinha commented 1 year ago

Can you please explain me what are all these entities/ sensors eg. UN 20118, UN 20119, etc ?

Almost 30 of them... Could they be the radiators from my pellets heater ?

Thanks

leoshusar commented 1 year ago

In my logs is only this when it timeouts: image

and that's my error message I added, this one doesn't set values to unavailable. Do you have somewhere in logs this exact message or something different? There might be for example socket error / failed to connect or unexpected error, those are also mine.

Regarding those sensors - nobody knows yet. I decompiled the Android APK and they are not mentioned there too, I was able to find only 3:

I think the best would be to check on the stove controller what changes to what value and match the value in HA, I'm planning to do that for mine. But I also think some stove models can have some sensors missing, because my module doesn't report 30040 and 30044 at all. My uncle has higher model with touchscreen (I have with 6 buttons), I'll try to check his when I'll see him.