theMusicMan
Posts: 114
Joined: Tue May 23, 2017 8:41 pm

Can't convert 'int' object to str implicitly

Sat Feb 23, 2019 1:46 pm

I have a BME280 sensor connected and returning values as expected, which I am then able to upload to adafruit.

However, the BME280's pressure reading requires some local calibration, and I would simply like to add 3 to its returned pressure value. The code that gets the values from the sensor and prints them all to the console is...

Code: Select all

        # Grab the latest enviromental values and print to the console (if open)
        envi = bme.values
        print(envi)
and the code that specifically pushes the pressure value up to adafruit is shown here;

Code: Select all

           c.publish(conf['user']+"/feeds/pres", envi[1])
When I simply type;

Code: Select all

           c.publish(conf['user']+"/feeds/pres", envi[1] + 3)
... to the code, I get the above error as I gather I am trying to add an integer to a character, or something such as that!

So, can anyone advise please how I would go about simply adding 3 (or any value) to the env[1] variable please? Many thanks.

Andyroo

Re: Can't convert 'int' object to str implicitly

Sat Feb 23, 2019 2:25 pm

Hi,

In python you can find the type of a variable by using:

Code: Select all

print(type(variablename))
In this case assuming it is a string / character you can use the int function for integers:

Code: Select all

c.publish(conf['user']+"/feeds/pres", int(envi[1]) + 3)
Note that above does not round up - it goes to the integer closest to zero (both pos and neg).

If you want to keep the decimal places then use the float function:

Code: Select all

c.publish(conf['user']+"/feeds/pres", float(envi[1]) + 3)
If you want to round to a given number of places then use the round function:

Code: Select all

print(round(3.145,2))
will give 3.15 where as:

Code: Select all

print(round(3.145,1))
will give 3.1

Sorry for the waffle but it may help others long term :lol:

theMusicMan
Posts: 114
Joined: Tue May 23, 2017 8:41 pm

Re: Can't convert 'int' object to str implicitly

Sat Feb 23, 2019 2:33 pm

Andyroo wrote:
Sat Feb 23, 2019 2:25 pm
Hi,
Sorry for the waffle but it may help others long term :lol:
You're kidding me yeah...? waffle...? Not a chance... This is a superb answer, thank you so much. :D

Andyroo

Re: Can't convert 'int' object to str implicitly

Sat Feb 23, 2019 2:42 pm

theMusicMan wrote:
Sat Feb 23, 2019 2:33 pm
Andyroo wrote:
Sat Feb 23, 2019 2:25 pm
Hi,
Sorry for the waffle but it may help others long term :lol:
You're kidding me yeah...? waffle...? Not a chance... This is a superb answer, thank you so much. :D
Ahh but does it work?

theMusicMan
Posts: 114
Joined: Tue May 23, 2017 8:41 pm

Re: Can't convert 'int' object to str implicitly

Sat Feb 23, 2019 3:25 pm

Andyroo wrote:
Sat Feb 23, 2019 2:42 pm
theMusicMan wrote:
Sat Feb 23, 2019 2:33 pm
Andyroo wrote:
Sat Feb 23, 2019 2:25 pm
Hi,
Sorry for the waffle but it may help others long term :lol:
You're kidding me yeah...? waffle...? Not a chance... This is a superb answer, thank you so much. :D
Ahh but does it work?
Alas not! The result of the type is class = tuple

Still an excellently informed reply 👍

Andyroo

Re: Can't convert 'int' object to str implicitly

Sat Feb 23, 2019 7:11 pm

OK - I would expect. That as it’s basically an array of values being returned. Did you try int(envi[1]) or just int(envi)?

Do you have a link to the board and the software library you are using?

If all fails (Pythons reasonably new to me) ave you seen this library https://github.com/adafruit/Adafruit_Python_BME280. Though deprecated it may still work and let you add the fiddle factor.

My sensor is still in its bag and my play Pi Zero is tied up in a WLAN test at the mo so I’m a bit blind (and daft)...

theMusicMan
Posts: 114
Joined: Tue May 23, 2017 8:41 pm

Re: Can't convert 'int' object to str implicitly

Sat Feb 23, 2019 8:46 pm

Andyroo wrote:
Sat Feb 23, 2019 7:11 pm
OK - I would expect. That as it’s basically an array of values being returned. Did you try int(envi[1]) or just int(envi)?

Do you have a link to the board and the software library you are using?

If all fails (Pythons reasonably new to me) ave you seen this library https://github.com/adafruit/Adafruit_Python_BME280. Though deprecated it may still work and let you add the fiddle factor.

My sensor is still in its bag and my play Pi Zero is tied up in a WLAN test at the mo so I’m a bit blind (and daft)...
I tried assign a new variable ...

Code: Select all

p = int(envi[1]) + 3
And I had an error returned which is; invalid syntax for integer with base 10

I am guessing this is because envi[1] is a string that ends with characters... it is actually 1029.34hPa

So what I need to be able to do is to convert just the number part of that string to an integer, add 3 to it, then convert it back to a string to parse to the MQTT server.

pcmanbob
Posts: 9467
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Can't convert 'int' object to str implicitly

Sat Feb 23, 2019 9:50 pm

So your value is actually a float value because it has a decimal , int would remove the decimal, so 1029.34 would become 1029.

so to do what you want assuming you want the string to read the same but with just 3 added to the value you need this simple bit of code.

Code: Select all

p = "1029.34hpa"			 # your string replace with p = envi[1]
string = p.split("h") 			# split the number from the text at h
value = float(string[0]) + 3.0		 #convert number in string to float value and add 3
p = str(value) + "hPa"			 # convert back to string and add text back on end of new value
print(p)  				# just here to show the result you can delete this line.
If you let p = envi[1] as suggested you can just copy the code in you your program and p will equal the result you want at the end.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

theMusicMan
Posts: 114
Joined: Tue May 23, 2017 8:41 pm

Re: Can't convert 'int' object to str implicitly

Sat Feb 23, 2019 9:57 pm

pcmanbob wrote:
Sat Feb 23, 2019 9:50 pm
So your value is actually a float value because it has a decimal , int would remove the decimal, so 1029.34 would become 1029.

so to do what you want assuming you want the string to read the same but with just 3 added to the value you need this simple bit of code.

Code: Select all

p = "1029.34hpa"			 # your string replace with p = envi[1]
string = p.split("h") 			# split the number from the text at h
value = float(string[0]) + 3.0		 #convert number in string to float value and add 3
p = str(value) + "hPa"			 # convert back to string and add text back on end of new value
print(p)  				# just here to show the result you can delete this line.
If you let p = envi[1] as suggested you can just copy the code in you your program and p will equal the result you want at the end.
Worked a treat, many thanks Bob👍

lovelmark
Posts: 1
Joined: Wed Feb 05, 2020 5:01 am

Re: Can't convert 'int' object to str implicitly

Wed Feb 05, 2020 5:03 am

A TypeError can occur if the type of an object is not what the Python interpreter expected to see. This error is a common mistake made by beginning developers is to use the '+' operator between values of incompatible types. This error message Can't convert 'int' object to str implicitly is clear, when concatenating strings with integers - you can't directly stick together a string and an integer. So, in order to resolve this problem, you have to explicitly parse the integer to a string by the str() built-in function .

Return to “Troubleshooting”