Home » Blog » How To Restore Light State at Restart of Home Assistant

How To Restore Light State at Restart of Home Assistant

Restore Light State in Home Assistant

If Home Assistant has to be restarted after an update or a crash, it can happen that a number of lamps turn off or on. That is of course very annoying because you have to manually set your lamps again after a restart of Home Assistant. Fortunately, you can also do this automatically by saving the status of your lamps. Then you can automatically restore the light state at restart. In this Home Assistant tutorial, I show you how you can easily implement this yourself.

Click the image to watch the video


โญโญโญ NOTE: โญโญโญ

This article accompanies a YouTube video. I wrote it for people who would rather read than watch a video. To keep doing this, I want to ask you to check out the video, leave a comment under the video, give the video a thumbs up, and subscribe to my YouTube channel. This means that the video is offered more often to new visitors so that they also stay informed of the latest Home Assistant tutorials.

Thank you for your support!

Ed

Watch the video here!


Overview

The code used in this video can be downloaded here.

In this example, we restore the status of a lamp, but you can of course apply this to all kinds of devices such as switches and whatnot.

Let’s jump right in to make sure that we restore the light state at a restart of Home Assistant.

You need three things for this:

  1. A toggle helper (also called a boolean helper) that stores the status of your lamp.
  2. An automation that monitors the status of your lamp and gives the toggle helper the same status as your lamp.
  3. An automation that turns your lamp on or off based on the toggle helper’s status upon restart.

The Helper

We start by creating a helper.
Go to Settings -> Devices & Services and choose the Helpers tab.
Click on Create Helper and choose Toggle.
Give your Toggle Helper a meaningful name. For example “Office Light Front Left Status”.
Choose an icon. I choose mdi:list status here.
Click Create.

You have now created a helper. The entity ID is: input_boolean.office_light_front_left_status

The Automations

Automation 1: Monitor the status of the lamp

We are now going to create the automation that monitors the status of the lamp and ensures that the toggle helper gets the status on or off.

To do this, go to Settings -> Automations & Scenes and click on Create Automation.
Choose here for Start with an empty automation.
Give your automation a name. For example “Light: Monitor Light Status Office Front Left”.
Leave the mode on Single.

The Trigger

At Triggers we add one trigger. Since we are checking for one lamp in this tutorial, we will choose Trigger type: Device here.
In our case, we choose as Device: Office Front Left.
The trigger is: Office Front Left turned on or off. After all, we want to save the status as soon as the lamp turns on or off.

The Actions

Now we go to the Actions
At Actions, we add an If-Then Action Type.
Under “If” we choose Condition Type Device.
At Device, we choose Office Front Left.
At the Condition, we choose: Office Front Left is On.
Under “Then” we choose the Action type: Call Service
At Service, we then choose: Input boolean: Turn on
With Targets, we choose our helper “Office Light Front Left Status” by clicking on Choose Entity.

So, what happens now is that our helper is set to On when the lamp switches from off to on.

At “Else” we click on Add Action.
With Action type we choose again: “Call service” and with Service, we choose Input boolean: Turn off.
With Targets, we choose our helper “Office Light Front Left Status” by clicking on Choose Entity.

Now click on Save.

Let’s test automation 1

Let’s test the automation first. Open your lamp in your dashboard and switch your lamp on or off. You will see that the helper now switches along with the lamp.

Dashboard example light state

The Code

The code used in this video can be downloaded here.


Automation 2: Restore the Light State at a Restart of Home Assistant.

We are now going to create the automation that restores the status of the lamp when Home Assistant restarts.

To do this, go to Settings -> Automations & Scenes and click on Create Automation.
Choose here for “Start with an empty automation”.
Give your automation a name. For example “Light: Restore Light Status at Restart”.
Leave the mode on Single.

I need your help!

You will be doing me a huge favor if you subscribe to my channel if you haven’t already. And, you will help me a lot if you also give this video a thumbs up and leave a comment. This way, YouTube will present this video to new people, making the channel grow! In the video description, you will also find information about how you can sponsor me so that I can continue to make these tutorials for you.

Thank you!

The Trigger

At Triggers we add one trigger. For trigger type, choose Home Assistant.
At Event you choose Start.

The Actions

Now we go to the Actions
At Actions, we add an If-Then Action Type.
Under “If” we choose Condition Type: State.
At Entity, we choose our helper: Office Light Front Left Status
We leave the Attribute field empty.
At State, we enter: on
Then we go to “Then”. If our helper’s state is set to “on”, we want the lamp to turn on after a reboot, so we choose Action type: Device and choose the device: Office Front Left.
As Action, we choose Turn on Office front Left. We’ll leave the rest of the settings alone.

At Else, we choose Add Action
The Action type is again Device.
The Device in our case is Office Front Left and the action is: Turn off Office Front Left”.

Now click on Save.

Let’s test automation 2: Restore Light State at a Restart of Home Assistant

We can now test this quite easily without restarting Home Assistant. To do this, set your helper to the opposite status as the current status of the lamp. So, if the lamp is on, turn the helper off.
Now go back to the automation and click on Run Actions.

Your lamp should now have the same status as your helper.

Now you can test it by restarting Home Assistant. Switch off your lamp via Home Assistant.
Restart Home Assistant now. And meanwhile, turn the lamp on manually by turning the lamp’s physical switch off and on again. You switch on the lamp manually. However, the helper is still set to “off”, so the lamp will be turned off again after Home Assistant has started.

If this works, you can repeat this for every lamp in your house.
So you need one boolean helper for each lamp and one automation to synchronize the status for that specific lamp.

The automation that triggers at the start of Home Assistant to restore the light state at restart can be expanded by adding an If-Then Action type for each lamp.

The Code

The code used in this video can be downloaded here.


Advanced Options to restore the light state at a restart of Home Assistant

I have now shown an example to restore the light state at the restart of Home Assistant for one lamp. You can also make this based on a light group. Then you can no longer check for a Device at the trigger, but then you have to trigger based on a state. I believe you can set this yourself based on the information in this tutorial. If it doesn’t work, let me know in the comments. Maybe I can make a tutorial for that too.

You can save even more, for example, the light intensity or the color. I think you can now figure this out yourself based on this tutorial. Let me know in the comments if you succeeded and if I should make a tutorial for that too.

I hope this video helped you and inspired you to create your own automations. Please, let me know in the comments of the video if it helped. I really appreciate hearing from you!

Your support allows me to make new videos all the time. You can support me by becoming my Patron, buying me a cup of coffee, or joining this channel.

Please don’t forget to give this video a thumbs up, subscribe to my channel, and check the notification bell.

I look forward to seeing you next time!

Bye bye.


,

Back to all articles