Opening curtains after Android and iOS alarm clocks (free)

Opening curtains after Android and iOS alarm clocks

The automation will be good for you and probably won’t need any tweaking,
If one person in your pair has Android and the other has iOS.

Description

When the alarm rings, the automation checks to see if your partner is home.
If your partner is not home, it opens the curtains.
If your partner is home, the automation will wait for their alarm clock to go off (but no more than 3 hours).
As soon as the partner’s alarm rings, the curtains are opened.

I propose to add an additional restriction that the automation will not start more than once every 10 hours.
This would be handy if, after the alarm, you want to close the curtains and sleep some more,
but forget to turn off the next alarm - without the proposed restriction, the curtains might open again and you’d be pissed.

I sometimes use alarms in the evening as well, so I add a time condition: 08:00 to 12:30 in the morning.
So the automation doesn’t trigger falsely and open my blinds in the evening.

The Home Assistant app must be installed on Android and iOS, must be logged in to the server.

Requirements

You must have home presence trackers set up for your person objects.
This is needed so that when your partner is not home, the curtains will open without waiting for their alarm clock.

Creating automation

Auxiliary Object

Create a button (input_button.alarm_stopped) in Home Assistant. This button will be pressed by iphone when alarm is stopped.
You can name it anything you like, for example “iOS alarm_stopped”.

Creating automation in the “Commands” application

  • Install and open the Commands app on your iphone
  • Go to the “Automation” tab
  • Select “Create automation for yourself.”
  • Select “Alarm Clock”, check “Stopped”, “Any”, press “Next”
  • Press “Add Action”, search for “Home Assistant”, select “Call Service”.
  • Press on “Service”, choose input_button.press from the list that appears.
  • Click on the arrow after “with data”, select your Home Assistant server
  • Enter `{“entity_id”: “input_button.alarm_stopped”} in the “Service Data” field.
  • Make sure that the “Show on startup” toggle is disabled
  • Click “Next.”
  • Remove the “Ask before startup” toggle, confirm it with “Don’t ask” in the dialog box that appears
  • Click on “Done”.

Enable access to the alarm clock on Android

  • Open the Home Assistant app
  • Press “Settings”, select “Mobile App”, go to “Sensor Management”
  • Find “Next alarm” in the list, tap on that sensor
  • Make sure the “Enable Sensor” toggle is on, turn it on if necessary

Creating HomeAssistant Automation

Instead of automation.cover_open_on_alarm_clock write the name of your automation.
You will be able to find it out or change it after the first save of your automation.

Replace the sensor sensor.android_next_alarm with yours.
You can find out its name by opening the page of your android device and selecting the sensor “Next Alarm”.

alias: Open curtains after Android and iOS alarms
description: >-
  Opens curtains when 2 alarms have been stopped between 08:00 and 12:30


  There is an automation set up on the iPhone that triggers the push button service,
  checks who pressed it.

  On android it looks to see if there is < 30 seconds left to the alarm.
trigger:
  - platform: state
    entity_id:
      - input_button.alarm_stopped
    alias: When the ios alarm was stoppe
    id: ios_hook
  - platform: template
    value_template: >
      {{ state_attr('sensor.android_next_alarm', 'Time in Milliseconds') !=
      None and state_attr('sensor.android_next_alarm', 'Time in
      Milliseconds') / 1000 - as_timestamp(now()) < 30}}
    alias: When the android alarm rings after 30 seconds
    id: android_hook
condition:
  - condition: time
    after: "08:00:00"
    before: "12:30:00"
    weekday:
      - sun
      - sat
      - fri
      - thu
      - wed
      - tue
      - mon
    alias: Time 08:00-12:30
    enabled: true
  - condition: template
    value_template: >-
      {{(as_timestamp(now()) -
      as_timestamp(state_attr("automation.cover_open_on_alarm_clock",
      "last_triggered") | default(0)) | int > 10 * 60 * 60 )}}
    alias: Last triggered more than 10 hours ago
    enabled: true
action:
  - if:
      - condition: trigger
        id: ios_hook
    then:
      - choose:
          - conditions:
              - condition: state
                entity_id: person.android
                state: home
                alias: Android at home
            sequence:
              - wait_for_trigger:
                  - platform: template
                    value_template: >
                      {{ state_attr('sensor.android_next_alarm', 'Time in
                      Milliseconds') != None and
                      state_attr('sensor.android_next_alarm', 'Time in
                      Milliseconds') / 1000 - as_timestamp(now()) < 30}}
                    alias: Alarm on android rings in less than 30 seconds
                timeout:
                  hours: 3
                  minutes: 0
                  seconds: 0
                  milliseconds: 0
                alias: Wait for android's alarm clock
                continue_on_timeout: false
              - service: cover.open_cover
                data: {}
                target:
                  entity_id: cover.curtain_bedroom
                alias: Open the curtains
            alias: If android is home, we wait for his alarm 
        default:
          - service: cover.open_cover
            data: {}
            target:
              entity_id: cover.curtain_bedroom
            alias: Open the curtains
        alias: Wait for android alarm if android is home + open
    alias: If triggered by ios, wait for android alarm to approach
  - if:
      - condition: trigger
        id: android_hook
    then:
      - choose:
          - conditions:
              - condition: state
                entity_id: person.iphone
                state: home
                alias: Iphone at home
            sequence:
              - wait_for_trigger:
                  - platform: state
                    entity_id:
                      - input_button.alarm_stopped
                    alias: When the alarm was stopped
                timeout:
                  hours: 3
                  minutes: 0
                  seconds: 0
                  milliseconds: 0
                alias: Wait for iphone's alarm clock
                continue_on_timeout: false
              - service: cover.open_cover
                data: {}
                target:
                  entity_id: cover.curtain_bedroom
                alias: Open the curtains
        default:
          - service: cover.open_cover
            data: {}
            target:
              entity_id: cover.curtain_bedroom
            alias: Open the curtains
        alias: Wait for ios alarm if iphone is home + open
    alias: If android triggered, wait for ios alarm to stop
mode: single

You can modify the automation to suit your needs, for example, determine who pressed the button and act on it.
Checks if user admin pressed the button.

value_template for the trigger
value_template: >-
    {% set andrew_id = states.person.admin.attributes.user_id
    %}
    {% set nastya_id = states.person.nastya.attributes.user_id
    %}
    {% set who_stopped_id =
    states.input_button.alarm_stopped.context.user_id %}
    {% set andrew_stopped = who_stopped_id == andrew_id %}

    {{ andrew_stopped }}

I will be glad if automation takes root in your home.