Start creating rules for OH3 in VSC

Hi All, I’m new to scripts and rules and not rely sure how to start !
I want to use Microsoft Visual Studio (VSC) with the OH extension to create the code because of all the features of VSC. So I want to start with a simple “problem”. I want to create a item to sum every hour all the energy used by all infrared panels and display this total in a graph.

To explain it in more “structured way” :blush: I put something on paper.

How can I program this with VSC? or where can I find similar examples (create a item that sums on the values and reset the sum at a moment in time)?

…IR_Total_Power as value
…IRKamerNode022_ElectricmeterkWh1 as value
…IRKamerNode022_ElectricmeterkWh2 as value
…IRKantoor_kWh2_Sensor_003 as value
…IRSerre011wallplug_ElectricmeterkWh as value

—Every hour
------Then // sum IR panel energy
------IR_Total_Power = IRKamerNode022_ElectricmeterkWh1 +
----------IRKamerNode022_ElectricmeterkWh2 +
----------IRKantoor_kWh2_Sensor_003 +
—Else if
------Time = 00:00:10 hour // reset all meter readings 10 seconds after midnight
--------IR_Total_Power = “0”
--------IRKamerNode022_ElectricmeterkWh1 = “0”
--------IRKamerNode022_ElectricmeterkWh2 = “0”
--------IRKantoor_kWh2_Sensor_003 = “0”
--------IRSerre011wallplug_ElectricmeterkWh = “0”
----End if
–End if

You clearly have some experience with programmatic thinking, so it’s just going to be the syntax that you need to get comfortable with. The two best places to get started are the docs and the examples on this forum.

Go through the docs carefully. Try to understand how the rule structure and event driven triggers work (hint: what you want will best be represented by two different rules, see if after reading the Triggers section, you can see why).

See if you can get something like one of the very simple rules in the examples at the bottom of that page working first. If you can then you can work on expanding that to give you the features you need.

When you get stuck search the forum for the specific keywords on the syntax that doesn’t seem to be working and if you can’t find any examples, then post your whole code in a question here and someone will walk you through it.

The DSL language (what’s documented on that docs page) is based on Xtend, but don’t try to learn it by starting there just work form the examples all around for starters.

1 Like

The difficult part for those with some programming experience may be getting to grips with openHABs event driven nature. (do stuff in response to some event)

For this particular task, you’ll likely want to exploit the persistence services as well. (data recording)

e.g. for your hourly sum -
Persist your meter Items.
Persist your hourly sum Item for easy charting.
Each hour, run a rule that -
retrieves the meter reading(s) from an hour ago
calculates the difference to now
updates the hourly sum Item

It’s a bit more in at the deep end than the usual “hello world” of automating a light.

I’ve moved this to a more appropriate category. The Tutorial and Solutions category is for posting tutorials and solutions, not to request a tutorial or solution.

When posting code, even pseudo code, or logs or anything else “computer” please use code fences. There are icons in the editor or

code goes here

Items are created and managed separately from rules. If you want to use VSCode to create Items, you need to use .items files which are documented at Items | openHAB.

This is typically a job for persistence, as rossko57 suggested. You may not even need a rule though to implement your pseudo code you will.

The best VSCode experience will be using Rules DSL. Unfortunately that means that means using the rules language that is the most frustrating for people who already know how to code because it lacks things like functions, classes, arrays, import libraries, ability to create data structures, etc.

However, VSCode lacks integration with OH when using the other languages (Jython, Nashorn JavaScript, GraalVM JavaScript, jRuby, etc.). And writing rules in these languages without use of a helper library is pretty miserable so if you do change languages you’ll need to select and use the appropriate helper library.

As mentioned, OH rules are event driven. Something happens and a rule may trigger in response. You have two events (every 10 minutes, and 10 seconds after midnight) and want to do something different in response to the two events. So that points to using two separate rules.

Note, once you have the persistence configured, the charting basically comes for fee.

Thanks all, for the help :ok_hand: :+1: it gives me some direction :point_right: :muscle:

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