Can you use a script in an automation to see if the clock is within the desired time?
I have several motion sensors that I currently only want to activate between 06:00-18: 00. However, it may happen that I want to adjust that time in the future, and then it would have been easier to just change one script, instead of several automations.
I am a beginner and program via UI, obviously below is wrong
The 2 input_datetime can then be reused for your other automation(s) that have the same condition. Also, you can put the input_datetime into your dashboard to be modified easily.
The following is a single automation to control a light based on a motion sensor (turn light on when there’s motion, turn it off when there’s no motion). It can be simplified if it’s acceptable to simply turn the light on without explicitly specifying a brightness_pct value. However, I retained the method you had used.
It can also be easily enhanced so that the command to turn the light on/off is not sent if the light is already on/off.
If you want to adjust the time range, just change the values in condition. The automation turns off the light when there has been no motion for 3 minutes. You can adjust that value to whatever you prefer.
There’s a small window of opportunity for the light to be turned on but not turned off. It can happen near 18:00. If the light is turned on at 17:59, it won’t be turned off 3 minutes later because at that time, 18:02, it will be past the time range and the light will be left on. If that’s a concern, it’s easy to enhance the automation’s logic to ensure the light is turned off.
I manage to set up the automation, as long as I have like your suggestion, the time condition inside the automation.
But I would like to have the time condition at a single place (script?), and then be able to check time condition from several automations. Then I will be able to update time for several automations, on a single place. Do you have a sullotion for that? Thanks!
Now you will have a centralized place to define your “Day Period”. If the current time is between 6:00 and 18:00, the state of binary_sensor.day_period will be on, otherwise it will be off. You can refer to its state in any automation (or script).
This approach is convenient unless you want to frequently change the hours of “Day Period” or allow any user to change it. In that situation, it’s advisable to use ardysusilo’s suggestion which employs input_datetimes to represent the start and end of the “Day Period”. The input_datetimes appear in the Lovelace UI where they are accessible to any user.
You can even combine the two suggestions and make a Template Binary Sensor with ardysusilo’s template.