Looping to generate MQTT.publish commands to a group of entities?

You can only template the value part of a key: value pair. You have both keys and values in your template.

I believe the usual solution to this is to use a python script or use AppDaemon.

Here’s an example of a python script.

Though the script syntax has been updated with quite a few repeat functions. There may be a way to use them: