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

Issue with Temperature value less than 10degC #2

Closed
randomdisco opened this issue Jul 18, 2018 · 8 comments
Closed

Issue with Temperature value less than 10degC #2

randomdisco opened this issue Jul 18, 2018 · 8 comments

Comments

@randomdisco
Copy link

randomdisco commented Jul 18, 2018

Hi,
Actually got the wrong code and debugged that but here I am and I think this is the one included with Home Assistant...

It appears as if the sensor platform code stops returning values if the temperature is below 10degC. This possibly also affects humidity but I can't verify this.

I'm running Hass.io on a RPI 3 with several of these sensors. The ones above 10degC continue to work normally; only one is dropping below 10deg C and when it does temperature, humidity, and battery values for only this sensor freeze and don't update until temp goes back above 10degC.

I believe there is a single digit temperature (and humidity) bug in the code. I'd never debugged / written python code before tonight (so I'm hacking a bit) but I got this far.

A double figure temperature (15degC) returns a 14 byte code: 54 3d 32 30 2e 33 20 48 3d 34 36 2e 35 00
But a single digit temperature (8 degC) returns a 13 byte code: 54 3d 38 2e 32 20 48 3d 34 36 2e 36 00
I can't work out what type the data is in when it gets passed in here

    def handleNotification(self, handle, raw_data):  # pylint: disable=unused-argument,invalid-name
        """ gets called by the bluepy backend when using wait_for_notification
        """
        if raw_data is None:
            return
    data = raw_data.decode("utf-8").strip(' \n\t')

But this then calls self._check_data() which calls self._parse_data()

And the bug I think lies here:

        res[MI_HUMIDITY] = float(data[9:13])
        res[MI_TEMPERATURE] = float(data[2:6])

If temp is single digit this should be 2:5 and 8:12 and if humidity is single digits it should be 8:11.

I think the data is a byte array string... in which case this should work in _parse_data:

        data = self._cache

        temp,humidity  = data.replace("T=", "").replace("H=", "").rstrip(' \t\r\n\0').split(" ")

        res = dict()
        res[MI_HUMIDITY] = float(humidity)
        res[MI_TEMPERATURE] = float(temp)

Great work on the sensor code - it was a breeze to get the sensors configured and running. Happy to help with this bug further if I can! I have no idea how to check this with Home Assistant or run unit tests and submit.

Cheers,
Stewart

@rmiddlet
Copy link

My fix for this issue is to parse the response like text - strip the null character, split on space, split on equals. Negative temperatures should work too - the current forecast says I can test that by the weekend.

This is my tested change to def _parse_data(self):

        res = dict()
        for dataitem in data.strip('\0').split(' '):
            dataparts = dataitem.split('=')
            if dataparts[0] == 'T':
                res[MI_TEMPERATURE] = float(dataparts[1])            
            elif dataparts[0] == 'H':
                res[MI_HUMIDITY] = float(dataparts[1])
        return res

@schemacs
Copy link

schemacs commented Sep 24, 2018

Same here:

2018-09-24 10:40:59 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.mitemp_bt_temperature fails
Traceback (most recent call last):
  File "/home/pi/.virtualenvs/ha/lib/python3.5/site-packages/homeassistant/helpers/entity.py", line 224, in async_update_ha_state
    yield from self.async_device_update()
  File "/home/pi/.virtualenvs/ha/lib/python3.5/site-packages/homeassistant/helpers/entity.py", line 353, in async_device_update
    yield from self.hass.async_add_job(self.update)
  File "/usr/lib/python3.5/asyncio/futures.py", line 380, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.5/asyncio/tasks.py", line 304, in _wakeup
    future.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 293, in result
    raise self._exception
  File "/usr/lib/python3.5/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/pi/.virtualenvs/ha/lib/python3.5/site-packages/homeassistant/components/sensor/mitemp_bt.py", line 142, in update
    data = self.poller.parameter_value(self.parameter)
  File "/home/pi/.virtualenvs/ha/lib/python3.5/site-packages/mitemp_bt/mitemp_bt_poller.py", line 132, in parameter_value
    return self._parse_data()[parameter]
  File "/home/pi/.virtualenvs/ha/lib/python3.5/site-packages/mitemp_bt/mitemp_bt_poller.py", line 177, in _parse_data
    res[MI_HUMIDITY] = float(data[9:13])
ValueError: could not convert string to float: '7.9\x00'

@rmiddlet Thanks for your fix, and I have tested it by putting the sensor in the refrigerator.

@Mirarkitty
Copy link

Seems like this is an issue with high negative numbers as well (<=-10.0C) but then it starts to parse at '=' since the humidity is shifted to the right.

@Mirarkitty
Copy link

Can anyone get the fix into pip? I don't understand python pip repositories.

@S0lmyr
Copy link

S0lmyr commented Jan 28, 2023

Hi, is it possible that this fix mess temperatures between -0,1°C and -0,9°C?
It somehow removes minus sign..
poller error

@rmiddlet
Copy link

From memory, on reviewing the raw output of the xiaomi temperature module, the fault is actually with the device for -0.1 -> -0.9. That is, the fault is in the raw data, not in the interpretation of the raw.

@S0lmyr
Copy link

S0lmyr commented Jan 30, 2023

Thank you Robert for quick answer. I assume that there is nothing that can be fixed from our side.

Have a nice day!

@S0lmyr
Copy link

S0lmyr commented Feb 16, 2023

Even official Mi Home app shows the temperature incorrectly...
20230216_062841
Screenshot_20230216_062825_Mi Home

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

No branches or pull requests

6 participants