Some entities won’t have adevice_class defined. The equality comparison can handle that but the search test can’t.
You should first select the entities that have a device_class defined. You should do this for both templates, even though it’s not causing errors in the first template.
I also changed the test from search to in because the device class should be exactly equal to one of those options, so no need to search.