I’m working on a project where I want to monitor my refrigerator door using a PIR sensor and send notifications through a Telegram bot. The main issue I’m facing is that the PIR sensor needs to continuously check if the door is open or closed, but this constant monitoring creates an infinite loop that prevents my Telegram bot from starting properly.
When I put the sensor monitoring loop at the beginning of my code, it never gets to the part where I set up the Telegram bot commands. But if I move the PIR sensor code after the bot setup, I can’t use the sensor data in my Telegram commands.
I tried creating a separate function for the PIR monitoring that starts when called, but placing it at the end of my program doesn’t seem to work either. The timing and execution order is really confusing me.
What’s the best approach to handle both the continuous sensor monitoring and the Telegram bot functionality at the same time? Any suggestions would be really appreciated!
just use a simple timer - way easier than threading. set up your bot, then run a timer that checks the PIR sensor every few seconds and updates a global variable. something like schedule.every() works perfectly for this.
Had this exact issue last year building a temperature monitor with Discord alerts. Here’s what worked: use a queue-based setup where your PIR sensor runs in a background thread and pushes status changes to a queue, while the main thread handles Telegram bot stuff. Create a shared queue with the queue module. Have your sensor thread monitor continuously but only queue messages when the door state actually changes - not every single reading. Your main bot thread checks this queue periodically with a non-blocking get() call. This kills the infinite loop problem since you’re not constantly polling - just processing real state changes. Big advantage over straight threading or asyncio is you avoid race conditions and don’t need to mess with shared variables. Bonus: you can easily add buffering for multiple events or retry logic if messages fail.
Totally agree! threading makes it easier. i had this issue with my relays n the bot too. once i used threading for the sensor, both worked well together. make sure to use locks if you need to to prevent issues.
Had the same issue with a motion detection system + Telegram setup. Asyncio fixed it for me instead of threading. Run your PIR monitoring and Telegram bot as coroutines in the same event loop - no more blocking problems since asyncio switches between tasks smoothly. Make your PIR readings an async function with await asyncio.sleep() for delays, and use python-telegram-bot’s async features. The sensor won’t block bot startup anymore, and you can share data through global variables or class attributes without messy thread sync.
I’ve been fighting sensor integration issues for years. Threading and asyncio work, but they’re way more complicated than you need.
Skip the complex threading logic. Use automation that handles continuous monitoring and bot messaging for you. I’ve used Latenode to solve this exact problem multiple times.
Here’s how: Set your PIR sensor to trigger webhooks when the door state changes. Latenode catches these and manages door state automatically. Door opens or stays open too long? It sends Telegram messages through their built-in integration.
No infinite loops to manage. No worrying about blocking your bot startup. Latenode handles timing and state management in the cloud. Your local code stays simple.
I built a server room door monitor last month this way. PIR sensor hits Latenode endpoints, Latenode tracks state and timing, sends Telegram alerts when needed. Zero threading headaches.
Your local code just reads sensors and pings Latenode. Everything else runs automatically in their workflows.