Turn off automation based on wake up alarm time

Hey everyone,

I managed to sync my wake up alarm with HA using this guide. I have an entity called ‘input_datetime.ios_sync’ that ‘receives’ the time of my wake up alarm each night at 03:00.

Now I want to turn an automation off based on the given time: if it’s a weekday and the alarm clock is set at 08:30, turn off automation X and turn back on again after X hours. I am not very good at YAML, any help is appreciated.

Ruben

My solution is to use the Google Calendar integration and using a calendar event as the trigger, with a time off-set (as I use all-day events).

alias: "#10-22 Wake-up  6:00 am"
description: ""
trigger:
  - platform: calendar
    event: start
    offset: "6:0:0"
    entity_id: calendar.12_sb_6_00_am
condition: []
action:
  - service: light.turn_on
    data:
      color_name: blue
      brightness_pct: 20
    target:
      entity_id: light.north_west_2
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - type: turn_on
    device_id: 13b4cfc43e569b2b9416415d541817b8
    entity_id: light.14_29_mbr_desk_lamp_lifx
    domain: light
    brightness_pct: 75
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - type: turn_on
    device_id: c49cb0c7d996c47e85532b42fa36c769
    entity_id: light.14_18_mbr_seno_reading_light
    domain: light
    brightness_pct: 15
  - delay:
      hours: 0
      minutes: 25
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: c49cb0c7d996c47e85532b42fa36c769
    entity_id: light.14_18_mbr_seno_reading_light
    domain: light
  - delay:
      hours: 0
      minutes: 8
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: 01e9104f06785d6ec9b55fac451fb7e7
    entity_id: light.north_west_2
    domain: light
  - delay:
      hours: 0
      minutes: 8
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: 13b4cfc43e569b2b9416415d541817b8
    entity_id: light.14_29_mbr_desk_lamp_lifx
    domain: light
mode: queued

Thanks a lot, I’ve been thinking of doing this as an alternative! Is your calendar event called ‘start’? And off-set triggers the actions 6 hours from the start of the day (00:00h), do I understand this correctly?

Edit: didn’t reply to comment directly

Edit2: apparently I did, don’t mind me :man_facepalming:

It is generally a bad idea to automate disabling/enabling automations, instead you should design the conditional logic of your automations to keep their actions from being executed when you don’t want them to be executed.

Reserve disabling automations for when you need to debug existing automations or test new ones.

Thanks! Good to know. In that case I would like to have the automation to turn on my lights when movement is detected take into account the time of my alarm clock input_datetime.ios_sync. Any suggestions of how I would do that?

This is the YAML code for the current automation:

alias: Beweging slaapkamer aan (werkdagen)
description: >-
  Als er beweging wordt gedetecteerd in de slaapkamer, als de lux is onder 25,
  als het voor 22:30 is op een weekdag schakel dan de lichten in de slaapkamer
  in.
trigger:
  - type: motion
    platform: device
    device_id: 6cf96cb6e8a10411427c8a3ce13895eb
    entity_id: binary_sensor.hue_motion_sensor_1_motion
    domain: binary_sensor
condition:
  - type: is_illuminance
    condition: device
    device_id: 6cf96cb6e8a10411427c8a3ce13895eb
    entity_id: sensor.hue_motion_sensor_1_illuminance
    domain: sensor
    below: 25
  - condition: time
    before: "22:30:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
    after: "06:45:00"
action:
  - type: turn_on
    device_id: 797cf80e91e860be277f7820c8c837ba
    entity_id: light.slaapkamer
    domain: light
    brightness_pct: 50
mode: single

Can clarify how the alarm time should be taken into account?

Should the light not turn on at all on days when your alarm is set to 8:30, should it not turn on until after 8:30 on those days, etc?

If on a weekday (Monday through Friday) my alarm is set to 08:30 or later then the lights should not turn on at movement.

Otherwise, the lights should turn on when movement is detected and the lux is below 25 starting from 06:45 (not before that time to prevent turning the lights on when going to the bathroom in the night).

In either case the lights should turn on when it’s dark enough in the room (hence the lux variable) and when its before 22:30.

Hope I explained clearly :slight_smile:

Thanks a lot, I’ve been thinking of doing this as an alternative! Is your calendar event called ‘start’? And off-set triggers the actions 6 hours from the start of the day (00:00h), do I understand this correctly?

alias: Beweging slaapkamer aan (werkdagen)
description: >-
  Als er beweging wordt gedetecteerd in de slaapkamer, als de lux is onder 25,
  als het voor 22:30 is op een weekdag schakel dan de lichten in de slaapkamer
  in.
trigger:
  - type: motion
    platform: device
    device_id: 6cf96cb6e8a10411427c8a3ce13895eb
    entity_id: binary_sensor.hue_motion_sensor_1_motion
    domain: binary_sensor
condition:
  - type: is_illuminance
    condition: device
    device_id: 6cf96cb6e8a10411427c8a3ce13895eb
    entity_id: sensor.hue_motion_sensor_1_illuminance
    domain: sensor
    below: 25
  - condition: time
    before: "22:30:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
    after: "06:45:00"
  - condition: template
    value_template: >
      {{ states('input_datetime.ios_sync') < "08:30:00" }}
action:
  - type: turn_on
    device_id: 797cf80e91e860be277f7820c8c837ba
    entity_id: light.slaapkamer
    domain: light
    brightness_pct: 50
mode: single

The above assumes your Input datetime is a time-only type. For a date & time type that includes the current day’s date you would use:

{{ states('input_datetime.ios_sync') | as_datetime | as_local 
< today_at("08:30:00") }}

Is your calendar event called ‘start’? And off-set triggers the actions 6 hours from the start of the day (00:00h), do I understand this correctly?
I am not the king of Yaml and created this automation with the Visual Editor.
I come from the Universal Devices “eisy” environment and its Google calendar nodeserver (integration) only works with all day events, and I have not explored how to make an automation based on timed event. So, the answer is that the all day event starts at midnight and therefore there is an offset of 06:00:00, as this particular schedule should start at 6 am. I have some 6 different wake-up automations based on different wake-up times as per my wife’s and my schedule, depending on our work and other commitments.

Thanks! Should I create two automations to differentiate between the morning and the rest of the day? With the added condition, the lights don’t turn on when movement is detected when it’s dark enough while that is what I want to happen, despite my alarm clock in the morning.

Thanks for elaborating, I get what you mean :slight_smile:

That is a condition I have been thinking about for a while, so that lights go on when it is dark outside. During Summer, it would be sufficient to open the curtains, turn the TV on and have Alexa make an announcement. That is still on the drawing board.

Remember when I asked…

:confused:

Under what conditions should the light turn on when the alarm is set for 8:30 or later?

Sorry, thought I mentioned that, but I realize I wasn’t specific enough.

In either case the lights should turn on when it’s dark enough in the room (hence the lux variable) and when its before 22:30

Under what conditions should the light turn on when the alarm is set for 8:30 or later?

The lights can turn from 08:30 onwards when movement is detected and lux is below 25.

Ok, add an “or” to the template in the condition so the condition passes if the current time is at least the time stored in the helper:

alias: Beweging slaapkamer aan (werkdagen)
description: >-
  Als er beweging wordt gedetecteerd in de slaapkamer, als de lux is onder 25,
  als het voor 22:30 is op een weekdag schakel dan de lichten in de slaapkamer
  in.
trigger:
  - type: motion
    platform: device
    device_id: 6cf96cb6e8a10411427c8a3ce13895eb
    entity_id: binary_sensor.hue_motion_sensor_1_motion
    domain: binary_sensor
condition:
  - type: is_illuminance
    condition: device
    device_id: 6cf96cb6e8a10411427c8a3ce13895eb
    entity_id: sensor.hue_motion_sensor_1_illuminance
    domain: sensor
    below: 25
  - condition: time
    before: "22:30:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
    after: "06:45:00"
  - condition: template
    value_template: >
      {% set alarm = states('input_datetime.ios_sync') %}
      {{  alarm < "08:30:00" or now() >= today_at(alarm) }}
action:
  - type: turn_on
    device_id: 797cf80e91e860be277f7820c8c837ba
    entity_id: light.slaapkamer
    domain: light
    brightness_pct: 50
mode: single

:grinning: It is useful that I was born in the Netherlands and understand the comments in the automation.