If you are desperate to generate your YAML configuration, I actually had a use case and use gomplate for this.
I didn’t document this, nor did I intend to do so, but in a nutshell:
- Have a GO template (Using Go Templates | Gopher Academy Blog)
- Have a JSON describin the entities
- Run gomplate (when launching HA in my case) to generate YAML files
- include those YAML through
include_dir_merge_list
Examples:
Template
## gomplate template
## install:
## go install github.com/hairyhenderson/gomplate/v4/cmd/gomplate@latest
## docker:
## docker run -v .:/config hairyhenderson/gomplate:stable -d 'data=file:///config/gomplate/templates_calendar.json' -f /config/gomplate/templates_calendar.tmpl -o /config/platforms/templates/templates_calendar.yaml
## run:
## cat gomplate/templates_calendar.json | gomplate -d 'data=stdin:?type=application/array%2Bjson' -f gomplate/templates_calendar.tmpl -o platforms/templates/templates_calendar.yaml
- sensor:
{{- range $c := (ds "data") }}
- name: "{{ $c.name }}"
unique_id: "{{ $c.uid }}"
state: >
{% from 'next_cal.jinja' import next_cal_state %}
{{ "{{" }} next_cal_state([ {{ $c.calendars }} ]) {{ "}}" }}
attributes:
all_day: >
{% from 'next_cal.jinja' import next_cal_allday %}
{{ "{{" }} next_cal_allday([ {{ $c.calendars }} ]) {{ "}}" }}
start_time: >
{% from 'next_cal.jinja' import next_cal_startdate %}
{{ "{{" }} next_cal_startdate([ {{ $c.calendars }} ]) {{ "}}" }}
end_time: >
{% from 'next_cal.jinja' import next_cal_enddate %}
{{ "{{" }} next_cal_enddate([ {{ $c.calendars }} ]) {{ "}}" }}
{{- end }}
JSON
[
{
"name": "Next calendar event: Chris",
"uid": "next_calendar_event_chris",
"calendars": "states.calendar.default_calendar, states.calendar.business, states.calendar.famille"
},
{
"name": "Next calendar event: Chouchou pour Chris",
"uid": "next_calendar_event_chouchou_for_chris",
"calendars": "states.calendar.chouchou"
},
{
"name": "Next calendar event: Chouchou",
"uid": "next_calendar_event_chouchou",
"calendars": "states.calendar.chouchou, states.calendar.famille"
},
{
"name": "Next calendar event: Birthdays",
"uid": "next_calendar_birthday",
"calendars": "states.calendar.contact_birthdays"
}
]
Command (in docker-compose.yml
)
gomplate_tmpl_calendars:
image: hairyhenderson/gomplate:stable
volumes:
- ${HA_PATH}/config:/config
command: -d 'data=file:///config/gomplate/templates_calendar.json' -f /config/gomplate/templates_calendar.tmpl -o /config/platforms/templates/gmp_calendars.yaml
Generated
## gomplate template
## install:
## go install github.com/hairyhenderson/gomplate/v4/cmd/gomplate@latest
## docker:
## docker run -v .:/config hairyhenderson/gomplate:stable -d 'data=file:///config/gomplate/templates_calendar.json' -f /config/gomplate/templates_calendar.tmpl -o /config/platforms/templates/templates_calendar.yaml
## run:
## cat gomplate/templates_calendar.json | gomplate -d 'data=stdin:?type=application/array%2Bjson' -f gomplate/templates_calendar.tmpl -o platforms/templates/templates_calendar.yaml
- sensor:
- name: "Next calendar event: Chris"
unique_id: "next_calendar_event_chris"
state: >
{% from 'next_cal.jinja' import next_cal_state %}
{{ next_cal_state([ states.calendar.default_calendar, states.calendar.business, states.calendar.famille ]) }}
attributes:
all_day: >
{% from 'next_cal.jinja' import next_cal_allday %}
{{ next_cal_allday([ states.calendar.default_calendar, states.calendar.business, states.calendar.famille ]) }}
start_time: >
{% from 'next_cal.jinja' import next_cal_startdate %}
{{ next_cal_startdate([ states.calendar.default_calendar, states.calendar.business, states.calendar.famille ]) }}
end_time: >
{% from 'next_cal.jinja' import next_cal_enddate %}
{{ next_cal_enddate([ states.calendar.default_calendar, states.calendar.business, states.calendar.famille ]) }}
- name: "Next calendar event: Chouchou pour Chris"
unique_id: "next_calendar_event_chouchou_for_chris"
state: >
{% from 'next_cal.jinja' import next_cal_state %}
{{ next_cal_state([ states.calendar.chouchou ]) }}
attributes:
all_day: >
{% from 'next_cal.jinja' import next_cal_allday %}
{{ next_cal_allday([ states.calendar.chouchou ]) }}
start_time: >
{% from 'next_cal.jinja' import next_cal_startdate %}
{{ next_cal_startdate([ states.calendar.chouchou ]) }}
end_time: >
{% from 'next_cal.jinja' import next_cal_enddate %}
{{ next_cal_enddate([ states.calendar.chouchou ]) }}
- name: "Next calendar event: Chouchou"
unique_id: "next_calendar_event_chouchou"
state: >
{% from 'next_cal.jinja' import next_cal_state %}
{{ next_cal_state([ states.calendar.chouchou, states.calendar.famille ]) }}
attributes:
all_day: >
{% from 'next_cal.jinja' import next_cal_allday %}
{{ next_cal_allday([ states.calendar.chouchou, states.calendar.famille ]) }}
start_time: >
{% from 'next_cal.jinja' import next_cal_startdate %}
{{ next_cal_startdate([ states.calendar.chouchou, states.calendar.famille ]) }}
end_time: >
{% from 'next_cal.jinja' import next_cal_enddate %}
{{ next_cal_enddate([ states.calendar.chouchou, states.calendar.famille ]) }}
- name: "Next calendar event: Birthdays"
unique_id: "next_calendar_birthday"
state: >
{% from 'next_cal.jinja' import next_cal_state %}
{{ next_cal_state([ states.calendar.contact_birthdays ]) }}
attributes:
all_day: >
{% from 'next_cal.jinja' import next_cal_allday %}
{{ next_cal_allday([ states.calendar.contact_birthdays ]) }}
start_time: >
{% from 'next_cal.jinja' import next_cal_startdate %}
{{ next_cal_startdate([ states.calendar.contact_birthdays ]) }}
end_time: >
{% from 'next_cal.jinja' import next_cal_enddate %}
{{ next_cal_enddate([ states.calendar.contact_birthdays ]) }}