Create an own Overview in the Panel for my family to create own scheduled rules for shutters (but without touching rules in the OH3 Section itself)

  • Platform information:
    • Hardware:RasperryPi / Openhabian
    • OS: Raspbian
    • Java Runtime Environment: Java 11
    • openHAB version: 3

Hi everyone,

i am really new to OH, but I love it already.

I am a web developer, so JS, CSS, HTML, JSON are normally no problem, but I don’t know where to add the code to reach the following goal:

I want to give my family the possibility to change the time where a shutter will go up or down or set a specific time for specific lights to switch on and off automatically…

But I down want to give the kids direct access to openhab3 to create some rules, cause they also can destroy many things then :smiley:

so my question is now

  1. For the User Interface: Do I need an own widget or an own Page or an own Sitemap for this thing? If yes where and how do I have to add my code?

  2. For the dynamically logic: Do I need to fire some API request to the OH Api OR how can I create some time rules like “Kitchen Shutter down every day at 8pm and up every day at 7am” ???

  3. Also for this thing I want to style the elements by myself with my own CSS code, but where can I add my CSS code for stuff like that?

Can anyone tell me how I have to proceed here?

For any hints I would be really happy <3

Thx for your help

cheers

Alex

  1. On OH 3 you can set an input widget on a DateTime Item. You can even use a time picker though I usually just use a text field. See OH 3 Examples: How to boot strap the state of an Item.

  2. Take a look at Design Pattern: Time Of Day. Notice there is a JavaScript version you can download and use. The general approach is when OH restarts or one of the DateTime Items changes the rule runs. Any DateTime that isn’t for today gets moved forward. Then a Timer is created to go off at each of those times. The Timer publishes a “time of day” String to a TimeOfDay Item. You can then trigger rules to run when the TimeOfDay changes to a given value (e.g. when TimeOfDay changes to “MORNING”). The only real limitation is you’ll have to predefine your “times of day” ahead of time. Your family won’t be able to create their own unless you give them access to create new Items and teach them how to set metadata on the Items.

  3. I know that the Pages Widgets support some CSS stuff but you’ll have to dig into the code and go from there for that. MainUI Pages are built using F7 and most of what’s available there is also available in your custom widgets. See the docs for an extensive set of information on Pages and Widgets which should explain it all.

First of all, thx for your help and your fast answer :slight_smile: :slight_smile:

ok i have completed step 1): My dateTime text Field looks now like this:

But I don’t understand your 2. point >> because

i will create the items before my wife or my kids for example can change the shutter times etc.

the only thing I need to find out now is: How can I send within a HTML / Angular Widget the “shutter down” or “shutter up” scheduled times?

i don’t want to define “MORNIG” or “EVENING” before in the system somewhere, I just want to give the user a timePicker and he can save his scheduled times for s specific items (e.g. shutter_kitchen)

So I think I need the opposite of itemValue(‘ITEM_NAME’)

OR

change a rule by firing a command (e.g. after the user clicked something like a “save” button)

What you think about my 2 approaches?

thx

Cheers Alex

EDIT: Maybe something like this???

ng-click="sendCmd(‘Sonos_Ensuite_Preset’, ‘{{i.command}}’)"

If you want to use Angular/HTML you’ll be writing your own UI from scratch for the most part. If you want to write your own UI you’ll want to spend a whole lot of time reading and learning the openHAB REST API. That’s how the UIs interact with OH. You can find the docs under Developer Tools in MainUI.

But sending the command to make the shutter go up or down at the scheduled time won’t come from the UI. It’ll come from a rule. The DateTime changes state (from the time picker) which triggers a rule which in turn cancels the existing timer and creates a new timer. When the timer goes off it issues the command to the shutter’s Item. The only thing done from the UI is setting the time.

If you use the time of day DP, you define what a given DateTime Item means based on the Item’s metadata. So instead of using a time of day string, you can use an Item name and command and a rule to send that command to the Item when the TimeOfDay Item changes. Or you can rewrite the rule to send commands to different Items instead of just the one TimeOfDay Item.

The concept of the Time Of Day rule is that most people have certain times when a bunch of stuff, much of it unrelated, is supposed to happen (or not happen). That code gives one a way to set those time periods for the whole house. If you want to set a separate schedule for each and every Item then you’ll need to essentially recreate the rule to work on a one to one basis. Above is one possibility. Though the number of Items you have is going to explode.

I’m not sure what you mean by this.

You can code the rule to handle different cases but you can’t dynamically change the rule from another rule (at least not easily). You can enable and disable rules based on the states of a given Item. So you could code it up in multiple rules and pick and choose which one to enable.

But there is no “save” button. Each Item change will immediately change after clicking the check and it’s those Item events that will trigger the rules to run.

ok thx for your great answer and long description.

I have already checked the REST API and it works great. But only when I am sending the request from outside of OH to the API.

BUT

How can I fire an API request (e.g the one on the screenshot is marked) after the user has clicked any Button in the Widget?

Do you have any idea here?

itemValue(‘ITEM_NAME’) will read out a current value from an item, but my question was: “How can I set a value?” ← but I think its will not help me, because after your explanation I know now, that I need to create or modify rules after the user set his time via the timePicker.

correct?

You can’t. Unless of course you decide to create your own custom UI in which case you can do what ever you want.

By issuing a command or posting an update. If you are in the UI that’s done through the REST API.

Yes, doing something based on an event (e.g. change to an Item) is the job of Rules. In this case you’d cancel an existing Timer and create a new Timer based on the new DateTime.

GREAT :slight_smile:

But HOW can I fire (out of a widget) an REST Api request like

/rules/{ruleUID}/config

OR

/rules/{ruleUID}

because before I will change an existing rule, I need to know the ruleID for the current rule which is responsible for a specific item…

Thx

Edit: I can try to run an external script, which will handle the “magic”, what YOU think about that?

Edit2: I think its time for a new topic, cause this is an own separat question and topic :wink:

Thx

Presumably using JavaScript to make the right REST API call to update or command an Item, trigger a Rule (something that should be a rare occurance. Look at the Items/Rules/Things sections of the REST API Docs to find the endpoint that does what you want and it will show you what HTTP call you need to make to do it. As for the mechanics of making the call, I guess you would use JavaScript or something. If you’re building your own UI you are outside of openHAB Land so everything you do will be by making REST API calls.

Honestly, I think this is a case of “if your only tool is a hammer all problems look like nails”. Put another way, you are trying really hard to move the solution to the problem from openHAB into the “front end” where you are more confortable.

I think over all you will have a better end result with less work if you instead focus more on how to do it with just the openHAB stuff and learning more about how openHAB itself works. Only then look into building your custom UI.

you are absolute right and I will try to dive deeper into your suggestions and hints :slight_smile:

thx for ur help

Hello again, I have tried to follow your suggestions like this:

  1. I have now an Element in my widget which change an items value:
<button href="#" class="btn btn-primary" ng-click="sendCmd('Kuechenrollozeit_hoch_stunde', '24');">Stunde speichern</button>
  1. then I have a rule like this:
triggers:
  - id: "1"
    configuration:
      itemName: Kuechenrollozeit_hoch_stunde
      state: ""
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "3"
    configuration:
      type: application/javascript
      script: // FIRE A REST API REQUEST TO THE OPENHAB API
    type: script.ScriptAction

i have tried to search here in the forum for something like “REST API Request in ECMA” or “Api request in Javascript” but I can not find anything what helps me out…

So can you help me please :frowning:

The big target is still: Change the shutter scheduled by a Widget in the OpenHab Panel and after your hints I am the same opinion I have to do it without leaving the openHab Land, but I am really frustrated, cause I don’t know how to change the Cron Times, based on a rule trigger.

Can I avoid maybe REST Api requests and just use standard openHab default functions like (items, rules, scheduler) ???

My biggest success for now is and was:

  1. Click a button in the widget which sends the shutter time to the item
  2. on Item Change, a rule will listen and
  3. trigger something else (like a script for example)

works great, but how can I save my new time (which the user has entered in the HabPanel) now in as a new cron entry or similar to let me shutter goes down at 8pm for example?

Can you please give my new food for thought? :smiley:

Thank you so much!

Greetings from Germany to Colorado Springs :slight_smile:

Hi @nbg12,

what goal do you want to achieve in changing the rule?

Isn’ it enough to change the items which are the base for your rule? E.g. your items which define the times?
Then you could do this easily with the stuff you already have. I do not understand why and how you want to change the “rule”.

Bye
HFM (also from Germany :slight_smile: )

EDIT:

I assume some things and put in my 2 cents:

  • Use a “cron”, maybe every minute, as trigger
  • in your rule you take the current time and compare it with the item which defines your time (which was updated by your wife or kids via widget)
  • if the condition is true, the rule does the action

Of course there are better solutions, for example from cool guys like Rick. :slight_smile: But I think it will work.

Once you are running inside a rule you are running in the server. You don’t have to make a REST API call. You just interact with OH classes directly. The REST API is only needed in your custom UI.

Again, I suggest reading the concepts section off the docs, the getting started tutorial and the rules and actions sections of the docs. Then based on that information try to get something to work using just basic openHAB. Only then worry about a custom UI.

This is an XY Problem in a lot of ways and you are focused on the custom UI part and getting to make OH work the way you want your custom UI to work. But OH isn’t that flexible. You’ll have to adjust your custom UI to OH, and to do that you need to know how to make this work without your custom UI?

So my suggestion is, once again, as and over all approach is too trigger a rule when a date time Item changes. In than rule create a Timer to go off at The New time. When the timer goes off it sends the command to the item that controls the device. There is some additional book keeping to do like moving date times to today and at midnight and cancelling existing timers and such. The Actions section discusses the create timer Action which is used to schedule coffee too run later.

While it’s theoretically possible to change a rule programmatically, that’s not a good approach for this problem. And this is the sort of thing that only very advanced users who are expert in a OH usually use to solve a prolblem that can’t be solved any other way.

All of this is implemented in three different languages in the Time of Day design pattern I linked to above which can bet used as an example.

Over all you’ll probably want to stick to coding in text files as those will have the most examples right now and you’ll need to store those timers across multiple runs of the rule. Rules DSL had the must docs and examples right now.