Where to put configuration for template sensors

I have a number of template sensors. I currently configure my template sensors as follows:

binary_sensor:
  - platform: template
    sensors:
      washer_running:
        delay_off:
          minutes: 3
        value_template: >
          {{ states('sensor.power_washer_3_1min') | float > 20.0 }}

This works fine. However, the Template documentation makes me think I should do this like this:

template:
  - binary_sensor:
      - name: "Washing Machine"
        delay_off:
          minutes: 3
        state: >
          {{ states('sensor.power_washer_3_1min') | float > 20.0 }}

Both methods work. Is either method preferred and better for performance or more likely to not be affected by a future breaking change?

The second format (“modern”) is the newer one and effectively supersedes the first one (“legacy”). However, legacy format remains acceptable but is unlikely to receive enhancements.

For example, you can directly assign state_class to a Template Sensor’s modern configuration but not in legacy format (only via Customizations). In addition, modern Template Sensors can be trigger-based whereas legacy Template Sensors have no equivalent.

On the other hand, it’s easy to assign a desired friendly name to a legacy Template Sensor’s configuration, that differs from its entity_id, whereas that’s not possible for a modern Template Sensor in its configuration (only via Customizations).

FWIW, I use a mix of both legacy and modern formats, largely because I’ve been procrastinating about converting existing legacy-based Template Sensors; any new Template Sensors I create are in modern format.

3 Likes

Thanks, great description of the two methods and your advice it is what I was looking for. I have been replacing a number of my input_booleans where I was manually maintaining the state with binary_sensors that I can use templates to maintain the state. I had been adding them to my “legacy” sensors. I will start adding them in the “modern” way and like you I am not sure when I will get around to migrating the “legacy” stuff over.