Dynamic Dashboard Widgets?

I am working on my home automation. In doing so I maintain a database of all of the switches and dimmers and their current state. I would like to have a switch widget available for each switch/dimmer in the database. Is there a way for me to create these dynamically? So that when I add switches the ui automatically gets updated? I also want to be able to have the switch widgets reflect the current state of the switch. But I think I am able to figure that part out once i Get more into it.

I am hopefully trying to avoid manually creating 40+ switch widgets.

Yes. A UI template with the angular ng-repeat would be a good place to start digging.

Start with an inject node and array of objects to simplify testing. Once working with an inject, hook it up to database.

thanks. It just so happens that my database output is an array of objects.

I have gotten quite a ways down the road with my project but I am getting sporadic results in the UI.

Here is what my flow looks like:

The top part of the flow queries the db and gets all lights that are ON and the the bottom part gets the lights that are off. Each termplate node are part of a different UI group. My issue is that I am getting sporadic results in the UI. The db query works correctly all of the time. The code in the template nodes are almost identical

    <tr ng-repeat="obj in msg.payload">
        <td> <md-button ng-click="send({Baction:'OFF',topic:obj.friendlyName})"> OFF </md-button> {{obj.friendlyName}}

The only difference is the Baction parameter. One is ON and the other is OFF

The normal UI should look something like:

But sometimes nothing gets displayed, other times the columns are wrong. I do have the inject node running every 5 seconds in order to simulate an active display response.

I am out of ideas as to where to look. Probably because this is my first time working with the Template nodes?