Using external Scripts in OpenHab as Switch/Button

Hello Guys,

i learned a lot in the last view days, from all of you because here in the community are a lot of useful information für my smart home project. I was able to implement the Milight iBox2 witch the Snapshot version and so on.

But at the Moment i have a Problem with the implementation of a C++ Script as Button or Switch. But first i some words about the enviroment:

I installed OpenHabian 2 (Snapshot) on my Raspberry P 3 Model B and read out the sent codes of the Remote control of the outlets with an 433Mhz Receiver connected over GPIO of the Raspberry, than I wrote C++ Scripts (for all of the my four Outlets) to switch them on/off via 433 Mhz Transmitter - Also connected over GPIO of my RaspPi. So i have know four compiled scripts:
steuerung1.cpp
steuerung2.cpp
steuerung3.cpp
steuerung4.cpp

First Command For “switch on” Outlet #1:

sudo /wiringPi/433Utils/RPi_utils/steuerung1 1

Second For “switch off” the Outlet #1:

sudo /wiringPi/433Utils/RPi_utils/steuerung1 0

And it works very well. But now i have the Problem to integrate it to OpenHAB. I think the EXEC-Binding ist the right one, but i don’t understand, how i schould configure my Thing ans Item.

Target schould be, that i habe in my Android App an Item “Outlet #1” with two Buttons “on” and “off”. If i press “on” the first command will be execute - if I press “off” the second command is execute.

Next step is to extent it to more Outlets but this is my work to duplicate and modify the Thing and Item.

I know in this community are a lot of smart people and I am sure somebody will be able to help me :wink: I appreciate your work for this Open Source project.

Best wishes
Dennis

In the meanwhile i found out, that sudo not worked because the OS wants the password, if I execute a sudo command

i modified the command:

#Switch on
wiringPi/433Utils/RPi_utils/steuerung1 1, 

#Switch off
wiringPi/433Utils/RPi_utils/steuerung1 1

…and it works via putty!

Now I tried the EXEC Item:

Switch TEST1 "Funksteckdose 11111 A"  {exec="OFF:/wiringPi/433Utils/RPi_utils/steuerung1 0, ON:/wiringPi/433Utils/RPi_utils/steuerung1 1"}

But there is no reaction from the Outlet, if i switch over the openhab2

I have no idea what is going wrong :frowning:

Nobody here who can help me?

Hello @rlkoshak

thank you for your post, but I don’t unterstand your solution.

When openHAB is installed in the automated way (i.e. via apt-get) an openhab user is created and the service runs under that user.

I don’t install openHAB seperatly I used the image of Openhabian - so its running the “All-Inclusive”-Image

If the command or the called script calls sudo, make sure openhab is a member of sudoers and is configured with NOPASSWORD

in the first post I was using sudo. but i change the command to avoid problems with rights… the current command needs no password with user openhabian (default user from the openhabian-image)

I understand that the cause of my problem is located in the rights management - but your explanation don’t match with my solution. May you can add some details?

Thanks!

The statement still stands. openHAB is running under the limited openhab user.

But openHAB is running under the openhab user, not openhabian.

I suspect the problem is that the openhab user doesn’t have permission to run the script or access resources you are trying to access.

Ok @rlkoshak , i understand, that my problem is located in the permission of openhab user!

But I still don’t understand, how i can fix that! I am a noob with Linux.

There could be a lot of things, hence the article I linked to.

Please go through the How to Debug section to identify what the problem might be. Once you know what the problem is it is a simple matter to give the openhab user the right permissions.

Hello Again,
Sorry but i guess i am to stupid for that.
I tried all point which you had written but nothing is working! The result still the same…
But i am also don’t understand this point with you rule:

I put openhab into sudoer, i make this user to the owner of the directory or the script. I don’t understand why it is not working!

So is there nobody who has realize this kind of control? I cannot believe that, that i am the only one try to control Outlets with 433mhz transmitter via GPIO?

This is the only way to get the error your script is generating when you try to execute it from openHAB.

This first step in solving an error is figuring out what the error is.

I’m sure there is but realize that anything that deals with GPIO is going to be custom and the more custom it is the more unique it becomes. When you go custom like this, while there is lots of generic help, you are largely on your own.

I understand the sense of this rule, but not, how i can implement it! May you can give me a step by step introduction or any link were i can find an itroduction.

Could you please write “ls -la” under the folder “wiringPi/433Utils/RPi_utils/” under putty and post your result here? So that we can see the permiisons of your files.

Switch MySwitch
rule "my rule"
when
    Item MySwitch received command
then
    val results = executeCommand( cmd, 5000 l
    logInfo("myrule", results)
end

Hello @rlkoshak,

I set your Code into “rule.rules”

But there is a error:

So i thinking, that “l” was a misstake and replace it with “)” but than i get another error:

“TEST1” is the name of my outlet switch…

@febz11
Here the results:

Please don’t be confused - for the several tries i changed the directory of the scripts. But via Putty with user openhabian i can execute the file like before…

Some mention form me: At the moment i have create only items - should I create also Things? How schould them looks like?

Try:

val results = executeCommandLine("/opt/raspberry-remote/steuerung1@@1", 5*1000)

instead of

val results = executeCommand( cmd, 5000 l

line. Make sure you have the EXEC binding installed.

For the permissions you have two different users, openhab and openhabian. I believe the user for your openhab installation is “openhabian”, altough the steuerung files are executable in user level, you better chown and chgrp the steuerung files file to openhabian in order for openhab to use it.

I have change the owner from Openhabian to openhab, because before it was also not working and @rlkoshak told, that die Binding is using the user openhab.

I took a look to the directory openhab2-logs/openhab.log there is a warning:

2017-06-04 15:43:28.554 [WARN ] [hab.binding.exec.handler.ExecHandler] - Couldn’t transform response because transformationService of type ‘REGEX’ is unavailable

Can this help to solve my problem or where can i find the result uf the rule?

loginfo line should write the result in opanhab.log. However maybe you should also install REGEX Transformation from paper ui-addons-transformations page as exec binding seems to have a dependency on regex transformation.

Hello,

since i had instal REGEX Transformation i get multiple error messages, here some of them:

2017-06-04 23:29:51.605 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item ‘Garten’ for widget org.eclipse.smarthome.model.sitemap.Group
2017-06-04 23:29:51.607 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item ‘Garten’ for widget org.eclipse.smarthome.model.sitemap.Group
2017-06-04 23:29:51.609 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item for widget org.eclipse.smarthome.model.sitemap.Group
2017-06-04 23:29:51.611 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item for widget org.eclipse.smarthome.model.sitemap.Group
2017-06-04 23:30:14.050 [WARN ] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve color item Last_Update for widget
2017-06-04 23:30:14.057 [WARN ] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve color item Last_Update for widget
2017-06-04 23:30:14.079 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item ‘Wohnzimmer’ for widget org.eclipse.smarthome.model.sitemap.Group
2017-06-04 23:30:14.081 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item ‘Wohnzimmer’ for widget org.eclipse.smarthome.model.sitemap.Group
2017-06-04 23:30:14.083 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item for widget org.eclipse.smarthome.model.sitemap.Group
2017-06-04 23:30:14.085 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item for widget org.eclipse.smarthome.model.sitemap.Group
2017-06-04 23:30:14.089 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item ‘Essecke’ for widget org.eclipse.smarthome.model.sitemap.Group
2017-06-04 23:30:14.091 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item ‘Essecke’ for widget org.eclipse.smarthome.model.sitemap.Group
2017-06-04 23:30:14.100 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item for widget org.eclipse.smarthome.model.sitemap.Group
2017-06-04 23:30:14.102 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item for widget org.eclipse.smarthome.model.sitemap.Group
2017-06-04 23:30:14.111 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item ‘Garten’ for widget org.eclipse.smarthome.model.sitemap.Group
2017-06-04 23:30:14.113 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item ‘Garten’ for widget org.eclipse.smarthome.model.sitemap.Group
2017-06-04 23:30:14.115 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item for widget org.eclipse.smarthome.model.sitemap.Group
2017-06-04 23:30:14.117 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item for widget org.eclipse.smarthome.model.sitemap.Group

I was able to stop them with uninstalling REGEX Transformation and EXEC Binding…
So i have no idea, what is going wrong here… everything is working, only this small part makes problems…

Is there any other way to implement this Outlets, for example with pilight or something else? I guess with this way i waste time it don’t worth it.

The errors mean you have entries in your sitemap that reference Items that do not exist.

but all etries in my sitemap are working well… So the errors makes no sense!

I can’t answer whether it makes sense it not but that is what the errors mean.

As far as the sunshine is concerned you do not have the following Groups and Items:

  • Garten
  • Last_Update
  • Wohnzimmer
  • Esseke

Make sure these items and groups exist and are identical, including case.