Ankündigung

Einklappen
Keine Ankündigung bisher.

Multiple if statements?

Einklappen
X
 
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

    Multiple if statements?

    Hello Guys.
    I'm having a bit difficult with a logic, the code is:
    Code:
    if sh.Sleeping_Room.Heating.Mode == 1:
    	if sh.Sleeping_Room.Heating.SP() > 5.0:
    		sh.Sleeping_Room.Heating.Valve('1')
    	else:
    		sh.Sleeping_Room.Heating.Valve('0')
    
    elif sh.Sleeping_Room.Heating.Mode == 3:
    	if sh.Sleeping_Room.Heating.SP() > 2.0:
    		sh.Sleeping_Room.Heating.Valve('1')
    	else:
    		sh.Sleeping_Room.Heating.Valve('0')
    
    else:
    	sh.Sleeping_Room.Heating.Valve('0')
    This doesn't work, in the debugger i can see that the mode changes to 1, then it seems like it doesn't execute the next IF statement??

    If i do like this:
    Code:
    	if sh.Sleeping_Room.Heating.SP() > 5.0:
    		sh.Sleeping_Room.Heating.Valve('1')
    	else:
    		sh.Sleeping_Room.Heating.Valve('0')
    it works like it should.... What am i doing wrong?

    The Heating.Mode is 1 byte output from the RTR widget.....

    #2
    No one has a idea?

    Kommentar


      #3
      Try this one:
      Code:
      if sh.Sleeping_Room.Heating.Mode() == 1 and sh.Sleeping_Room.Heating.SP() > 5.0:
          sh.Sleeping_Room.Heating.Valve('1')
      else:
          sh.Sleeping_Room.Heating.Valve('0')

      Kommentar


        #4
        Okay, i will try that...

        But, shouldn't it be possible with multiple statements? I think in other cases that would be needed....

        Kommentar


          #5
          You forgot some brackets:

          Code:
          if [URL="http://redaktion.knx-user-forum.de/lexikon/sh/"]sh[/URL].Sleeping_Room.Heating.Mode[COLOR=Red][B]()[/B][/COLOR] == 1:     
                  if [URL="http://redaktion.knx-user-forum.de/lexikon/sh/"]sh[/URL].Sleeping_Room.Heating.SP() > 5.0:
                          sh.Sleeping_Room.Heating.Valve('1')
                  else:
          
          and so on
          Btw: Without actually having checked the whole logic itself, it seems a bit complicated to me.

          Why not doing something like this (UNTESTED of course)?

          Code:
          if [URL="http://redaktion.knx-user-forum.de/lexikon/sh/"]sh[/URL].Sleeping_Room.Heating.Mode() == 1 and [URL="http://redaktion.knx-user-forum.de/lexikon/sh/"]sh[/URL].Sleeping_Room.Heating.SP() > 5:
               sh.Sleeping_Room.Heating.Valve('1')
          elif: if [URL="http://redaktion.knx-user-forum.de/lexikon/sh/"]sh[/URL].Sleeping_Room.Heating.Mode() == 3 and [URL="http://redaktion.knx-user-forum.de/lexikon/sh/"]sh[/URL].Sleeping_Room.Heating.SP() > 2:
               sh.Sleeping_Room.Heating.Valve('1')
          else:
               sh.Sleeping_Room.Heating.Valve('0')
          Cheers, Martin

          Kommentar


            #6
            Yes, that's also possible.
            Was just my first try to see what i can and can't do with the logics

            The example you write is much lesser complicated.... But that also uses the elif, as i was trying to get working - thanks a lot

            Kommentar


              #7
              Without elif (maybe):

              Code:
              if ([URL="http://redaktion.knx-user-forum.de/lexikon/sh/"]sh[/URL].Sleeping_Room.Heating.Mode() == 1 and [URL="http://redaktion.knx-user-forum.de/lexikon/sh/"]sh[/URL].Sleeping_Room.Heating.SP() > 5:) or ([URL="http://redaktion.knx-user-forum.de/lexikon/sh/"]sh[/URL].Sleeping_Room.Heating.Mode() == 3 and [URL="http://redaktion.knx-user-forum.de/lexikon/sh/"]sh[/URL].Sleeping_Room.Heating.SP() > 2): 
                   [URL="http://redaktion.knx-user-forum.de/lexikon/sh/"]sh[/URL].Sleeping_Room.Heating.Valve('1')
              else:
                   [URL="http://redaktion.knx-user-forum.de/lexikon/sh/"]sh[/URL].Sleeping_Room.Heating.Valve('0')

              Kommentar


                #8
                Zitat von Sipple Beitrag anzeigen
                Without elif (maybe):

                Code:
                if ([URL="http://redaktion.knx-user-forum.de/lexikon/sh/"]sh[/URL].Sleeping_Room.Heating.Mode() == 1 and [URL="http://redaktion.knx-user-forum.de/lexikon/sh/"]sh[/URL].Sleeping_Room.Heating.SP() > 5:) or ([URL="http://redaktion.knx-user-forum.de/lexikon/sh/"]sh[/URL].Sleeping_Room.Heating.Mode() == 3 and [URL="http://redaktion.knx-user-forum.de/lexikon/sh/"]sh[/URL].Sleeping_Room.Heating.SP() > 2): 
                     [URL="http://redaktion.knx-user-forum.de/lexikon/sh/"]sh[/URL].Sleeping_Room.Heating.Valve('1')
                else:
                     [URL="http://redaktion.knx-user-forum.de/lexikon/sh/"]sh[/URL].Sleeping_Room.Heating.Valve('0')
                Yes that's also possible i think, but the expressions gets very long then, and not pretty

                Normally it's possible to use multiple IF, or even better would be a CASE statement

                Kommentar


                  #9
                  Hello again

                  I've done some coding in Visual Studio with the Python package installed, which gives me a big help writing logics and stuff for the sh.py.
                  And again this thread confuses me: If i do like this in Visual Studio, it works great:
                  Code:
                  if weekday == 6 or weekday == 7 and int_t3_on_hour != 0 and int_t3_on_minute != 0 :
                      print(7)
                      if hour == int_t3_on_hour and minute == int_t3_on_minute and outside_light == False :
                          outside_light = True
                          print(8)
                      if hour == int_t3_off_hour and minute == int_t3_off_minute and outside_light == True :
                          outside_light = False
                          print(9)
                  But if i use that in the sh.py it only prints the number "7" in the debugger, which seems like it doesn't "eat" the next IF statement - even if it does in Visual Studio..... Could some one please explain why or how i use multiple if statements?


                  ------------edit:

                  Seems like the best way is to use a / to break a line and then use the AND operator :/

                  Kommentar

                  Lädt...
                  X