Page 1 of 1

Python script error : a bytes-like object is required

PostPosted: Wed May 18, 2022 12:20 pm
by DeltaMike1200
I have a script running to read my Daikin ac value's
After updating from indigo 7.5 to 2022 it doesn't work anymore :( .
It generates this erro: a bytes-like object is required, not 'str'
error on line 14: split_response = data_getStatus.split(',')

This is the script:


Code: Select all
#get data in HTMl Response from Daikin HVAC
from urllib.request import urlopen
urlReturned = urlopen('http://192.168.0.54/aircon/get_sensor_info')
data_getStatus = urlReturned.read()

#transform HTMl Response in JSON format
# Split the result into bits
split_response = data_getStatus.split(',')


# Split the bits into variable/value pairs
new_list = []
for element in split_response:
   new_list.append(element.split('='))


# Convert the variable/value pairs into something json-y
new_dict = {}
for element in new_list:
   new_dict[element[0]] = element[1]



#get new json string variables to python variables

htemp=str(new_dict['htemp'])
otemp=str(new_dict['otemp'])


#update indigo variables
indigo.variable.updateValue(520038281, ((htemp)))
indigo.variable.updateValue(75438798, ((otemp)))

Re: Python script error : a bytes-like object is required

PostPosted: Wed May 18, 2022 2:01 pm
by jay (support)
[MODERATOR NOTE]: moved to the python 3 conversion forum

In Python 3, network communication results in bytes objects, not strings, so you need to decode the bytes into a valid str object (which is unicode) before performing string operations like split on it:

Code: Select all
bytes_instance = urlReturned.read()
data_getStatus = bytes_instance.decode('utf8')

Re: Python script error : a bytes-like object is required

PostPosted: Wed May 18, 2022 2:44 pm
by neilk
I am not sure if it is the same API, but you may want to try https://www.indigodomo.com/pluginstore/264/ as it looks like your script may be using the same calls as my plugin.

Neil

Re: Python script error : a bytes-like object is required

PostPosted: Thu May 19, 2022 7:16 am
by DeltaMike1200
Thanks Jay for your quick response, the script is back in action.

@ Neil, thanks for the info, I'm going to tryout your plugin.