Getting desperate with template for event sensor

I had created a template for a waste event some time ago that worked until recently. I do not know since when it ceased to work since it is for green waste which is not active in winter.

I tried everything I can imagine to get it work. But it doesn’t. So I looked around in the forum here and found a template (Template Sensor Configuration for Calendar Events - #10 by Nickduino) which I adopted since it seems better than mine. But it does nothing at all. Zero. No error, no sensor creation. I wonder what I do wrong. Any idea?

I check also with actions to verify if events are there. There are two:

And here is my template:
Test.yaml in homeassistant/template directory

- trigger:
      - platform: time_pattern
        seconds: /10
    action:
      - action: calendar.get_events
        target:
          entity_id: calendar.anderes
        data:
          duration:
            days: 60
        response_variable: scheduled_waste_events
      - variables:
          all_events: "{{ scheduled_waste_events['calendar.anderes'].events  }}"
          sperr_ev: "{{ all_events | selectattr('summary', 'search', 'Sperrmüll', true) | list }}"
          garten_ev: "{{ all_events | selectattr('summary', 'search', 'Grünabfall', true) | list }}"
    sensor:
      - name: Sperrmuell Events
        state: {{ non_recycle_ev | count() }}
        attributes:
          scheduled_events: {{ sperr_ev }}
          next_collection_date: |{{ (sperr_ev | first).start }}
          countdown: |
            {{ int((as_timestamp((sperr_ev | first).start) - as_timestamp(today_at('00:00')))/86400)}}

      - name: Gartenabfall Events
        state: {{ garden_ev | count() }}
        attributes:
          scheduled_events: |
            {{ garten_ev }}
          next_collection_date: {{ (garten_ev | first).start }}
          countdown: |
            {{ int((as_timestamp((garten_ev | first).start) - as_timestamp(today_at('00:00')))/86400)}}

I see that I have an error in the template for the first sensor:

state: {{ non_recycle_ev | count() }}

I corrected to sperr_ev.

Still the same - no senor created, no errors.

Hi @uwefrank

Shouldn’t trigger, action and sensor line up?

- trigger:
  ...
  action:
  ...
  sensor:

Single-line templates should be quoted (rule 1), and count is being used as a filter here so no need for ():

state: "{{ sperr_ev | count }}"

There’s a few more in your attributes that need fixing.

and if you use | the template should be on the next line.

Thanks for the hints. I have changed the template and checked it also in yamllint.com.

Still does absolutely nothing at all. I check for the sensors, after restart of HA, in
1 - Developer Tools - States
2 - Settings - Devices & Services - Entities
Nothing there.

Code:

- trigger:
    - platform: time_pattern
      seconds: /10
  action:
    - action: calendar.get_events
      target:
        entity_id: calendar.anderes
      data:
        duration:
          days: 60
      response_variable: scheduled_waste_events
    - variables:
        all_events: "{{ scheduled_waste_events['calendar.anderes'].events  }}"
        sperr_ev: "{{ all_events | selectattr('summary', 'search', 'Sperrmüll', true) | list }}"
        garten_ev: "{{ all_events | selectattr('summary', 'search', 'Grünabfall', true) | list }}"
  sensor:
    - name: Sperrmuell Events
      unique_id: mysperrmuell
      state: "{{ sperr_ev | count }}"
      attributes:
        scheduled_events: "{{ sperr_ev }}"
        next_collection_date: {{ (sperr_ev | first).start }}
        countdown: |
          {{ int((as_timestamp((sperr_ev | first).start) - as_timestamp(today_at('00:00')))/86400)}}
    - name: Gartenabfall Events
      unique_id: mygarten
      state: "{{ garden_ev | count }}"
      attributes:
        scheduled_events: |
          {{ garten_ev }}
      next_collection_date: "{{ (garten_ev | first).start }}"
      countdown: |
        {{ int((as_timestamp((garten_ev | first).start) - as_timestamp(today_at('00:00')))/86400)}}

Still a syntax error here. It may seem valid yaml (though I highly doubt that), but it isn’t a valid template. Did you ever check the logs to see the errors that come from loading templates, and or press the check config button before you restart HA or reload templates?

Also, I’m not sure if the variables you declare are visible to the sensors below. The response variable is, but that is a special construct, designed to prevent exactly this problem. Variables typically do not persist outside the block they are defined in.

Unless your configuration.yaml has been setup to explicitly load template entities from that folder, putting files in there isn’t going to do anything. That is not a part of the default configuration.

Unless somebody broke it recently, they are.

Unless I am missing something that is not the same thing? That part of the documentation describe a variables block on the same level as action, trigger, sensor, etc., not one within the action block. The way to get data out of the action block ought to be stop with a response_variable.

image

Also, for the empiricists, I just checked my 10 sensors that assign variables in the action block and use them in the entity templates and all are functioning as expected on 2025.1.4.

1 Like

Okay. While absolutely much more user friendly, it seems super inconsistent with the normal limitations on variable scope.

Hi Edwin, yes I checked some logs (Logbook in the main menu, Core, Supervisor in the Settings/System/Logs). I am nit sure where exactly I should check - tehre are many logs where I do not know what they are for, eg, the supervisor, the host log and more.

Hello, yes it is explicitly mentioned. There are more templates in the folder that are running.

homeassistant:
  allowlist_external_dirs:
    - /config/sensor
    - /config/template

while the templates there may be included in some way, this part of the config is not loading those templates. It is saying these folders are trusted, not loaded.

Yes, that’s right. I forgot the lines after that:

homeassistant:
  allowlist_external_dirs:
    - /config/sensor
    - /config/template

sensor: !include_dir_list sensor/
template: !include_dir_list template/

Ok. Got it fixed and running.
First I changed in configuration.yaml
!include_dir_list to !include_dir_merge_list

After restart I saw a log error (I try to remeber correctlx): next_collection_date is not a valid atrtribute.

There was a wrong indentation in the last two lines of the attributes!! Should have seen that.

Here is the working template:

- trigger:
    - platform: time_pattern
      seconds: /10
  action:
    - action: calendar.get_events
      target:
        entity_id: calendar.anderes
      data:
        duration:
          days: 60
      response_variable: scheduled_waste_events
    - variables:
        all_events: "{{ scheduled_waste_events['calendar.anderes'].events  }}"
        sperr_ev: "{{ all_events | selectattr('summary', 'search', 'Sperrmüll', true) | list }}"
        garten_ev: "{{ all_events | selectattr('summary', 'search', 'Grünabfall', true) | list }}"
  sensor:
    - name: Sperrmuell Events
      unique_id: mysperrmuell
      state: "{{ sperr_ev | count }}"
      attributes:
        scheduled_events: "{{ sperr_ev }}"
        next_collection_date: "{{ (sperr_ev | first).start }}"
        countdown: |
          {{ int((as_timestamp((sperr_ev | first).start) - as_timestamp(today_at('00:00')))/86400)}}
    - name: Gartenabfall Events
      unique_id: mygarten
      state: "{{ garden_ev | count }}"
      attributes:
        scheduled_events: |
          {{ garten_ev }}
        next_collection_date: "{{ (garten_ev | first).start }}"
        countdown: |
          {{ int((as_timestamp((garten_ev | first).start) - as_timestamp(today_at('00:00')))/86400)}}