Splitting config for template:

Hi All,

I was struggling also with trigger templates and slit config. This is how I solved it:
only a error in VSC with the !include but all sensors, binary_sensors and triggers working.

Incorrect type. Expected: "object".

template:
  - sensor: !include integrations/template_sensor.yaml
  - binary_sensor: !include integrations/template_binary_sensor.yaml
  - !include_dir_merge_named integrations/template_trigger.yaml

/template_sensor.yaml

---
- name: "Group Family Manual"
  unique_id: "3e43a954-abc1-447a-af7c-8f789ba73f2f"
  state: "{{ is_state('input_boolean.helpers_kay_home_homekit', 'on') or is_state('input_boolean.helpers_peter_home_homekit', 'on') }}"
  device_class: presence
  delay_off: "00:15:00"

etc, etc

/template_binary_sensor.yaml

---
- name: "Asus devices offline"
  state: "{{ integration_entities('asusrouter') | select('is_state', 'not_home') | list | count > 0 | float(false) }}"
  unique_id: "42a19e1a-9396-42a9-81a1-94e8ee74fa5d"
  attributes:
    entity_id: "{{ integration_entities('asusrouter') | select('is_state', 'not_home') | list }}"

etc, etc

/template_trigger.yaml

---
trigger:
  - trigger:
      - platform: event
        event_type: "imap_content"
        id: "custom_event"
        event_data:
          sender: !secret imap_sender_2
    sensor:
      - name: "Imap import DW NS"
        state: >-
          {% if 'DiSys' in trigger.event.data["subject"] %}
            Nieuwe DW
          {% endif %}
        attributes:
          Message: "{{ trigger.event.data['text'] }}"
          Date: "{{ trigger.event.data['date'] }}"

etc, etc

This route will cause you issues. Instead use this:

  1. create folder in integrations folder named templates

  2. add this to configuration.yaml

    template: !include_dir_merge_list integrations/templates
    
  3. Use this format in your files, you can even combine or separate your sensors/binary_sensors/trigger based. Name the file whatever you want, the name won’t matter. You can organize your files based on what it’s doing.

    - binary_sensor:
      - name: "Group Family Manual"
        unique_id: "3e43a954-abc1-447a-af7c-8f789ba73f2f"
        state: "{{ is_state('input_boolean.helpers_kay_home_homekit', 'on') or is_state('input_boolean.helpers_peter_home_homekit', 'on') }}"
        device_class: presence
        delay_off: "00:15:00"
    
      ... etc
    
    - sensor:
      - name: ...
    

If you continue with your current method, you’ll need to make a separate include for every template entity type. You’ll have to maintain a correct name, and there will be no organization to your filenames. They will be rigid.

This is what my folder looks like, it tells me exactly what each file does.

image

1 Like

…or you can go a bit further and use a “task-level” splitting.

Thanks. What will go wrong. Because everything is working.

I will check the other solution you mention. thanks

when you add anything new, like another trigger sensor. Merge dir named won’t work for that. Not sure how it’s even working for you now.

I don’t know as well. But better to do it the right way.

thank you for sharing
i changed my setup as you explained
helps me a lot

Hi everyone, first comment in Home Assistant forum.

I’ve been following this guide but I cant manage to get it work:

This is my configuration.yaml:

template: 
  - sensor: !include template_sensors.yaml

My template_sensors.yaml:

- name: "Electricity Price"
  unit_of_measurement: EUR/kWh
  state: >
        {% set current_hour = now().hour %}
        {% set current_day = now().weekday() %}
        
        {% if current_day in [5, 6] %}
          0.09
        {% else %}
          {% if 0 <= current_hour < 7 %}
            0.09
          {% elif 8 <= current_hour < 9 %}
            0.14
          {% elif 10 <= current_hour < 13 %}
            0.19
          {% elif 14 <= current_hour < 17 %}
            0.14
          {% elif 18 <= current_hour < 21 %}
            0.19
          {% elif 22 <= current_hour < 23 %}
            0.14
          {% else %}
            0.09
          {% endif %}
        {% endif %}
- name: "Habitacion_last_updated"
  value_template: >
            {% if is_state('binary_sensor.sensor_habitacion_contact', 'on') %}
              {{ as_timestamp(states.binary_sensor.door_sensor.last_changed) | timestamp_custom('%d-%m %H:%M') }}
            {% else %}
              {{ as_timestamp(states.binary_sensor.door_sensor.last_changed) | timestamp_custom('%d-%m %H:%M') }}
            {% endif %}

I dont get any errors but I cant see the sensors

“state >-“

Thank you so much!

I had to change from:

 value_template: >

To:

 state:  >