Fire and pause next last watched Kodi Tv Show episode

So far I’ve manage to start an automation to fire last watched TvShow next episode on my Kodi when I returned from Work routine.

But I’m struggling with some parts of my automation rules

What it does:

If switch “Leave work” is on when my presence turn on it:

  • T.V. turn on
  • Request last TvShows watched (loop until it receive an response :roll_eyes:)
  • Take last Tv.Show ID
  • With this id it request next episode not watched ID
  • With this episode id it fire it on Kodi
  • It attempt to pause it (May interfere with my automatic pause on subTitle search and download Kodi AddOn)

Here My rule:

rule "Start Next TvShow Episode"
when
 Item QLexMedia_PartyPlayls received command NextTvShow
then
 var String json ="%7B%22jsonrpc%22%3A%222.0%22%2C%0A%20%22method%22%3A%22VideoLibrary.GetTVShows%22%2C%0A%20%22params%22%3A%20%7B%0A%20%20%20%22properties%22%3A%5B%22lastplayed%22%5D%2C%0A%20%20%20%22sort%22%3A%7B%22order%22%3A%22descending%22%2C%22method%22%3A%22lastplayed%22%7D%7D%2C%0A%20%22id%22%3A1%7D"
 var String shows = sendHttpGetRequest("http://192.168.0.199:8080/jsonrpc?request="+ json)
 logWarn ("Qtronik old","Last tvShow response: "+shows)
 if(shows !== null){
  var Number lastTvShowPlayedId = 
  Integer::parseInt(transform("JSONPATH","$.result.tvshows.[0].tvshowid",shows))
  logWarn ("Qtronik old","Last played tvShowId: "+lastTvShowPlayedId)
  logWarn ("Qtronik old","next episode is indeed a number")
  QLexMedia_lastTvShowWatched.sendCommand(lastTvShowPlayedId)
  var String getNextEpisodeLastTvShowJson = "%7B%22jsonrpc%22%3A%222.0%22%2C%0A%20%22method%22%3A%22VideoLibrary.GetEpisodes%22%2C%0A%20%22params%22%3A%7B%0A%20%20%20%22sort%22%20%3A%20%7B%22order%22%20%3A%20%22ascending%22%2C%20%22method%22%20%3A%20%22episode%22%7D%2C%0A%20%20%20%22tvshowid%22%3A%20"+lastTvShowPlayedId+"%2C%0A%20%20%20%22filter%22%3A%7B%22field%22%3A%22playcount%22%2C%22operator%22%3A%22lessthan%22%2C%22value%22%3A%221%22%0A%20%20%20%7D%0A%20%7D%2C%0A%20%22id%22%3A1%7D"
  var String nextEpisodes = sendHttpGetRequest("http://192.168.0.199:8080/jsonrpc?request="+ getNextEpisodeLastTvShowJson)
  try {
   val int episodeToPlay = Integer::parseInt(transform("JSONPATH","$.result.episodes.[0].episodeid",nextEpisodes))
   logWarn ("QtronikOld","episodeToPlay: "+episodeToPlay)
   var String playEpisodeString = "%7B%22jsonrpc%22%3A%222.0%22%2C%0A%20%22method%22%3A%22player.open%22%2C%0A%20%22params%22%3A%7B%0A%20%20%20%22item%22%20%3A%20%7B%22episodeid%22%3A"+episodeToPlay+"%7D%0A%20%7D%2C%0A%20%22id%22%3A1%7D"
   var String playEpisode = sendHttpGetRequest("http://192.168.0.199:8080/jsonrpc?request="+ playEpisodeString)
   logWarn ("QtronikOld","Playing episode response: "+playEpisode)
   sendCommand(gFF_QLexMedia_Control,"PAUSE")
  } catch (NumberFormatException e) {
   logWarn("QtronikOld","Episode returned is not a number. Redo NextTvShow")
   //TODO if empty json do next lastTvShow watched ------------------------------------------------------
  }
 } else {
   logWarn ("Qtronik lasttvShowPlayed","Returned null so redo lastPlayedtvShow rule")
   QLexMedia_PartyPlayls.sendCommand("NextTvShow")
  }

Here some problems to solve:

  1. So I am not able to change waiting time for http response some what the paperUI parameters wont save on change…
  2. If there is no more episodes to play I want to be able to get the next last TvShow watched last episode not played to fireup
  3. I want to watch it until it pause properly

Finally I am sure that alot of you people would like this type of automation to their routine :blush: