How to add a "power button" if the device has no binding?

I am using OH4 on a windows PC.

Now I do have a device which has no interface like serial, wifi etc. but I can switch it On/Off via a batch file. This is working with a rule in OH without problem. Now I would like to have a “Switch Item” which switch On/Off this device.
How can I do that if there is no “batch binding” to create such Item? Is there a workaround or…? For sure there is an easy way but at the moment I have no idea to do that.
If there is a binding I can use it afterwards for a widget, rules etc…

Check out the Exec binding. Note that you need some extra configuration compared to other bindings, you need to manually add an exec.whitelist file with all shell commands that should be allowed to run. It’s all explained in the binding docs.

Since you already seem to have a rule that executes your script you can just create a switch item that is not linked to any thing channel. Instead you can use this item as a trigger for your rule.

Depending on your use case you might however need some more logic to synchronise the state of your computer back to the item (e.g. when the computer was switched on directly and not through openHAB).

If it helps, I had the use case of wanting to suspend a workstation machine regardless of whether it’s running Windows or Linux, with a OH switch.
I decided to install some software on both OSes to react to a message on a MQTT topic (connecting to a MQTT broker that I have on the network anyways).

Since IOT Link (for Windows) has its own hierarchy of MQTT topics I made a systemd service to subscribe to the same topic when the machine is running Linux (adjust to your situation):

[Unit]
Description=suspend with mqcontrol

[Service]
Type=simple
ExecStart=/root/go/bin/mqcontrol -h mqttbroker:1883 -i mqclinux -c "systemctl suspend" -t "pc/workgroup/myworkstation/commands/suspend"

[Install]
WantedBy=multi-user.target

Then you can make a single Switch item representing the “powered on” status of that machine:

  1. add a rule to publish the suspend message to the MQTT topic (pc/workgroup/myworkstation/commands/suspend) when the item receives the OFF command
  2. link it to a thing from the Network binding to make the state reflect the “powered on” status
  3. add another rule to wake it up using Wake-on-LAN when the item receives the ON command.

This is the most simple answer. Should be the accepted solution.

Thanks for the answer. When the computer is turned on, the default state is always False (Off). How can I achieve it? Or is the default case always OFF?

I would recommend to use the network binding to create a pingdevice thing for your computer. Then you could link your switch item to the Online channel of the pingdevice to have the switch showing you the current state of the device.

Depending on the startup time of your computer you might observe that the switch goes from ON (after you manually switched it) to OFF again (because it takes the pingdevice a couple of moments to get online) and back to ON when it finally is able to answer the ping requests.

I think there is a misunderstanding. Correct I do have a rule but WITHOUT trigger function at the moment. I only used this rule to check the batch file.

Now, I would like to “link” such batch file (with exec binding) to a switch. This means when I set the switch to ON it shall call the batch file “On” and when the switch is set to OFF it calls the batch file “Off”.

In other words, the question is how do I link such batch file to an empty switch item??

Can you post the rule you have? It would probably only require a rather simple modification to make it work.

Sure but this is not the point to post the rule because it is a simple exec-binding which calls a batch file - thats it!

But, I need a Switch which calls (links) to a simple exec-binding when the switch is either in ON or OFF position.

Question:
How can I do that?

If you want yo use the Exec binding you might need two items, since the input-channel only accepts a string. But if you have activated autorun on the Thing it will automatically run whenever this the Item linked to it gets a command. It might be possible to create a switch widget in the UI for this Item directly, otherwise you will need a second Switch item that sends a command to the input-Item when it’s changed.

Edit: the rule approach would probably be simpler in your case, especially since you already have a rule in place.

Thanks that worked for me :slight_smile: