Script with sunrise and sunset times from an online API

How to create in Ilevia Manager a script that retrieves sunrise and sunset times from an online API and activates a Lock component based on the current time.

INTRODUCTION

The following guide explains in detail how to build the script and how it works. The script retrieves information online through HTTP requests to APIs, then processes it and activates the configured components.

Component configuration

Step 1:

Open the Ilevia Manager configuration software and go to the Components section. Open the Home automation menu and add the following components:

Components required to build the script in Ilevia Manager

Step 2:

Open the Script component editor.

How to open the Script component editor in Ilevia Manager

Script overview

Script overview

Step 3:

Create the first variable, which contains the HTTP request for sunrise and sunset times.

Variable containing the HTTP request for sunrise and sunset times

Step 4:

Add the first function, called Sunrise_sunset, which will Parse the payload, represented in this example by the “Payload” variable. Then create a variable called “Sunrisetime_ok”, which contains the parsed sunrise time from the HTTP request. Finally, write the value in component ID 18.

First part of the first function: reading the sunrise time from the HTTP request JSON

Step 5:

The same process is applied to the sunset time: the new variable called “sunsettime” will contain the parsed sunset time obtained from the HTTP request JSON, starting from the “Payload” variable. Finally, set the value in component ID 19.

Second part of the first function: reading the sunset time from the HTTP request JSON

Step 6:

Create the “currenttime” variable, which will make an HTTP request to obtain the current time based on your time zone. Then create the “timesettings” variable, which will parse the JSON coming from the HTTP request and extract the current time from it.

HTTP request for the current time

Step 7:

The next step is to extract the hour, minute, and second values: three new variables are created, each taking part of the information from the JSON. Then these values are assembled into a single time value in the todayTime variable. Finally, the result is written to component ID 20.

Extraction of hours, minutes, and seconds from the HTTP request JSON

Step 8:

The next step determines whether the time is AM or PM. The new variable “period” is used to set the AM or PM label for the current time. Then the current time is converted from 24-hour format to 12-hour format. Finally, the current time is assembled and the value is written to component ID 21.

Conversion of the time from 24-hour format to 12-hour format

Step 9:

To update the Sunrise_sunset function periodically, create a new function called “Over”. This function sets an Interval on the main function, which will run it repeatedly.

Time interval for periodically running the Sunrise_sunset function

Step 10:

The last function in the script changes the value of a Lock component if the current time matches the sunset time. The first variable, “Time digest”, contains the current time; the second, “IF_Pm”, contains the sunset time; the last, “lock”, contains the value of the Lock component. Finally, the function is completed with an If Else logic that changes the state of the Lock component if the current time is before sunset time.

Function that checks whether sunset time has passed and controls the lock

Step 11:

Finally, configure the setTriggers: they are required for the functions to run. The first setTrigger runs the “Sunrise_sunset” function only if component ID 12 changes its state to ON. The second setTrigger runs the “Over” function every time component ID 12 changes state. The last setTrigger runs the “Locking_home” function every time component ID 23 changes state.

Configuration of the setTriggers that start the functions when component states change

Click the link below to download the demo project and try it out.

Download the demo project!

Last updated: