Running a Script with a Switch, Revisited

For a few years now I’ve been using this post on Running a Script with a Switch for several devices, including a media PC and my Vizio TV. I migrated them in a very basic way into Openhab 3. I’d like to know 1) if there is a better way today, and 2) how I mightuse a widget like the four button list item to run the script with one of four (or more) options, to say select an input on the TV.

Using the TV as an example. my shell script is run by:

Usage: ./ <on|off|status|cast|mythtv|wii>
  [on|off] - Power the system on or off
  [status] - Display power status
  [cast|mythtv}wii] - Select input on TV

Under OH2 I used these files:
File: viewscreen.things

Thing exec:command:device-viewscreen-control "Vizio Control (On/Off)" [ command="/etc/openhab/scripts/ %2$s", interval=0, timeout=10, autorun=true ]
Thing exec:command:device-viewscreen-status "Vizio Online Status" [ command="/etc/openhab/scripts/ status", interval=20, timeout=4 ]

File: viewscreen.items

String network_device_viewscreen_control "Main Viewscreen Control" <television> { channel="exec:command:device-viewscreen-control:input", channel="exec:command:device-viewscreen-status:output", autoupdate="false" }
String network_device_viewscreen_status "Main Viewscreen Online Status"  { channel="exec:command:device-viewscreen-status:output" }
Switch network_device_viewscreen_switch "Main Viewscreen Power" <television> [ "Switchable" ]

File: viewscreen.rules

   rule "Vizio Switch"
    	Item network_device_viewscreen_switch changed
    	logInfo("Network", "Switching MainViewscreen " + network_device_viewscreen_switch.state.toString + ": " + network_device_viewscreen_control.state.toString)

    rule "Vizio State"
    	Item network_device_viewscreen_status changed
    	logInfo("Network", "Switching MainViewscreen " + network_device_viewscreen_status.state.toString + ": " + network_device_viewscreen_control.state.toString)

For Openhab 3 , I removed the viewscreen.things and viewscreen.items files, recreated the 2 things through the gui under the Exec binding, and used the Add items from textual definition option to create the three things. In this way I could add them to my Semantic Model.

I tried to use the 4 way switch, to send off , mythtv, cast , wii as the command for the off , low/ medium, and high buttons , that did not work. I remember back in the day there was a lot of discussion on how to get this to work, so the switch would update to reflect external changes (ie, if the comptuer / tv was powered on via it’s physical switch, OpenHab would know about the change). That is why I’m wondering if there isn’t a better starting point now under OH3.

Any help apreciated!

Some details will be different but the over all approach will be the same. You still need your Things and Items pretty much like you’ve always had them.

So that means you’ll probably have to add the additional channel link to the network_device_viewscreen_control Item if you’ve not done that already. You’ll also need to set the autoupdate Item metadata to off.

And since the network_device_viuewscreen_switch isn’t linked to anything at all, you’ll have to create that one by hand.

The rules and sitemap can remain unchanged. If you want to use MainUI instead of BasicUI, see the Getting Started Tutorial, UI section of the docs and examples posted in the Add-Ons → UI section and the Marketplace → UI Widgets section for details. In general you’ll create a custom widget that is made up of multiple widgets that are configured to work like buttons.