Migrating to new template integration from legacy, can someone verify?

I have 3 old templates that are completely unlike each other. I tried my best to convert/consolidate them to the new template integration. Can someone please confirm the new consolidated templates are effectively identical to the legacy? Once I know these are functionally identical to each other (including their respective names), I can proceed with migrating all my other similar templates.

Legacy templates:

sensor:
  - platform: template
    sensors:
      horizon_c_drive_read_bytes:
        value_template: >-
                {% set read_byte = state_attr('sensor.diskio_0', 'read_bytes') %}
                {% set time_since = state_attr('sensor.diskio_0', 'time_since_update') %}
                {% if (read_byte != None ) and (time_since != None ) %}
                  {{ ((read_byte | int) / (time_since | int) / 1000000) | round (2) }}
                {% endif %}
        availability_template: >-
                {{ states("sensor.diskio_0") not in ["unknown", "unavailable", "none"] }}
        unit_of_measurement: 'MB/s'


sensor:
  - platform: template
    sensors:
      tautulli_total_bandwidth:
        friendly_name: 'Total bandwidth'
        value_template: "{{ (states('sensor.total_bandwidth') | int / 1000) | round(2) }}"
        availability_template: >-
          {{ states("sensor.total_bandwidth") not in ["unknown", "unavailable", "none"] }}
        unit_of_measurement: "Mbps"


binary_sensor:
  - platform: template
    sensors:
      plex_plex_for_android_mobile_galaxy_note9_is_visible:
        value_template: >
          {{ states('media_player.plex_plex_for_android_mobile_galaxy_note9') in ['on', 'playing', 'paused'] }}

New template integration: (consolidated)

template:
  - sensors:
      - name: "horizon c drive read bytes"
        state: >
          {% set read_byte = state_attr('sensor.diskio_0', 'read_bytes') %}
          {% set time_since = state_attr('sensor.diskio_0', 'time_since_update') %}
          {% if (read_byte != None ) and (time_since != None ) %}
            {{ ((read_byte | int) / (time_since | int) / 1000000) | round (2) }}
          {% endif %}
        availability: "{{ states('sensor.diskio_0') not in ['unknown', 'unavailable', 'none'] }}"
        unit_of_measurement: "MB/s"

      - name: "tautulli total bandwidth"
        state: "{{ (states('sensor.total_bandwidth') | int / 1000) | round(2) }}"
        availability: "{{ states('sensor.total_bandwidth') not in ['unknown', 'unavailable', 'none'] }}"
        unit_of_measurement: "GB"

  - binary_sensor:
        - name: "plex plex for android mobile galaxy note9 is visible"
          state: "{{ states('media_player.plex_plex_for_android_mobile_galaxy_note9') in ['on', 'playing', 'paused'] }}"

For example, I’m not even sure which binary sensor below uses the correct format (to function identically to my old-format respective binary sensor). If they are both correct, maybe someone could suggest which way would be more common/preferred:

  - binary_sensor:
      - name: "plex plex for android mobile galaxy note9 is visible"
        state: "{{ states('media_player.plex_plex_for_android_mobile_galaxy_note9') in ['on', 'playing', 'paused'] }}"

  - binary_sensor:
      - name: "plex plex for android mobile galaxy note9 is visible"
        state: >
          {{ states('media_player.plex_plex_for_android_mobile_galaxy_note9') in ['on', 'playing', 'paused'] }}"

…also, I’m not sure if I’m using parenthesis correctly in the line below:

state: "{{ (states('sensor.total_bandwidth') | int / 1000) | round(2) }}"

The multi-line indicators, > or |, are normally only used if your template is long or you want to break it up visually. They are not necessary. If you do want to use them I would recommend getting in the habit of using |… I’ve seen some issues with > being overwritten to quotation marks when used in the UI editors. This isn’t usually a problem, but occasionally it will cause errors that can be frustrating to figure out.

Your parentheses are fine.

For what it’s worth, you can simplify your availability templates by using has_value('sensor.example').

1 Like

Thank you for explaining. That helped a lot.

Could you please give me a full example of how to use has_value('sensor.example')? …Assuming it is replacing:

 availability: "{{ states('sensor.total_bandwidth') not in ['unknown', 'unavailable', 'none'] }}"

Do I just do this?

availability: "{{ has_value('sensor.total_bandwidth') }}"

Either should work:

availability: "{{ has_value('sensor.total_bandwidth') }}"

availability: "{{ 'sensor.total_bandwidth' | has_value }}"

1 Like