Run something only once in while loop without breaking the loop
<pre><code>import RPi.GPIO as g
import time
g.setmode(g.BOARD)
g.setup(33, g.OUT)



while True:
  tempfile = open("/sys/devices/w1_bus_master1/28-00152c2631ee/w1_slave")
  thetext = tempfile.read()
  tempfile.close()
  tempdata = thetext.split("\n") [1].split(" ")[9]
  temperature = float(tempdata[2:])
  finaltemp = temperature / 1000
  time.sleep(1)

  if finaltemp &gt; 22:
    g.output(33,g.LOW)
    time.sleep(0.3)
    g.output(33,g.HIGH)
  else:
    g.output(33,g.LOW)
    time.sleep(0.3)
    g.output(33,g.HIGH)
</code></pre>

<p>I have searched numerous sites including this one, but never found my solution.</p>

<p>As you can see, the code currently grabs the temperature from the system file and stores the temperature in the variable "finaltemp". <br>
The way I have my hardware setup is so that my relay switch is connected to the push button on AC remote control, which is why I have my GPIO set up to turn on and off very quickly (0.3 seconds), to mimic the push of the button on the remote. </p>

<p>My goal is to 'blink' (push the button) the GPIO only once(!) when the temperature changes according to the condition. </p>

<p><em>For example</em>:</p>

<p>The temperature in room is 20 and the AC is off at the moment. Therefore, the temperature is slowly rising, and right when the temperature goes above 22, I want to run the 3 lines of code to run. What is happening however, is that it keeps checking it every time. Since the condition meets everytime the while loop starts over, it keeps running the code over and over, so essentially what's happening is that my AC keeps turning on and off and on and off. </p>

<p>What you're currently doing is just checking the temperature and using a condition to keep switching the AC on and off, which as you have already figured out will not work.</p>

<p>This is because your condition statements only look at the temperature and not and the current state of the AC, for example if you want the AC to turn on when the temperature is above 22C you're if should be something along the lines of :</p>

<pre><code>if temperature &gt; 22 &amp;&amp; AC == off
    // turn on AC
</code></pre>

python