Dynamic sensors in 1 template

I have several sensors that only look different at the end, such as:

  • binary_sensor.uptime_abc
  • binary_sensor.uptime_dce
  • binary_sensor.uptime_fgh

New binary sensors are added regularly.

they have an attribute: Monitor response time
for which I want to create separate sensors (for all)

is there a smart way to combine all sensors in 1 template? (and so that new ones are added automatically?)

sensor:
  - platform: template
    sensors:
      uptime_response_time:
        friendly_name: 'Uptime Response time'
        value_template: "{{ state_attr('binary_sensor.uptime*', 'responsetime') }}"

a * after binary_sensor.uptime is not going to work?

No, you cannot use templates to build the configuration of individual template sensors dynamically in situ. It is possible to set up a single sensor with an attribute that contains a dynamic list, but that doesn’t seem to meet your needs. It is also possible to use the Template editor tool to construct configuration text that you can copy/paste, but it is usually more work to get the template correct than it’s worth unless you need to add dozens of entities in one go.

You can use YAML Anchors and Merges to reduce the amount of configuration you have to repeat.

2 Likes