My first and complicated blueprint for an alarm clock

I am trying to create a blueprint for the Magister alarm clock automation. This is my first attempt at a blueprint, but I’m in over my head I’m afraid. So I’m turning for help to my fellow community members :slight_smile:

This does not seem to work, can anyone help me out here? I can create an automation from this, but when I activate it nothing happens. The automation trace is empty.

blueprint:
  name: Magister alarm clock
  description: Trigger an alarm based on the starting time of the next lesson in Magister
  domain: automation
  author: Emphyrio
  input:
    ical_suffix:
      name: Name for iCal calendar entities
      description: This is the (unique) name you enter when you add the iCal Sensor. Suggestion is to use a format like magister_john
      default: ""
    alarm_offset:
      name: Alarm offset
      description: The input_number helper that contains the offset. The offset is the number of minutes before the first lesson that the alarm should sound. Think of it as the time needed to wake up, prepare and travel to school.
      selector:
        entity:
          filter:
            domain: input_number
    alarm_toggle:
      name: Alarm toggle
      description: The input_boolean helper that toggles this alarm on or off.
      selector:
        entity:
          filter:
            domain: input_boolean
    wakeup_song:
      name: Wake up song
      description: Audio file to be played when it is time to wake up.
      selector:
        media:

mode: single

trigger_variables:
  ical_suffix: !input ical_suffix
  alarm_offset: !input alarm_toggle
  alarm_time: >-
    {% set y = expand('group.'~ical_suffix) | map(attribute='entity_id') | list | join(', ') %}
    {% set x = states.sensor
      | selectattr('object_id', 'match', 'ical_'~ical_suffix)
      | rejectattr('attributes.location', 'in', ('','verborgen','verborgen (persoonlijk)'))
      | rejectattr('attributes.all_day', 'eq', true)
      | sort(attribute='attributes.start')
      | map(attribute='attributes.start') | list %}
    {{ (as_timestamp(x|first if x != [] else '') - (states(alarm_offset)|int(default=90) * 60) )|timestamp_custom('%H:%M', true) }}
  alarm_date: >-
    {% set x = states.sensor
      | selectattr('object_id', 'match', 'ical_'~ical_suffix)
      | rejectattr('attributes.location', 'in', ('','verborgen','verborgen (persoonlijk)'))
      | rejectattr('attributes.all_day', 'eq', true)
      | sort(attribute='attributes.start')
      | map(attribute='attributes.start') | list %}
    {{ (as_timestamp(x|first if x != [] else '') - (states(alarm_offset)|int(default=90) * 60) )|timestamp_custom('%Y-%m-%d', true) }}

# The seemingly random 'expand' is my attempt to force a refresh when one of the 50 iCal entities is changed.

variables:
  current_date: "{{ states('sensor.date') }}"
  alarm_toggle: "{{ states('input_boolean.alarm_enabled')}}"

triggers:
  - trigger: template
    id: sound_the_alarm
    value_template: "{{ alarm_time == states('sensor.time') }}"
condition:
  - condition: state
    entity_id: !input alarm_toggle
    state: 'on'
  - condition: template
    value_template: "{{ alarm_date == states('sensor.date') }}"
actions:
  - action: media_player.play_media
    data: >-
      !input wakeup_song

Hello Roelof,

Looks pretty good. I always recommend that to build a blueprint, you first write an automation without using the device_id stuff and debug that. Once that is working with static values, then build a blueprint.

First glance you are doing a LOT of stuff in the trigger_variables. You need to remember you only have limited templates available there. I’m not sure that attributes can be used, as I just avoid templates in triggers except for very basic ones.

Will it try to run, do you get a trace at all?

Does the alarm_date variable work, have you tried it in developer - templates - sandbox? This doesn’t mean it works as a limited template, but if it works at all.

For that are the 2 dates in the same format? Can also check in dev-template.

Open your Home Assistant instance and show your template developer tools.