Have a look at my tested example at the end of this thread:
https://github.com/home-assistant/home-assistant/pull/1882
The name of the variable should be accessible directly in the template. I don’t think that the entity_id key supports templating though. I’d be happy to be corrected.