i create the display text in the component and save it as an attribute:
class AbfallSensor(Entity):
def __init__(self, data, sensor_type):
self.data = data
self.type = sensor_type
self._name = SENSOR_PREFIX + SENSOR_TYPES[self.type][0]
self._unit = SENSOR_TYPES[self.type][1]
self._icon = SENSOR_TYPES[self.type][2]
self._state = None
self._attributes = {}
@property
def name(self):
return self._name
@property
def icon(self):
return self._icon
@property
def state(self):
return self._state
@property
def unit_of_measurement(self):
return self._unit
@property
def device_state_attributes(self):
"""Return attributes for the sensor."""
return self._attributes
def update(self):
self.data.update()
abfallData = self.data.data
try:
if self.type == 'gelbersack':
self._state = abfallData.get("gelberSack")
elif self.type == 'restabfall':
self._state = abfallData.get("restAbfall")
elif self.type == 'papiertonne':
self._state = abfallData.get("papierTonne")
elif self.type == 'biotonne':
self._state = abfallData.get("bioTonne")
elif self.type == 'problemstoffmobil':
self._state = abfallData.get("problemStoffMobil")
if self._state is not None:
weekdays = ["Mo", "Di", "Mi", "Do", "Fr", "Sa", "So"]
self._attributes['days'] = (self._state.date() - datetime.now().date()).days
self._attributes['display_text'] = self._state.strftime('{}, %d.%m.%Y (in {} Tagen)').format(weekdays[self._state.weekday()],self._attributes['days'])
except ValueError:
self._state = None
Then I only need one template sensor in the configuration.
- platform: template
sensors:
gelbersack_text:
value_template: "{{states.sensor.waste_gelber_sack.attributes.display_text}}"
biotonne_text:
value_template: "{{states.sensor.waste_bio_tonne.attributes.display_text}}"
papiertonne_text:
value_template: "{{states.sensor.waste_papier_tonne.attributes.display_text}}"
problemstoffmobil_text:
value_template: "{{states.sensor.waste_problemstoffmobil.attributes.display_text}}"
restmuell_text:
value_template: "{{states.sensor.waste_rest_mull.attributes.display_text}}"