core: Trigger based template error in Package: Component template can not be merged

The problem

building sensors using the new trigger template component, the above error is thrown, even on a single sensor

What is version of Home Assistant Core has the issue?

2021.4.4

What was the last working version of Home Assistant Core?

x

What type of installation are you running?

Home Assistant OS

Integration causing the issue

template

Link to integration documentation on our website

https://www.home-assistant.io/integrations/template/#configuration-for-trigger-based-template-sensors

Example YAML snippet

template:
  - trigger:
      - platform: time
        at: '00:00:00'
    sensor:
      - name: Afzuigkap zolder power daystart
        state: >
          {{states('sensor.afzuigkap_zolder_actueel')|float}}
        unit_of_measurement: W

Anything in the logs that might be useful for us?

021-04-14 17:02:03 ERROR (MainThread) [homeassistant.config] Package package_zwave setup failed. Integration template cannot be merged. Expected a dict. (See /config/packages/package_zwave.yaml:6).
Schermafbeelding 2021-04-14 om 16 35 52

the reference to ;line 6 in the package doesn’t make sense, the component doest get configured there at all. If I take out this little snippet in the package, config checks ok.

as posted here Ive also tried this with a single line template and all fields quoted. Same error is thrown. to be 100% sure, taking this snippet to a single dedicated package with only that as contents yields the same error, albeit with reference to line 0.

edit just found this https://community.home-assistant.io/t/trigger-based-template-sensors-in-configuration-package-not-working/297693/3

is it the fact the config is in a package?

edit2

yes it is, adding all sensors in the configuration.yaml makes the config check ok. Adjusting title to reflect this bug.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 8
  • Comments: 18 (15 by maintainers)

Most upvoted comments

Ok I figured out what’s going on. Within the function it uses for merging things configured in packages into the main config it looks at the component to see whether its schema is a list or a dictionary here: https://github.com/home-assistant/core/blob/dev/homeassistant/config.py#L722

If the component contains PLATFORM_SCHEMA (like sensor does) then it assumes the config is a list and merges it as a list. If the component does not contain PLATFORM_SCHEMA (template does not) then it looks for CONFIG_SCHEMA and runs it through this logic here to try and figure out if its a dictionary or a list: https://github.com/home-assistant/core/blob/dev/homeassistant/config.py#L627

This doesn’t work with template though because CONFIG_SCHEMA doesn’t exist here: https://github.com/home-assistant/core/blob/dev/homeassistant/components/template/__init__.py

Instead the template component passes the pieces to the respective platforms within it. So sensors are validated with SENSOR_SCHEMA here and binary sensor are validated with BINARY_SENSOR_SCHEMA here. Since it didn’t find a CONFIG_SCHEMA it assumes it is a dictionary and returns an exception when it finds a list instead here.

So I think this is an issue and should be re-opened. This isn’t a use case that was removed by design, the package loading process has logic in there to handle merging in configs which are dictionaries as well as merging in configs that are lists. The problem is that its not able to identify right now that template is a list-based config. So either template should be adjusted to present its schema the way the package loading process wants it (using a CONFIG_SCHEMA) or the package loading process should be adjusted to recognize this new situation if this is the way configuration should be going forward.

# a package for this test
package_test:

  template: 

    sensor:  
    - name: sensor1
      state: "{{ ...}}"
    - name: sensor2
      state: "{{ ... }}"

    binary_sensor:
    - name: binary_sensor1
      state: "{{ ... }}"

… and so on.

Package files are included in configuration.yaml like below. All configuration yaml files are in a directory packages.

  packages: !include_dir_merge_named packages/

It would be great if templates could be used in packages.