Motion sensor with countdown (Custom widget)
How to configure, using the Custom widget component, a logic that turns on a light on motion and switches it off after a user-adjustable time.
Introduction
This guide explains how to configure a motion sensor logic that, once executed, turns on a light and, after a specific period of time, switches it off automatically. This logic is configured with the Custom widget component. In the KNX world this logic is already present in most motion sensors, among their configuration parameters; it can therefore be useful when the motion sensor is not KNX.
Component configuration
Step 1:
Add the following components to the project:
2 Switch components. You can find this component in the section called Base within the Component library tab;
1 Info component. You can find this component in the section called Base within the Component library tab;
1 Custom widget component. You can find this component in the section called Base within the Component library tab;
1 Adjustable value component. You can find this component in the section called Base within the Component library tab.

Step 2:
Add the variables in the JSON text field. The image below shows the required variables.

JavaScript code configuration
Step 3:
In the JavaScript text field, add the code required for the logic to work. The script is shown below:
Click the following link to view the JavaScript code.

Step 4:
Add the variables in the script: these variables get the value of the component specified in the JSON text, among the parameters of the Custom widget component.

Step 5:
The first function in the JavaScript code is called countDown. This function takes as input the time set by the user in the Adjustable value component and starts the countdown that will switch off the light.

Step 6:
The second function handles the value coming from the motion sensor and turns on the light when motion is detected. When the motion sensor state changes to Off, this function starts a time interval linked to the first function, called “countDown”.

Step 7:
Finally, set the setTrigger: this command runs the function called motion_detection every time the Motion sensor component changes state.

Step 8:
Add the components to the user interface and upload the project to the server.

Visual result
This is the final result of the Motion sensor logic in the Ilevia Remote Plus app.

Click the link below to download the demo project for the motion sensor script with the Custom widget component and try it yourself.