Pass entity to script?

I cannot get this to work. I want a tap action to run a script from the frontend, and the script to use the entity I pass to the script?

This is what I tried to do after some searching on the forums:

                tap_action:
                  action: call-service
                  service: script.volym_ner
                  data:
                    entity: media_player.vardagsrum

And the script:

action: media_player.volume_set
metadata: {}
data_template:
  volume_level: |
    {{ state_attr(entity, 'volume_level') | float - 0.02 }}

What am I doing wrong here?

Where does it fail? Does the card’s tap_action fail to execute the script or fail to pass the value of entity to the script?

The script’s trace can reveal the answers.

I get this error:

must contain at least one of entity_id, device_id, area_id, floor_id, label_id.

I tried changing entity to entity_id but it still doesn’t work.

The media_player.volume_set action in your script should specify which media_player will have its volume set.

action: media_player.volume_set
target:
  entity_id: "{{ entity }}"
data:
  volume_level: "{{ state_attr(entity, 'volume_level') | float - 0.02 }}"

Yes, but I want to pass the entity from the tap_action, otherwise I need to make duplicates of every script for each entity.

The example I posted supplies the action with whatever is passed by the tap_action. Only one script is needed to control the volume of any media_player.