[SOLVED} Arithmetic operations in rules

syntax
variables
Tags: #<Tag:0x00007fe050c3bbe8> #<Tag:0x00007fe050c3ba58>

(Michael Ingraham) #1

I’ve looked through the topics regarding variables/values and using arithmetic operations and tried many variations based on those posts. I can’t find where I’m failing.

Items:

Group:Switch:OR(ON, OFF) gRegion "Someone In Town"
Switch U1_region "U1 in Town" (gRegion) {mqttitude="openhab_tcpbroker:owntracks/user/u1d/event:R1"}
Switch U2_region "U2 in Town" (gRegion) {mqttitude="openhab_tcpbroker:owntracks/user/u2d/event:R1"}
String Nest_Away "Structure Home/Away [%s]" {nest="=[structures(Home).away]"}
Number thermostat_TargetTemperatureMax "Target Temperature Max [%.1f °F]" {nest="=[thermostats(Home Thermostat).max_set_point]"}
Number thermostat_TargetTemperatureMin "Target Temperature Min [%.1f °F]" {nest="=[thermostats(Home Thermostat).min_set_point]"}

Rule:

rule "Adjust Nest Thermostat Targets"
    when
        Item gRegion changed
    then
        TargetMax = thermostat_TargetTemperatureMax.state as DecimalType
        TargetMin = thermostat_TargetTemperatureMin.state as DecimalType

        if (Nest_Away.state == "HOME")
        {
            if (gRegion.state == OFF)
            {
                TargetMax = TargetMax + 5.0
                TargetMin = TargetMin - 5.0
            }
            else
            {
                TargetMax = TargetMax - 5.0
                TargetMin = TargetMin + 5.0
            }
            thermostat_TargetTemperatureMax.sendCommand(TargetMax)
            thermostat_TargetTemperatureMin.sendCommand(TargetMin)
        }
end

I am not listing the errors I’ve received because as I’ve tried different syntax variations, I’ve received different errors (can’t assign to void, “minus sign” or “plus sign” is not recognized, etc.). I’m sure the solution will be simple for someone with a modicum of openHAB coding experience, i.e., not me :wink:

Basically, when I arrive/depart, I want to adjust the thermostat temperature set points. My Nest is configured for HEAT_COOL mode.

Regards.

Mike


(Rich Koshak) #2

You are missing a var in your definition of TargetMax and TargetMin.

Also, you should use Number instead of DecimalType to avoid the “ambiguous function call” error when you sendCommand later.

var TargetMax = thermostat_TargetTemperatureMax.state as Number

Not an error, but one thing that will make it easier for users on this forum to help will be to use the standard naming conventions. It isn’t really complicated:

  • Items start with a capital letter and use “_” and/or camel case (e.g FirstSecond)
  • vars start with a lower case and use camel case (e.g. firstSecond)
  • vals use all caps and “_” (e.g. FIRST_SECOND)

It’s a little thing, but when you follow the common convention, it is really easy for us to read and understand immediately what is an Item and what is a variable. Above I had to jump all over the place to figure out what was what.

Also, instead of using quotes, use How to use code fences.

The code above was simple enough but it is a lot easier to read when all the indentation and line spacing is preserved.

Let me know if that fixes it. If not, please do post the exact errors you are receiving with the exact code you are posting. It may not tell you anything but it tells us a ton.


(Michael Ingraham) #3

Thanks Rich - Number is what did it. One of the many iterations I had tried did have the ‘var’ in the definition. I just hadn’t tried ‘as Number’. Many, many examples… and just found using Int, Float, DecimalType… but didn’t run across Number. Once again, casting and typing have been my downfall. The error messages that were logged provided not a clue to that being an issue.

Now is a good time for me to adopt naming conventions before I get too far into my setup. Having said that, the names in the code I posted is not what I’m using in my actual code - mainly in the naming convention of my items.