Templating to create repetitive template sensors?

Hi All,

I basically want to use the custom:button-card card to create button entities that show the current state of my lights as well as the time elapsed since the last state change. I’m able to accomplish this by creating a template sensor for each light and pull that value into the label of the custom:button-card.


type: 'custom:button-card'
entity: light.bedroom_1
  action: toggle
  action: more-info
label: '[[[ return ` ${states[''sensor.bedroom_1_time_since_state_change''].state}` ]]]'
name: Bedroom 1
show_label: true
    - color: grey

Template Sensor

      entity_id: light.bedroom_1
      friendly_name: bedroom 1 time since state change
      icon_template: mdi:clock-outline
      value_template: "{{ relative_time(states.light.bedroom_1.last_changed) }}"

My question is, is there a better way to do this other than manually creating a template sensor for each light in my setup? Either by using templates for creating the template sensor for all devices in the light domain OR being able to use the relative_time option in the button-card? Thanks

Just a quick addon question: The last_changed attribute always resets at server restart, is there a way to avoid that moving forward? Possibly a different way to calculate the last time the state changed from on/off that persists through restarts? Thanks

It looks like I was overcomplicating this. It turns out that button-card actually has an option to show the last-changed state as the label already.

show_last_changed: True

My original question still stands for other reasons though, is there a way to create a template sensor that creates other template sensors?