This is my attempt to cope with the source data of a template (fed via a rest call) coping with the odd occasion when the rest call fails. The existing template, without the if statement is sometimes throwing this error in the logs
TemplateError('UndefinedError: 'None' has no attribute 'attributes'')
But if I restart the rest entities from the developer tools yaml list, I get this:
TemplateError('UndefinedError: 'None' has no attribute 'split'') while processing template 'Template<template=({{ state_attr('sensor.bin_collection_restful','Results').split('Your rubbish')[1].split('<br/>')[1].split('</em>')[0] | default(this.state, true) }}) renders=8>' for attribute '_attr_native_value' in entity 'sensor.black_waste_bin_collection_date'
In retrospect I understand how these are different, and I get that I can’t reference .split if the object is not a string.
Both the split and original sensors come from a single rest call that has two attributes, label and results. In label, the data is clean and I can simply display it. In Results, the data I need is buried in a paragraph of text, and hence I use split to grab the bit I need.
Without explaining it, I think thats why I started my attempt by using
Which (I thought) would cover whether the object exists or not, and work for both the split and original issue.
I’ve come up with a semi generic way of doing it, not sure this doesn’t have side effects, but my approach has been simply to check if the object is a string or not: