Help needed with "template:"

I’ve tried a thousand options and searched the internet but unfortunately can’t find the answer.

I use the frontend theme from Matt8707. To get this work I added the specific part to my configuration.yaml.
Because I want to have my total energy usage for the day I need to sum two sensors. The function works fine but i have a conflict with using “template:” twice in my configuration.yaml. Error: duplicate key

Can anybody help me how to solve this? Thank you in advance!

sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'
      - 'date_time_utc'
      - 'date_time_iso'
      - 'time_date'
      - 'time_utc'
      - 'beat'

# Matt8707 theme
# https://github.com/matt8707/hass-config
frontend: !include themes.yaml
template: !include sidebar.yaml
lovelace:
  mode: yaml
  resources:
    - url: /hacsfiles/button-card/button-card.js
      type: module
    - url: /hacsfiles/lovelace-layout-card/layout-card.js
      type: module
    - url: /hacsfiles/swipe-card/swipe-card.js
      type: module

      #lovelace multiple-entity-row
    - url: /local/multiple-entity-row.js?v=4.4.1
      type: module

#Energy templates:
template:
    - sensor:
      - name: 'Daily Energy Total'
        device_class: energy
        unit_of_measurement: kWh
        state: >
          {% if is_number(states('sensor.elektra_vandaag_dal')) and is_number(states('sensor.elektra_vandaag_hoog')) %}
            {{ states('sensor.elektra_vandaag_dal') | float + states('sensor.elektra_vandaag_hoog') | float }}
          {% else %}
            None
          {% endif %}

The following line indicates all Template entities are configured in a separate file named sidebar.yaml.

template: !include sidebar.yaml

You should put the configuration for sensor.daily_energy_total in that file. Don’t include its first line (containing the template: key word).

1 Like

Thank you for the fast reply. I’ll explore that direction.

If you encounter problems, post the contents of sidebar.yaml.

I added the code to sidebar.yaml but the entity isn’t created. Here the file:

sidebar:
  show_state: false
  show_icon: false
  tap_action:
    action: none
  name: >
    [[[
      if (entity) {
          let attr = [];
          for (let [k, value] of Object.entries(entity.attributes))
              window.navigator.userAgent.match(/iPhone/i)
                  ? k !== 'time' && k !== 'date' && value !== false && (attr += `<p>${k === 'greet' ? `<span class="iphone">${value}</span>` : `${value}`}</p>`)
                  : value !== false && (attr += `<p>${value}</p>`);
          return attr;
      }
    ]]]
  extra_styles: >
    #card {
      padding: 0;
      border-width: 0;
      font-family: SF Pro Display, Roboto, system-ui;
    }
    #container {
      display: flex !important;
    }
    #name {
      padding: 1vw 2.5vw 0 var(--custom-layout-card-padding);
      white-space: normal;
      text-align: left;
    }
    .iphone {
      font-size: 1.5em;
      color: rgba(255, 255, 255, 0.8);
    }
    .time {
      font-size: var(--sidebar-time-font-size);
      font-weight: 300;
      line-height: var(--sidebar-time-line-height);
      letter-spacing: 0.11vw;
      margin-left: -0.3vw;
      color: rgba(255, 255, 255, 0.8);
    }
    @supports not (-moz-appearance:none) {
      .time-colon {
        position: relative;
        top: -.11em;
      }
    }
    p {
      font-size: var(--sidebar-font-size);
      line-height: var(--sidebar-line-height);
      font-weight: 200;
      letter-spacing: 0.07vw;
      color: #6a7377;
    }
    b {
      font-weight: 700;
    }
    p > b {
      color: rgba(255, 255, 255, 0.8);
    }
    /* portrait */
    @media screen and (max-width: 1200px) {
      .time {
        font-size: calc(var(--sidebar-time-font-size) * 1.4 );
        line-height: calc(var(--sidebar-time-line-height) * 1.4 );
      }
      p {
        font-size: calc(var(--sidebar-font-size) * 1.4 );
        line-height: calc(var(--sidebar-line-height) * 1.4 );
      }
    }
    /* phone */
    @media screen and (max-width: 800px) {
      .time {
        font-size: calc(var(--sidebar-time-font-size) * 2.6 );
      }
      p {
        font-size: calc(var(--sidebar-font-size) * 2.6 );
        line-height: calc(var(--sidebar-line-height) * 2.6 );
        letter-spacing: 0.16vw;
      }
      #name {
        padding: 0 0 0 1vw;
      }
    }

sensor:
  - name: 'Daily Energy Total'
    device_class: energy
    unit_of_measurement: kWh
    state: >
      {% if is_number(states('sensor.elektra_vandaag_dal')) and is_number(states('sensor.elektra_vandaag_hoog')) %}
        {{ states('sensor.elektra_vandaag_dal') | float + states('sensor.elektra_vandaag_hoog') | float }}
      {% else %}
        None
      {% endif %}

      

It fails to work because the contents of your sidebar.yaml file don’t comply with what Home Assistant normally uses the template: key for and that’s to configure Template entities.

I think I’ve found it! There were two sidebar.yaml files. Of course I had the wrong one :-). I suppose this format was what you mentioned? Thanks for pointing me in the right direction. Very appreciated.

- sensor:
    - unique_id: sidebar
      state: template
      attributes:
        time: >
          {% set hours = now().strftime('%H') %}
          {% set minutes = now().strftime('%M') %}
          <span class="time">
            {{ hours }}<span class="time-colon">:</span>{{ minutes }}
          </span>
        date: >
          <font color='#6a7377'><b>
          {% if strptime(states('sensor.date'), '%Y-%m-%d').day != None %}
          {% set days = ['Maandag', 'Dinsdag', 'Woensdag', 'Donderdag', 'Vrijdag', 'Zaterdag', 'Zondag'] %}
          {% set months = ['januari', 'februari', 'maart', 'april', 'mei', 'juni',
          'juli', 'augustus', 'september', 'oktober', 'november', 'december'] %}
            {{ days[now().weekday()] }}<br>
            {{ strptime(states('sensor.date'), '%Y-%m-%d').day }} {{ months[now().month-1] }}
          {% endif %}
          </b></font>
        greet: >
          <b>
          {% set time = now().hour %}
          {% if time <= 1 %} Goedenacht {{'\U0001F611'}}
          {% elif time <= 3 %} Goedenacht {{'\U0001F62A'}}
          {% elif time <= 5 %} Goedenacht {{'\U0001F634'}}
          {% elif time <= 7 %} Goedemorgen {{'\U0001F4A9'}}
          {% elif time <= 9 %} Goedemorgen {{'\u2615\uFE0F'}}
          {% elif time <= 10 %} Goedemorgen {{'\U0001F642'}}
          {% elif time <= 13 %} Goedendag {{'\U0001F60A'}}
          {% elif time <= 15 %} Goedendag {{'\U0001F60E'}}
          {% elif time <= 17 %} Goedendag {{'\U0001F44B\U0001F3FB'}}
          {% elif time <= 19 %} Goedenavond {{'\U0001F44B\U0001F3FB'}}
          {% elif time <= 22 %} Goedenavond {{'\U0001F60C'}}
          {% elif time <= 23 %} Goedenavond {{'\U0001F974'}}
          {% else %} Goedenavond {{'\U0001F974'}}
          {% endif %}
          </b>

#Energy templates:
- sensor:
    - name: 'Daily Energy Total'
      device_class: energy
      unit_of_measurement: kWh
      state: >
        {% if is_number(states('sensor.elektra_vandaag_dal')) and is_number(states('sensor.elektra_vandaag_hoog')) %}
          {{ states('sensor.elektra_vandaag_dal') | float + states('sensor.elektra_vandaag_hoog') | float }}
        {% else %}
          None
        {% endif %}

You can safely remove the second instance of this line because it’s not needed:

- sensor:

The first instance permits several sensor configurations to be placed under it.

1 Like