Solved. How to do script data template with variables?

I am struggling to get a script to work for raising the volume on one of my media players. I have the following config:

Call script from input_text with state_card_tiles:

  - entity: script.media_player_volume_up
      data: {mediaplayer: "media_player.kodi_kok"}
      icon: mdi:volume-plus

Script:

  media_player_volume_up:
    alias: Volym upp
    sequence:
      service: media_player.volume_set
      data_template:
        entity_id: '{{mediaplayer}}'
        volume_level: '{{ states.{{mediaplayer}}.attributes.volume_level | float + 0.1 }}'

My log tells me this, but I don’t why it doesn’t work:

2018-01-12 16:22:25 ERROR (MainThread) [homeassistant.config] Invalid config for [script]: invalid template (TemplateSyntaxError: expected name or number) for dictionary value @ data['script']['media_player_volume_down']['sequence'][0]['data_template']['volume_level']. Got None
invalid template (TemplateSyntaxError: expected name or number) for dictionary value @ data['script']['media_player_volume_up']['sequence'][0]['data_template']['volume_level']. Got None.

I finnaly solved it! This works.
Call script from input_text with state_card_tiles:

  - entity: script.media_player_volume_up
      data: {mediaplayer: "kodi_kok"}
      icon: mdi:volume-plus

Script:

  media_player_volume_up:
    alias: Volym upp
    sequence:
      service: media_player.volume_set
      data_template:
        entity_id: '{{states.media_player[mediaplayer].entity_id}}'
        volume_level: '{{ states.media_player[mediaplayer].attributes.volume_level + 0.1}}'
2 Likes

Thanks, needed to change the array to the object instead, but otherwise it worked:

sonos_volume_up:
  sequence:
    service: media_player.volume_set
    data_template:
      entity_id: '{{states.media_player.vardagsrum.entity_id}}'
      volume_level: '{{ states.media_player.vardagsrum.attributes.volume_level + 0.05}}'

sonos_volume_down:
  sequence:
    service: media_player.volume_set
    data_template:
      entity_id: '{{states.media_player.vardagsrum.entity_id}}'
      volume_level: '{{ states.media_player.vardagsrum.attributes.volume_level - 0.05}}'