Home Assistant

How To Simulate A Sunrise With Home Assistant

It is possible to simulate a sunrise in Home Assistant using a simple script and smart bulb. We can trigger this script at any time so that a beautiful sunrise effect can be enjoyed at any time, even if it is still dark outside.

In this tutorial we will create a simple script to simulate a sunrise using a light entity of our choice.

We will also add a simple card to the lovelace user interface that allows us to enter a wake-up time and enable the script.

Bitmoji Image

Prerequisite

For this tutorial we will assume that you have Home Assistant up and running already. A very basic knowledge of configuration.yaml would also be advantageous, but we will cover as much detail as possible.

The automation will work with any light entity in Home Assistant, however I will be using a bulb flashed with Tasmota. I will also cover a little Tasmota configuration for fading which can help smooth the transition, although this is optional.

Create the sunrise script

Firstly we will need to write a script that will progressively increase the brightness of our lights to simulate the sunrise. This script will be triggered from card that we create and insert into our user interface.

We will give the card an option to switch the wake-up feature on and off, the ability to choose a time to activate the sunrise and the ability to disable the sunrise over the weekend, or on any days of your choice.

You can make the duration of your sunrise any length you like, but for the purpose of this tutorial we will make it 30 minutes.

Adding script

Firstly you need to make sure you have added script: to your configuration.yaml file.

script:

If it is already written in the file, you should add the new code beneath the existing code. You should only have one instance of script in your configuration.yaml file.

Also donโ€™t forget that YAML files are very sensitive to the formatting. You must make sure the tabs/spacing are correct or it will produce a syntax error.

If you are having problems getting things to work, the most likely cause is the formatting of the YAML file so double-check that it is all correct.

Add the sequence

Next we need to create a new script entry, we will call it sunrise. We will specify an alias, description and define sequence. Your code should looks something like this.

script:
   sunrise:
       alias: Simulate sunrise
       description: 'fade lights to simulate sunrise'
       sequence:

We want to populate the sequence with a series of light.turn_on services, each with an incremental brightness. We will add a delay between each step.

Therefore we need to decide how many steps to include. The more steps, the smoother the transition will be but the more code we will have to include.

For the purpose of this tutorial we will include 10 steps, each one with a 10 percent increase in brightness. If you want to improve the smoothness of the transition, you can add more steps if you like.

For example you could include 20 steps and increment the brightness by 5 percent for each step. We will cover the basic calculation that determines the values required for any given number of steps.

Specify colour temperature

We will also specify a change in the colour temperature so that the light starts out as a warm white and fades to daylight white. If your bulb does not support colour temperature you can skip over this if you like.

In order to find out if our bulb supports colour temperature and also what the temperature range is, click developer tools in sidebar and then the states tab.

Scroll down a little and enter the name of your light in the filter entities box. Alternatively you can enter “light.” to display all the light entities so that you can locate it manually.

In the right-hand column named attributes, you will see a list of the attributes associated with your bulb. The min_minreds and max_minreds attributes define the colour range of our bulb in mireds, where 153 is cool white and 500 is warm white.

If we subtract the minimum value from the maximum value it will give us the colour temperature range, in my case 347.

Now we need to divide the colour temperature range by the number of steps to get the value that we need to increment the colour temperature by in each step. As I am including 10 steps, therefore I will need to increment the colour temperature by 34.7, although we will stick to integers and round this up to 35.

Now we know for each step we will increase the brightness by 10 percent and the colour temperature by 35 mireds. We will begin with the brightness set to 10 and the colour temperature set to 500.

First we should specify a new alias and give it a name. We also need to specify the light.turn_on service, as we wish to switch on our light entity.

script:
   sunrise:
       alias: Simulate sunrise
       description: 'fade lights to simulate sunrise'
       sequence:        
          - alias: Sun Bulb On
            service: light.turn_on

Next we need to add data: and specify the entity_id, which is the entity name of the light we wish to use for our sunrise. We will also specify our starting values for brightness_pct and colour_temp.

script:
   sunrise:
       alias: Simulate sunrise
       description: 'fade lights to simulate sunrise'
       sequence:        
          - alias: Sun Bulb On
            service: light.turn_on
            data:
              entity_id: light.lohasone
              brightness_pct: 10
              color_temp: 500

Now we need to add a delay before the next step change in brightness. The delay value will depend on the length of time you choose for your sunrise and the number of steps.

In this example we will use 30 minutes. We need to divide this by our numbers of steps. This gives us 3 minutes per step, therefore we will set our delay to 3 minutes.

script:
   sunrise:
       alias: Simulate sunrise
       description: 'fade lights to simulate sunrise'
       sequence:        
          - alias: Sun Bulb On
            service: light.turn_on
            data:
              entity_id: light.lohasone
              brightness_pct: 10
              color_temp: 500
           - delay:
               minutes: 3

We can use minutes here as the calculation gives us a round number, however if you end up with a decimal point you may want to multiply the number of minutes by 60 to get the delay in seconds, then use seconds: to specify the delay length.

           - delay:
               seconds: 180

Copy the step

Now we need to make another step in the sequence. We can do this by copying the step that we just created and changing the value for brightness_pct and color_temp.

As we are increasing the brightness by 10 percent each step, our next brightness value will be 20.

A value of 500 mireds will give the warmest yellow that our bulb can produce. As we are stepping towards cool daylight white, we need to reduce this number by our calculated value of 35. Therefore the next value for color_temp will be 465.

script:
   sunrise:
       alias: Simulate sunrise
       description: 'fade lights to simulate sunrise'
       sequence:        
          - alias: Sun Bulb On
            service: light.turn_on
            data:
              entity_id: light.lohasone
              brightness_pct: 10
              color_temp: 500
           - delay:
               minutes: 3
          - alias: Sun Bulb On
            service: light.turn_on
            data:
              entity_id: light.lohasone
              brightness_pct: 20
              color_temp: 465
           - delay:
               minutes: 3

We need to repeat this another 8 times until our brightness value reaches 100 percent. Our completed code will then have 10 steps, each with the associated incrementation of brightness_pct and color_temp.

Our final color_temp value will be 185, this will be close enough. I have omitted most of the steps for clarity, as the finished code is quite long.

Note that you do not need a delay following the final alias.

script:
   sunrise:
       alias: Simulate sunrise
       description: 'fade lights to simulate sunrise'
       sequence:        
          - alias: Sun Bulb On
            service: light.turn_on
            data:
              entity_id: light.lohasone
              brightness_pct: 10
              color_temp: 500
           - delay:
               minutes: 3
          - alias: Sun Bulb On
            service: light.turn_on
            data:
              entity_id: light.lohasone
              brightness_pct: 20
              color_temp: 465
           - delay:
               minutes: 3
             .
             .
             .
             .

          - alias: Sun Bulb On
            service: light.turn_on
            data:
              entity_id: light.lohasone
              brightness_pct: 100
              color_temp: 185
Bitmoji Image

Create the inputs

In order to trigger our sunrise script we need to add several entries to our configuration.yaml file. We need to add input_datetime, input_boolean and an automation.

Create the booleans

The boolean entity allows us to create a “yes or no” state of our choice that will be remembered by Home Assistant. In programming a boolean is fundamentally a “true or false” state.

We will create a boolean for whether or not our sunrise should occur, this will be our general on/off switch for the function. We will also create one for whether or not the sunrise should occur over the weekend.

First we need to add input_boolean to our configuration.yaml file.

input_boolean:

Next we will define an input boolean for enabling our sunrise. We will name it sunrise_enabled and give it the initial condition “on,” which is the default state of the switch when Home Assistant starts.

Home Assistant uses the Material Design Icons, which makes it very easy to add cool looking icons! We will assign the icon mdi:weather-sunset but you can use any icon you like.

input_boolean:
   sunrise_enabled:
       name: "Morning Sunrise"
       initial: on
       icon: mdi:weather-sunset

Now we will add a second boolean for whether or not the sunrise should occur over the weekend. This gives us the option to enable the sunrise if we need to wake early on the weekend, but you can specify other days if you prefer. We will define the days when we write the automation.

input_boolean:
   sunrise_enabled:
       name: "Morning Sunrise"
       initial: on
       icon: mdi:weather-sunset
   sunrise_weekend:
       name: "Weekend Sunrise"
       initial: off
       icon: mdi:calendar-blank

Create datatime

Now we need to add an entry to configuration.yaml for input_datetime as we wish to trigger our sunrise using time.

input_datetime:

Now we can add the time element sunrise_time. We will only specify time as it is all that is required and we will give it the name “start sunrise at.”

input_datetime:
   sunrise_time:
       name: "Start sunrise at"
       has_time: true
       has_date: false

Create the automation

Now that we have finished our sunrise script and added the necessary input elements, we can tie it all together with an automation. We need to add this to our automations section of configuration.yaml.

automation:

We will be creating an automation with a trigger, condition and action. The trigger will be the time that we set for our sunrise to start. The conditions will be whether or not we have the sunrise enabled and whether or not it is a weekend day. The action will be to start our sunrise lighting script.

Adding the trigger

In order to leverage the time within Home Assistant we will use the platform template.

This is a little more advanced so don’t worry if you don’t understand it, you can simply copy and paste it as is. However if you have used different names in your input_datetime section, be sure to amend sunrise_time within the value_template attribute.

If you want to understand more about templates in Home Assistant, check out the official page.

automation:
   - alias: "Cool artificial sunrise"
     trigger:
       platform: template
       value_template: "{{ states('sensor.time') ==(states.input_datetime.sunrise_time.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"

Adding the conditions

Next we will add our first condition. This will be our boolean that defines whether or not our sunrise should be turned on.

automation:
   - alias: "Cool artificial sunrise"
     trigger:
       platform: template
       value_template: "{{ states('sensor.time') ==(states.input_datetime.sunrise_time.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
  
     condition:
        - condition: state
          entity_id: input_boolean.sunrise_enabled
          state: 'on'

We also need to add a condition for whether or not the sunrise should trigger based upon the day. As I work Monday to Friday I want to add a switch that will allow me to disable the sunrise on Saturday and Sunday. Over the weekend I usually get to stay in bed until the real sunrise!

Bitmoji Image

We need to add an or condition that will tell our automation to run if the sunrise_weekend boolean is true or if it is a weekday.

You can of course specify day you like, for example if you work weekends and have mon and tues off, you can amend the days accordingly.

First lets add the or condition. Afterwards we can create a subset of conditions that will fall into the or catagory.

automation:
   - alias: "Cool artificial sunrise"
     trigger:
       platform: template
       value_template: "{{ states('sensor.time') ==(states.input_datetime.sunrise_time.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
  
     condition:
        - condition: state
          entity_id: input_boolean.sunrise_enabled
          state: 'on'
        - condition: or
          conditions:

Next let’s add our condition for sunrise_weekend boolean, this is the switch that will allow us to turn on and off weekend sunrise.

automation:
   - alias: "Cool artificial sunrise"
     trigger:
       platform: template
       value_template: "{{ states('sensor.time') ==(states.input_datetime.sunrise_time.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
  
     condition:
        - condition: state
          entity_id: input_boolean.sunrise_enabled
          state: 'on'
        - condition: or
          conditions:
             - condition: state
               entity_id: input_boolean.sunrise_weekend
               state: 'on'

Now we can add our second or condition that will trigger the script on our selected days.

automation:
   - alias: "Cool artificial sunrise"
     trigger:
       platform: template
       value_template: "{{ states('sensor.time') ==(states.input_datetime.sunrise_time.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
  
     condition:
        - condition: state
          entity_id: input_boolean.sunrise_enabled
          state: 'on'
        - condition: or
          conditions:
             - condition: state
               entity_id: input_boolean.sunrise_weekend
               state: 'on'
             - condition: time
               weekday:
                  - mon
                  - tue
                  - wed
                  - thu
                  - fri

Adding the action

Finally we need to specify the action that will take place when our automation is triggered.

We will use the service script.turn_on in order to activate our sunrise light script that we created earlier. We also need to specify the entity_id which in this case will be the name of our script.

automation:
   - alias: "Cool artificial sunrise"
     trigger:
       platform: template
       value_template: "{{ states('sensor.time') ==(states.input_datetime.sunrise_time.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
  
     condition:
        - condition: state
          entity_id: input_boolean.sunrise_enabled
          state: 'on'
        - condition: or
          conditions:
             - condition: state
               entity_id: input_boolean.sunrise_weekend
               state: 'on'
             - condition: time
               weekday:
                  - mon
                  - tue
                  - wed
                  - thu
                  - fri

     action:
        - service: script.turn_on
          entity_id: script.sunrise

Create the lovelace card

Now that all of our YAML is complete, we need to add a card to our lovelace interface that will allow us to control the sunrise.

In order to do this you will need to configure your lovelace UI manually. In order to do this you need to click on the three dots in the top right-hand corner of the screen and choose configure UI.

Create a new card

This will display the option to add a new card to lovelace. Click the plus symbol in the orange circle towards the bottom right-hand side of the screen.

You will be presented with a list of the types of card that can be added to the user interface. We need to choose entities (top right-hand corner) in order to add an entity card.

Add the card attributes

In the configuration page go ahead and give your card a title, I called mine Cool Sunrise! We can also disable show header toggle as we do not need the card’s global toggle switch.

We also need to add our three inputs to the entities list. Assuming you used the same naming as in the tutorial, you can simply type “sunrise” and a dropdown list will appear with all of the entities that we have created.

Click save and we are all done! Now go ahead and give your new artificial sunrise a try!

Bitmoji Image

Tasmota fade feature

If you are using Tasmota on your bulb it is possible to switch on the fade option in order to make the sunrise transition smoother.

At the time of writing in Tasmota 8.1 the fade algorithm is still a little choppy in the lower end of the brightness scale. However an improvement has been developed to solve this problem and I am expecting it to become part of a future Tasmota release, so you may just need a little patience for a super-smooth sunrise.

In order to enable the fade algorithm, navigate to your Tasmota configuration page and goto the console. In order to enable fade, type the following command.

fade on

Next we need to set the speed to very slow. The Tasmota does not yet specify what the number actually means in the documentation but the slowest fade is approximately 10 seconds.

speed 20

That’s it! Hopefully this will improve the transition of your sunrise without having to add many step to the light script.

Conclusion

I have never been a morning person and getting up early for work really sucks! The addition of a sunrise script combined with automating my kettle to boil has made things a little less painful when the alarm sounds at 6am.

I am pleased with the functionality, although the fade is not perfect the concept of gradually increasing the brightness in the bedroom really does help me wake up!

I hope you have successfully managed to implement your own version and if I helped make your morning routine that little bit easier then it was absolutely worth me writing this tutorial. Please take a moment to let me know what you thought in the comments below!

Bitmoji Image

Join the discussion

  1. Nidhin Kurian

    I tried this today with a Syska 7W Smart LED tasmotized. Your code works perfectly. Thank you!

    • Siytek

      Very glad to hear it Nidhin! Thanks for visiting ๐Ÿ™‚

      • Nidhin Kurian

        One thing I would like to note, we need to include the below code in config file for the sensor.time in the value template in automation to work, I did not foresee this and lost some time figuring it out ๐Ÿ™‚
        sensor:
        – platform: time_date
        display_options:
        – ‘time’

        • Siytek

          Thanks for the tip Nidhin, but I couldn’t quite see what you meant. Please could you post all of your code here? Maybe HA changed something since I wrote the tutorial.

  2. Tristyn Russelo

    Error loading /config/configuration.yaml: while parsing a block collection
    in “/config/configuration.yaml”, line 220, column 11
    expected , but found ”
    in “/config/configuration.yaml”, line 226, column 12

    Line 220 is
    ” – alias: Sun Bulb On”
    I have a feeling the spacing didnt copy correct to notepad++

    script:
    sunrise:
    alias: Simulate sunrise
    description: ‘fade lights to simulate sunrise’
    sequence:
    – alias: Sun Bulb On
    service: light.turn_on
    data:
    entity_id: light.master_bedroom_lights
    brightness_pct: 10
    color_temp: 500
    – delay:
    minutes: 3

    BLAH BLAH YADA YADA….

    – alias: Sun Bulb On
    service: light.turn_on
    data:
    entity_id: light.master_bedroom_lights
    brightness_pct: 90
    color_temp: 180
    – delay:
    minutes: 3
    – alias: Sun Bulb On
    service: light.turn_on
    data:
    entity_id: light.master_bedroom_lights
    brightness_pct: 100
    color_temp: 140

    not sure where im going wrong.

Leave a Reply

Your email address will not be published. Required fields are marked *

back to top