Input Datetime


The input_datetime component allows the user to define date and time values that can be controlled via the frontend and can be used within automations and templates.

To add three datetime inputs to your installation, one with both date and time, and one with date or time each, add the following lines to your configuration.yaml:

# Example configuration.yaml entry
input_datetime:
  both_date_and_time:
    name: Input with both date and time
    has_date: true
    has_time: true
  only_date:
    name: Input with only date
    has_date: true
    has_time: false
  only_time:
    name: Input with only time
    has_date: false
    has_time: true

Configuration Variables

input_datetime

(map)(Required)Alias for the datetime input. Multiple entries are allowed.

name

(String)(Optional)Friendly name of the datetime input.

has_time

(Boolean)(Optional)Set to true if the input should have a time. At least one has_time or has_date must be defined.

Default value: false

has_date

(Boolean)(Optional)Set to true if the input should have a date. At least one has_time or has_date must be defined.

Default value: false

initial

(datetime | time | date)(Optional)Set the initial value of this input, depending on has_time and has_date.

Default value: 1970-01-01 00:00 | 1970-01-01 | 00:00

Attributes

A datetime input entity’s state exports several attributes that can be useful in automations and templates.

Attribute Description
has_time true if this entity has a time.
has_date true if this entity has a date.
year
month
day
The year, month and day of the date.
(only available if has_date: true)
hour
minute
second
The hour, minute and second of the time.
(only available if has_time: true)
timestamp A timestamp representing the time held in the input.
If has_date: true, this is the UNIX timestamp of the date / time held by the input. Otherwise if only has_time: true, this is the number of seconds since midnight representing the time held by the input.

Restore State

This component supports the restore_state function which restores the state after Home Assistant has started to the value it has been before Home Assistant stopped. To use this feature please make sure that the recorder component is enabled and your entity does not have a value set for initial. Additional information can be found in the Restore state section of the recorder component documentation.

Services

This component provides a service to modify the state of the input_datetime.

Service Data Description
set_datetime time This can be used to dynamically set the time.
set_datetime date This can be used to dynamically set the date.

Automation Examples

The following example shows the usage of the input_datetime as a trigger in an automation (note that you will need a time sensor elsewhere in your configuration):

# Example configuration.yaml entry
# Turns on bedroom light at the time specified.
automation:
  trigger:
    platform: template
    value_template: "{{ states('sensor.time') == (states.input_datetime.bedroom_alarm_clock_time.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
  action:
    service: light.turn_on
    entity_id: light.bedroom

To dynamically set the input_datetime you can call input_datetime.set_datetime. The following example can be used in an automation rule:

# Example configuration.yaml entry
# Sets input_datetime to '05:30' when an input_boolean is turned on.
automation:
  trigger:
    platform: state
    entity_id: input_boolean.example
    to: 'on'
  action:
    service: input_datetime.set_datetime
    entity_id: input_datetime.bedroom_alarm_clock_time
    data:
      time: '05:30:00'