Script as condition (clock)?

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 :sweat_smile:

Automation:

type: motion
platform: device
device_id: 7c90d2d154c3bb7635f4035460075ec6
entity_id: binary_sensor.kitchen
domain: binary_sensor

condition: state
entity_id: script.daytime
state: ''

type: turn_on
device_id: 6896cc24e2c0c53a6c4e2a4b9231feb2
entity_id: light.kitchen
domain: light
brightness_pct: 100

Script:

condition: time
after: '06:00:00'
before: '18:00:00'

Maybe you can create 2 input_datetime, then you can create a template condition.

condition:
  - condition: template
    value_template: >-
      {{ states('input_datetime_morning') <= as_timestamp(now())|timestamp_custom('%H:%M:%S') < states('input_datetime_evening') }}

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.

alias: example 567
trigger:
- platform: state
  entity_id: binary_sensor.kitchen
  to: 'on'
- platform: state
  entity_id: binary_sensor.kitchen
  to: 'off'
  for: '00:03:00'
condition: '{{ 6 <= now().hour < 18 }}'
action:
- choose:
  - conditions: "{{ trigger.to_state.state == 'on' }}"
    sequence:
    - service: light.turn_on
      target:
        entity_id: light.kitchen
      data:
        brightness_pct: 100
  default:
  - service: light.turn_off
    target:
      entity_id: light.kitchen

NOTE

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.

1 Like

Hi,

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!

You can’t use a script for that, it’s not what they were designed to do.

I suggest you create a Template Binary Sensor like this one:

template:
  - binary_sensor:
      - name: "Day Period"
        state: '{{ 6 <= now().hour < 18 }}'

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).

For example:

alias: example 567
trigger:
- platform: state
  entity_id: binary_sensor.kitchen
  to: 'on'
- platform: state
  entity_id: binary_sensor.kitchen
  to: 'off'
  for: '00:03:00'
condition: "{{ is_state('binary_sensor.day_period', 'on') }}"
action:
- choose:
  - conditions: "{{ trigger.to_state.state == 'on' }}"
    sequence:
    - service: light.turn_on
      target:
        entity_id: light.kitchen
      data:
        brightness_pct: 100
  default:
  - service: light.turn_off
    target:
      entity_id: light.kitchen

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.

Just want to clarify-

condition:
  - condition: template
    value_template: >-
      {{ states('input_datetime_morning') <= as_timestamp(now())|timestamp_custom('%H:%M:%S') < states('input_datetime_evening') }}

The above template only works if for example you want the light to be ON from 06:00 to 18:00.

However, if you have another condition, maybe night lights to be turned ON from 18:00 to 06:00 (tomorrow’s morning), you can change the template to-

condition:
  - condition: not
    conditions:
      - condition: template
        value_template: >-
          {{ states('input_datetime_morning') <= as_timestamp(now())|timestamp_custom('%H:%M:%S') < states('input_datetime_evening') }}

Basically, you add NOT condition so the logic is reversed.