-4
if IsInBrew() == 'InBrew':
    # first lets work out where we are...
    thisBrew = getCurrentBrewID()
    latestStage = getCurrentStage(thisBrew)

    elapsedtime = now - latestStage[1]
    hours = divmod(elapsedtime.total_seconds(),3600)
    minutes = divmod(hours[1], 60)

    totalMinutes = (hours * 60) + minutes

    #addDebug('%d hours, %d minutes' % (hours[0], minutes[0]))

    if latestStage[0] == 'Fill HLT':
        # we can't do anything until HLT is full
        addDebug('Current Temp:' + str(getHLTTemperature()))
    elif latestStage[0] == 'Boil HLT':
        # we are now boiling up so check temp against our max value
        addDebug('In Boil HLT %s' % getHLTTemperature())
        MaxHLT = float(getKeyValue(thisBrew, 'HLTReleaseTemp'))
        addDebug('Current: %s Max: %s' % (getHLTTemperature(), MaxHLT))
        if float(getHLTTemperature()) >= MaxHLT:
            # switch off the boil and release the water
            GPIO.digitalWrite(HLTBOIL, GPIO.HIGH)
            GPIO.digitalWrite(HLTRELEASE, GPIO.HIGH)
            moveToNextStage = True
    elif latestStage[0] == 'Maintain Mash':     
        currentMashTemp = float(getMashTemperature())
        print currentMashTemp
        MaintainMashTemp = float(getKeyValue(thisBrew, 'MashTempMaintain'))

        print 'Current Mash %.2f Maintain Temp %.2f' % (currentMashTemp, MaintainMashTemp)

        #if Mash Temp too hot turn off boil
        if currentMashTemp > (MaintainMashTemp + TOLERANCE):
            print 'Mash temp high, turn off boil'
            GPIO.digitalWrite(MASHMAINTAIN, GPIO.LOW)

        #if Mash Temp too low turn on boil
        if currentMashTemp < MaintainMashTemp:
            print 'Mash temp low, turn on boil'

            GPIO.digitalWrite(MASHMAINTAIN, GPIO.HIGH)

        # release if we are at temp and elapsed time = maintain duration    
        if  totalMinutes >= int(getKeyValue(thisBrew, 'MashDuration')) and currentMashTemp >= MaintainMashTemp:
            GPIO.digitalWrite(MASHMAINTAIN, GPIO.LOW)
            GPIO.digitalWrite(MASHRELEASE, GPIO.HIGH)
            moveToNextStage = True

print 'Latest Stage is %s' % latestStage[0] 

if latestStage[0] == 'Maintain Mash':
    print 'Equal'

So I have this strange problem where my second elif (elif latestStage[0] == 'Maintain Mash':) never runs the code within it. The first if and elif work fine.

I even added a check at the bottom so I can see 'Equal' being output. I cannot see for the life of me why that second elif never executes.

The getCurrentStage(thisBrew) function will return a string and a datetime value, hence latestStage[0] which is the string and latestStage[1] which is the date of last update.

garyk1968
  • 121
  • 3
  • 8
  • 2
    How are we supposed to know what ````getCurrentStage(thisBrew)```` does? Please be more precise with your question and try to reduce it to the significant part. Your code is unnecessary complicated and hard to read. – mattmilten Jun 01 '15 at 16:58
  • I didn't want to include the whole lot but thought it might help. – garyk1968 Jun 01 '15 at 17:04
  • In order to reach `elif latestStage[0] == 'Maintain Mash'` you first need to pass `if IsInBrew() == 'InBrew'`. Add a print statement to see the value of `IsInBrew`. – Steven Rumbalski Jun 01 '15 at 17:06
  • Yes I get past that so it fires on 'Fill HLT' and 'Boil HLT' just fine. It just never evaluates 'Maintain Mash' – garyk1968 Jun 01 '15 at 17:07
  • Yes, but in processing those other steps do you end up changing the brew state? Add a print statement and you will know for sure. – Steven Rumbalski Jun 01 '15 at 17:10
  • try printing out the value of `latestStage[0]` before the entire if block, that'd help you determine if that `elif` block should ever fire – oxymor0n Jun 01 '15 at 17:11
  • Put `print repr(latestStage[0]), type(latestStage[0])` *inside* the outmost `if` block. If that doesn't make it clear to you what the problem is, post the *exact* output here. – ekhumoro Jun 01 '15 at 17:16
  • OK, so added repr as you suggested @ekhumoro and I get: u'Maintain Mash' – garyk1968 Jun 01 '15 at 17:24
  • Was that print statement inside of `if IsInBrew() == 'InBrew':`? – Steven Rumbalski Jun 01 '15 at 17:24
  • Yes it was @StevenRumbalski – garyk1968 Jun 01 '15 at 17:30
  • 1
    @user1536187. The only conclusion I can draw from that, is that the code you are running is not what you have posted. If I add a few dummy functions and comment out the irrelevant parts, it works fine for me. My advice is that you should try to produce a small, self-contained example that reproduces the problem - 9 times out of 10, you will discover the bug long before you finish doing that. – ekhumoro Jun 01 '15 at 17:40
  • Thanks @ekhumoro will do, yes I can't see any reason why it should fail. So as you suggest will start with a simple example and build it up. – garyk1968 Jun 01 '15 at 17:44

1 Answers1

0

It was the indentation!

I'm not a newbie coder but I am a python newbie. I knew indentation was important but when I copied the code and used a linux text editor (nano) I could see a mixture of spaces and tabs. So I checked the settings I was using in Sublime, and made sure everything is indented as 4 spaces and voila!

Thanks for everyone's help!

garyk1968
  • 121
  • 3
  • 8