API Query and Negative Value

This topic contains 4 replies, has 3 voices, and was last updated by  Rob Simmers 1 month ago.

  • Author
    Posts
  • #102730

    Alan Williams
    Participant

    I have the following basic code returning a giant array:

    $request = 'http://10.111.113.253/api'
    $values = Invoke-RestMethod $request
    foreach ($value in $values){
        $value.Setpoints.Refrigeration
    }
    

    This returns the following values:

    RoomTemp : @{current=38.0 F; currentValue=38.0; units=F}
    Refrigerant : @{current=R-404A; currentIndex=0; options=System.Object[]}
    MinCompRuntime : @{current=2 MIN; currentValue=2; units=MIN}
    MinCompOffTime : @{current=5 MIN; currentValue=5; units=MIN}
    AirTempDiff : @{current=1.0 F; currentValue=1.0; units=F}
    2ndRoomTemp : @{current=-50.0 F; currentValue=-50.0; units=F}
    AuxTemp1 : @{current=T1 Suct Temp; currentIndex=3; options=System.Object[]}
    AuxTemp4 : @{current=T4 Coil Temp; currentIndex=3; options=System.Object[]}
    RefrigFanMode : @{current=Permanent; currentIndex=1; options=System.Object[]}
    FanSpeed : @{current=0.0 %; currentValue=0.0; units=%}
    TempUnits : @{current=Fahrenheit; currentIndex=0; options=System.Object[]}
    MultiAirTempControl : @{current=Warmest Air; currentIndex=0; options=System.Object[]}

    I can query/pull the nested values such as:

    $value.Setpoints.Refrigeration.AirTempDiff
    

    Which returns:
    current currentValue units
    ——- ———— —–
    1.0 F 1.0 F

    However when I try the following I recieve an error:

    $value.Setpoints.Refrigeration.2ndRoomTemp
    

    Error:
    + $value.Setpoints.Refrigeration.2ndRoomTemp
    + ~
    Missing property name after reference operator.
    + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingPropertyName

    I'm assuming this is caused by the one of two things, the "2ndRoomTemp" begins with a number or the negative value it returns, but not 100% sure. I can query all of the values except this one.

    Any help or suggestions would be greatly appreciated.

  • #102731

    Don Jones
    Keymaster

    It's probably the 2.

    Try $value.Setpoints.Refrigeration.{2ndRoomTemp}

  • #102740

    Alan Williams
    Participant

    Thank you Don! That worked great. Can I ask why this is the case? Is the "-" sign considered a special character in this case? This is discussed here:

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_variables?view=powershell-6&viewFallbackFrom=powershell-Microsoft.PowerShell.Core

    C:\PS> ${save-items} = "a", "b", "c"
    C:\PS> ${save-items}
    a
    b
    c

    Is there a better way to handle the possibility of negative values in this case? I'm working with a refrigeration controller API interface so there is a possibility to have other fields with negative values since temperatures can change...

    Thank you again!

    Kind Regards,

    Alan

  • #102742

    Don Jones
    Keymaster

    It's that property names can't legally start with a digit in .NET (and many languages). The {} is kind of a PowerShell way of working around it anytime you've got a normally-illegal name in a variable or property. And yeah, – is not legal in a variable name.

    ${This! is a stupid "variable" name}

    Is actually legal thanks to the {}. Terrible idea, but legal.

  • #102745

    Rob Simmers
    Participant

    Another workaround:

    PS C:\WINDOWS\system32> $obj = @()
    $obj += [pscustomobject]@{
        'MinCompOffTime' = [pscustomobject]@{
            current='5 MIN';
            currentValue='5';
            units='MIN';
        }
    }
    $obj += [pscustomobject]@{
        'AirTempDiff' = [pscustomobject]@{
            current='1.0 F';
            currentValue='1.0';
            units='F';
        }
    }
    $obj += [pscustomobject]@{
        '2ndRoomTemp' = [pscustomobject]@{
            current='-50.0 F';
            currentValue='50';
            units='F';
        }
    }
    
    PS C:\WINDOWS\system32> $obj.2ndRoomTemp
    At line:1 char:6
    + $obj.2ndRoomTemp
    +      ~
    Missing property name after reference operator.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : MissingPropertyName
     
    
    PS C:\WINDOWS\system32> $obj.'2ndRoomTemp'
    
    current currentValue units
    ------- ------------ -----
    -50.0 F 50           F   
    

You must be logged in to reply to this topic.