Python script error : a bytes-like object is required

Posted on
Wed May 18, 2022 12:20 pm
DeltaMike1200 offline
Posts: 11
Joined: Jun 02, 2014

Python script error : a bytes-like object is required

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)))

Posted on
Wed May 18, 2022 2:01 pm
jay (support) offline
Site Admin
User avatar
Posts: 18224
Joined: Mar 19, 2008
Location: Austin, Texas

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

[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')

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed May 18, 2022 2:44 pm
neilk offline
Posts: 715
Joined: Jul 13, 2015
Location: Reading, UK

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

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

Posted on
Thu May 19, 2022 7:16 am
DeltaMike1200 offline
Posts: 11
Joined: Jun 02, 2014

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

Thanks Jay for your quick response, the script is back in action.

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

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests