Skip to content

Easy Sleep Mode with Home Assistant

Sean edited this page Sep 24, 2020 · 7 revisions

This is going to be a super basic tutorial on how to begin using the Home Assistant app to to notify your Home Assistant server that you are ready to go to sleep. This tutorial will turn off all your lighting whenever you plug in in your phone to charge at night before going to sleep. You should be able to take this example and also adapt it to other automations as needed.

What You'll Need:

  • An Smart Phone

  • Smart Bulbs or Smart Switches

  • The official Home Assistant app


The first step is to get the HA Mobile App integration added to your server. Go to your Integrations panel and add the Mobile App integration if you haven't already.

The second step you need to take is acquiring the Home Assistant app. Once downloaded, input your server information and get connected to your HA server. Once your personal settings are to your liking we need to make sure that some sensors are enabled for the app to report.

With the HA app open, tap on "App Configuration" and then tap on "Sensors." The sensors we're looking for should already be enabled by default, but it's good to make sure. Scroll down until you find the Battery section. Once found, tap on "Battery State" and then enable it if it wasn't already.

Once you have set up the mobile app correctly and connected it, the device should should up under the Mobile App section on your integration panel.


Now it's time to start making automations, but first we need our trigger. Go take a look at your States under the Developer Tools and scroll down to your Sensors until you find your phone's battery status. It will either say "discharging" or "charging." In my experience the app is almost instant when plugging in a charging cable so there's almost no delay when triggering. We found our trigger so let's make this automation.

- alias: 'Bedtime'
  initial_state: on
  trigger:
    platform: state
    entity_id: sensor.sean_pixel_3_battery_state
    to: 'charging'
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: group.family
        state: 'home'
      - condition: time
        after: '21:00:00'
        before: '03:00:00'
  action:
    - service: light.turn_off
      data:
        entity_id: light.bedroom
    - service: switch.turn_on
      data:
        entity_id: switch.bedroom_fan
    - service: input_boolean.turn_on
      data:
        entity_id: input_boolean.bedtime_sean

The Conditions

  1. We only want this automation firing if we're at home. This is to prevent you accidentally triggering anything when you're in your car at night an decide to charge your phone.
  2. We also only want this automation starting at a specific time of the evening. In my example the automation will only trigger between 9:00pm and 3:00am. Adjust this to your own schedule.

The Actions

  1. The first action will turn off the bedroom lights. In my home I have it turn off all lights in the house, but this is up to you.
  2. The second turns on our bedroom ceiling fan. This action is mostly an example of what other things you can do.
  3. The third action is entirely optional but recommended. This action will trigger an Input Boolean which I use in other automations as a condition. For example, we use motion sensors in our home and use them to turn on lights. With this Input Boolean, I can have a condition in other automations that only will trigger if we're awake. Very useful when you have cats who like to trigger everything.

Now let's make an automation for when you wake up and want to get out of bed.

- alias: 'Good morning'
  initial_state: on
  trigger:
    platform: state
    entity_id: sensor.sean_pixel_3_battery_state
    to: 'discharging'
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: group.family
        state: 'home'
      - condition: time
        after: '06:00:00'
        before: '10:00:00'
  action:
    - service: light.turn_on
      data:
        entity_id: light.bedroom
        brightness_pct: 100
        transition: 60
    - service: switch.turn_off
      data:
        entity_id: switch.bedroom_fan
    - service: input_boolean.turn_off
      data:
        entity_id: input_boolean.bedtime_sean

The conditions are pretty much the same as the previous automation. Just edit the times to your own personal schedule.

The Actions

  1. The first action is an example for when you have dimmable smart bulbs. This will have your bulbs start at 0% brightness and then gradually brighten to 100% brightness over the course of one minute. You can adjust these timings and brightness to your liking. If you have smart switches, you can remove the brightness and transition options.
  2. The bedroom fan is switched off.
  3. The Sleeping Input Boolean is switched off to allow other automations to run without issue.

And that's it! The Home Assistant Companion App has opened a lot of possibilities for easy automations. I encourage you to take a look through all the sensors that app can use and make some automations of your own.