Hi there.
My music player sometimes gets stuck at a song, which I want to (band-aid) fix with home assistant. If a song title is the same for [time] while [playing], this automation should skip a song.
The part that does not work for me is here. It seems to have difficulties comparing the trigger state in the service_template.
action:
- service_template: >
{%- if trigger.to_state.attributes.media_title == trigger.from_state.attributes.media_title %}media_player.media_next_track
{%- endif %}
data_template:
entity_id: media_player.mpd
A version that does work is listed below:
action:
- service_template: >
{%- if STRING == STRING %}media_player.media_next_track
{%- endif %}
data_template:
entity_id: media_player.mpd
The full automation is given below:
- alias: 'Release Mopidy from loop if stuck'
trigger:
platform: state
entity_id: media_player.mpd
condition:
condition: and
conditions:
## Check if song changed
- condition: template
value_template: >
{{ trigger.from_state and
trigger.to_state.attributes.media_title !=
trigger.from_state.attributes.media_title }}
## Check if media player is in playing state
- condition: state
entity_id: 'media_player.mpd'
state: 'playing'
action:
- data_template:
message: Mopidy changed song to {{trigger.to_state.attributes.media_title}} .
service: notify.me
- delay:
# minutes: 11
seconds: 10
- data_template:
message: 10 seconds passed
service: notify.me
- service_template: >
{%- if trigger.to_state.attributes.media_title == trigger.from_state.attributes.media_title %}media_player.media_next_track
{%- endif %}
data_template:
entity_id: media_player.mpd
# - service_template: >
# {%- if tabel == tabel %}media_player.media_next_track
# {%- endif %}
- service_template: >
{%- if trigger.to_state.attributes.media_title == trigger.from_state.attributes.media_title %}notify.me
{%- endif %}
data_template:
message: Mopidy is playing the same song for 10 seconds.
- data_template:
message: Checks done
service: notify.me
This results in the following error of HA Core:
2020-08-18 15:57:15 ERROR (MainThread) [homeassistant.components.automation.release_mopidy_from_loop_if_stuck] Release Mopidy from loop if stuck: Error executing script. Unexpected error for call_service at pos 4: Template rendered invalid service:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 115, in async_prepare_call_from_config
domain_service = cv.service(domain_service)
File "/usr/src/homeassistant/homeassistant/helpers/config_validation.py", line 418, in service
raise vol.Invalid(f"Service {value} does not match format <domain>.<name>")
voluptuous.error.Invalid: Service does not match format <domain>.<name>
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 191, in _async_step
await getattr(
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 352, in _async_call_service_step
domain, service, service_data = async_prepare_call_from_config(
File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 121, in async_prepare_call_from_config
raise HomeAssistantError(
homeassistant.exceptions.HomeAssistantError: Template rendered invalid service: