Migrating from legacy templates

I am a newbie to Home Assistant and it is unbelievable the support this forum provide to users. I do try to follow guidelines that I can find on the internet but I cannot figure out this error that I am getting migrating to the new template format.

Here are two of my templates and the error I am getting. Any help would greatly be appreciated.

platform: "sensors"
template:
  ### For Basement Humidity
  - binary_sensor:
      - name: "tag_basement_humidity"
        state: >
          {{state_attr('binary_sensor.basement', 'humidity')}}
          {% set humidity_level = state_attr('binary_sensor.basement','humidity') %}
          {% if is_state('binary_sensor.basement', 'off') %}
              off
          {% elif humidity_level > '52.0' %}          
              mdi:water
          {% else %}
              mdi:water-percent-{{humidity_level}}
          {% endif %} 
          unit_of_measurement: "%"

  ### For Basement Temperature
  - binary_sensor:
      - name: "tag_basement_temperature_new"
        state: >
          {{state_attr('binary_sensor.basement', 'temperature')}}
          {% set temperature_level = state_attr('binary_sensor.basement','temperature') %}
          {% if temperature_level < '85.0' %}
            mdi:thermometer
          {% else %}
            mdi:thermometer-{{temperature_level}}
          {% endif %}
          unit_of_measurement: "°f"

Platform error sensor.sensors - Integration ‘sensors’ not found.

You are mixing new and old by the looks of it, see the documentation as follows:

template:
  - sensor:
      - name: "Average temperature"
        unit_of_measurement: "°C"
        state: >

Also your unit of measurement is tabbed out one to many:

Thank you so much for your response. I fixed the indentation error. However, when I change to the new format as follows below, I get - missing property “platform” - but I don’t know how to fix without going back to the old format.

Thanks so much for your guidance.

template:
  - sensor:
  ### For Basement Humidity
      - name: "tag_basement_humidity"
        state: >

It sounds to me like you are still trying to mix up the two configs.

the old way:

sensor:
  - platform: template
    binary_sensors:
      some_template_sensor:
        friendly_name: ......
        value_template: "{{the template}}"
      some_other_template_sensor:
        friendly_name: ......
        value_template: "{{ the other template }}"

the new way:

template:
  - binary_sensor:
      - name: some template sensor
        state: "{{ the template }}"
      - name: some other template sensor
        state: "{{ the other template }}"

notice in the new way there is no main “sensor:” entry at the top. It starts with “template:” instead.

and there is no longer the need for “platform:” since the template sensors are now in their own domain (template) instead of under the sensor domain.

finity

Thank you for your guidance. Maybe I just don’t understand templates which is obvious. Below is all the code that gives me the platform error. I see in your example, you have in the state line - the template . is this something different than entity that I am using. If so, where do I find “the template and the other template”. To be honest I am totally confused.

template:
  - binary_sensor:
  ### For Basement Humidity
      - name: "tag_basement_humidity"
        state: >
          {{state_attr('binary_sensor.basement', 'humidity')}}
          {% set humidity_level = state_attr('binary_sensor.basement','humidity') %}
          {% if is_state('binary_sensor.basement', 'off') %}
              off
          {% elif humidity_level > '52.0' %}          
              mdi:water
          {% else %}
              mdi:water-percent-{{humidity_level}}
          {% endif %} 
        unit_of_measurement: "%"

  ### For Basement Temperature
      - name: "tag_basement_temperature_new"
        state: >
          {{state_attr('binary_sensor.basement', 'temperature')}}
          {% set temperature_level = state_attr('binary_sensor.basement','temperature') %}
          {% if temperature_level < '85.0' %}
            mdi:thermometer
          {% else %}
            mdi:thermometer-{{temperature_level}}
          {% endif %}
        unit_of_measurement: "°f"

What’s your version of Home Assistant?

Are you aware the state templates you’ve created are invalid for a binary_sensor? A binary_sensor’s state template should simply report true/false, not an assortment of alphanumeric values and icon names. Perhaps you have mistakenly combined a state template with an icon template?

Taras

I am using 4.7 version downloaded at the end of April 2022. I assumed I could get to the attributes of the sensor by using state_attr. That is how I have been getting data from the sensor with the legacy format.

Is that not true for the new template version?

I appreciate you interest in trying to help me out.

Yes, state_attr is the correct function for getting an attribute’s value. However why would you try to use a humidity value (which is a typically a numeric value) to represent a binary_sensor’s state? In addition to the state_attr function, the template continues with an if-else that reports additional information, none of it appropriate for a binary_sensor’s state (which is simply on/off).

Taras

I hate to say this but I am a hacker. What I mean by this is I watched many utube videos to get me started. I realize now that some of them are old which resulted in my use of the legacy templates.
I am new to Home Assistant and I was looking for something that I could use to quickly give me the various values from of some wireless sensors that I have reporting to an ISY 994i home automation controller. I wanted a dashboard that with a quick visual, I could see with colored icons, battery, temperature and humidity values.

When I realized I was using the legacy templates, I thought I should convert to the new format but I guess I just don’t understand enough to do that. What I have now works just fine for my purposes but I did not want to get too far behind if I decided to use more of the functions of Home Assistant. I am retired and not a programmer. My programming skills are FORTRAN and COBOL which are not even used anymore.

The templates you posted above wouldn’t work correctly for the value_template of a Template Binary Sensor in legacy format.

Taras

I do appreciate your help, but I just realized I am in way over my head and I hate to admit it, but I cannot follow what you are telling me. I have been using mushroom cards for what I am doing and so far, right or wrong, I am getting the information I need.

To be honest, I may just have to live with what I have and continue to get my day to day notifications from the ISY.

I don’t want to take up anymore of you time. I thank you for the time you have given me so far.

Your trouble is not unsolvable, it is just more than a problem with converting old templates to new. It seems like you are building a sensor from scratch, and missing some basic concepts.

First is the difference between a binary_sensor and a sensor. Binary means 2, so a binary sensor can just have two values. The value template must be True or False. Depending on the device_class for that sensor it may show differently in the user interface (on/off, open/closed, …) but basically it must always answer a yes or no question. Is the light on? Is the door open?

You seem to want a humidity, which should be a number. That cannot be a binary_sensor, but must be a sensor. Sensors can have different types of values, numerical being the most used. You specify a unit_of_measurement that is in %. That makes sense for relative humidity. But as taras said, the value you try to give it is both something binary (on), or an icon name (mdi:water) or an icon name that is composed of the name and the humidity. But that would require icons for all levels, that do not exist. Percentages are numbers between 0 and 100.

It looks like your template was put together of parts that could make sense for defining a sensor, but missing the parts to explain what is what. The state should represent a number. You can also specify icons, by specifying the right field, in this case icon_template. What fields exist and how to use them can be found in the documentation:

Based on what you stated, you appear to have created Template Sensors using the legacy format and you use Mushroom cards to display their values.

Post the configuration of your Template Sensors in their original legacy format. It’ll be easier for us to convert them for you as opposed to debugging what you posted above.

And to add…

there is really no reason (especially now) to migrate from the “legacy” format to the “modern” format.

You won’t be losing out on anything at all by sticking with the older format and as of now there is no plans that I have heard about to get rid of the old format.

Just try to figure out how templates in general work and clean up the ones you have and move on from there.

Sorry those were just example placeholders for your actual templates. They have zero significance except for illustrative purposes.

What happend here? For groups the new format looks like the old format in HA and vice versa?!?

Where do you see a new yaml format here? The new format is edit in the UI, the format described here is the old.

I am talking about the pre-ui yaml groups.
But I am just realizing that there were way more changes to the templates.

The one thing that sticked out was the usage of domain / platform: that was present in the legacy templates and the “newer” pre-ui groups.

pre-ui YAML groups                            | Older yaml groups
                                              |
binary_sensor:                                | group:
  - platform: group                           |   name: "Patio Doors"
    name: "Patio Doors"                       |   entities:
    device_class: opening                     |     - binary_sensor.door_left_contact
    entities:                                 |     - binary_sensor.door_right_contact
      - binary_sensor.door_left_contact
      - binary_sensor.door_right_contact



New Templates                                  | Old templates
                                               |
template:                                      | binary_sensor:
  - binary_sensor:                             |   - platform: template
      - name: "Washing Machine"                |     friendly_name: xxx
        [...]                                  |     sensors:
											   |       - [...]

Taras

Thank you for not giving up on me as I had on myself. Here is the legacy templates and below it is the code for the dashboard for them. I am only posting two of them since if I can get this to work, I can convert the remaining 11.

As I mentioned previously, I continued to hack stuff together until it works for me. I would guess there are probably much better and cleaner ways to do this.

### For Basement Humidity
- platform: template
  sensors:
    tag_basement_humidity:
      friendly_name: basement humidity new
      value_template: >-
        {{ state_attr('binary_sensor.basement','humidity') }}
      icon_template: >
        {% set humidity_level = state_attr('binary_sensor.basement','humidity') %}
        {% if humidity_level > '52.0' %}
         mdi:water
        {% else %}
        mdi:water-{{humidity_level}}
        {% endif %}
      unit_of_measurement: "%"

### For Basement Temperature
- platform: template
  sensors:
    tag_basement_temperature_new:
      friendly_name: basement temperature new
      value_template: >-
        {{ state_attr('binary_sensor.basement','temperature') }}
      icon_template: >
        {% set temperature_level = state_attr('binary_sensor.basement','temperature') %}
        {% if temperature_level < '85.0' %}
         mdi:thermometer
        {% else %}
        mdi:thermometer-{{temperature_level}}
        {% endif %}
      unit_of_measurement: "°f"
type: custom:mushroom-template-card
entity: binary_sensor.basement
icon: |-
  {% if state_attr('binary_sensor.basement','humidity')  > '52.0' %}
          mdi:water-percent
        {% else %}
          mdi:water-outline
        {% endif %}
icon_color: |-
  {% if state_attr('binary_sensor.basement','humidity')  > '52.0' %}
          red
  {% elif state_attr('binary_sensor.basement','humidity')  > '50.0' %}
          yellow
  {% else %}
          green
  {% endif %}
primary: |-
  {% if state_attr('binary_sensor.basement','humidity')  > '52.0' %}
          Basement Humidity High - Above 52% - Turn on Dehumidifer
  {% elif state_attr('binary_sensor.basement','humidity')  > '50.0' %}
          Basement Humidity Caution - Above 50%             
  {% else %}
          Basement Humidity - Below 52%
  {% endif %}
multiline_secondary: false
fill_container: false
secondary: Current Humidity is {{ state_attr('binary_sensor.basement','humidity') }}

type: custom:mushroom-template-card
entity: binary_sensor.basement
icon: |-
  {% if state_attr('binary_sensor.basement','temperature')  > '52.0' %}
          mdi:thermometer
        {% else %}
          mdi:thermometer
        {% endif %}
icon_color: |-
  {% if state_attr('binary_sensor.basement','temperature')  > '52.0' %}
          green
  {% elif state_attr('binary_sensor.basement','temperature')  > '50.0' %}
          orange
  {% else %}
          red
  {% endif %}
primary: |-
  {% if state_attr('binary_sensor.basement','temperature')  > '52.0' %}
          Basement Temp OK > 45°f
  {% elif state_attr('binary_sensor.basement','temperature')  > '50.0' %}
          Basement Temp Getting Cold < 45°f          
  {% else %}
          Basement Temp Cold < 40°f
  {% endif %}
multiline_secondary: false
fill_container: false
secondary: >-
  Current Temperature is {{ state_attr('binary_sensor.basement','temperature')
  }}

You have two Template Sensors where one reports humidity and the other reports temperature. Both get their values from the same entity, binary_sensor.basement.

However, you don’t appear to use the two Template Sensors in the Mushroom card. The card gets humidity and temperature directly from binary_sensor.basement.

So what’s the purpose for creating the two Template Sensors? Are they used anywhere else?

Taras

Yes I am using the second sensor somewhere else with a different card. Maybe reading between the lines but are you saying I don’t need the templates to do what I am doing with the mushroom template card.