Template multiple entities problem

So I am basically stuck with the same method I used. Is there any way to simplify the code by any other mean?

Your best option would be a python script. Otherwise, what you have is the best way to approach this with jinja.

thanks for the help.