Binary math in a openhab2 rule

bit-math
Tags: #<Tag:0x00007fe066352cc8>

(Ian Macdonald) #1

I have a homeseer HS-WD200 https://homeseer.com/wp-content/uploads/2018/02/HS-WD200-Manual-4.pdf

I am trying to use Blink in Status mode which is described as

Bitmask defines specific LEDs to enable for blinking:
Note: this decimal value is derived from a hex code
calculation based on the following:
Bit 0 = led 1, Bit 1 = led 2, Bit 2 = led 3, Bit 3 = led 4,
Bit 4 = led 5, Bit 5 = led 6, Bit 6 = led 7
IE: value of 1 = first LED, 64 = led 7

I have two motion sensors that detect if the two garage doors are open. In a rule I would like to be able to read the current state of the Blink in Status and turn on LED7 if Door 1 is open and LED6 if Door 2 is open.

so far I have

logInfo(“TEST”,"BlinkStatus: " + tv_led_blink_status.state.toString)
tv_led_blink_status.sendCommand(64)
logInfo(“TEST”,"BlinkStatus3: " + tv_led_blink_status.state.toString)

I would like to be able to convert tv_led_blink_status.state to something i can do bit math on to set the just bit 5 or bit6 depending on which do has been open.

I thought i could do something like
int state = 32;
int door1 = 32;
int door2 = 64;
int newstate = state | door2
so i would have a value of 96 that I can use in tv_led_blink_status.sendCommand, but int newstate = state | door2 give me an error.

Anything thoughts on how to do this?

Thanks


(Ian Macdonald) #3

a few minutes after posting i found [Solved] Binary Operations in Rules which points to https://groups.google.com/forum/#!topic/openhab/z4cYibhnoHc

With that pointer can do

    var Integer state = (tv_led_blink_status.state as DecimalType).intValue
    var Integer mask = state.bitwiseOr(64)
    tv_led_blink_status.sendCommand(mask)

(Ian Macdonald) #4

For reference here is what I have so far. I am using switches instead of motion sensors for testing.

** Items

Dimmer light_tv_dimmer "TV light" (GF,gfLights) [ "Lighting" ] {channel="zwave:device:9d6ae427:node20:switch_dimmer"}
Switch light_tv_switch "TV light switch"  (GF,gfLights,gSunset,g1am) [ "Lighting" ] {channel="zwave:device:9d6ae427:node20:switch_dimmer"}

Number tv_mode "TV Mode" {channel="zwave:device:9d6ae427:node20:config_decimal_param13"}
Number tv_led1 "TV LED 1" {channel="zwave:device:9d6ae427:node20:config_decimal_param21"}
Number tv_led2 "TV LED 2" {channel="zwave:device:9d6ae427:node20:config_decimal_param22"}
Number tv_led3 "TV LED 3" {channel="zwave:device:9d6ae427:node20:config_decimal_param23"}
Number tv_led4 "TV LED 4" {channel="zwave:device:9d6ae427:node20:config_decimal_param24"}
Number tv_led5 "TV LED 5" {channel="zwave:device:9d6ae427:node20:config_decimal_param25"}
Number tv_led6 "TV LED 6" {channel="zwave:device:9d6ae427:node20:config_decimal_param26"}
Number tv_led7 "TV LED 7" {channel="zwave:device:9d6ae427:node20:config_decimal_param27"}
Number tv_blink_feq "TV Blink Frequency" {channel="zwave:device:9d6ae427:node20:config_decimal_param30"}
Number tv_led_blink_status "TV Blink Status" {channel="zwave:device:9d6ae427:node20:config_decimal_param31"}

**RULES

rule "G1Open"
when
        Item light_living_room_switch changed to ON
then
        logInfo("TEST","G1 ON")
        tv_mode.sendCommand(1)
        tv_blink_feq.sendCommand(1)
        tv_led7.sendCommand(1)
        var Integer state = (tv_led_blink_status.state as DecimalType).intValue
        var Integer mask = state.bitwiseOr(64)
        tv_led_blink_status.sendCommand(mask)
end

rule "G1Closed"
when
        Item light_living_room_switch changed to OFF
then
        logInfo("TEST", "G1 OFF")
        var Integer state = (tv_led_blink_status.state as DecimalType).intValue
        var Integer mask = state.bitwiseXor(64)
        if (mask == 0){
                tv_mode.sendCommand(0)
        }
        tv_led7.sendCommand(0)
        tv_led_blink_status.sendCommand(mask)
end

rule "G2Open"
when
        Item light_door_switch changed to ON
then
        logInfo("TEST","G2 ON")
        tv_mode.sendCommand(1)
        tv_blink_feq.sendCommand(1)
        tv_led6.sendCommand(2)
        var Integer state = (tv_led_blink_status.state as DecimalType).intValue
        var Integer mask = state.bitwiseOr(32)
        tv_led_blink_status.sendCommand(mask)
end

rule "G2Closed"
when
        Item light_door_switch changed to OFF
then
        logInfo("TEST", "G2 OFF")
        var Integer state = (tv_led_blink_status.state as DecimalType).intValue
        var Integer mask = state.bitwiseXor(32)
        if (mask == 0) {
                tv_mode.sendCommand(0)
        }
        tv_led6.sendCommand(0)
        tv_led_blink_status.sendCommand(mask)
end

(Rich Koshak) #5

Thanks for posting the working result. It will be a lot easier for us to read though if you How to use code fences.


(Ian Macdonald) #6

Updated, thanks for the pointer on code fences, each site does it a little differently :slight_smile: