hi, can u plz help me guys with this… my items and rule:
My items:
Switch Alarm_switch "Alarm"
Contact LivingRoom_Window "Window [%s]"
String ALARM2_SENSOR "Sensor"
String ALARM2_SENSOR_STATUS "Sensor status"
my rules:
rule "ALARM sensors"
when
Item Alarm_switch changed
then
if (ALARM2_SENSOR.state.toString == ALARM2_SENSOR_STATUS.state.toString)
{
logInfo("TEST", ALARM2_SENSOR.state.toString)
logInfo("TEST", ALARM2_SENSOR_STATUS.state.toString)
sendCommand(LivingRoom_AirCon , ON)
}
end
ALARM2_SENSOR has a selection of [LivingRoom_Window=“Window sensor”]
ALARM2_SENSOR_STATUS has a selection of [OPEN=“Open”,CLOSED=“Closed”]
There is no error in the log but nothing is working… the loginfo returns below:
2018-03-26 23:25:45.518 [INFO ] [.eclipse.smarthome.model.script.TEST] - LivingRoom_Window
2018-03-26 23:25:45.519 [INFO ] [.eclipse.smarthome.model.script.TEST] - CLOSED
if i use:
if LivingRoom_Window == CLOSED
it works…so,what could be wrong with below:
if (ALARM2_SENSOR.state.toString == ALARM2_SENSOR_STATUS.state.toString)
Thanks
hr_2
(hr2)
March 27, 2018, 7:57am
2
Try this und post the log
then
logInfo("TEST1", ALARM2_SENSOR.state.toString)
logInfo("TEST2", ALARM2_SENSOR_STATUS.state.toString)
if (ALARM2_SENSOR.state.toString == ALARM2_SENSOR_STATUS.state.toString)
{
sendCommand(LivingRoom_AirCon , ON)
}
end
job
(Joachim Boeddeker)
March 27, 2018, 7:58am
3
You are basically comparing to strings:
if ("LivingRoom_Window" == "Closed")
Which never evaluates to true.
hi, so whats will be the correct one?
2018-03-27 12:01:14.718 [INFO ] [eclipse.smarthome.model.script.TEST1] - LivingRoom_Window
2018-03-27 12:01:14.719 [INFO ] [eclipse.smarthome.model.script.TEST2] - CLOSED
hr_2
(hr2)
March 27, 2018, 8:03am
6
You see, they are not equal
i did not understand ur point…
if i use:
if LivingRoom_Window == CLOSED, it works…the values retured in the log is the same…
if (ALARM2_SENSOR.state.toString == ALARM2_SENSOR_STATUS.state.toString)
vzorglub
(Vincent Regaud)
March 27, 2018, 8:11am
8
ALARM2_SENSOR.state.toString is a string containing your item name. It will not work like that.
What is your purpose?
Why do you need to have an item containing the name of another item? It makes things really complicated.
What are you trying to achieve?
Can you explain your idea and maybe we can come up with a solution. It probably has been done before.
job
(Joachim Boeddeker)
March 27, 2018, 8:31am
9
You need to get the item from your string value.
To achieve this, you need to put the sensor items in a group (gSensors) and filter the group by name. It’s close to the associated items pattern.
Please see Design Pattern: What is a Design Pattern and How Do I Use Them for how to read and use DPs.
Problem Statement
Often one will have a number of separate Items which are all related to each other in some way. For example, one might have a Contact on a door and a DateTime to represent when the last time the door was opened. This is easy enough to keep track of if there are only one or two such Items, but if one has a lot of similar Items or is using a lot of generically coded rules where…
...
val mysensor = gSensors.members.filter[ i | i.name == ALARM2_SENSOR.state.toString ].head
if (mysensor.state.toString == ALARM2_SENSOR_STATUS.state.toString)
...
But i second @vzorglub , this complicates everything. What are you really trying to achieve?
thanks for ur reply, what i want to achieve is that i want to write one rule to execute multiple tasks based on the items states from habpanel, and i dont want to keep repeating the rule for each item, im using variable item and state based on the habpanel selection… see below screenshot:
i appreciate if can help me to solve it using string state in if condition…
hr_2
(hr2)
March 27, 2018, 8:32am
11
What you mean is
if (ALARM2_SENSOR.state == "LivingRoom_Window"
&& ALARM2_SENSOR_STATUS.state == "CLOSED")
mmm… i got ur point, its comparing it as strings not as an item name and its state…
vzorglub
(Vincent Regaud)
March 27, 2018, 8:35am
13
Hi Mo,
You need to put your items in groups and follow this design pattern
Forget about putting the item name in another item, you won’t get anywhere.
Edit: Updates for OH 4, inclusion of JS Scripting and Blockly examples, removed the doors example as it’s superfluous.
Please see Design Pattern: What is a Design Pattern and How Do I Use Them for how to read and use DPs.
Problem Statement
One powerful way to consolidate code in rules is to use array/list manipulation operations on the members of a Group. For example, filter down to just those members that are ON, get a list of the labels of those Items that are NULL, etc.
Concept
[image]
T…
i tried yesterday below statement and it worked, u helped me yesterday with the same:
sendCommand(ALARM2_DEVICE.state.toString , ALARM2_COMMAND.state.toString)
im wondering why this one is not working:
if (ALARM2_SENSOR.state.toString == ALARM2_SENSOR_STATUS.state.toString)
vzorglub
(Vincent Regaud)
March 27, 2018, 8:39am
15
Because:
sendCommand
accepts 2 strings as arguments
so if condition doesn’t accept strings… so only way is Design Pattern
what abt:
if (ALARM2_SENSOR.state == ALARM2_SENSOR_STATUS.state)
vzorglub
(Vincent Regaud)
March 27, 2018, 8:50am
18
That’s the same, It will not work!
vzorglub
(Vincent Regaud)
March 27, 2018, 8:55am
19
Read the design pattern:
Edit: Updates for OH 4, inclusion of JS Scripting and Blockly examples, removed the doors example as it’s superfluous.
Please see Design Pattern: What is a Design Pattern and How Do I Use Them for how to read and use DPs.
Problem Statement
One powerful way to consolidate code in rules is to use array/list manipulation operations on the members of a Group. For example, filter down to just those members that are ON, get a list of the labels of those Items that are NULL, etc.
Concept
[image]
T…
It is quite clever stuff
Don’t copy and paste the code.
Write it, yourself line by line, best way to learn
Try to understand the code, bit by bit, what it does and why
That’s how I started back in 80’s copying code from book
Making mistakes and learning that way
Maybe read a dummy guide of programming and understand the differences in types, variables, core functions and statements
Good luck!
2 Likes