Automation: time switch with conditions

Hi,

my wife wants to have an automation in Home Assistant, that switches a single light.

The conditions for switching on are:

  1. automation is enabled
    I have added a boolean helper:
    input_boolean.automation_moodlight_kitchenwindow_activation

  2. the light shall be switched on depending the value of a list:
    sundown -2 hours
    sundown -1 hour
    sundown
    sundown +1 hour
    sundown + 2 hours
    5 PM
    6 PM
    7 PM
    8 PM
    9 PM
    10 PM

I have added a dropdown helper with the values.
Name of the entity:
input_select.automation_moodlight_kitchenwindow_activation_on

For example:
activation is enabled, 6 PM is selected → kitchenwindow moodllight shall be switched on at 6 PM

  1. Duration shall be selected from another list:
    1 hour
    2 hours
    3 hours
    4 hours
    5 hours
    6 hours

I have added a dropdown helper with the values.
Name of the entity:
input_select.automation_moodlight_kitchenwindow_activation_off

The kitchenwindow moodllight shall be switched off after the selected value.

How do I get this working?
I have set up a card in the right dashboard with a condition:
when input_boolean.automation_moodlight_kitchenwindow_activation is on, the two dropdown list with the conditions are shown.

I have tried to set up an automation, but I dont get it managed.

Post what you have tried ( see Community Guidelines 9 & 11)

From what you have described, one way to approach it is to have individual triggers for each of the item from the first list. Set the ID of each trigger to match the values in the Input Select, so a simple template condition can reject non-selected triggers:

triggers:
  - id: 5PM
    trigger: time
    at: '17:00:00'
  - id: sundown -1 hour
    trigger: sun
    event: sunset
    offset: '-01:00:00'
... more triggers
conditions:
  - condition: state
    entity_id: input_boolean.automation_moodlight_kitchenwindow_activation
    state: 'on'
  - condition: template
    value_template: "{{ trigger.id == states('input_select.automation_moodlight_kitchenwindow_activation_on') }}"
actions:
....

Hi,

I have tried to create an automation, but I got no result that seems to be close to a solution. Thats why I dont had something to show except the card in the dashboard. But I think, that this is uninteresting.

With your input, I have created:

description: ""
triggers:
  - triggers:
      - id: 5PM
        trigger: time
        at: "17:00:00"
      - id: sundown -1 hour
        trigger: sun
        event: sunset
        offset: "-01:00:00"
conditions:
  - condition: state
    entity_id:
      - input_boolean.automation_moodlight_kitchenwindow_activation
    state: "on"
actions:
  - type: turn_on
    device_id: 123xyz
    entity_id: 456abc
    domain: light
mode: single

I know, that there are not all triggers inside and the duration is missing. I will add that when the triggers are right. Are they ok?
I dont think so. Automation will switch on the light if ist is 5 PM or if it is sunset -1 hour.
I want to have it it 5 PM when 5 PM is selected from the list.

Might be easier to get another wife if the automation is too troublesome :stuck_out_tongue:

That is the purpose of the Template condition I posted above… it only allows the actions to be executed if the Trigger ID matches the state of the input… i.e. “5 PM when 5 PM is selected”

Regarding the delay, I would add an action after you turn the light on to set a Timer. Use a second automation that turns the light “off” when the timer finishes (there are examples in the link that show how to use an Event trigger to listen for the timer_finished event).

I dont see the point with the trigger ID.
In helpers, I think that all values from the list have the same ID.

You are right, I will make another automation for switching off, when switching on works.

What are you talking about? I think you may be confusing Entity ID and Trigger ID…

HA Docs - Trigger ID

You are right. I was confusing it. This is my first time with trigger IDs.
With your help, I got the automation for switching on working.
Switching off is missing now.

Do I have to add 6 automations:

If trigger ID is “1 hour” then set duration to 1 hour

If triffer ID is “two hours” then set duration to 2 hours

Or can that be done in one automation?

alias: moodlight_kitchenwindow_deactivation 1hour
description: ""
triggers:
  - trigger: state
    entity_id:
      - light.moodlight_kitchenwindow
    to: "on"
    from: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 1
  - triggers:
      - trigger: event
        event_type: 1 hour
conditions:
  - condition: state
    entity_id: automation.moodlight_kitchenwindow_activation
    state: "on"
actions:
  - delay:
      hours: 1
      minutes: 0
      seconds: 0
      milliseconds: 0
  - action: light.toggle
    metadata: {}
    data: {}
    target:
      device_id: abc123
mode: single

The switching on automation doesnt work correctly.
It switches on at 4 PM, 5 PM, 6 PM, 7 PM, 8 PM 9 PM, 10 PM, 2 hours before sunset, 1 hour before sunset, at sunset, 1 hour after sunset and 2 hours after sunset.

I havent worked with the triggers so I cannot judge, if it is the right way.

I want the light to switch on at the time that is set in the dropdown and not at every time that is possible to choose from the dropdown.

Do the trigger IDs you have assigned exactly match the options available in the Input Select? For example 5PM is not the same as 5 PM and sundown -1 hour is not the same as Sundown -1 Hour… make sure they match.

No.

Don’t use delays, they don’t survive restart or reloading automations… use a Timer. I would also avoid using toggle unless that the actual action you want to happen every time. What if someone turned the light off manually at some point during the 6 hour delay? Do you really want it to turn on?

Turn On automation with Timer start
alias: Moodlight
description: ""
triggers:
  - id: 4 PM
    trigger: time
    at: "16:00:00"
  - id: 5 PM
    trigger: time
    at: "17:00:00"
  - id: 6 PM
    trigger: time
    at: "18:00:00"
  - id: 7 PM
    trigger: time
    at: "19:00:00"
  - id: 8 PM
    trigger: time
    at: "20:00:00"
  - id: 9 PM
    trigger: time
    at: "21:00:00"
  - id: 10 PM
    trigger: time
    at: "22:00:00"
  - id: 2 hours before sundown
    trigger: sun
    event: sunset
    offset: "-2:00:00"
  - id: 1 hour before sundown
    trigger: sun
    event: sunset
    offset: "-1:00:00"
  - id: sundown
    trigger: sun
    event: sunset
  - id: 1 hour after sundown
    trigger: sun
    event: sunset
    offset: "+1:00:00"
  - id: 2 hours after sundown
    trigger: sun
    event: sunset
    offset: "+2:00:00"
  - id: "off"
    trigger: event
    event_type: timer.finished
    event_data:
      entity_id: timer.YOUR_TIMER_ID_HERE
conditions:
  - condition: state
    entity_id: input_boolean.automation_moodlight_kitchenwindow_activation
    state: 'on'
  - condition: template
    value_template: >-
      {{ trigger.id in ['off', states('input_select.automation_moodlight_kitchenwindow_activation_on')] }}
actions:
  - action: light.turn_{{ 'off' if trigger.id == 'off' else 'on' }}
    metadata: {}
    data: {}
    target:
      entity_id: light.YOUR_LIGHT_ID_HERE
  - condition: template
    value_template: "{{ trigger.id != 'off'}}"
  - action: timer.start
    metadata: {}
    target:
      entity_id: timer.YOUR_TIMER_ID_HERE
    data:
      duration: >
        {% set hours =
        (states('input_select.automation_moodlight_kitchenwindow_activation_off')).split()[0]|int(1) %}
        {{hours * 3600}}
mode: queued

Yes. I checked that.
They are exactly the same.

Today, the dashboard selection is:

On: 8 PM
Off: 1 hour

The log of the light says:

switched on by automation
automation_moodlight_kitchenwindow_activation sunset with offset
15:10:42 - 3 hours ago

Sundown was at 17.11.

That means, that the automation was activated by this part of the automation:

      - id: 2 hours before sundown
        trigger: sun
        event: sunset
        offset: "-02:00:00"

The helper is called “2 hours before sundown”.

Does the sequence matter?
In the automation, it is
4 PM
5 PM
6 PM
7 PM
8 PM
9 PM
10 PM
2 hours before sundown
1 hour before sundown
sundown
1 hour after sundown
2 hours after sundown

In the helper list, it is:
2 hours before sundown
1 hour before sundown
sundown
1 hour after sundown
2 hours after sundown
4 PM
5 PM
6 PM
7 PM
8 PM
9 PM
10 PM

When switching on works correclty, I will look for switching of like you said.

Still not working

What does that mean…?

  1. Did it turn on incorrectly?
  2. Did it turn off incorrectly?
  3. Did it not trigger at all?
  4. Something other issue?

No one here has access to your Home Assist instance. We can’t help debug if you don’t share the data…

  • Post the entire automation in it’s current form (properly formatted)
  • Post the entity data for all the input helpers from the States tab of Developer Tools.
  • If the problem is either #1 or #2 above, download and post at least one trace json from a time the problem occurred.