APNT-29 - Extending Fibaro HC2 Sunrise/Sunset capabilities

The Fibaro Home Center 2 has a few limitations on the Sunset and Sunrise times you can use in your scenes. For instance you can select 15, 30, 45 and 60 minutes before sunrise and 15, 30, 45 and 60 minutes after sunset. But you can't select 60 minutes before sunset or any other values.

It is actually very easy to change a single line of LUA code to extend the Sunset/Sunrise times to any time you like.

As an example we will use the Sunset scene we used in Application Note 28 -

Sunset On Scene

 

This scene turns on a light exactly at Sunset. This works well for outside lights, but if you used the same scene for an indoor light you would probably want it to be activated before sunset as it gets darker inside a building.

So lets change this so the scene is activated 1 hour before Sunset.

View the LUA Code

To do this we need to change one line of the LUA code that actually runs this scene.

  • In the Scene's Advanced tab go to the Switch scene edit mode to LUA base
  • Click 'Change'
  • The LUA code is displayed in a HC2 window (shown below)

HC2 LUA Scene Code

 

  • The line of code we're interested in is on Line 14.

( ((currentDate.wday == 2 or currentDate.wday == 3 or currentDate.wday == 4 or currentDate.wday == 5 or currentDate.wday == 6) and string.format("%02d", currentDate.hour) .. ":" .. string.format("%02d", currentDate.min) == fibaro:getValue(1, "sunsetHour")) )

 

  • To change this so that the scene is activated 60 minutes before sunset, we change line 14 to:

( ((currentDate.wday == 2 or currentDate.wday == 3 or currentDate.wday == 4 or currentDate.wday == 5 or currentDate.wday == 6) and os.date("%H:%M", os.time()+60*60) == fibaro:getValue(1, "sunsetHour")) )

 

  • The code that controls the time before or after sunset is marked in bold - os.time()+60*60)
    • If this is positive '+' the scene will activate before Sunset, if negative '-' it will activate afterwards
    • The time before/after is determined by the numbers, '+60*60' in this example activates 60 minutes before sunset, 90 minutes would be '+90*60'
  • Note: To test this code I set the original scene to 1 hour after Sunset. The LUA code for that scene was identical to new code we have used above except for the time being os.time()-60*60). So by simply changing the section of code to os.time()+60*60) we now have a scene that runs 1 hour before sunset.

Change the LUA Code

To change the LUA code to use our new line of code (line 14) we need to turn on the LUA Editing mode.

Important - I would suggest you do this after you have the rest of the scene working correctly - you cannot go from the LUA based editor back to the Graphic Block editor.

When you are ready - change the editor to LUA based.

  • If you're not already in the LUA section of the 'Advanced' tab go to the Switch scene edit mode to LUA base, click 'Change'
  • Click 'Turn On'

HC2 LUA Editor

 

  • Click 'OK' in the warning dialog
  • Change line 14 to the new code
  • Click the 'Save' button on the right-hand menu

HC2 final LUA Code


 

The scene will now run 60 minutes before sunset.

Using this simple code change we can have our scenes running before or after Sunset/Sunrise and at what ever time we like.

Related Information