Amazing @Nir , I have been trying to get pool temp for the last 18 months and here it is:
I have been looking around for an inexpensive option for pool temp, but most solutions were very expensive, except for InkBird, which had had some success with HA. I purchased 2x IBS-M1 on the hope of getting a gen 1 version, but I ended up with two gen 2 I purchased an RTL dongle but couldn’t get that integrated either. Thank you…
I didn’t want to head down the complexity of ssh and the MQTT route, so I combined your two scripts into one python script that returns the value and then setup a simple command_line sensor.
- platform: command_line
name: Pool Temperature
command: 'python3 /config/scripts/get_pool_temp.py'
unit_of_measurement: "°C"
scan_interval: 60
#!/usr/bin/env python3
import json
import requests
import hmac
import hashlib
import time
# Declare constants
ClientID="<<ENTER CLIENT ID HERE>>"
ClientSecret="<<ENTER CLIENT SECRET HERE>>"
DeviceID = "<<ENTER DEVICE ID HERE>>"
ChannelID = "<<ENTER DEVICE CHANNEL HERE e.g. ch_1 or ch_rtd1"
BaseUrl = "https://openapi.tuyaeu.com"
EmptyBodyEncoded = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
# Character conversion dictionary for decoding the temperature value
charcter_conversion = {
"A": 0, "E": 1, "I": 2, "M": 3, "Q": 4, "U": 5, "Y": 6, "c": 7,
"g": 8, "k": 9, "o": 10, "s": 11, "w": 12, "0": 13, "4": 14, "8": 15
}
# Get current time in milliseconds
tuyatime = int(time.time()) * 1000
# Prepare signing for Access Token
URL = "/v1.0/token?grant_type=1"
StringToSign = f"{ClientID}{tuyatime}GET\n{EmptyBodyEncoded}\n\n{URL}"
AccessTokenSign = hmac.new(ClientSecret.encode(), StringToSign.encode(), hashlib.sha256).hexdigest().upper()
# Get Access Token
headers = {
'client_id': ClientID,
'sign_method': 'HMAC-SHA256',
't': str(tuyatime),
'sign': AccessTokenSign
}
response = requests.get(f"{BaseUrl}{URL}", headers=headers)
AccessToken = response.json()['result']['access_token']
# Send Device status request
URL = f"/v1.0/iot-03/devices/status?device_ids={DeviceID}"
StringToSign = f"{ClientID}{AccessToken}{tuyatime}GET\n{EmptyBodyEncoded}\n\n{URL}"
RequestSign = hmac.new(ClientSecret.encode(), StringToSign.encode(), hashlib.sha256).hexdigest().upper()
headers.update({
'sign': RequestSign,
'access_token': AccessToken
})
response = requests.get(f"{BaseUrl}{URL}", headers=headers)
RequestResponse = response.text
# Parse and process the request response
device_param = json.loads(RequestResponse)
number_of_status = len(device_param["result"][0]["status"])
for i in range(number_of_status):
if device_param["result"][0]["status"][i]["code"] == ChannelID:
raw_temp = device_param["result"][0]["status"][i]["value"]
# Decode the temperature value
raw_temp_first_charecter = raw_temp[1:2]
raw_temp_second_charecter = raw_temp[2:3]
raw_temp_third_charecter = raw_temp[3:4]
temp_by_ten = (ord(raw_temp_first_charecter) - 65)*16 + charcter_conversion[raw_temp_second_charecter]
if raw_temp_third_charecter == "B":
temp_by_ten = temp_by_ten + 256
# Calculate and print the pool temperature
Pool_temp = temp_by_ten / 10
print(Pool_temp)