Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
You can use command blocks proximity detector command. justJust stuff the command blocks away. hereHere is a example
-=redstone
|-|=powered rail
c= command block: