I would like to share part of my template with you and ask you if there is a more practical way to avoid the errors described in the title of this discussion. the code is this:
- platform: template
sensors:
gdrive_last_bkup_slug:
friendly_name: Gdrive last backup slug
value_template: >
{{ states.sensor.backup_state.state }}
attribute_templates:
alias: >
{% if this.state != 'none' and this.state != 'unknown' and this.state != '' and this.state != 'unavailable'%}
{{ states.sensor.backup_state.attributes.backups
| selectattr('date', 'contains', states.sensor.backup_state.attributes.last_backup)
| map(attribute='slug') |join(', ')}}
{% else %} {{this.state}} {% endif %}
name: >
{% if this.state != 'none' and this.state != 'unknown' and this.state != '' and this.state != 'unavailable'%}
{{ states.sensor.backup_state.attributes.backups
| selectattr('slug', 'contains', this.attributes.alias)
| map(attribute='name') |join(', ')}}
{% else %} {{this.state}} {% endif %}
date: >
{% if this.state != 'none' and this.state != 'unknown' and this.state != '' and this.state != 'unavailable'%}
{{ states.sensor.backup_state.attributes.backups
| selectattr('slug', 'contains', this.attributes.alias)
| map(attribute='date') |join(', ')}}
{% else %} {{this.state}} {% endif %}
slug_state: >
{% if this.state != 'none' and this.state != 'unknown' and this.state != '' and this.state != 'unavailable'%}
{{ states.sensor.backup_state.attributes.backups
| selectattr('slug', 'contains', this.attributes.alias)
| map(attribute='state') |join(', ')}}
{% else %} {{this.state}} {% endif %}
During the startup phase of HA, many sensors are slow to load and this sensor which determines its state by recalling the state of another
value_template: >
{{ states.sensor.backup_state.state }}
finds itself assuming the same initial values, the ones I tried to avoid when declaring the attributes:
alias: >
{% if this.state != 'none' and this.state != 'unknown' and this.state != '' and this.state != 'unavailable'%}
{{ states.sensor.backup_state.attributes.backups
| selectattr('date', 'contains', states.sensor.backup_state.attributes.last_backup)
| map(attribute='slug') |join(', ')}}
{% else %} {{this.state}} {% endif %}
Is there a simpler way or alternative to avoid this waste of “if” blocks?
thanks in advance to anyone who responds.