It's been a looooong while since I've messed with this , but here is the full thing to play a movie with preshow using HA.
The script below requires parameters unique to your setup at the top, replace everything inside the '' including the brackets. This script is designed to return a list of all movies that have been unwatched and does not contain any movies with the tag of christmas, holiday, or Concert. It also excludes movies with the genre of Documentary (you can change this if you like). Once a random movie is selected, a few data points are output to a csv file for history tracking, this is not required for functionality, just a fun thing to look back on the history of what has been watched. If all has gone well, the movie should start with the preshow experience sequence designated for that movie based the on criteria that is set for sequences in the preshow experience addon.
#Returns a list movie and movie ID
#required param ID
$username ='[kodi web username]'
$pawd = '[kodi web userpassword]'
$hostname = '[host ip address]'
$port = '[web port number]'
$secpwd = ConvertTo-SecureString $pawd -AsPlainText -Force
$CSVfilepath = '[full path of csv file, including the name of the file]'
###########################################################################
###########################################################################
$data = @{
'jsonrpc' = '2.0'
'method' = 'VideoLibrary.GetMovies'
'id' = 1
params = @{
properties = @('genre','playcount','year')
filter = @{
and = @{
field = 'playcount'
operator = 'lessthan'
value = '1'
},
@{
field = "tag"
operator = "doesnotcontain"
value = @('Christmas','Holiday','Concert')
},
@{
field = "genre"
operator = "doesnotcontain"
value = "Documentary"
},
@{
field = "tag"
operator = "doesnotcontain"
value = "concert"
}
}
}
}
$json = $data | ConvertTo-Json -Depth 100
$url = 'http://'+$hostname+':'+$port+'/jsonrpc'
$mycreds = New-Object System.Management.Automation.PSCredential ($username, $secpwd)
$webreq = Invoke-WebRequest -Uri $url -Credential $mycreds -Body $json -ContentType application/json -Method POST
$unwatchedrandommovie = ($webreq.content|convertfrom-json).result.movies|get-random
###########################################################################
###########################################################################
#Store the movie ID in a variable
$pickedmovie = $unwatchedrandommovie
###########################################################################
#Append movie to CSV file for tracking
$now = Get-Date
$Content = [PSCustomObject]@{MovieName = $pickedmovie.label; DateWatched=$now; Genre=$pickedmovie.genre[0];ReleaseYear=$pickedmovie.year|out-string}
#Append to CSV
$Content| Export-Csv -Path $CSVfilepath -Append -NoTypeInformation
###########################################################################
###########################################################################
#Starts a PreShow Experience sequence
$movieid = $pickedmovie.movieid
$data = @{
'jsonrpc' = '2.0'
'method' = 'Addons.ExecuteAddon'
'id' = 1
params = @{
addonid = 'script.preshowexperience'
params = @{movieid = "$movieid"}
}
}
$json = $data | ConvertTo-Json -Depth 100
$url = 'http://'+$hostname+':'+$port+'/jsonrpc'
$mycreds = New-Object System.Management.Automation.PSCredential ($username, $secpwd)
Invoke-WebRequest -Uri $url -Credential $mycreds -Body $json -ContentType application/json -Method POST |out-null
Store this in a powershell file somewhere on your desktop or server.
Next, you will need the HA agent installed on the machine that the script lives on...this is the forked version of the agent that is actively supported, but you can use the OG version if you like, but it is no longer actively being updated.
https://github.com/hass-agent/HASS.Agent
Once installed, and configured for your HA instance; create a Powershell command with entity type 'button'..give it a name and friendly name and point to the script using the full path of the script
Afterwards you should have a button in the MQTT integration that initiates the script...you can do whatever you like with it from here.
I have an automation that stops whatever is playing, clears the playlist, then starts the script which kicks off the preshow sequence. If you use this automation you will need to replace the media_player names with your kodi instance name and also the name of the button to whatever you named it in the Hass Agent.
alias: Andrew Office - Kodi - PreShow Experience - Random Movie - Everyday
sequence:
- alias: Stop Currently Playing Item
service: kodi.call_method
metadata: {}
data:
method: Input.ExecuteAction
action: stop
target:
entity_id: media_player.kodi_instancename
- alias: Clear Current Playlist
service: kodi.call_method
metadata: {}
data:
method: Playlist.Clear
playlistid: 0
target:
entity_id: media_player.kodi_instancename
- service: button.press
metadata: {}
data: {}
target:
entity_id: button.name of button entity for kodi script
- alias: Open Player
service: kodi.call_method
metadata: {}
data:
method: Player.Open
item:
playlistid: 0
position: 0
target:
entity_id: media_player.kodi_instancename
mode: single
icon: mdi:kodi
Hope that helps, let me know if you have any questions. I don't come here much so you can send me a message on discord if you like. Discord name: shredder5262