Merge pull request #1991 from xillibit/k30_bugs12

Regression after updating template.xml from blue_eagle
Backport fix to optimize code for polls to avoid run queries in loop