I’m getting the following errors when I try to enable/disable a thing in a rule according to a change switch event:
Script execution of rule with UID ‘Tibberswitch-1’ failed: The name ‘osgi’ cannot be resolved to an item or type; line 11, column 20, length 4 in Tibberswitch
2024-12-17 14:44:37.635 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘Testrule2-1’ failed: The name ‘things’ cannot be resolved to an item or type; line 10, column 19, length 6 in Testrule2
Any idea what is wrong with my rule ?
import org.openhab.core
import org.osgi.framework
import org.openhab.core.thing
import org.openhab.core.automation.thingsupport
rule "testrule2"
when
Item Tibberswitch changed
then
var mything = things.getThing("netatmo:indoor:637d301eac:70ee50368ff8:0300000cb70a");
if(Tibberswitch.state == ON) {
mything.setEnabled(true)
} else {
mything.setEnabled(false)
}
end
This rule look like RulesDSL.
and with RulesDSL this is not possible in this way. You have to do a sendHttpPutRequest to the Rest API to enable/disable a thing per RulesDSL.
like:
To elaborate just a little, on @Baschtlwaschtl’s correct answer, you might need to deal with authentication. By default I don’t think that Basic Auth is enabled. You’ll need to enable that to use the user:password authentication as shown. Note that the user needs to be an OH user with the admin role to access Things.
This line comes from JS Scripting. This rule, using the rule builder in a file would look something like the following in JS Scripting. Note that the add-on needs to be installed and the file needs to be named with the .js extension and placed in the $OH_CONF/automation/js folder.
rules.when().item("Tibberswitch").changed().then(event => {
const mything = things.getThing("netatmo:indoor:637d301eac:70ee50368ff8:0300000cb70a");
mything.setEnabled(event.newState == "ON");
}).build("testrule2", "My Test Rule 2");