If you want to try it out, just copy that entire file to {hass_dir}/custom_components/sensor/template.py and then you can add icon_template to all your template sensors.
Actually, if you keep it in custom components, it will remain in place. And it will use that instead of the updated code.
The only problem is if there is a fix or a bug in the version that was modified, that fix will have to be applied to the custom code manually. Also, if they drastically change the template.py code for some reason, you may run into issues running HA. But you can just remove the code from custom_components, comment out your specific references to the template icon and restart.
I copied your code and diligently made sure the spacing was perfect (it is!) but I get an error when trying to run it
âInvalid config for [sensor.template]: [icon_template] is an invalid option for [sensor.template]. Check: sensor.template->sensors->dark_sky_template->icon_template. (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.template/â
I couldnât find any documentation on the types of templates allowed, but it appears that an icon template isnât one of them, can you guide my wayward ways?
FYI, the next release should allow custom state cards. I think it should be possible to do this with a custom card, but itâs going to be more involved than using a template in the config.
Created a PR for this, but not sure if it will get accepted.
There is a difference between this and the one I posted earlier, which is that in this PR the icon template is only rendered when the template sensor is updated rather than on every call to get the icon. Because of this, you shouldnât use the state of the template sensor itself for the icon template since the state may not be updated before the icon template is rendered. Just use the state of the other sensor instead, like I did in my Dark Sky example above.
Thank you! I was ready to give up with the icons until I noticed on your post that, it was better to use the original value for the sensor. Now it works! Thanks!