I decided to press ahead with creating the building blocks of my rules. I said I was going to go down the Jython route, but the flesh is weak, so I’m going to use Javascript.
I read here that you couldn’t put non-rule utility function .js files in the rules folder, but I found the opposite to be the case. If I put them in another parallel folder, they are found by the rules engine, but attempts to call the functions seem to fail. If I put them in the “personal” folder, they work. I include them in the same way as the rules.js file:
Lots of progress today. First, I realised I needed to add the Awair bridge manually (duh). As soon as I did that, I was able to (try) to add a device and saw entries in the log. The device didn’t add because of a stupid typo in the binding. I’ll try the corrected version now.
I also saw a post that cleared up the confusion about the WeatherUnderground Personal Weather Station binding. If you have a PWS, the WeatherUnderground binding does not work, you need the Weather Forecast Company one.
A bit more progress. The Awair binding shows the right things and items, and it connects to the account, but discovery doesn’t work, and something goes wrong with reading the values when a device is added manually.
Added an items file to create groups and items for last update time stamps for various sensors and stuff, as described here:
The groups and items were no problem (I hadn’t noticed that items have a “parent groups” configuration". The tricky bit was working out how to implement the rule in Javascript. After a lot of fiddling I came up with a helper function to extract the name of the item updated from the “input” parameter:
// Adapted from https://gitlab.com/RNTs_3/openhab-jsr223-javascript-helper/tree/master
function GetTriggerItemForEvent(input)
{
var ev = input.get("event")+"";
// Splits into: 'gRecordLastUpdate,changed,from,7.96612745098039237,to,7.95362745098039237,through,BalconyAeotec_SensorTemperature'
var evArr = ev.split("'").join("").split("Item ").join("").split(" ");
return evArr[7];
}
It seems that evArr[0] contains the group name, not the item name, when you’re capturing changes to all members of a group.
The rule looks like this, in case it helps somebody. I stuck three different triggers in there because I wasn’t sure which I needed. The last bit of the puzzle was working out that the date sent to sendCommand needed to be in ISO format (.toISOString).
JSRule
(
{
name: "Rule_LastUpdate",
description: "Store last update stamps for all items that require them",
triggers:
[
UpdatedEventTrigger("gRecordLastUpdate"), // Any Member of gRecordLastUpdate received update, even if the value didn't change
ChangedEventTrigger("gRecordLastUpdate"),
ItemStateChangeTrigger("gRecordLastUpdate")
],
execute: function( module, input)
{
// LastUpdate item has same name as item update, plus _LastUpdate
var itemLastUpdate; // Item containing last update date
// Get current date
var dateNow = new Date();
var sTriggerItemName = GetTriggerItemForEvent(input);
// LastUpdate item has same name as item update, plus _LastUpdate
itemLastUpdate = getItem(sTriggerItemName + "_LastUpdate");
sendCommand(itemLastUpdate, dateNow.toISOString());
}
}
);
After learning that I could access all items via the cloud through the REST API, I set up a myopenhab.org account, installed the REST API docs (in PaperUI: Add-ons -> User interfaces -> REST documentation) and looked up the methods for reading and setting an item state (look under https://home.myopenhab.org/doc/index.html).
I stumbled a bit when I decided to test the calls in the Advanced REST client tool. I’d read that the URL should take the form:
with the @ symbol of the email encoded as %40. I did this, but always got “unauthorized” back. After beating my head against the wall for an hour, I realised that there is a specific section for inputting the basic authorisation user name and password . In the app I use on my phone to transmit certain information back to the controller (Llama Automate, sort of like Tasker), the HTTP Request blocks work in exactly the same way. So I’m now updating my first item over the cloud.
I’ve built a large part of my rules but there’s still quite a long way to go. I’m having trouble with exposed items at the moment. Despite apparently exposing them in PaperUI, the values seem hardly ever to arrive, or only very erratically. The UI for selecting items to expose is horrible, so I’m going to look at using the config file instead. At least then it will be clear what’s being exposed.
What about items without things (and therefore without channels)? Can you just make up a channel to associate with the items?
I have items that receive data from external sources via the cloud and associated time stamps with them via a rule. There’s no thing, and currently no channel either.
Hmm. Not sure. I’ll try this with the Aeotec sensors, which don’t have their own timestamp. Currently I use my own rule-based time stamp, but it only seems to get triggered when values change, not when values are updated but remain the same. Perhaps the profile method might solve this problem.
Channels are the structure linking Item to Thing.
Can’t be used to link Item to Item.
Profiles modify link behaviour. As there is no link available, you can’t use them with a pair of Items.
Never mind, linked behaviour can always be accomplished with rules.
Rule triggers are available both for Item state changes and state updates (which may or may not also cause a change).
Choose rule triggers carefully.
There is no reason to “expose” Items any longer. That feature has long since been turned off because far too many people were needlessly exposing all their Items which caused the cloud server to crash periodically.
The only reason to expose Items through the Cloud Connector is for IFTTT integration. And since that feature has been turned off for a couple years now, there is no reason to expose any Items at all.
To make Items available to Alexa or Google Assistant one uses Item metadata, which cannot be set from PaperUI anyway.
Perhaps what you are referring to when saying “exposing items” is something different.
We’ve talked about this before but I don’t think I was clear about what I need to do. What I need is to send data to OH from my phone (amongst other things). How do I do that?