You cannot catch this because it is not thrown to the scriptās layer ⦠the exception is catched inside the HTTP actions and then logged, you will receive null as return value if anything went wrong.
Assuming this isnāt just a toy problem for demonstration purposes, the HTTP binding would probably be a more flexible and more straight forward way to implement a periodic poll of a web page.
What are you hoping to do by catching the exception? Maybe this is really na XY Problem?
grep and cut are not going to be available in your rule either so using a rule here over the binding doesnāt really give you anything. You could use executeCommandLine I suppose but as best as I can tell without seeing the actual page returned you should be able to easily use the HTTP binding and REGEX transform to achieve this. The XPath transform might also be useful here.
I canāt give you the REGEX without seeing the page returned but if would be something like .*webdata_total_e "(.*)".*.
Once you have that, if the HTTP GET request failed, the HTTP binding will set the Thing status from ONLINE to something else (Iām seeing UNKNOWN in my experiments but I know Iāve seen OFFLINE in the past too).
There is also a Last Success and Last Failure pair of Channels which get updated with a timestamp of the last success or failure.
You could trigger a rule with the Thing status change or a change to the Last Failure to do whatever you need to do in cases where the web server doesnāt respond.
Unfortunately, what I expected to happen didnāt. I expected/hoped that the linked Items would have been set to NULL or UNDEF when the page didnāt respond so the rule would still probably be required. But at least you wonāt be doubling the network traffic and processing to detect when the server goes offline as you would with two rules polling the same URL, one to get the data and another to report when it goes offline. It also means that you donāt really have to do anything special like exception handling to detect when the device goes offline.
Youād have a rule that triggers when the Thing status changes from ONLINE or when the Item linked to the Last Failure Channel changes and update/command your Item to 0 in that rule. When it is online, the Thing will parse out and update any Items necessary, no rule is involved.
You donāt even need a Script Action here, you can do it with a simple UI rule since all it needs to do it trigger and update that one Item to 0.
As written, your rule queries the HTTP server once per minute whether itās online or not independently from and in addition to any polling you do to with your curl to get the data. With the HTTP binding youād just have the one polling period which handles both detecting offline and extracting the data.
Are you saying that you plan on bringing the logic from your curl/grep/cut into the rule? In that case then yes, youād not have additional polling of the server.
Note that NULL is not the same thing as null. The sendHttpGetAction returns null on error, not NULL.
Can you post an example of the data returned by the server? If itās really html, it might be simpler to use XPath.
As written you could also simplify the processing some using the REGEX transform to find and extract the data all in one go.
var energyTotal = actions.Transformation.transform('REGEX', '.*var webdata_total_e = \"([0-9]+[\.][0-9]+).*', response);
When using the transform the expression needs to match the whole document (hence the .* on the front and back). But then you can add to the pattern to find the spot you want to extract (the var wendata_total_e = \" part) and the first matching pattern is what the transform returns (the ([0-9]+[\.][0-9]+) part). Note I escape the . because otherwise REGEX treats . as āany characterā instead of a literal . character.
If you were to move this to the HTTP binding using these REGEX expressions the Thing would look something like this (I assumed not using UoM):
UID: http:url:testHTTPThing
label: Test HTTP Thing
thingTypeUID: http:url
configuration:
authMode: BASIC
ignoreSSLErrors: false
baseURL: http://192.168.x.x/status.html
delay: 0
stateMethod: GET
refresh: 60
commandMethod: GET
timeout: 3000
bufferSize: 2048
channels:
- id: last-failure
channelTypeUID: http:request-date-time
label: Last Failure
configuration: {}
- id: last-success
channelTypeUID: http:request-date-time
label: Last Success
configuration: {}
- id: energyTotal
channelTypeUID: http:number
label: Energy Total
description: ""
configuration:
mode: READONLY
stateTransformation: REGEX:.*var webdata_total_e = \"([0-9]+[\.][0-9]+).*
- id: currentPower
channelTypeUID: http:number
label: Current Power
description: ""
configuration:
mode: READONLY
stateTransformation: REGEX:.*var webdata_now_p = \"([0-9]).*
Your rule would become:
rules.JSRule({
name: 'reset Deye when offline',
description: 'Triggered when the Thing goes offline',
triggers: [triggers.ThingStatusChangeTrigger('some:thing:uuid','ONLINE')],
execute: (event) => {
console.info('Device is offline, resetting deye_actualpower');
items.deye_actualpower.postUpdate(0);
},
tags: ['deye', 'reset Deye values'],
id: 'reset Deye values'
});
OK, looking at the webpage the format is indeed XHTML which means Xpath could work theoretically. The bad news is the data is actually embedded in some JS which renders Xpath not that useful here. We have to use the REGEX transformation after all.
Ultimately, its stuff like this which makes me no longer user nor support .things files (or other text file based OH configs). Somewhere you have a syntax error. Since Iām not a computer, I canāt say what the error is. But the ā14,73ā is likely the line number and column number in the file.
But I have confirmed that the REGEX expressions I created above do work on that webpage.
Type number : energyTotal "Energy Total" [ stateTransformation="REGEX:.*var webdata_total_e = \\"([0-9]+[\\.][0-9]+).*" ]
now I get
Configuration model 'deye.things' has errors, therefore ignoring it: [14,107]: mismatched input '(' expecting ']'
[14,109]: missing '}' at '0-9'
[14,120]: no viable alternative at input '0-9'
[14,128]: mismatched character '<EOF>' expecting '"'
syntax errors like these do no occur when using managed Things
one does not need to mess with double escaping and stuff like that with managed Things
Like I said, I donāt do .things files any more so all I can really say is there is a syntax error. What is the error? I donāt have recent experience with .things files/syntax. Iām not a computer so canāt parse the file myself. I prefer to spend my time solving home automation problems instead of syntax errors so Iām not inclined to spend the time to relearn the .things file syntax and mentally parse this .things file to find the error.
The best I can offer is if you stick with .things files is to pay attention to the errors and keep adjusting until you get it right. I can be of little further help with that part.
Hmmmm, does this web page always return the same thing? If the REGEX doesnāt match anything it returns the empty string and obviously an empty string canāt be converted to a number.
If this is indeed the problem, you can add a REGEX filter in front of the main one to only process if the search string is in the page. Iām not 100% sure thatās the problem nor am I certain that the REGEX filter trick works with REGEX. But itās worth a shot.
If this is the cause of this log statement, itās being logged at the WARN level so itās not an error.