Indentation error in Telegram alert system

On running the code it says unexpected indent. I’ve only used spaces(no tabs used), rechecked indentations and am unable to find what is going wrong. Below is the code for the same. Will be grateful for any help!

I see where the error is

sensor_value = int(data[‘value’])
return sensor_value

you have written them inside the try block

these two lines are supposed to be outside the try block and inside the block of main function
that is ---->get_sensor_value_from_pin().

I hope you got the hint what I am trying to convey here.

No @spriyam095 those lines should be in try block. That part is correct.
@aditya198018d , as far as my knowledge goes, there shouldn’t be any error in your code. But still, as a last resort, try writing the line response = mybolt.analogRead(pin) after the colon i.e instead of


write like this


and then make the necessary indentation changes in the subsequent lines.

Okay. I shall give it a shot.
Thank you so much for your help. Much appreciated!

Update: Same error still persists.

@aditya198018d @kshitishjgadre

I see I was wrong. I didn’t give enough time to your post.

I checked the code again and what I found was that, at every indentation you have given 3 spaces but in your first IF statement you have given 4 spaces for indentation maybe there is an error.

But still I would suggest you to use tabs that way its easy to indent our code and you don’t have to remember the number of spaces.
And surely it will get rid of the error too

Hi @aditya198018d,

Just want to add one more points. Mixing tabs and spaces can also result in indentation error. One of the possible solution is to avoid such error is to configure you nano editor so that whenever you press tab, it will convert the tabs into spaces and you can define the number spaces you want on each tab.

To do that -
Open /etc/nanorc

sudo nano /etc/nanorc

and add the below line -

set tabsize 4
set tabstospaces

and save the file.

Keep the number of spaces same for all the code block. If you are using 3 spaces then use 3 spaces everywhere and if you are using four spaces then use four spaces everywhere in your code.

Do let me know in case you need any other information.

Okay. Thank you for the advice. Will do.

Thank you sir for bringing up this method. This was new for me.

Yes sir, I’ve used spaces only and no tabs and I’ve kept 3 spaces everywhere (except while nesting), rechecked them as well. I’ve made sure that there are no indentation errors from my side.
Still, no idea what the problem is. The “try:” line in line 9 was the first line to have an indentation. What sense does it make to show an indentation error there I’m unable to understand.

send the screenshot of your edited code

Hi @aditya198018d,

Python requires you to use 4 spaces for indentation. Check if changing the spaces from 3 to 4 helps. Alternatively, what @rahul.singh1 said also works.

@garain.ridyumna I have tried doing so. Still the same results.

@spriyam095 Here is the updated code and the result :point_down:t2:

1 Like

Try removing the single space before the ‘’’ . I believe this could be the issue. Alternatively, try commenting out (using #) the description of the function instead of putting it in quotes.

@aditya198018d Bro in your code you have an error!!..You have written-

mybolt = Bolt(conf.Api_key, device_id)

in the above line it should be conf.device_id like below

mybolt = Bolt(conf.Api_key, conf.device_id)

And I don’t see any indentation error now…but still if after adding conf to your device_id doesn’t solve it…I suggest you delete all the spaces,…get your code in align without any indentation and then use tabs to give indentation again

@garain.ridyumna Those are indeed single quotes without spaces. They actually are illusive to see at first.

@spriyam095 I corrected the mistake you pointed out. Still, Alas! you can guess what was the outcome.
I already have replaced all the indentations.

Check if indenting before the quotes helps. Though, if this is indeed causing the issue, it should point to line 8 where the function description is and not to line 9.
Everything else seems to be okay with the code.
Lastly, if it still doesnt work, try what @spriyam095 said.


@aditya198018d @garain.ridyumna

Guys! indentation also applies to the comments and I saw the comments are not indented properly.

For now delete the comments below the get _sensor_value_from_pin(): and send_telegram_message(): functions

And after the code runs you may add comments again with proper indentation.


Yes it did work! Thank you so much for paying such an attention. Much appreciated!!!

1 Like

Yes brother, finally picked up the nerve which was causing the problem. Thanks for bringing this up. I didn’t know that comments need to be indented properly too. After indenting the comments with the following blocks the code has started working.
Thank you so much man for your efforts! Much appreciated!!!

1 Like

That’s great! Good luck!

1 Like