eric1905
(Eric)
February 3, 2022, 7:22am
1
Hello everyone,
I am running OH 3.2.0 and I have defined a rule like this:
try {
var HttpUtil = Java.type("org.openhab.core.io.net.http.HttpUtil")
var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
var testProperty = Java.type("java.util.Properties"); //alternate way of defining header
var headerTest = new testProperty();
headerTest.put("Authorization","Basic xxxxx");
var response = HttpUtil.executeUrl("GET", "xxxx", headerTest,null,null, 5000);
var einnahmen = JSON.parse(response)[0].total_sales
//logger.info(einnahmen)
events.postUpdate("einnahmen", einnahmen)
} catch (Exception) {
logger.error("---------------------------------------------------------------- Exception invoking method", e);
}
but I get the error that e is not defined. If i add e after Exception, then I get the error:
Expected ) but found e
How is it possible to catch an exception?
eric1905
(Eric)
February 3, 2022, 7:54am
2
When I remove the whole try catch block I get the following exception:
Fail to execute action: 2
What does this mean? Maybe it would be better to solve this first
Matze0211
(Matthias)
February 3, 2022, 7:59am
3
Regarding your first issue:
You have
catch(exception)
but within the catch block you use e instead of exception
try with catch(e)
and you should get rid of the first issue.
Regarding you second issue:
Maybe post the full rule and error message to get a better understanding of the context
1 Like
eric1905
(Eric)
February 4, 2022, 7:55am
4
I changed it to e in the catch block, thank you:
My whole rule looks like this:
configuration: {}
triggers:
- id: "3"
configuration:
cronExpression: 0 * * * * ? *
type: timer.GenericCronTrigger
conditions: []
actions:
- inputs: {}
id: "2"
configuration:
type: application/javascript
script: >-2
try{
var HttpUtil = Java.type("org.openhab.core.io.net.http.HttpUtil")
var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
var testProperty = Java.type("java.util.Properties"); //alternate way of defining header
var headerTest = new testProperty();
headerTest.put("Authorization","Basic xxxxx");
var response = HttpUtil.executeUrl("GET", "xxxxx", headerTest,null,null, 5000);
var einnahmen = JSON.parse(response)[0].total_sales
//logger.info(einnahmen)
events.postUpdate("woocommerceEinnahmenHeute", einnahmen)
} catch (e) {
logger.error("---------------------------------------------------------------- Exception invoking method", e);
}
type: script.ScriptAction
Matze0211
(Matthias)
February 4, 2022, 8:08am
5
If you “only” want to make a http get, parse the json really and store it into an item you don’t need any rule.
Use the http binding to create a thing, what will represent your http get request.
Create a channel and use the json transformation to get the data you need and simple link an item to your channel