Where can I put my code - Will it work or kill my home assistant

Hi all, brand new to the world of HA and coding (two weeks in to either), I used the dark arts of youtube, AI and far too much time to try and create an coding solution (if that is what it is called) for my problem.
Basically, I want to try and get a routine to start before a trigger based on the given time that the trigger/ alarm is planned for.
THE VISION -
I want to try and get my heating to turn on in the bedroom 2 hours before my alarm and lights to run a gentle wake up light fade on 1 hour before my first alarm as long as the following criteria are met:

  1. it is between August and May
  2. the first alarm of the day only
  3. as long as the alarm is set between 5am and 9am
  4. from either my phone or my partners phone
  5. but ensuring that the person is at home (so ignoring all alarms if the respective phone is not connected to the wifi)
    my plan is to run the following code at 2am as a routine and try and use the output times to trigger the heating and lighting as automations. But I have no idea where I can put this code or how to get the times out of the code and into somewhere where I can use them in HA. Am I being completely stupid trying to use this in routines? Any help, and or comments on the actual code would be greatly appreciated.
{% set current_time = now() %}
{% set current_date = current_time.date() %}
{% set current_hour = current_time.hour %}
{% set start_of_august = '08-01' | as_datetime %}
{% set end_of_may = '05-31' | as_datetime %}
{% set alarm_list = [] %}

{# 1. Trigger time is 2 AM #}
{% if current_hour == 2 %}
  
  {% if current_date >= start_of_august.date() and current_date <= end_of_may.date() %}
    {# Check if Mike's phone is connected #}
    {% if is_state('sensor.mikeys_phone_wi_fi_connection', 'connected') %}
      {% set mike_next_alarm = states('sensor.mike_phone_device_next_alarm') %}
      {% if mike_next_alarm != 'unavailable' %}
        {% set mike_alarm_time = mike_next_alarm | as_datetime %}
        {% if mike_alarm_time.hour >= 5 and mike_alarm_time.hour <= 9 %}
          {% do alarm_list.append(mike_alarm_time) %}
        {% endif %}
      {% endif %}
    {% endif %}

    {# Check if Marita's phone is connected #}
    {% if is_state('sensor.marita_phone_wi_fi_connection', 'connected') %}
      {% set marita_next_alarm = states('sensor.marita_phone_next_alarm') %}
      {% if marita_next_alarm != 'unavailable' %}
        {% set marita_alarm_time = marita_next_alarm | as_datetime %}
        {% if marita_alarm_time.hour >= 5 and marita_alarm_time.hour <= 9 %}
          {% do alarm_list.append(marita_alarm_time) %}
        {% endif %}
      {% endif %}
    {% endif %}

    {# Check if alarm list is empty #}
    {% if alarm_list | length == 0 %}
      {% set first_alarm_time = 'no_alarm_set' %}
    {% else %}
      {# Find the earliest alarm time from the list #}
      {% set first_alarm_time = alarm_list | min %}
      {% set first_alarm_time = first_alarm_time.strftime('%H:%M') %}
    {% endif %}
  {% else %}
    {% set first_alarm_time = 'no_alarm_set' %}
  {% endif %}

  {{ first_alarm_time }}

{% else %}
  {# If it's not 2 AM, no action is performed #}
  No action triggered at this time.
{% endif %}

HI - please format your code when posting here.

Press the button that looks like this
image
and paste your code in there. You will get more help that way. Peering at screenshots is not good!

1 Like

Work a step at a time, break it down and don’t expect help debugging AI robot vomit. Step away from the code and use HA’s tools for most of it, which will help with debugging and tracing.

I would approach this by setting two template sensor helpers to 2h and 1h before the first alarm. That’s probably easy but I’m not trying to read that screenshot.

Then have two automations that trigger from those sensors.

Conditions that it’s Aug-May, time span, only run once and at home. Here are some examples:

conditions:
  - alias: "August to May"
    condition: template
    value_template: "{{ now().month <= 5 or now().month >= 8 }}"

  - alias: "Timespan"
    condition: time
    after: "05:00"
    before: "09:00"

  - alias: "Mike on wifi"
    condition: state
    entity_id: sensor.mikey_phone_wi_fi_connection
    state: 'connected'

  - alias: "Only run once"
    condition: template
    value_template: "{{ this.attributes.last_triggered | default(as_datetime(0)) < today_at() }}"
1 Like

Thank you Troon, I do not blame you for not wanting to read the screen shot. Appreciate the help

Thanks for the heads up, had no idea how to do that, I have changed it.

1 Like