LG TV Webos: A rule to restore the last application on power on

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:

  1. A virtual group gTVPower (not a part of the semantic model)
  2. A virtual group gTVApplication (not a part of the semantic model)
  3. A virtual Group item set as the TV’s “Equipment” Semantic model. For example, it can be called LivingRoom_TV
  4. 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 this LivingRoom_TV_Power
  5. A TV Application item with Semantic tag Status. It’s a member of the equipment group (3) and the gTVApplication group. For example you can call this LivingRoom_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.

3 Likes

I’ve updated the original post above with my latest rule.

Could you also use store_states?

No, because I need to save event.last (the state before the change, not the current state). I just had an idea to use metadata for storage instead of a class variable. I’ll update my code above.

I do something similar to this, but I just use an unbound string item to store the most recent state.

That’s a great idea too. Why not metadata?

An unbound item was generally the recommended solution in OH2 for anyone wanting global variables, so I’ve never given it more thought than that.

My scenario is that I’m controlling an IR fan through OH with a Logitech Harmony remote as an intermediary. I’m basically just sending button presses to the fan. The fan automatically returns to the last setting when you turn it on, but OH doesn’t know that since there’s no feedback.

So, I store the setting in the unbound item. Whenever I change the setting via OH, a case statement triggers the correct sequence of button presses based on the last stored setting.

With persistence, the value will survive a system restart (I assume that will also be the case with metadata). It continues to work in OH3, so I’ve no reason to change it.

1 Like

So if the fan setting is changed on the fan itself, it would get out of sync?

Yep, but it’s in a corner far from the door, and I can control it via Google Assistant. So, I have no reason to touch the controls.

Can someone provide implementation instructions? Are you doing this vis SSH?

Welcome to the community. Can you be more specific as to what you want to accomplish? The discussion above was a long time ago.

This is all done with items and rules in the openHAB 3.x UI or in text files.

The communication with the LG tv is done through the lgwebos binding

Hello! Thanks for the post, I have only one question. The process for aplying the rules is creating a rule which runs a script (with the code of the addon JRuby for example)? I don’t know how to create those rules and how to apply them to the TV. Thank you so much

  1. Install jrubyscripting addon according to the installation instructions
  2. Create a file (e.g. call it tv.rb) in <OPENHAB_CONF>/automation/ruby/
  3. Paste the rule in that

But before that, you need to set up your TV things and Items first and refer to those items in the rule. Are you familiar with that part?

Thank you so much, sorry but I am not familiar I am starting to use openhab, I would apreciate all the support that you can lend me for this thing. Thanks!

Perhaps the first thing is to familiarise yourself with openhab before tackling the more advanced rules topic.

Start here: Introduction | openHAB

And if you have any specific questions, feel free to post a question, perhaps start a new topic if the question is not related to this specific script. There are plenty of people who will be happy to help.

Thank you, I made the setup for the TV and created a model for this implementation:

And I created the file with the code:

Group gTVApplication

Switch LGTV_Power  "Office TV Power" { channel="lgwebos:WebOSTV:4e039267-1fc7-6439-d175-ec462aecf94d:power", autoupdate="false" }
String LGTV_Application "Application [%s]" (gTVApplication) { channel="lgwebos:WebOSTV:4e039267-1fc7-6439-d175-ec462aecf94d:appLauncher"}

rule 'TV: Save Last App' do
  changed gTVApplication.items, to: UNDEF, for: 5.seconds
  run do |event|
    event.item.meta['last_app'] = event.last
    logger.info("TV State Saved last app: #{event.last} on #{event.item.name}")
  end
end

rule 'TV: Restore Last App' do
  changed gTVApplication.items, from: UNDEF, for: 5.seconds
  only_if { |event| %w[com.webos.app.livetv com.webos.app.hdmi HDMI].include? event.state.to_s }
  only_if { |event| event.item.meta['last_app']&.value }
  run do |event|
    event.item << event.item.meta['last_app'].value
    logger.info("TV State Restored last app: #{event.item.meta['last_app'].value} on #{event.item.name}")
    event.item.meta.delete 'last_app'
  end
end

But the rule is not working. Is something wrong with my configuration? Or shall I add the rule on the TV Thing? Sorry for those questions and thank you for the patience :slight_smile:

The above isn’t part of the rule. It’s a textual configuration of the group and items. They were supposed to go into an .items file, not merged into the rules. Two different things. Since you’ve created your items using the UI, you don’t need this textual items definition.

Just remove it from the rb rule file.

Next- it seems that you haven’t created the group / items correctly…

In any case, please accept my apologies. I took a look at the code above, which I posted in 2021. A lot has changed since then, and I believe that code doesn’t work anymore because it’s not compatible with the current jruby library.

I will post an updated rule tomorrow and I’ll let you know

@Gabriel_Gil_Bordon I’ve updated the original post with the latest rule.

1 Like

Thank you so much!