Page 1 of 1

Tuya temp sensor not able to parse negavtive temperatures

Posted: Thursday 30 November 2023 14:21
by Achterhoeker
Now winter is coming, my outside tuya temp. sensor (Model: "TS0601-temphumi") is giving errors on negative temperatures.

It seems that the temp coming from the sensor is a signed value, for example -18 .8 C is logged as 0xffffff44, but in domoticz an error is logged:

2023-11-30 14:04:42 .726 Error: zigbee: Aberrant Temp: 429496710 .8 (below -50 or above 100) for device: df8d

4294967108 is unsigned 0xffffff44.

Ep:
01:
0000:
0004: "_TZE200_yjjdcqsq"
0000: 3
0001: 72
0005: "TS0601"
0007: 3
fffe: 0
ffe2: 56
ffe4: 0
ffdf: "N,iN,iN,iN,iU,iU,iU,iU,i\,i\,i\,i\,i"
0004: Object {}
0005: Object {}
0019: Object {}
Type: "Temp/Humi"
ClusterType:
2081: "Temp"
2082: "Humi"
0402:
0000: 429496710 .8
0405:
0000: 8 .9

Tuya:
Temp: "ffffff44"
Humi: 8 .9
dp:3-dt:4: "02"
dp:9-dt:4: "00"
TUYA_MCU_VERSION_RSP: "40"

Re: Tuya temp sensor not able to parse negavtive temperatures

Posted: Thursday 30 November 2023 14:26
by pipiche
I guess you are referring to the Zigbee for Domoticz plugin.
Negative temperature are normaly correctly handle.

What is the version of the plugin you are using ?
What is the device Model Name ? It should be TS0601-temphumi .

Re: Tuya temp sensor not able to parse negavtive temperatures

Posted: Thursday 30 November 2023 15:20
by Achterhoeker
I am indeed referring to the Zigbee for Domoticz plugin
The version is Plugin: 7 .1 .005 [2 .148]
Model: "TS0601-temphumi"

I'm not an python expert, but currently looking at (int(data, 16) in tuya .py line 1271 and 1272. Is this code handling signed values correctly in python ?

Re: Tuya temp sensor not able to parse negavtive temperatures

Posted: Thursday 30 November 2023 15:33
by pipiche
I’ll check that evening. It has been fixed for some but probably not all:-(


Envoyé de mon iPhone en utilisant Tapatalk

Re: Tuya temp sensor not able to parse negavtive temperatures

Posted: Thursday 30 November 2023 15:37
by Achterhoeker
With the following change i've made it working, (in tuya.py) (introduced a helper function hex_to_signed)
Don't know if this is the correct place. :-)

def hex_to_signed(source):
"""Convert a string hex value to a signed hexidecimal value.
This assumes that source is the proper length, and the sign bit
is the first bit in the first byte of the correct length.

hex_to_signed("F") should return -1.
hex_to_signed("0F") should return 15.
"""
if not isinstance(source, str):
raise ValueError("string type required")
if 0 == len(source):
raise valueError("string is empty")
sign_bit_mask = 1 << (len(source)*4-1)
other_bits_mask = sign_bit_mask - 1
value = int(source, 16)
return -(value & sign_bit_mask) | (value & other_bits_mask)

def tuya_temphumi_response(self, Devices, _ModelName, NwkId, srcEp, ClusterID, dstNWKID, dstEP, dp, datatype, data):

self.log.logging("Tuya", "Log", "tuya_temphumi_response - %s %s %s %s %s" % (NwkId, srcEp, dp, datatype, data), NwkId)
if dp == 0x01: # Temperature,
store_tuya_attribute(self, NwkId, "Temp", data)
MajDomoDevice(self, Devices, NwkId, srcEp, "0402", (hex_to_signed(data) / 10))
checkAndStoreAttributeValue(self, NwkId, "01", "0402", "0000", (hex_to_signed(data) / 10))

Re: Tuya temp sensor not able to parse negavtive temperatures

Posted: Thursday 30 November 2023 15:39
by pipiche
There is such code I guess it is in the tuyaTS0601.py


Envoyé de mon iPhone en utilisant Tapatalk

Re: Tuya temp sensor not able to parse negavtive temperatures

Posted: Thursday 30 November 2023 20:01
by pipiche
I will push the fix with the December release, but to do it properly the right code should be

Code: Select all

    if dp == 0x01:  # Temperature, 
        unsigned_value = int( data,16)
        signed_value = struct.unpack('>i', struct.pack('>I', unsigned_value))[0]

        store_tuya_attribute(self, NwkId, "Temp", signed_value)
        MajDomoDevice(self, Devices, NwkId, srcEp, "0402", (signed_value / 10))
        checkAndStoreAttributeValue(self, NwkId, "01", "0402", "0000", (signed_value/ 10))

Re: Tuya temp sensor not able to parse negavtive temperatures

Posted: Sunday 03 December 2023 9:04
by Achterhoeker
I have copied the code example and can confirm that it works correctly. Thx.

Re: Tuya temp sensor not able to parse negavtive temperatures

Posted: Sunday 03 December 2023 10:23
by pipiche
thanks for sharing .