My first steps with OpenHab

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:

load(Java.type("java.lang.System").getenv("OPENHAB_CONF")+'/automation/jsr223/javascript/personal/WeekdaysAndHolidays.js');

One thing I’ve noticed is that changes are only applied on restart.

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.

Awair binding now fully working. I need to get the 3.0 version compiling and work out how to get it included in future builds.

I spoke a bit too soon. I can’t get the item units to appear in PaperUI.

I’ve migrated quite a few rules from the Zipabox.

Trying to work out how to access the last position that Somfy RTS blinds and awnings were set to.

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:

https://<myopenhab email>:<myopenhab password>@myopenhab.org:443/rest/items/<item name>/state

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 :man_facepalming:. 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.

Next stop: persistence.

1 Like

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.

you dont need to have rule for it, it can be easily done by profile, for example I’m using this approach

Number:Temperature      Water_Temperature               "Water [%.1f°]"                 <temperature>   (gStore10, gGarden, gWeather, Graph_Water, Graph_Temperatures, Graph_InOutWater, gMinMaxDaily, gMinMaxAlltime, gMinMaxLastYear, Graph_Weather) ["Measurement","Temperature"] { channel = "mqtt:topic:garden_control:water" }
DateTime                Water_Temperature_LastUpdate    "Last change [%1$tR]"           <time>          { channel="mqtt:topic:garden_control:water"[profile="timestamp-update"] }

How does that work? I’m not familiar with profiles. How are the two items linked? (Also: is this an OH3 feature?)

Does this work when a value is updated but not changed?

whenever main item receives and update, this item is updated on timestamp as well.
They are linked by the channel, in this example via MQTT

mainitem  -> { channel="mqtt:topic:garden_control:water" }
timestamp -> { channel="mqtt:topic:garden_control:water"[profile="timestamp-update"] }

OK got it, they’re linked by the channel. Any update to the channel causes the time stamp to update.

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.

not sure if linking items is doable (as it is not my usecase) but from logical point of view - why should not :wink:

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.

Yeah, I don’t think the update triggers work in 2.5

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?

Maybe I never needed to expose the items if I use the REST API. In that case, the problem I have must lie elsewhere.