Announcement

Collapse
No announcement yet.

New to C#: I have one hurdle to get working with HS

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • New to C#: I have one hurdle to get working with HS

    I have a basic script that I'm starting to work on in C# to take some counter devices I have setup that are either 1 or 0 to indicate if they're occupied.
    I've successfully pulled the values from these, but now that I'm trying to work with them, I'm having trouble translating what I read about C# syntax into a homeseer script.
    My issues are around the If statements, the script runs fine without them. When I put in a namespace clause the compiler Fails, and it seems that the IF statement is not valid in the context.

    I'll be able to figure the rest out, i just can't bridge this syntax to compiled C#

    Code:
    using System;
    
    public object Main(object[] Parms) {
        //Temp Schedule
        Double thermostatDay=hs.DeviceValueByNameEx("Counters-Timers Counters ThermostatDay");
        Double thermostatEvening=hs.DeviceValueByNameEx("Counters-Timers Counters ThermostatEvening");
        Double thermostatNight=hs.DeviceValueByNameEx("Counters-Timers Counters ThermostatNight");
        Double thermostatPeakDay=hs.DeviceValueByNameEx("Counters-Timers Counters ThermostatPeakDay");
    
        //Room Occupancy
        Double occupancyF1Hallway=hs.DeviceValueByNameEx("Counters-Timers Counters OccupancyF1Hallway");
        Double occupancyF1Garage=hs.DeviceValueByNameEx("Counters-Timers Counters OccupancyF1Garage");
        Double occupancyF1GuestRoom=hs.DeviceValueByNameEx("Counters-Timers Counters OccupancyF1GuestRoom");
        Double occupancyF2Atrium=hs.DeviceValueByNameEx("Counters-Timers Counters OccupancyF2Atrium");
        Double occupancyF3MasterBedroom=hs.DeviceValueByNameEx("Counters-Timers Counters OccupancyF3MasterBedroom");
        Double occupancyF3LeftRoom=hs.DeviceValueByNameEx("Counters-Timers Counters OccupancyF3LeftRoom");
        Double occupancyF3RightRoom=hs.DeviceValueByNameEx("Counters-Timers Counters OccupancyF3RightRoom");
    
    
        //Floor Occupancy
        if(occupancyF1Hallway==1||occupancyF1Garage==1||occupancyF1GuestRoom==1){
            Boolean occupancyF1=true;
        }else{
            Boolean occupancyF1=false;
        }
        
        if(occupancyF2Atrium==1){
            Boolean occupancyF2=true;
        }else{
            Boolean occupancyF2=false;
        }
        
        if(occupancyF3MasterBedroom==1||occupancyF3LeftRoom==1||occupancyF3RightRoom==1){
            Boolean occupancyF3=true;
        }else{
            Boolean occupancyF3=false;
        }
    
        //Building Occupancy
        if(occupancyF1||occupancyF2||occupancyF3){
            Boolean occupancyBuilding=true;
        }else if( !OccupancyF1&&!OccupancyF2&&!OccupancyF3 ){
            Boolean occupancyBuilding=false;
        }
    
        //Away Schedule
        if(!occupancyBuilding){
            hs.WriteLog("HVAC Script", "Trigger Away Temperature");
            hs.TriggerEvent("AwayTemperature");
            return 0;
        }
    
        //Floor 1/2 Schedule
        if((((occupancyF1||occupancyF2)&&occupancyBuilding)&&thermostatDay)&&!thermostatPeakDay){
            hs.WriteLog("HVAC Script", "Trigger F1/2 Day Temperature");
            hs.TriggerEvent("F1_2-DayTemperature");
        }else if((((occupancyF1||occupancyF2)&&occupancyBuilding)&&thermostatDay)&&thermostatPeakDay){
            hs.WriteLog("HVAC Script", "Trigger F1/2 PeakDay Temperature");
            hs.TriggerEvent("F1_2-PeakDayTemperature");
        }else if(((occupancyF1||occupancyF2)&&occupancyBuilding)&&thermostatEvening){
            hs.WriteLog("HVAC Script", "Trigger F1/2 Evening Temperature");
            hs.TriggerEvent("F1_2-EveningTemperature");
        }else if(((!occupancyF1&&!occupancyF2)&&occupancyBuilding)&&thermostatEvening){
            hs.WriteLog("HVAC Script", "Trigger F1/2 Evening Hold Temperature");
            hs.TriggerEvent("F1_2-HoldTemperature");
        }else if(((occupancyF1||occupancyF2)&&occupancyBuilding)&&thermostatNight){
            hs.WriteLog("HVAC Script", "Trigger F1/2 Night Temperature");
            hs.TriggerEvent("F1_2-NightTemperature");
        }else if(((!occupancyF1&&!occupancyF2)&&occupancyBuilding)&&thermostatNight){
            hs.WriteLog("HVAC Script", "Trigger F1/2 Night Hold Temperature");
            hs.TriggerEvent("F1_2-HoldTemperature");
        }else{
            hs.WriteLog("HVAC Script", "Trigger F1/2 Away Temperature");
            hs.TriggerEvent("AwayTemperature");
        }
    
        //Floor 3 Schedule
        if(((occupancyF3&&occupancyBuilding)&&thermostatDay)&&!thermostatPeakDay){
            hs.WriteLog("HVAC Script", "Trigger F1/2 Day Temperature");
            hs.TriggerEvent("F1_2-DayTemperature");
        }else if(((occupancyF3&&occupancyBuilding)&&thermostatDay)&&thermostatPeakDay){
            hs.WriteLog("HVAC Script", "Trigger F1/2 PeakDay Temperature");
            hs.TriggerEvent("F1_2-PeakDayTemperature");
        }else if((occupancyF3&&occupancyBuilding)&&thermostatEvening){
            hs.WriteLog("HVAC Script", "Trigger F1/2 Evening Temperature");
            hs.TriggerEvent("F1_2-EveningTemperature");
        }else if((!occupancyF3&&occupancyBuilding)&&thermostatEvening){
            hs.WriteLog("HVAC Script", "Trigger F1/2 Evening Hold Temperature");
            hs.TriggerEvent("F1_2-HoldTemperature");
        }else if((occupancyF3&&occupancyBuilding)&&thermostatNight){
            hs.WriteLog("HVAC Script", "Trigger F1/2 Night Temperature");
            hs.TriggerEvent("F1_2-NightTemperature");
        }else if((!occupancyF3&&occupancyBuilding)&&thermostatNight){
            hs.WriteLog("HVAC Script", "Trigger F1/2 Night Hold Temperature");
            hs.TriggerEvent("F1_2-HoldTemperature");
        }else{
            hs.WriteLog("HVAC Script", "Trigger F1/2 Away Temperature");
            hs.TriggerEvent("AwayTemperature");
        }
    
        return 0;
    }
    Also...Is the C# memory leak still an issue I 've seen in other posts?
    Last edited by ACiDGRiM; July 11th, 2018, 01:58 PM.

  • #2
    Remove semicolon after closing bracket in every if and else statement. They don't belong there. Also, I don't think elif is keyword in C#. Replace it with else if. Normally, C# is case sensitive, so If and Else should be if and else.

    Comment


    • #3
      Thanks, the case sensitivity of the if else resolved the issues, the colons were because a syntax validator indicated they were required, but because of the issues with case.

      Code:
      using System;
      
      public object Main ( object[] Parms ) {
          //Temp Schedule
          Double thermostatDay = hs.DeviceValueByNameEx ( "Counters-Timers Counters ThermostatDay" );
          Double thermostatEvening = hs.DeviceValueByNameEx ( "Counters-Timers Counters ThermostatEvening" );
          Double thermostatNight = hs.DeviceValueByNameEx ( "Counters-Timers Counters ThermostatNight" );
          Double thermostatPeakDay = hs.DeviceValueByNameEx ( "Counters-Timers Counters ThermostatPeakDay" );
      
          //Room Occupancy
          Double occupancyF1Hallway = hs.DeviceValueByNameEx ( "Counters-Timers Counters OccupancyF1Hallway" );
          Double occupancyF1Garage = hs.DeviceValueByNameEx ( "Counters-Timers Counters OccupancyF1Garage" );
          Double occupancyF1GuestRoom = hs.DeviceValueByNameEx ( "Counters-Timers Counters OccupancyF1GuestRoom" );
          Double occupancyF2Atrium = hs.DeviceValueByNameEx ( "Counters-Timers Counters OccupancyF2Atrium" );
          Double occupancyF3MasterBedroom = hs.DeviceValueByNameEx ( "Counters-Timers Counters OccupancyF3MasterBedroom" );
          Double occupancyF3LeftRoom = hs.DeviceValueByNameEx ( "Counters-Timers Counters OccupancyF3LeftRoom" );
          Double occupancyF3RightRoom = hs.DeviceValueByNameEx ( "Counters-Timers Counters OccupancyF3RightRoom" );
      
          //Floor Occupancy
          Boolean occupancyF1;
          Boolean occupancyF2;
          Boolean occupancyF3;
          Boolean occupancyBuilding;
      
          if ( occupancyF1Hallway == 1 || occupancyF1Garage == 1 || occupancyF1GuestRoom == 1 ) {
              occupancyF1 = true;
          } else {
              occupancyF1 = false;
          }
          
      
          if ( occupancyF2Atrium == 1 ) {
              occupancyF2 = true;
          } else {
              occupancyF2 = false;
          }
      
          if ( occupancyF3MasterBedroom == 1 || occupancyF3LeftRoom == 1 || occupancyF3RightRoom == 1 ) {
              occupancyF3 = true;
          } else {
              occupancyF3 = false;
          }
          
          //Building Occupancy
          if ( occupancyF1 || occupancyF2 || occupancyF3 ) {
              occupancyBuilding = true;
          } else if ( !occupancyF1 && !occupancyF2 && !occupancyF3 ) {
              occupancyBuilding = false;
          }
      
          //Away Schedule
          if ( !occupancyBuilding ) {
              hs.WriteLog ( "HVAC Script", "Trigger Away Temperature" );
              hs.TriggerEvent ( "AwayTemperature" );
              return 0;
          }
      
          //Floor 1/2 Schedule
          if ( (occupancyF1 || occupancyF2) && occupancyBuilding ) {
              if ( thermostatDay == 1 && thermostatPeakDay != 1 ) {
                  hs.WriteLog ( "HVAC Script", "Trigger F1/2 Day Temperature" );
                  hs.TriggerEvent ( "F1_2-DayTemperature" );
              } else if ( thermostatDay == 1 && thermostatPeakDay == 1 ) {
                  hs.WriteLog ( "HVAC Script", "Trigger F1/2 PeakDay Temperature" );
                  hs.TriggerEvent ( "F1_2-PeakDayTemperature" );
              } else if (  thermostatEvening == 1 ) {
                  hs.WriteLog ( "HVAC Script", "Trigger F1/2 Evening Temperature" );
                  hs.TriggerEvent ( "F1_2-EveningTemperature" );
              } else if  (  thermostatNight == 1 ) ) {
                  hs.WriteLog ( "HVAC Script", "Trigger F1/2 Night Temperature" );
                  hs.TriggerEvent ( "F1_2-NightTemperature" );
              } else {
                  hs.WriteLog ( "HVAC Script", "Trigger F1/2 Failsafe Temperature" );
                  hs.TriggerEvent ( "AwayTemperature" );
              }
          } else if ( !occupancyF1 && !occupancyF2) && occupancyBuilding ) {
              hs.WriteLog ( "HVAC Script", "Trigger F1/2 Hold Temperature" );
              hs.TriggerEvent ( "F1_2-HoldTemperature" );
          } else {
              hs.WriteLog ( "HVAC Script", "Trigger F1/2 Failsafe Temperature" );
              hs.TriggerEvent ( "AwayTemperature" );
          }
      
          //Floor 3 Schedule
          if ( occupancyF3 && occupancyBuilding ) {
              if ( thermostatDay == 1 && thermostatPeakDay != 1 ) {
                  hs.WriteLog ( "HVAC Script", "Trigger F3 Day Temperature" );
                  hs.TriggerEvent ( "F3-DayTemperature" );
              } else if ( thermostatDay == 1 && thermostatPeakDay == 1 ) {
                  hs.WriteLog ( "HVAC Script", "Trigger F3 PeakDay Temperature" );
                  hs.TriggerEvent ( "F3-PeakDayTemperature" );
              } else if (  thermostatEvening == 1 ) {
                  hs.WriteLog ( "HVAC Script", "Trigger F3 Evening Temperature" );
                  hs.TriggerEvent ( "F3-EveningTemperature" );
              } else if  (  thermostatNight == 1 ) ) {
                  hs.WriteLog ( "HVAC Script", "Trigger F3 Night Temperature" );
                  hs.TriggerEvent ( "F3-NightTemperature" );
              } else {
                  hs.WriteLog ( "HVAC Script", "Trigger F3 Failsafe Temperature" );
                  hs.TriggerEvent ( "AwayTemperature" );
              }
          } else if ( !occupancyF3 && occupancyBuilding ) {
              hs.WriteLog ( "HVAC Script", "Trigger F3 Hold Temperature" );
              hs.TriggerEvent ( "F3-HoldTemperature" );
          } else {
              hs.WriteLog ( "HVAC Script", "Trigger F3 Failsafe Temperature" );
              hs.TriggerEvent ( "AwayTemperature" );
          }
      
          return 0;
      }
      Last edited by ACiDGRiM; July 11th, 2018, 09:42 PM.

      Comment

      Working...
      X