As others have pointed out the original post was lacking and seeing as telepathy hasn’t been mastered yet I’ll attempt to provide detail of what I’m trying to achieve.
I have 3 media players running kodi:
media_player.lhtpc - (lounge)
media_player.bhtpc - (bedroom)
media_player.khtpc - (kitchen)
When I run a script(or call it via echodot) based on what media is playing and on what media_player I would like it to get that data and apply to another media player.
Using logic the below script makes a decision what media player to get the kodi playing data from the media player that is currently being watched. Then calls another script to make the kodi api call via the event platform. The below script also declares a variable “is_kitchen: yes” which appears to be needed due to the way the services “kodi.call_method” and “kodi.call_method_result” events work. The reason this variable (I think) is needed will be explained below.
E.g.
going_to_kitchen:
alias: "Going to kitchen"
sequence:
# - service: homeassistant.turn_on
# data:
# entity_id: switch.kitchen_watch_htpc
- wait_template: "{{ states('media_player.khtpc') == 'idle' or states('media_player.khtpc') == 'playing' }}"
timeout: '00:00:45'
- service: script.turn_on
data:
variables:
is_kitchen: 'yes'
entity_id: >-
{%- if ( states('media_player.lhtpc') == 'idle' or states('media_player.lhtpc') == 'playing' ) and ( states('media_player.bhtpc') == 'idle' or states('media_player.bhtpc') == 'playing' ) %}
script.kodi_get_playing_from_lounge
{%- elif states('media_player.bhtpc') == 'idle' or states('media_player.bhtpc') == 'playing' %}
script.kodi_get_playing_from_bedroom
{% else %}
script.kodi_get_playing_from_lounge
{% endif -%}
In this example we assume media is being played from media_player.bhtpc therefore the script “script.kodi_get_playing_from_bedroom” is run (shown below):
kodi_get_playing_from_bedroom:
alias: "kodi get playing from bedroom"
sequence:
- service: kodi.call_method
data:
method: Player.GetItem
playerid: 1
properties:
- file
variables:
is_kitchen: "{{ is_kitchen }}"
entity_id: media_player.bhtpc
This doesn’t work because variables: isn’t accepted by “service: kodi.call_method” and results in a incorrect api call made to kodi and an error.
Variables needs? to be part of “service: kodi.call_method” as it needs to be pass the automation which runs on the event " event_type: kodi_call_method_result". It needs to be passed to the below automation. I do not know how to do this.
The automation is:
automation:
alias: "Kodi - On get playing event"
initial_state: 'on'
trigger:
- platform: event
event_type: kodi_call_method_result
event_data:
result_ok: true
input:
method: Player.GetItem
action:
- service: script.turn_on
data:
variables:
file: "{{ trigger.event.data.result.item.file }}"
entity_to: >-
{%- if is_kitchen == 'yes' %}
media_player.khtpc
{%- else %}
{%- if trigger.event.data.entity_id == 'media_player.lhtpc' %}
media_player.bhtpc
{%- elif trigger.event.data.entity_id == 'media_player.bhtpc' %}
media_player.lhtpc
{% endif -%}
{% endif -%}
entity_from: "{{ trigger.event.data.entity_id }}"
entity_id: script.kodi_set_playing
This makes a decision based on the variables “is_kitchen” , “entity_to” and “entity_from” then calls the script “script.kodi_set_playing” (below):
kodi_set_playing:
alias: "kodi set playing"
sequence:
- service: kodi.call_method
data:
method: Player.Open
item:
file: "{{ file }}"
entity_id: "{{ entity_to }}"
- delay: '00:00:02'
- service: kodi.call_method
data:
method: Player.Seek
value:
seconds: "{{ state_attr(entity_from, 'media_position') | int - 120 }}"
playerid: 1
entity_id: "{{ entity_to }}"
- service: media_player.media_pause
data:
entity_id: "{{ entity_to }}"
- service: media_player.media_play
data:
entity_id: "{{ entity_from }}"
WHY is the variable “is_kitchen:” needed? (I think) because of having to using “service: kodi.call_method_result”.
From the AUTOMATION:
{%- if trigger.event.data.entity_id == 'media_player.lhtpc' %}
media_player.bhtpc
{%- elif trigger.event.data.entity_id == 'media_player.bhtpc' %}
media_player.lhtpc
{% endif -%}
works fine as there is only 2 entities/media_players but adding a third causes a problem because the entity_to: variable could be 1 of 3 media_players which, due to requiring the kodi.call_method_result event to get data from the kodi api call, it needs? the variable is_kitchen: in a nested if statement work in the automation “Kodi - On get playing event”
Due to this and the source media_player, the logic if elsif else doesn’t work?