Service template with condition based on attribute

EDIT: TLDR, any ideas why this statement works in the template editor, but not in the automation?

{% if states.media_player.kodi_kodipilocal.attributes.media_content_type == 'movie' %}

The automation triggers, but it picks the else-case.

OP:
I want to have an automation turn off the light when I start playing in kodi. But only if it’s a movie.

I got it to work when trigger was “to: playing” and a template-condition in double curly brackets.

Now I have so many automations so I don’t want one for playing and one for stop, so I wanted to merge them into one automation, without condition and instead use service_template, and there seems to be something off with my syntax. It validates fine but nothing happens… Anything catch your eye? Thanks in advance for any help.

- id: movieTime
  alias: 'Movie time'
  trigger:
    platform: state
    entity_id: media_player.kodi_kodipilocal
  action:
    - service_template: >
        {% if (states.media_player.kodi_kodipilocal.attributes.media_content_type) == 'movie' %}
          light.turn_off
        {% elif (trigger.to_state.state) == 'idle' %}
          light.turn_on
        {% endif %}
      entity_id: light.livingroom_ceiling
      data:
        brightness_pct: 50

Have you found a solution yet?

FWIW, the main problem in your automation is that you can’t call the light.turn_off service with brightness_pct specified; you can only specify entity_id.

Next, you have no else clause for the service_template, so if both if’s evaluate to false, then you’re not specifying a service.

Good call, I assumed with turn_off it would just ignore it. But i’ll template out so that data is only sent with turn_on, worth a shot!

I know, I thought, pershaps wrongly - that if I don’t specify a service for else-cases then no service would be run. Which is what I want. Will play around with this too.

Not possible.

If you don’t want to call a service under certain conditions, then add a condition to the automation that only lets the action run when it should.

I would suggest trying scenes - one for on and one for off. Then a data_template can be used to specify which scene to turn on. And, again, use a condition or a different set of triggers to only run the action when you want it to.