tkgafs
(Tim)
April 6, 2023, 12:43am
1
Hello,
I am just starting to write simple scripts and can not work out how to pass variable to a script
I have created a script to play a kodi music playlist and have several others I’d like to add but assume there must be a better way to do this than copy the entire script every time to simply change the name of the playlist
my script to play suitable music for the family at dinner is as follows
kodi_dinner_songs:
alias: Dinner Songs
sequence:
- service: kodi.call_method
target:
entity_id: media_player.rasp3libre
data:
entity_id: media_player.rasp3libre
method: Player.Stop
playerid: 0
- service: kodi.call_method
target:
entity_id: media_player.rasp3libre
data:
entity_id: media_player.rasp3libre
method: Playlist.Clear
playlistid: 0
- service: kodi.call_method
target:
entity_id: media_player.rasp3libre
data:
entity_id: media_player.rasp3libre
method: Player.Open
item:
directory: /storage/.kodi/userdata/playlists/music/dinner.m3u
options:
repeat: all
shuffled: true
If i wanted to play the playlist SadSongs.m3u for example, how can I replace dinner.m3u with SadSongs.m3u
at the moment I’m duplicating the entire script to replace one word, which does not seem the correct way to go about it
tkgafs
(Tim)
April 7, 2023, 5:14pm
3
Thank you for that, but I’m afraid I’m not much further forward
I have now created a dropdown Input_selector
as follows
options:
- dinner
- speaker-test
- late_night
- SadSongs
editable: true
icon: mdi:music-box-multiple-outline
friendly_name: Playlist_Select
But I still dont understand how to get the option selected through this dropdown, into the script to in effect change the line
directory: /storage/.kodi/userdata/playlists/music/dinner.m3u
to
directory: /storage/.kodi/userdata/playlists/music/SadSongs.m3u
I presume I am missing a step somewhere but nothing seems to explain how to refer to the item I have selected.
Any pointers greatfully received
tkgafs
(Tim)
April 10, 2023, 12:05am
4
Turns out after a lot of reading it’s quite straight forward
you just need to reference the option you have chosen from your dropdown with a line similar to the following
{{ states('input_select.playlist_select') }}
so my script becomes the following
kodi_playlist:
alias: Fixed Playlist
sequence:
- service: kodi.call_method
target:
entity_id: media_player.rasp3libre
data:
entity_id: media_player.rasp3libre
method: Player.Stop
playerid: 0
- service: kodi.call_method
target:
entity_id: media_player.rasp3libre
data:
entity_id: media_player.rasp3libre
method: Playlist.Clear
playlistid: 0
- service: kodi.call_method
target:
entity_id: media_player.rasp3libre
data:
entity_id: media_player.rasp3libre
method: Player.Open
item:
directory: /storage/.kodi/userdata/playlists/music/{{ states('input_select.playlist_select') }}.m3u
options:
repeat: all
shuffled: true
mode: single
icon: mdi:music
hope this helps someone else starting out
I’m not sure yet if I can embed the actual dropdown selection into the script so that as soon as I choose the playlist I want it starts playing, currently I choose the dropdown then click on the script to start it playing.
tkgafs
(Tim)
April 10, 2023, 5:02pm
5
Finally got everthing working, when I choose the input select from my dashboard, an automation turns on the amplifier, sets its source and volume and starts the chosen playlist playing. Probably sounds trivial to a lot of you but Im feeling quite pleased with myself. I’d be intrested to know if there is any improvents I could make to the code which follows
The Input_select
{
"name": "KodiPlaylist_Select",
"icon": "mdi:music-box-multiple-outline",
"options": [
"Random.xsp",
"Short.xsp",
"Long.xsp",
"1950s.xsp",
"1960s.xsp",
"1970s.xsp",
"1980s.xsp",
"RockSteady.xsp",
"dinner.m3u",
"speaker-test.m3u",
"SadSongs.m3u"
],
"id": "kodiplaylist_select"
}
The Automation
- id: kodi_play_music
alias: Play Kodi Playlist
description: ''
trigger:
- platform: state
entity_id:
- input_select.kodiplaylist_select
condition: []
action:
- service: media_player.turn_on
data: {}
target:
device_id: 841c86f362c02fffb7bfeb5a627897dc
- delay:
hours: 0
minutes: 0
seconds: 2
milliseconds: 0
- service: media_player.select_source
data:
device_id: 841c86f362c02fffb7bfeb5a627897dc
source: Kodi
- service: media_player.volume_set
data:
volume_level: 0.3
target:
device_id: 841c86f362c02fffb7bfeb5a627897dc
- service: script.kodi_playlist
data: {}
mode: single
The Script
kodi_playlist:
alias: Kodi Playlist
variables:
playtype: >
{% if states('input_select.kodiplaylist_select').endswith(".xsp") %}
partymode
{% else %}
directory
{% endif %}
sequence:
- service: kodi.call_method
target:
entity_id: media_player.rasp3libre
data:
entity_id: media_player.rasp3libre
method: Player.Stop
playerid: 0
- service: kodi.call_method
target:
entity_id: media_player.rasp3libre
data:
entity_id: media_player.rasp3libre
method: Playlist.Clear
playlistid: 0
- service: kodi.call_method
target:
entity_id: media_player.rasp3libre
data:
entity_id: media_player.rasp3libre
method: Player.Open
item:
"{{ playtype }}": /storage/.kodi/userdata/playlists/music/{{ states('input_select.kodiplaylist_select') }}
options:
repeat: all
shuffled: true
mode: single
icon: mdi:music