Template: in package... still not possible? or user error

though today I cheered… 2021.8.2 brought us support in packages for template: top level integration. Or so I read the release notes. Did I cheer to soon, or am I mis configuring my templates in this package section:

template:
  - sensor:
      - unique_id: solaredge_lifetime_energy
        name: Lifetime energy
        state: >
          {% if state_attr('sensor.solaredge_overview','lifeTimeData') is not none %}
          {{(state_attr('sensor.solaredge_overview','lifeTimeData').energy
              |float/1000)|round(2)}}
          {% else %} Unknown
          {% endif %}
        <<: &energy
          unit_of_measurement: kWh
          device_class: energy
          state_class: measurement
          icon: mdi:counter
          attributes:
            last_reset: '1970-01-01T00:00:00+00:00'

      - unique_id: solaredge_lastyear_energy
        name: Last year energy
        state: >
          {% if state_attr('sensor.solaredge_overview','lastYearData') is not none %}
          {{(state_attr('sensor.solaredge_overview','lastYearData').energy
               |float/1000)|round(2)}}
          {% else %} Unknown
          {% endif %}
        <<: *energy

      - unique_id: solaredge_lastmonth_energy
        name: Last month energy
        state: >
          {% if state_attr('sensor.solaredge_overview','lastMonthData') is not none %}
          {{(state_attr('sensor.solaredge_overview','lastMonthData').energy
               |float/1000)|round(2)}}
          {% else %} Unknown
          {% endif %}
        <<: *energy

      - unique_id: solaredge_lastday_energy
        name: Last day energy
        state: >
          {% if state_attr('sensor.solaredge_overview','lastDayData') is not none %}
          {{(state_attr('sensor.solaredge_overview','lastDayData').energy
               |float/1000)|round(2)}}
          {% else %} Unknown
          {% endif %}
        <<: *energy

# snip

      - unique_id: solaredge_last_changed
        name: SolarEdge last changed
        state: >
          {% if states.sensor.solaredge_overview is not none %}
          {{relative_time(states.sensor.solaredge_overview.last_changed)}} ago
          {% else %} Not yet initialized
          {% endif %}

only brings the final template to life.

all templates used in the package are valid and producing a correct return in the previous config as value_template, and still in the dev-tools. I see no errors in the config check, nor in the homeassistant.log…

please help me out here?

not sure, had a good look at Petro’s config str home-assistant-config/system.yaml at 7e198e124a40f6ddef96be724b1384aefda91aff · Petro31/home-assistant-config · GitHub

which of course isnt yet in packages, but the hierarchy should be the same

Pretty sure this is correct

EDIT: To clarify, I would expect this is needed for every package, just like automation.

template:
- sensor:
  ...

- binary_sensor:
  ...

tbh, I would expect that package support would mean that the same config we use in regular configuration.yaml, like your template: config in your repo, would be valid in a package. Indeed like automation has always been… Not sure at all why this PR says it supports automation now, they have always been supported.

besides that. the - list indicator is just that, an indicator for a list. If my config doesnt check as a valid list, why wouldn’t that throw an error. Or put differently, why would we need the - indicator for each and every sensor…

I’ll try though and report back

UPDATE

Nope, doesnt work. first tried to C:template reload, nothing. then config/server controls/ Template reload, still nothing. Now a full restart… nothing, only the last template is configured. Maybe the yaml anchors are causing issues?

No it wasn’t that either, it was the fact I had set a unique_id (which was the entity_id of the sensors I had before the migration, hoping not to have to change all that and not have to reconfigure the frontend…)

this seems to work:

template:
  - sensor:
      - #unique_id: solaredge_lifetime_energy
        name: Solaredge Lifetime energy
        state: >
          {% if state_attr('sensor.solaredge_overview','lifeTimeData') is not none %}
          {{(state_attr('sensor.solaredge_overview','lifeTimeData').energy
              |float/1000)|round(2)}}
          {% else %} Unknown
          {% endif %}
        <<: &energy
          unit_of_measurement: kWh
          device_class: energy
          state_class: measurement
          icon: mdi:counter
          attributes:
            last_reset: '1970-01-01T00:00:00+00:00'

      - #unique_id: solaredge_lastyear_energy
        name: Solaredge Last year energy
        state: >
          {% if state_attr('sensor.solaredge_overview','lastYearData') is not none %}
          {{(state_attr('sensor.solaredge_overview','lastYearData').energy
               |float/1000)|round(2)}}
          {% else %} Unknown
          {% endif %}
        <<: *energy

      - #unique_id: solaredge_lastmonth_energy
        name: Solaredge Last month energy
        state: >
          {% if state_attr('sensor.solaredge_overview','lastMonthData') is not none %}
          {{(state_attr('sensor.solaredge_overview','lastMonthData').energy
               |float/1000)|round(2)}}
          {% else %} Unknown
          {% endif %}
        <<: *energy

now how to migrate all template sensors to use the former entity_id, without setting the unique_id on the config of the template:

Maybe by defining the name to correspond to your old entity_id-

For example, change ‘Solaredge Last month energy’ to ‘Solaredge Lastmonth Energy’.

Note: I’ve deleted my previous posts as the given solution doesn’t solve the problem.

yeah that is a thought I considered, but I have many sensors who’s name isnt identical to the unique_id.

Without using the unique_id, the template: integration doesnt automatically set a unique_id by itself, so the entities aren’t UI configurable… seems a catch 22.

but that seems to be the order: first dont use unique_id, let the system create that, next add that to the config, and adapt the name back to what you like…

Just tried with a small sensor that I already had in my packages. The new one is 1:1 to the legacy one:


template:
  - sensor:
      - unique_id: briefkasten
        name: Briefkasten
        icon: mdi:mailbox
        state: "{{ as_timestamp(states.binary_sensor.briefkasten.last_changed) | timestamp_custom('%d.%m., %H:%M Uhr') }}"
        device_class: timestamp
        attributes: 
          friendly_name: Briefkasten

And it works like a charm. So I think for testing proposes you should begin with the barest sensor data and extend the sensor step by step.

it’s alright.

only issues are with sensors using different names from entity_id/unique_id and sensors without names that get the template_ prefix.

Both are easily edited in the UI now :wink:

68 packages left to go…

:boom: :boom:Finally. It was time for it to work in packages !!! But why isn’t it mentioned in the changelog?

It’s in Release 2021.8.2 - August 5 - Packages to support config platforms.

1 Like