Choose time to run automaton

Is there a way we can manually choose a time when to run an automation? I want to create an automation to turn certain lights off and dim some other lights and leave a few lights on after dark. However, I want to be able to run those automations by choosing the time from within the Lovelace interface. Is that possible?

Yes.

Here’s one possible way:

  trigger:
    platform: template
    value_template: "{{ states('sensor.time') == states('input_datetime.my_on_time')[0:5] }}"

The input datetime should be time only (no date). e.g.

my_on_time:
  name: My On Time
  has_date: false
  has_time: true

This is the entity you add to the Lovelace front end.e.g. in an entities card.

This is what I have, but I can’t figure out how to create a card from where I can choose the date and time.

automation.yaml

- id: 'Set Time'
  alias: Set Time
  trigger:
    platform: template
    value_template: "{{ states('sensor.time') == (states.input_datetime.set_time.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
  action:
  - service: lights.turn_off
    data_template:
      entity_id: light.myswitch

configuration.yaml

input_datetime:
  set_time:
    name: Set Time
    has_date: false
    has_time: true

Add input_datetime.set_time to an entities card.

I totally forgot to reboot HA after I created the input. Opps!

I got the entity card to show the automation, but it doesn’t turn the light off. Should I be using homeassistant.turn_off service?

You can use either the light or homeassistant service for a light.

I thought you wanted to show the input datetime?

I am trying to use input to set the time when the automation is to run.Capture

It looks like you succeeded. What’s the problem?

When I set the time it doesn’t fire the automation. The light doesn’t turn off.

Is the automation enabled?

The automation was on, but I did get it working. Here is how my code looks:

Input:

# Input Time
input_datetime:
  set_time:
    name: Set Time
    has_date: false
    has_time: true

Automation:

# Run automation at a time chosen
- id: 'Set Time'
  alias: Set Time Mode
  trigger:
    platform: template
    value_template: "{{ states('sensor.time') == (states.input_datetime.set_time.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
  action:
  - service: homeassistant.turn_off
    entity_id: switch.office_lights

Lovelace Card:

title: Set Time
type: entities
show_header_toggle: false
entities:
  - entity: automation.turn_on_set_time_mode
  - entity: sensor.time
    name: Current Time
  - entity: input_datetime.set_time
    name: Start Automation At

My question now is how do I add more entities to the automaton with additional actions. For example, I want to add kitchen lights dim to 50%

So the problem was that you were trying to turn off a switch with the light turn off service.

Entity ids are defined like this: domain.object_id you have to make sure you use the right service for the domain, switch.turn_off/on for switch.something, light.turn_on/off for light.something, homeassistant.turn_on/off for mixed domain or anything that can be turned on or off.

You can easily add more actions by adding the services as a list:

  action:
  - service: switch.turn_off # for switch domain 
    entity_id: switch.office_lights
  - service: light.turn_off # for light domain
    entity_id: light.kitchen_lights 
  - service: etc...

Another way for entities with the same domain is to add the entity_ids as a list:

  action:
  - service: switch.turn_off
    entity_id:
    - switch.office_lights  # all entities are in the switch domain.
    - switch.another_switch
    - switch.etc...

For mixed domains use the homeassistant turn off service if adding entities as a list:

  action:
  - service: homeassistant.turn_off
    entity_id:
    - switch.office_lights # switch domain
    - light.kitchen_lights # light domain
    - automation.something # even disable automations with this service

There are even ways to add entities as comma separated lists but that seems buggy for some services so I’ll leave that out for now.

For your specific question about dimming the kitchen lights as well:

  action:
  - service: switch.turn_off 
    entity_id: switch.office_lights
  - service: light.turn_on
    entity_id: light.kitchen_lights # must be a light, not a switch
    brightness: 128
  - service: another if you need it here...
2 Likes

This is very detailed and very well put together explanation. It makes a lot of sense. THANK YOU!!!

For the dimming, if the light is already on do I still use light.turn_on service if I want to dim it to a certain percent?

Yes you do. Unless it is a LIFX light. They have a special set_state service that allows you to set the options even with the off (so when it turns on it has the required brightness and colour). But as I said, only for LIFX lights.

Got it. I will play around with it. Thanks again.

Is there a way I can have a drop down of set times instead of manually entering times?
Capture

Instead of an input_date_time you could use an input_select for a drop down list.

input_select:
  set_time:
    name: Set Time
    options:
      - '0:00'
      - '1:00'
      - '2:00'
      ... all the way down to
      - '22:00'
      - '23:00'

Depending on the resolution required this could be a very long list of options. So unless you only have a few preferred times this is probably a bad idea.

Another way could be with two input_numbers (sliders), one for hours one for minutes. If you search the forums for ‘alarm clock’ you will find a few implementations of this. e.g. Creating a alarm clock

Note: you will have to adjust your automation trigger template for both these methods.

If I make this change I will need to reboot home assistant?

Yes.

There are a few reload services that you can use without restarting (you must have Advanced mode on iny our user profile to see this):

Screenshot_2019-12-08 Home Assistant

But for everything else you need to restart if you have made a change. e.g. input_numbers, binary_sensors, sensors, input_datetimes, etc…

Don’t forget to do a configuration check before reloading or restarting. If you find restarting a pain add your vote here: