Calendar.get_events sensor with unknown status

@Didgeridrew

Hi, I try to do the same thing you propose here (“Days until X” sensors based on single local calendar) with one calendar who contain events but my sensors stay with and unknown status.
i try to understand but i don’t know how i can debug this in template tools

your help will be appreciate

Here my template :

template:
  - trigger:
      - platform: time
        at: "00:00:00"
    action:
      - service: calendar.get_events
        data:
          start_date_time: "{{today_at()}}"
          duration:
            hours: 168
        target:
          entity_id: calendar.poubelles
        response_variable: agenda
    sensor:
      - name: jours_poubelles_dechets
        state: >
          {% set midnight = today_at() %}
          {% set event = agenda["calendar.poubelles"].events | selectattr('summary', 'search', 'Déchets') | map(attribute='start') | map('as_datetime') | first %}
          {% set delta = (event - midnight).days %}
          {{ delta }}
          {% if delta == 0 %}
            Aujourd'hui
          {% elif delta == 1 %}
            Demain
          {% elif delta == 2 %}
            Après Demain
          {% else %}
            dans {{ delta }} jours
          {% endif %}
        unit_of_measurement: "Days"
      - name: jours_poubelles_recyclages
        state: >
          {% set midnight = today_at() %}
          {% set event = agenda['calendar.poubelles'].events | selectattr('summary', 'search', 'Recyclage') | map(attribute='start') | map('as_datetime') | first %}
          {% set delta = (event - midnight).days %}
          {{ delta }}
          {% if delta == 0 %}
            Aujourd'hui
          {% elif delta == 1 %}
            Demain
          {% elif delta == 2 %}
            Après Demain
          {% else %}
            dans {{ delta }} jours
          {% endif %}
        unit_of_measurement: "Days"

Also when i try the service in Developper tools i see one event :

The issue is that you have assigned a unit_of_measurement, which is only allowed for sensors whose state is purely numeric. Also, you have left the expression {{ delta }} in both templates before the if/then. This won’t break anything, but you likely don’t want it in there…probably leftover from your testing setup.

Yes i try differents thing before writing this. i updated the template without the " unit_of_measurement: “Days” and the “{{ delta }}” but the result still the same.

template:
  - trigger:
      - platform: time
        at: "00:00:00"
    action:
      - service: calendar.get_events
        data:
          start_date_time: "{{today_at()}}"
          duration:
            hours: 168
        target:
          entity_id: calendar.poubelles
        response_variable: agenda
    sensor:
      - name: jours_poubelles_dechets
        state: >
          {% set midnight = today_at() %}
          {% set event = agenda["calendar.poubelles"].events | selectattr('summary', 'search', 'Déchets') | map(attribute='start') | map('as_datetime') | first %}
          {% set delta = (event - midnight).days %}
          {% if delta == 0 %}
            Aujourd'hui
          {% elif delta == 1 %}
            Demain
          {% elif delta == 2 %}
            Après Demain
          {% else %}
            dans {{ delta }} jours
          {% endif %}
      - name: jours_poubelles_recyclages
        state: >
          {% set midnight = today_at() %}
          {% set event = agenda['calendar.poubelles'].events | selectattr('summary', 'search', 'Recyclage') | map(attribute='start') | map('as_datetime') | first %}
          {% set delta = (event - midnight).days %}
          {% if delta == 0 %}
            Aujourd'hui
          {% elif delta == 1 %}
            Demain
          {% elif delta == 2 %}
            Après Demain
          {% else %}
            dans {{ delta }} jours
          {% endif %}

That’s because of your trigger… it’s not midnight yet.

1 Like

Ok so i need to use now() if i want to see the result now !!

You can add a second trigger like:

    - platform: event
      event_type: event_template_reloaded

or

    - platform: homeassistant
      event: start

… really any trigger that you can control or make happen and won’t also mess up whatever you’re trying to do with your sensor’s normal behavior.

1 Like

Thank you very much !

i understand now