Oh, missed the first part. Try
register = register.setBit(nBit)
and
register = register.clearBit(nBit)
instead.
Oh, missed the first part. Try
register = register.setBit(nBit)
and
register = register.clearBit(nBit)
instead.
See also this post. Once upon a time, the import java.lang.math
worked for me and Biginteger bitwise.
Then it didnât work - donât know why, but substitute import java.math.BigInteger
sorted it out.
To declare variables?
No. Please take a look at the Rule. There are only two lines, which use register.setBit() respectively register.clearBit(). just add register =
in front of the register.[set|clear]Bit()
I changed the lines in the code above.
In question of the import
, I donât need any import for BigInteger (but I did in OH1)
Log.
The import âjava.lang.Mathâ is never used.
thereâs no library?
The import 'java.lang.Math' is never used. 2020-07-20 19:08:51.657 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'output.rules' 2020-07-20 19:08:54.945 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Device out': 'setBit' is not a member of 'BigInteger'; line 21, column 20, length 21 2020-07-20 19:08:56.039 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Device out': 'setBit' is not a member of 'BigInteger'; line 21, column 20, length 21 2020-07-20 19:08:56.701 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Device out': 'setBit' is not a member of 'BigInteger'; line 21, column 20, length 21 2020-07-20 19:08:57.078 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Device out': 'setBit' is not a member of 'BigInteger'; line 21, column 20, length 21 2020-07-20 19:08:58.080 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Device out': 'clearBit' is not a member of 'BigInteger'; line 23, column 20, length 23 2020-07-20 19:08:58.528 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Device out': 'clearBit' is not a member of 'BigInteger'; line 23, column 20, length 23 2020-07-20 19:08:59.090 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Device out': 'clearBit' is not a member of 'BigInteger'; line 23, column 20, length 23 2020-07-20 19:08:59.378 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Device out': 'clearBit' is not a member of 'BigInteger'; line 23, column 20, length 23 2020-07-20 19:09:09.817 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Device out': 'setBit' is not a member of 'BigInteger'; line 21, column 20, length 21 2020-07-20 19:09:10.285 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Device out': 'setBit' is not a member of 'BigInteger'; line 21, column 20, length 21 2020-07-20 19:09:10.933 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Device out': 'setBit' is not a member of 'BigInteger'; line 21, column 20, length 21 2020-07-20 19:09:11.354 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Device out': 'clearBit' is not a member of 'BigInteger'; line 23, column 20, length 23 2020-07-20 19:09:11.701 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Device out': 'clearBit' is not a member of 'BigInteger'; line 23, column 20, length 23 2020-07-20 19:09:12.089 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Device out': 'clearBit' is not a member of 'BigInteger'; line 23, column 20, length 23 2020-07-20 19:09:14.037 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Device out': 'setBit' is not a member of 'BigInteger'; line 21, column 20, length 21 2020-07-20 19:09:14.325 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Device out': 'setBit' is not a member of 'BigInteger'; line 21, column 20, length 21 2020-07-20 19:09:14.668 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Device out': 'setBit' is not a member of 'BigInteger'; line 21, column 20, length 21 2020-07-20 19:09:21.118 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Device out': 'setBit' is not a member of 'BigInteger'; line 21, column 20, length 21 2020-07-20 19:09:21.664 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Device out': 'setBit' is not a member of 'BigInteger'; line 21, column 20, length 21 2020-07-20 19:09:23.005 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Device out': 'setBit' is not a member of 'BigInteger'; line 21, column 20, length 21 2020-07-20 19:09:23.025 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Device out': 'clearBit' is not a member of 'BigInteger'; line 23, column 20, length 23
This helped
import java.math.*
It works, but it doesnât work correctly, in a different way
No, import with * is not allowed.
Please try
import java.math.BigInteger
instead. (as @rossko57 already suggested)
switch randomâŠ
hmmâŠ
2020-07-20 19:36:55.179 [ERROR] [se.xtext.ide.server.WorkspaceManager] - The document file:///etc/openhab2/rules/output.rules has not been opened. 2020-07-20 19:36:58.109 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'output.rules' 2020-07-20 19:38:29.800 [WARN ] [io.openhabcloud.internal.CloudClient] - Jetty request 3009479 failed: null 2020-07-20 19:38:29.802 [WARN ] [io.openhabcloud.internal.CloudClient] - Response Failure: null 2020-07-20 19:38:48.516 [WARN ] [io.openhabcloud.internal.CloudClient] - Jetty request 3010310 failed: null 2020-07-20 19:38:48.518 [WARN ] [io.openhabcloud.internal.CloudClient] - Response Failure: null 2020-07-20 19:43:05.693 [WARN ] [io.openhabcloud.internal.CloudClient] - Jetty request 3024214 failed: null 2020-07-20 19:43:05.694 [WARN ] [io.openhabcloud.internal.CloudClient] - Response Failure: null
Looks like what can happen when you edit a rules file. Some editors touch the file timestamp before they have properly saved your new file, and openhab tries to load it prematurely.
Later it loads successfully, as seen in your log.
Cloud warnings are unrelated.
there are no more errors in log. The algorithm just doesnât work correctly. And switches on other devices are not updated.
Show us your events.log for when you expect something to have happened.
Did you already restart openHAB (after the last changes)?
No restart.
2020-07-20 19:55:55.034 [ome.event.ItemCommandEvent] - Item âlight_1â received command ON
2020-07-20 19:55:55.046 [ome.event.ItemCommandEvent] - Item âInOutâ received command 1
2020-07-20 19:55:58.273 [ome.event.ItemCommandEvent] - Item âlight_2â received command ON
2020-07-20 19:55:58.285 [ome.event.ItemCommandEvent] - Item âInOutâ received command 2
2020-07-20 19:55:59.335 [ome.event.ItemCommandEvent] - Item âlight_3â received command ON
2020-07-20 19:55:59.346 [ome.event.ItemCommandEvent] - Item âInOutâ received command 4
2020-07-20 19:56:00.571 [ome.event.ItemCommandEvent] - Item âlight_4â received command ON
2020-07-20 19:56:00.580 [ome.event.ItemCommandEvent] - Item âInOutâ received command 8
4 switch on = 15
Thatâs pretty much what I would expect with autoupdate=âfalseâ. You wonât get any state changes until you have your MQTT configured correctly.
To test if the rule is working as expected, you can change this line
Number DeviceIO "In/Out" { channel="mqtt:topic:myBroker:myDevice:IO", autoupdate="false" }
to this:
Number DeviceIO "In/Out" { channel="mqtt:topic:myBroker:myDevice:IO", autoupdate="true" }
but this is only to change update behavior temporally.
No work.
Number DeviceIO âIn/Outâ { channel=âmqtt:topic:myBroker:myDevice:IOâ, autoupdate=âtrueâ }
Group gSwitches { autoupdate=âtrueâ }
Switch Switch_1 âOut 1 [%s]â (gSwitches) { autoupdate=âtrueâ }
Number DeviceIO âIn/Outâ { channel=âmqtt:topic:myBroker:myDevice:IOâ, autoupdate=âtrueâ }
Group gSwitches { autoupdate=âtrueâ }
Switch Switch_1 âOut 1 [%s]â (gSwitches) { autoupdate=âtrueâ }
Please donât change more than said⊠Do not change to autoupdate="true"
for any item other than DeviceIO
. And please be aware that this change is only temporary to test if the rule works as expected. This should result in a state change of DeviceIO whenever a Switch is switched.
Please check if the connection to your device is working at all. Maybe the topics are not correct? Did you change the host IP for the mqtt broker to the correct one?
I am not a very strong bitwise man, but I might suggest use of java.util.BitSet
which could be lighter than BigInteger
which is intended for higher precision math operations.
After digging in here: https://stackoverflow.com/questions/18247126/how-to-flip-a-bit-at-a-specific-position-in-an-integer-in-any-language#18247143 it seems to be possible to toggle a single bit using bitwise operators:
n ^ (1 << k)
In above n
is a byte representing a 8 channel relay status. The k
is channel index counted from 0 and right hand side.
Below Java code can be embedded in rules, and will give you everything with language operators:
class Main {
public static void main(String[] args) {
byte input = 0b10010;
int state = (int) input;
print("Input", state);
int k = 0;
state = state ^ (1 << k);
print("toggle last bit", state);
state = state ^ (1 << k);
print("toggle last again", state);
k = 1;
state = state & ~(1 << k);
print("switch off second bit", state);
state = state & ~(1 << k);
print("switch off second bit again", state);
state = state | (1 << k);
print("switch on second bit", state);
}
private static void print(String msg, int val) {
System.out.println(msg + " " + Integer.toBinaryString(val));
}
}
You can test it online: https://repl.it/languages/java. There is well defined naming of most/last significant bits which I intentionally didnât use to do not confuse more than needed.
Cheers,
Ćukasz
Number DeviceIO âIn/Outâ { channel=âmqtt:topic:myBroker:myDevice:IOâ, autoupdate=âtrueâ }
Group gSwitches { autoupdate=âfalseâ }
Switch Switch_1 âOut 1 [%s]â (gSwitches) { autoupdate=âfalseâ }
2020-07-21 07:17:05.437 [ome.event.ItemCommandEvent] - Item âlight_1â received command ON
2020-07-21 07:17:05.443 [ome.event.ItemCommandEvent] - Item âInOutâ received command 1
2020-07-21 07:17:05.452 [vent.ItemStateChangedEvent] - InOut changed from 0 to 1
2020-07-21 07:17:06.947 [ome.event.ItemCommandEvent] - Item âlight_2â received command ON
2020-07-21 07:17:06.952 [ome.event.ItemCommandEvent] - Item âInOutâ received command 3
2020-07-21 07:17:06.964 [vent.ItemStateChangedEvent] - InOut changed from 1 to 3
2020-07-21 07:17:07.805 [ome.event.ItemCommandEvent] - Item âlight_3â received command ON
2020-07-21 07:17:07.811 [ome.event.ItemCommandEvent] - Item âInOutâ received command 7
2020-07-21 07:17:07.819 [vent.ItemStateChangedEvent] - InOut changed from 3 to 7
2020-07-21 07:17:08.798 [ome.event.ItemCommandEvent] - Item âlight_4â received command ON
2020-07-21 07:17:08.807 [ome.event.ItemCommandEvent] - Item âInOutâ received command 15
2020-07-21 07:17:08.816 [vent.ItemStateChangedEvent] - InOut changed from 7 to 15
2020-07-21 07:17:13.998 [ome.event.ItemCommandEvent] - Item âlight_1â received command OFF
2020-07-21 07:17:14.001 [ome.event.ItemCommandEvent] - Item âInOutâ received command 14
2020-07-21 07:17:14.008 [vent.ItemStateChangedEvent] - InOut changed from 15 to 14
2020-07-21 07:17:14.819 [ome.event.ItemCommandEvent] - Item âlight_2â received command OFF
2020-07-21 07:17:14.822 [ome.event.ItemCommandEvent] - Item âInOutâ received command 12
2020-07-21 07:17:14.831 [vent.ItemStateChangedEvent] - InOut changed from 14 to 12
2020-07-21 07:17:15.593 [ome.event.ItemCommandEvent] - Item âlight_3â received command OFF
2020-07-21 07:17:15.596 [ome.event.ItemCommandEvent] - Item âInOutâ received command 8
2020-07-21 07:17:15.610 [vent.ItemStateChangedEvent] - InOut changed from 12 to 8
2020-07-21 07:17:16.382 [ome.event.ItemCommandEvent] - Item âlight_4â received command OFF
2020-07-21 07:17:16.386 [ome.event.ItemCommandEvent] - Item âInOutâ received command 0
2020-07-21 07:17:16.395 [vent.ItemStateChangedEvent] - InOut changed from 8 to 0