Before the possibility to move the new format into packages I played with template.yaml, too, but I used template: !include_dir_merge_list template
I have a flat file structure. I don’t like hunting through directories.
Now I cut everything out of the file except Taras’s triggered template. Same error.
I’m still on 2021.7.x
Is this only supported in 2021.8.x?
Your template configuration is nearly identical to mine (except I have a few Trigger-based ones at the beginning of the file). I don’t get that error message.
The message refers to sensors (plural) but that’s not the option’s name (it’s sensor
). Is it possible you have tradional sensors defined elsewhere?
Ah. I bet that’s it.
I commented them out as I went along, I’m guessing I missed one.
EDIT: Yes it was. Thanks Taras.
# - platform: template
# sensors:
# 4g_internet_out_mbps:
# value_template: "{{ (states('sensor.erx_4g_wan_out_derivative')|float*8/1000000)|round(2) }}"
# unit_of_measurement: 'Mbps'
# friendly_name: '4G WAN Out'
# erx_4g_wan_out_mb:
# value_template: "{{ (states('sensor.erx_4g_wan_out')|float/1048576)|round(2) }}"
# unit_of_measurement: 'MB'
# friendly_name: '4G Uploaded'
- platform: template
sensors:
# used_gb:
# friendly_name: "NBN Used"
# value_template: "{{ ( states('sensor.nbn_downloaded')|float + states('sensor.nbn_uploaded')|float )|round(2) }}"
# unit_of_measurement: "GB"
# used_gb_pct:
# friendly_name: "Data Used"
# value_template: "{{ (100 * states('sensor.used_gb')|float / 500 )|round(1) }}"
# unit_of_measurement: "%"
Now lets restart and see what I’ve stuffed up somewhere in 8 hours work…
EDIT2: I don’t believe it. That was my only mistake!
…though I would like three files in /config.
template_triggered_sensors.yaml
template_binary_sensors.yaml
template_sensors.yaml
Will this work (can I nest includes)?
configuration.yaml:
template: !include template_triggered_sensors.yaml
template_triggered_sensors.yaml
#################################################################
## Triggered Binary Sensors and Sensors
#################################################################
########### Binary Sensors
- trigger: # Dummy triggered sensor for compatibility
- platform: time
at: '00:00:00'
binary_sensor:
- name: 'Festive Season'
state: >
{% set m = now().month %}
{% set d = now().day %}
{{ m in [1,10,12] and
( (m == 12 and 1 <= d <= 31) or
(m == 1 and 1 <= d <= 10) or
(m == 10 and d == 31) ) }}
########### Sensors
- trigger: ...etc
- binary_sensor: !include template_binary_sensors.yaml
- sensor: !include: template_sensors.yaml
For my setup, I use-
- Inside
/config/configuration.yaml
-
homeassistant:
packages: !include_dir_named integrations
- Inside
/config/integrations/template.yaml
-
template:
- sensor: !include_dir_list ../entities/templates/sensors
- binary_sensor: !include_dir_list ../entities/templates/binary_sensors
For now, all sensors are showing up fine.
Just a suggestion (although different with what you intended), you can make it-
template:
- sensor: !include template_sensors.yaml
- binary_sensor: !include template_binary_sensors.yaml
- template_trigger: !include template_triggered_sensors.yaml
Ok, sounds promising. I’ll give it a go.
Sorry, just realized that you want to store all 3 template.yaml inside /config
, not /config/template
. Probably you’ll need to change it to just sensor: !include template_sensors.yaml
.
Furthermore, from the documentation, it is written-
Nesting !include
s (having an !include
within a file that is itself !include
d) isn’t going to work.
Yeah I was way ahead of you
template:
- sensor: !include template_sensors.yaml
- binary_sensor: !include template_binary_sensors.yaml
- template_trigger: !include template_triggered_sensors.yaml
I’m a little concerned about that last one though. It does not seem to follow the documentation for template triggers. https://www.home-assistant.io/integrations/template/#trigger-based-template-sensors
it needs to be -trigger:
not template_trigger:
and each triggered sensor/binary sensor has to start with -trigger:
EDIT: ayup,
Logger: homeassistant.config
Source: config.py:445
First occurred: 01:13:43 (1 occurrences)
Last logged: 01:13:43
Invalid config for [template]: [template_trigger] is an invalid option for [template]. Check: template->template_trigger. (See /config/configuration.yaml, line 162).
I don’t see any way to do the template triggered sensors.
My mistake
However if inside configuration.yaml
-
template:
- sensor: !include template_sensors.yaml
- binary_sensor: !include template_binary_sensors.yaml
- trigger: !include template_triggered_sensors.yaml
Then, inside template_triggered_sensors.yaml
, you write-
- trigger:
- platform: time_pattern
hours: 0
minutes: 0
sensor:
- name: Not smoking
state: ""
unit_of_measurement: "Days"
Wouldn’t at the end it would be like-
template:
- trigger:
- trigger:
- platform: xx
...
Yep. That’s the issue I can’t see a way around.
What if you add 2 lines of template:
template: !include template_triggered_sensors.yaml
template:
- sensor: !include template_sensors.yaml
- binary_sensor: !include template_binary_sensors.yaml
No error from Check Configuration but I’m afraid one of them will be ignored.
Pretty sure that will generate a duplicate template:
key error.
I don’t actually use triggered sensors as I find automations that publish retained mqtt messages and mqtt sensors initialise better after a restart. So I might just skip them.
In the off chance that I ever do need one I can bung it in configuration.yaml.
Tested on mine.
- Inside
/config/integrations/template.yaml
-
template:
- sensor: !include_dir_list ../entities/templates/sensors
- binary_sensor: !include_dir_list ../entities/templates/binary_sensors
- Inside
/config/integrations/test.yaml
-
template: !include_dir_list ../entities/templates/trigger_template
- Inside
/config/entities/templates/trigger_template/justatest.yaml
-
trigger:
- platform: time_pattern
# This will update every night
hours: 0
minutes: 0
sensor:
# Keep track how many days have past since a date
- name: Test
state: 'unavailable'
unit_of_measurement: "Days"
All sensors shows up fine including the trigger template.
That’s not a flat directory structure though. Which was the aim. All in /config.
Oh! I think this will work:
template:
- sensor: !include template_sensors.yaml
- binary_sensor: !include template_binary_sensors.yaml
- !include template_triggered_sensors.yaml
I could even have:
template:
- sensor: !include template_sensors.yaml
- binary_sensor: !include template_binary_sensors.yaml
- !include template_triggered_sensors.yaml
- !include template_triggered_binary_sensors.yaml
Just an update-
template:
- sensor: !include_dir_list ../entities/templates/sensors
- binary_sensor: !include_dir_list ../entities/templates/binary_sensors
- !include_dir_list ../entities/templates/template_trigger
The above template.yaml
passes config check. However, when inside `/config/entities/templates/template_trigger/justatest.yaml, I insert-
trigger:
- platform: time_pattern
# This will update every night
hours: 0
minutes: 0
sensor:
# Keep track how many days have past since a date
- name: This is just a test
state: ""
unit_of_measurement: "Days"
Resulting in-
Invalid config for [template]: expected a dictionary. Got [OrderedDict([('trigger', [OrderedDict([('platform', 'time_pattern'), ('hours', 0), ('minutes', 0)])]), ('sensor', [OrderedDict([('name', 'This is just a test'), ('state', ''), ('unit_of_measurement', 'Days')])])])]. (See ?, line ?).
I don’t know with yours, but it does not work with packages like mine.
Maybe because at the end it will turn out like-
template:
-
- trigger:
- platform: xx
...
I have this working at the moment:
template:
- sensor: !include template_sensors.yaml
- binary_sensor: !include template_binary_sensors.yaml
# - !include template_triggered_sensors.yaml
# - !include template_triggered_binary_sensors.yaml
If I change it to:
template:
- sensor: !include template_sensors.yaml
- binary_sensor: !include template_binary_sensors.yaml
# - !include template_triggered_sensors.yaml
- !include template_triggered_binary_sensors.yaml
And include Taras’s triggered binary sensor from above, it passes the config check. fails config check.
Damn. back to square one.
Though as I said, in the unlikely case that I ever need a triggered sensor I can put it in my configuration.yaml file.
I just tried the following and got no validation errors:
configuration.yaml:
template: !include template_sensors.yaml
template binary: !include template_binary_sensors.yaml
template triggered: !include template_triggered_sensors.yaml
template_sensors.yaml:
- sensor:
##...
template_binary_sensors.yaml:
- binary_sensor:
#...
template_triggered_sensors.yaml:
- trigger:
##...
sensor:
##...
binary_sensor:
##...
the only caveat is that those are exactly the way the files are written. They don’t have any actual sensor code in there at all, just the headings.
But it did pass the config check.
I assume it’s just like having multiple “automation:” or “script:” by adding another label after it to differentiate the two files/code. (“automation old:” or “script second:”)