URL assembly in oh3 rules

Hi I try to make the following rule in OH3:
the link is a actual weather for a online weather service:
but:

  1. I can not get the value from the Item with the first line
  2. How I can check the assembled link in OH3 before sending?

//var currentValue = itemRegistry.getItem(‘Idojarasallomas_weewxoutTemp_C’).getState();

//var valami = itemRegistry.getItem(‘Idojarasallomas_weewxoutTemp_C’).getState();
var values = [];
values.push(“user=…”);
values.push(“pass=…”);

var now = new Date();
values.push(“ev=” + now.getFullYear());
values.push(“ho=” + (now.getMonth() + 1));
values.push(“nap=” + now.getDate());
values.push(“ora=” + now.getHours());
values.push(“perc=” + now.getMinutes());
values.push(“mp=” + now.getSeconds());
values.push(“hom=16”); // C
values.push(“rh=45”); // %
values.push(“tipus=WS23XX”);

var valstr = values.join(“&”);
var server_url = “https://pro.idokep.hu/sendws.php”;
var url = server_url + “?” + valstr;
var response = actions.HTTP.sendHttpGetRequest(url);

//print(currentValue);
// Example GET Request
//var response = actions.HTTP.sendHttpGetRequest(‘https://pro.idokep.hu/sendws.php?user=...&pass=...&ev=2023&ho=03&nap=18&ora=16&perc=19&mp=00&hom=13&rh=42&tipus=WS23XX’);

This looks like Nashorn JavaScript, right? Also, pleas use code fences.

```
code goes here
```

So what happens on that line? Error in the log? What is the value of the Item? What is the type of the Item?

Log it.

If you are just getting started with OH 3, I strongly recommend installing the JS Scripting add-on which will provide ECMAScript 2022 which is almost a decade more recent than Nashorn. It also has an extensive helper library that comes with it that makes interacting with OH much easier.

Hi

  1. Thank you, i a newbee
  2. I use application/javascript;version=ECMAScript-2021 What I found in the add ons
var valami = itemRegistry.getItem('Idojarasallomas_weewxoutTemp_C').getState();

In this line I would like to read out the current outside temperature from the Weather unit but I can not, I gess the syntax is not good. (I copy it from blockly)

I can only log through the console on the server ar it is possible through the home.openhab.org?

I’m not sure where you are looking then because for ECMAScript 2021 the docs are: JavaScript Scripting - Automation | openHAB

The itemRegistry isn’t mentioned anywhere because the helper library goes to great lengths to make sure you never have to struggle with weirdness in dealing with Java Objects and Classes (itemRegistry is a Java Class which returns a Java Item Object) in a JavaScript context. When you use the library as documented there you will pretty much always have a JavaScript Object.

In OH 3, Blockly converts to the older Nashorn JavaScript and it uses the raw interface to openHAB. No helpers. So you cannot look at the code generated by Blockly and use it in ECMAScript 2021 (technically you could but it takes a little bit of work) and you probably wouldn’t want to anyway. In OH 4 Blockly uses the newer JS so it is easier to look at the code it produces to learn.

So my recommendation is to either:

  • continue to use Blockly, in OH 4 it will be converted to the newer ECMAScript for you
  • use Nashorn JS for this rule so you can use the code generated by Blockly to learn from
  • abandon using Blockly to generate examples to learn from and review the add-on docs for how to do things.

Given the docs for the newer JS, that line should be:

var valami = items.Idojarasallomas_weewxoutTemp_C.state

Note that .state returns the state of the Item as a string. If you need to do math with it use .numericState and if you need to use units use .quantityState.

Always watch openhab.log when coding. You can see them in the console. If you’ve installed openHABian Frontail is running on port 9000 which can be accessed in the browser and follows openhab.log and events.log (in MainUI it will be an option under “other uis”, the little icon in the upper right corner. But I don’t think that it can be accessed through the openHAB Cloud service, only on the LAN.

Hi Thank a lot

I Used a following format

var valami = items.getItem("Idojarasallomas_weewxoutTemp_C").state; 

it worked

Locally I was able to use the Console:
openhab-cli console on Linux
with habopen password!!!

The log:tail - I was able to see the event

Thabks

Note that when using at least openhab-js 4.0 (which ships with the 3.4.2 add-on) these are all equivalent:

items.getItem("MyItem")
items.MyItem
items["MyItem"]

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.