Wait before turning off (First Rule) (CLOSED)

Hi everyone I’m using PaperUI & HABPannel

I would like help with creating my first rule

GOAL: Press a button on HABPannel that sends a shutdown command to Kodi running on a Windows machine the rule needs to wait for around two minutes for the machine to shutdown before finally turning off the socket that powers the machine

What I need to know

  1. how do you trigger a rule from inside HABPannel
  2. how do you add a wait command

You change the state of an Item from HABPanel that is used to fire a rule.
Create an item (e.g. a Switch) that is not bound to anything but is used just to keep a state.
Map this Item on your HABPanel (on a Switch Widget) and turn it On/Off from there.
Then in your rule:

var	Timer	Kodi_Timer = null
val	Integer	Kodi_TimeOut = 2  //in minutes since the rule uses "now.plusMinutes"

rule "Shut Down Kodi !"
when
	Item HABP_Switch changed from ON to OFF  //this is your HABPanel Switch
then
	logInfo("Kodi_Down", "Shutting Down Kodi !")
	Win_Host.sendCommand(OFF)  //this will cover the first shutdown step (for the Win machine)
	Kodi_Timer = createTimer(now.plusMinutes(Kodi_TimeOut))
			[|
				Power_Outlet.sendCommand(OFF) //this will cover the second shutdown step (for the power outlet)
				Kodi_Timer = null
			]
end

One option is the createTimer method as displayed in the rule example above.
There is also the “sleep” option but it’s not really recommended for your case (long timeout). (check @rlkoshak’s post here: Link)

Extra hint: You could bind your HABPanel switch to the same config as the Win_Host Item and do step 1 (send the shutdown command to the Win host) when you turn off the HABPanel Switch (and fire the rule at the same time). Then, just create the Timer and after it expires, the rule will send the 2nd command to the 2nd item.

Another Idea to trigger the switch off would be to use the network binding. If the machine isn’t online switch the power off (after a while…). this way you would avoid data loss if the power down of kodi is somehow delayed.

Yes I thought about that option myself I would prefere to protect my data like you say

I’m realy struggling to understand how to setup items ect

Iv only just got my switches online

I have managed to trigger a rule from within Habpannel :smiley:

I have created a rule that sends a shutdown signal to Kodi works fine

It then triggers the rule that turns off the socket

Problem is the socket just goes off as soon as you press the Button in Habpannel no wait for shutdown

Any Ideas what I have done wrong

I’m sorry dim I still don’t understand what you have sent to me I’m learning as fast as I can :slight_smile: I will keep reading over it and see if it makes more sense it did help me to trigger a rule from within Habpannel thanks for that I now understand how to do that

Could you Maby explain more simply how to create timer event (I know what you sent me is already simple I just don’t understand)

Where would that code go ?

@Sharpy,

you seem to have created your rule in the web interface (PaperUI) - this is one way that is meant to be user friendly.

However, I prefer to use a rules file. I am also a beginner here. If you installed openhab on a linux machine (including raspberry Pi) then you should be able to ssh into that machine and access all the files. Type:

cd /etc/openhab2
ls

this will show you all the crucial folders in your openhab installation. Type:

cd rules
ls

You will now be in the rules folder and see any files already in there. You will probably have a file with extension .rules in there or you can make as many of these as you like in that folder. They all get read by openhab if they have the extension .rules

Type:

sudo nano Kodi.rules (or anything .rules if you want to just use one fie for all rules)

now paste the code in from the above post

Stu

My openhab is installed on a Windows machine I will prob move to a rasberry pi or something at a different date

I thought it better to use a system I am familiar with so that I can focus on learning openhab

You are correct all my setup has been done using PaperUI

EDIT

i have navigated to the folder

C:\Users\Sharpy\Downloads\openhab-2.1.0\conf\rules

created a new txt file copyed the entire block of code unsure what parts i was ment to copy ?

remamed the textfile to Kodi Timer.rules

Is it supposed to show in PaperUI rules after do I need to restart or something

Sounds like you are progressing fast! You should not need to restart. Now if you migrate to another platfom, you can keep these files that you create.

On my linux system i can not have spaces in the file name. Not sure about windows. It will also not appear as a rule in paperui

Make sure your item names are exactly the same as in paperui. If you want to see if your rules file is loading then look up the rules section in openhab documentation and put some simple lines in that you know will work

Using PaperUI is simple enough

I have a lot to learn still

I will have to have another read of a few things see if I can get my head around it

There’s a lot I don’t understand here
how will the rule run if I can’t select it in PaperUI ,

I can’t even read that simple code even with the explanations so I don’t know how I’m going to get that to work

, how does that code know where Kodi is and what switches to turn off

I know very little about programming also but I would encourage you pursue as it will boost your learning. I shall try to explain:

If your file ends in .rules and is in your openhab config folder then it will run the code

rules start with a name (rule “Shut Down Kodi !”)
and end with an end (end)

In between these lines is where the magic happens:

When (something happens)
Then (do something)

in your case:
Item HABP_Switch changed from ON to OFF
Win_Host.sendCommand(OFF)
Power_Outlet.sendCommand(OFF)

HABP_Switch is the name of your dummy item that you need to make and will be the item (switch) that you use
Win_Host is the name of the item that turns ON/OFF your Kodi
Power_Outlet is the name of the item that controls your power outlet swtich

EVERYTHING is an item in openhab.

var Timer Kodi_Timer = null
val Integer Kodi_TimeOut = 2 //in minutes since the rule uses “now.plusMinutes”

The above just declares variables that will be needed to run your specific rule

My advice to you would be to experiment and understand items and then create some simple rules

stuart

1 Like

Thank you very much for that explanation I will try now

I do have a slightly better understanding of what to do now will post again

So to get that code to work with my setup I have two options

  1. rename my items that I have already setup with PaperUI so that they match the codes names

  2. change the code names so they match the ones I have already created

I do not have an items or a sitemap file all my setup was done with PaperUI

I do have basic control of my devices via PaperUI control

And also some very simple buttons on Habpannel that work

EDIT

I HAVE MANAGED TO COMPLETE THIS USING THE UI I used the network binding to do it (I will keep reading this thread to try and learn)

Thanks everyone I have a new question will post new thread as its not related to this post