UPDATE: 14 March 2023 - updated to the latest jruby library
What bugs me with the LG TV is whenever I turn it on, it defaults to either Live TV, or some HDMI input. So if I was watching Netflix, turn off the TV, it won’t go back to Netflix when I powered it back on.
Sure, Netflix has its own dedicated button, but Plex, Youtube etc don’t. Sure, you can create application shortcut on the magic remote, but that requires pressing and holding the corresponding key, an extra few seconds.
So I wrote a rule to save the last used application prior to powering off the TV, and start up that app when the TV turns on.
- Follow the installation instructions for JRuby helper library version 5 to run the script.
- Semantic model for associating the items, e.g. linking the power item to the application item
- The rule will save the current app on the TV after it has been stable for 1 minute. So if you switched to a new app, then turn the tv off in less than 1 minute, it won’t be saved, and the previous app will be used.
- When the TV turns on, it usually goes to the “livetv” by default (without this rule). This rule will wait for 5 seconds before restoring the previous application. If during this time you changed to another app, the rule won’t restore the app, assuming you wanted to use a different app instead.
Items setup:
- A virtual group
gTVPower
(not a part of the semantic model) - A virtual group
gTVApplication
(not a part of the semantic model) - A virtual Group item set as the TV’s “Equipment” Semantic model. For example, it can be called
LivingRoom_TV
- A TV Power item with Semantic tag
Control
,Power
. It’s a member of the equipment group (3) and the gTVPower group. For example you can call thisLivingRoom_TV_Power
- A TV Application item with Semantic tag
Status
. It’s a member of the equipment group (3) and thegTVApplication
group. For example you can call thisLivingRoom_TV_Application
You can create this using the UI. A textual configuration may look like this:
Group gTVApplication
Group gTVPower
Group LivingRoom_TV ["Television"]
Switch LivingRoom_TV_Power "Living Room TV Power" ["Control", "Power"] (LivingRoom_TV, gTVPower) { channel="lgwebos:WebOSTV:living:power", autoupdate="false" }
String LivingRoom_TV_Application "Application [%s]" ["Status"] (LivingRoom_TV, gTVApplication) { channel="lgwebos:WebOSTV:living:appLauncher"}
This is the rule file. It needs to be created in <OPENHAB_CONF>/automation/ruby/
directory with an .rb
extension. For example, you can call it tv.rb
# If using text-based items definition, uncomment the following line
# provider!(:persistent)
rule "TV: Keep track of app changes" do
changed gTVApplication.members, for: 1.minute
run do |event|
next unless event.item.points(Semantics::Power).first.on?
next if !event.item.state || event.item.state.to_s.empty?
next if event.item.metadata["last_app"]&.value == event.item.state.to_s
event.item.metadata["last_app"] = event.item.state.to_s
logger.info "#{event.item.equipment.name} app saved: #{event.item.state}"
end
end
rule "TV: Restore Last App" do
changed gTVPower.members, to: ON, for: 5.seconds
run do |event|
application = event.item.points.member_of(gTVApplication).first
next unless %w[com.webos.app.livetv com.webos.app.hdmi HDMI].include? application.state.to_s
last_app = application.metadata["last_app"]&.value
next unless last_app
application << last_app
logger.info("TV State Restored last app: #{last_app} on #{event.item.equipment.name}")
end
end
These rules can also be created in the UI with some modifications. Instead of using the changed duration feature (only available in the file-based rule), you could use a reentrant timer.