Script --> call a https-url

Hi!

I’m using OpenHAB since a year for simple basic cases and I’m an absolute amateur. I use some bindings, made some rules and made some scripts and everything works so far.

Now I would like to make a very short script that simply calls a specific url and used some hours in searching and trying, but I didn’t succeed in this simple problem. I tried e.g.:

I also installed the “HTTP Binding”, but don’t know if it is necessary. Purpose is to hit a Pushcut-url to get a notification on my mobilephone, when something is happening.

Has anybody hints for me. Thank you in advance!
Hannes

  • Platform information:
    • Hardware: Intel(R) Core™ i5-9400 CPU @ 2.90GHz; 16GB RAM
    • OS: Windows 10 Pro
    • Java Runtime Environment: Version 8 Update 271
    • openHAB version: 3.3.0.M2

Welcome to the forums.

What you’re trying to do is actually at least a next step up from amateur. Taking a next step is great, there’s nothing wrong with that, but it is an indication that there’s probably a little more learning involved in this than you may have expected.

You’re going to need to give us more specific information before anyone can give you concrete answers.

  • What script language are you using?
  • Have you search the forums here for previous questions about Pushcut?
  • What happens when you do use the options you tried (that is, how do you know the call didn’t work)?
  • Do errors appear in the logs when you run the scripts?
    • If so, what are the exact errors?
  • Is there Pushcut documentation?
    • What other information is the Pushcut-url expecting you to send?
    • Are there examples of a correct url call?

I don’t know Pushcut in particular, but in general, a call to a url for something like this is more complex than just the address. You’ll need to know things like what headers to use and the expected format of the data transferred. Is there Pushcut documentation?

Hi Justin!

Thank you for your answer, I try to give you the missing informations.

There ist a specific url to trigger my particular Pushcut notification. When I use this url in my browser, a second after entering, the notification on my mobile phone appears. So the link works properly. There are no arguments needed, the link has just to be opened.

Script language:
in the attachment there is a screenshot from my OpenHAB and if I enter the “Script”-section there is written “application/javascript” on the top.

I searched this forum for Pushcut-url-infos and the whole internet … :wink:

Logs: Unfortunately I don’t know, where to find logfiles.

Pushcut documentation: on the website of Pushcut I didn’t find anything, that could help me.

From Pushcut I got the following informations to my personal link:


To trigger notification Flood Küche send a POST to
https://api.pushcut.io/AeMYRXXXXX/notifications/Flood%20Küche

You can use the following curl command:
curl -X POST https://api.pushcut.io/AeMYRXXXXX/notifications/Flood%20Küche


Hope this is enough to approach the solution. Thank you!
Hannes

Ok. that’s helpful. The scripting language you’re using is the older version of javascript that comes installed by default in OH3 versions. In order to use any of the http actions in this language, you have to specifically load them in. So, your script would have two lines:

var HTTP = Java.type("org.openhab.core.model.script.actions.HTTP");
HTTP.sendHttpPostRequest("https://api.pushcut.io/AeMYRXXXXX/notifications/Flood%20Küche");

Note: This version of javascript is VERY old (and will no longer even be installed by default in OH4). There is a better replacement available but you have to install the binding. If you are just starting out with javascript rules it is probably better for you so skip the old version and start right in on the new one. You can go to SettingsAutomation and look for the Javascript Scripting binding. If you install this it comes with an extensive helper library that makes a lot of the OH specific functions (such as actions) easier to use. Then when you make a rule or script you would select the option that says"application/javascript;version=ECMAScript-2021" to use the newer JSscripting. That way you would only need:

actions.HTTP.sendHttpPostRequest("https://api.pushcut.io/AeMYRXXXXX/notifications/Flood%20Küche");

You definitely should update your Java version before trying new things.
openHAB 3.x.x requires Java 11 for proper functionality.

1 Like

Hi Justin!

Obviously it was easy for you to tell me the correct code! Great, it works! I’m very happy and greatful! Thank you! :slight_smile:

I found the Javascript Scripting binding in OH and could install it easily. Is there any risk, that my previous scripts stop to run? Otherwise I will do that.

Thank you again!
Hannes

There was, if I recall, a conflict between the two javascript versions very early on in OH3 release cycle, but I’m pretty sure by the version your running that was resolved.

If you install the JSscripting add-on any rules you created using the old (Nashorn) version will still use that version and will still run. If you want to upgrade those rules to the new JSscripting version then you will have to create new scripts and make some modifications to the rule code itself (there are a few differences between the old and new javascript integrations that mean some of the ways the codes interact with OH are a little different).

Any new rules you create, you will be giving the option to select the older or newer javascript versions.

I just installed the newer version JSscripting and everything seems to be fine.
Thank you for all your tips and tricks!

Greetings,
Hannes