Input.datetime for a period during the year

I wish to create an automation to turn on/off seasonal decorations.
For example, from 12/01/23 until 01/08/24, during this period my devices will to turn on on at 14:50 (for example) and turn them off at 23:i45.
So my helpers are 12/02/23 14:50 and 01/08/24 23:45.

I play with automation to find if I could to do it. Using a input.datetime helper, I was able to trigger an automation at thr right time ONLY and ONLY if the the date is today (current date).

Any suggestions how to do it without having to create 2 additional helpers, one for the date and one for the time,

Option 1: Static Time triggers

trigger:
  - platform: time
    at: "14:50"
    id: "on"
  - platform: time
    at: "23:45"
    id: "off"
condition:
  - condition: template
    value_template: |
      {% set start = (states(start_dt ) | as_datetime).date() %}
      {% set end = (states(end_dt ) | as_datetime).date() %}
      {{ start <= now().date()  <= end }}
action:
...

Option 2: Template triggers

trigger_variables:
  start_dt: input_datetime.EXAMPLE_start
  end_dt: input_datetime.EXAMPLE_end
trigger:
  - id: "on"
    platform: template
    value_template: |
      {% set start = today_at((states(start_dt) | as_datetime | as_local).time()) %}
      {{ start <= now() }}
  - id: "off"
    platform: template
    value_template: |
      {% set end = today_at((states(end_dt) | as_datetime | as_local).time()) %}
      {{ now() >= end }}
condition:
  - condition: template
    value_template: |
      {% set start = (states(start_dt ) | as_datetime).date() %}
      {% set end = (states(end_dt ) | as_datetime).date() %}
      {{ start <= now().date()  <= end }}
action:
...

Thank you for your quick reply. All this code in such period of time really impress me.

Option 1; Does not meet my requirements. I want to avoid creating additional helpers 1for dates and 1 for time. Hardcoding the time is even worst than 2 helpers for start/end.
This automation that I try to create is the starting point. I have 9 devices that I wish to automate. Some are in outside in front of my house, some in the backyard and others in different rooms in the house. Each one have different date and time to start/end.
Ultimately, I would like one automation with multiple triggers :

  • start christmas tree from 12/15/23 until 01/02/24 every day from 16:30 until 11:30
  • start led on balcony from 12/15/23 until 01/02/24 every day from 16:00 to 12:30
    etc.
    Using naming convention input_datetime.start_‘device_name’ and input_datetime.end_‘device_name’. I think that I can ,with templates , easily determine wihch device can be turn_on/off and only within the date start and end limit.

I am trying to test option 2: When I save the automation, the trigger_variables get move to the end of the automation and I cannot get it to kick in.
Here is my code

description: “”
trigger:

  • id: “on”
    platform: template
    value_template: >
    {% set start = today_at((states(start_dt) | as_datetime |
    as_local).time()) %}

    {{ start <= now() }}

  • id: “off”
    platform: template
    value_template: |
    {% set end = today_at((states(end_dt) | as_datetime | as_local).time()) %}
    {{ now() <= end }}
    condition:

  • condition: template
    value_template: |
    {% set start = (states(start_dt ) | as_datetime).date() %}
    {% set end = (states(end_dt ) | as_datetime).date() %}
    {{ start <= now().date() <= end }}
    action:

  • if:

    • condition: trigger
      id: “on”
      then:
    • service: switch.turn_on
      target:
      entity_id:
      - switch.lampe_salon
      device_id:
      area_id:
      data: {}
  • if:

    • condition: trigger
      id: “off”
      then:
    • service: switch.turn_off
      target:
      entity_id:
      - switch.lampe_salon
      device_id:
      area_id:
      data: {}
      trigger_variables:
      start_dt: input_datetime.start_deco_gosund_5
      end_dt: input_datetime.end_deco_gosund_5

The order does not matter as long as it is properly indented.

There was a typo in the “off” trigger’s template which I have corrected in my post above.
If it isn’t working for you with that fix, describe how you are testing and post your debug traces.

You’re going to need “on” and “off” triggers for each target. And, since it’s the time change that causes the trigger to be true, the value of trigger.entity_id will be none, so every trigger will need a trigger-attached variable to have the necessary value available for both the condition and actions. I have tested the automation below on my instance and it is working as expected for both affirmative and negative date conditions.

trigger:
  - id: "on"
    platform: template
    value_template: |
      {% set time = (states('input_datetime.start_lampe_salon')|as_datetime|as_local).time() %}
      {{ now() >= today_at(time) }}
    variables:
      entity: input_datetime.start_lampe_salon
  - id: "off"
    platform: template
    value_template: |
      {% set time = (states('input_datetime.end_lampe_salon')|as_datetime|as_local).time() %}
      {{ now() >= today_at(time) }}
    variables:
      entity: input_datetime.end_lampe_salon
condition:
  - condition: template
    value_template: |
      {% set start = (states(start_dt)|as_datetime).date() %}
      {% set end = (states(end_dt)|as_datetime).date() %}
      {{ start <= now().date() <= end }}
action:
  - variables:
      target: "{{states[entity].object_id|replace('.start_','')|replace('.end_','')}}"
  - service: "switch.turn_{{ trigger.id }}"
    target:
      entity_id: "switch.{{ target }}"
variables:
  start_dt: "{{iif( trigger.id == 'on', entity, entity|replace('.end_', '.start_'))}}"
  end_dt: "{{iif( trigger.id == 'off', entity, entity|replace('.start_', '.end_'))}}"

HI, I am using a naming convention to control device turn_on and turn_off

  • for starting date/time :input_datetime.start_deco_gosund_5
  • for ending date/time :input_datetime.end_deco_gosund_5
  • for entity : swithch.gosund_5

I need help to complete my condition template. The starting date/time must be greater than the current time now() AND the ending date/time must be greater than current time now().

I wish to use only one helper to determine the other entities to use.
I provide the starting date/time “input_datetime.start_deco_gosund_5”
By replacing “start” with “end” I can determine the ending date/time “input_datetime.end_deco_gosund_5”. I did it in set end_helper.

I would like to determine the device by adding the last character of starting date/time “gosund_5” to the word “switch.” to get the device to turn_on
/off.

Finally I wish to use the define start_helper and end end_helper to get the timestamp from my helper. See line set timestamp_end using {{end_helper}}.

{%- set timestamp_now = as_timestamp(now()) %}
timestamp_now is {{timestamp_now }}

{%- set start_helper = ‘input_datetime.start_deco_gosund_5’ %}
start_helper is {{start_helper}}

{%- set end_helper = ‘input_datetime.start_deco_gosund_5’ |replace (“start”, “end”) %}
end_helper is {{end_helper}}

{%set device = ‘input_datetime.start_deco_gosund_5’ %}
device is {{device}}

{%- set timestamp_start = as_timestamp(states(‘input_datetime.start_deco_gosund_5’)) %}
timestamp_start is {{timestamp_start}}

{%- set timestamp_end = as_timestamp(states(‘input_datetime.end_deco_gosund_5’)) %}
timestamp_end is {{timestamp_end }}

{%- set validation_start = (timestamp_start < timestamp_now < timestamp_end) %}
{%- set validation_end = (timestamp_now < timestamp_end) %}

timestamp_now is > timestamp_start and < timestamp_end {{validation_start}}

How is this different that what I provided in your other thread?

Also, please follow the Community Guidelines and format code blocks properly so that users answering your questions can copy and paste more easily.

Duplicate topics merged.

Work great. I tested multiple scenarios with current/past/future date. Perfect result.
However I had to change 'entity_id: "switch.{{target}} ’ to ‘entity_id: "switch.lampe_salon’.
Target does not produce the right entity name. It generate lampe_salo instead of lampe_salon.

In order to support multiple input_datetime.start_xxx and input_datetime.end_xxx, would it be easier to have 2 automations ?

That seems to be a bug of the trim function… None of my test entities ended with an “n”, so that didn’t show up in my testing. I’ve fixed it by modifying the variable definition below to use replace() instead.

If you fix the issue with the target variable, you only need one automation, just add an “on” and “off” trigger for each target switch. Also, make sure to change the automation mode to either parallel or queued to accommodate overlapping time helpers.

trigger:
  - id: "on"
    platform: template
    value_template: |
      {% set time = (states('input_datetime.start_lampe_salon')|as_datetime|as_local).time() %}
      {{ now() >= today_at(time) }}
    variables:
      entity: input_datetime.start_lampe_salon
  - id: "off"
    platform: template
    value_template: |
      {% set time = (states('input_datetime.end_lampe_salon')|as_datetime|as_local).time() %}
      {{ now() >= today_at(time) }}
    variables:
      entity: input_datetime.end_lampe_salon
  - id: "on"
    platform: template
    value_template: |
      {% set time = (states('input_datetime.start_deco_gosund_5')|as_datetime|as_local).time() %}
      {{ now() >= today_at(time) }}
    variables:
      entity: input_datetime.start_deco_gosund_5
  - id: "off"
    platform: template
    value_template: |
      {% set time = (states('input_datetime.end_deco_gosund_5')|as_datetime|as_local).time() %}
      {{ now() >= today_at(time) }}
    variables:
      entity: input_datetime.end_deco_gosund_5
condition:
  - condition: template
    value_template: |
      {% set start = (states(start_dt) | as_datetime).date() %}
      {% set end = (states(end_dt) | as_datetime).date() %}
      {{ start <= now().date() <= end }}
action:
  - service: "switch.turn_{{ trigger.id }}"
    target:
      entity_id: "switch.{{ target }}"
variables:
  start_dt: "{{ iif( trigger.id == 'on', entity, entity | replace('.end_', '.start_')) }}"
  end_dt: "{{ iif( trigger.id == 'off', entity, entity | replace('.start_', '.end_')) }}"
  target: "{{ states[entity].object_id | replace('start_','') | replace('end_','') }}"
mode: parallel

Hi Drew, the fix for target did not work. However, I changed as follow and now works

  target: >-
    {{entity|replace('input_datetime.start_','')|replace('input_datetime.end_','')}}

One more question since ‘entity’ is defined in variables, Can I use it to define ‘time’

  {% set time = (states('input_datetime.start_lampe_salon')|as_datetime|as_local).time() %}

Doing so, will remove remove duplicate entry and required only entry per device (2 if we count one to start and one to end)

Just popping in here to say:

The simplest thing to do to control lights for a Christmas season is create a template sensor -

 - trigger:
   - platform: time
     at: "00:00:05"
   - platform: homeassistant
     event: start
   - platform: event
     event_type: event_template_reloaded
   binary_sensor:
     - name: Christmas Season
       state: >
         {%- set t = now() -%}
         {%- set m,d = t.month,t.day -%}
         {{ 'on' if ((m == 12) or (m == 1 and d < 7))  else 'off' }}
       icon: mdi:string-lights

Now I have a binary sensor, that I can simply use as a condition in the automation.

Thank you for the suggestion. This will work well if you intent to control all your Christmas devices with the same schedule. I
am having multiple device inside (in different rooms) and outside (back and front).

I need a a very flexible solution easy to change. Using input_datetime helper give me this flexibility and can be change in the dashboard.

With the help from Drew, I am very close to a solution that meets all my requirement.

How often do you expect you’ll need to change the start/end times? The time and duration of the Christmas season doesn’t drift.

Frequently, No justification required. Just ease of use and visibility in my dashboard.
This is also quite a learning experience.

No, because those variables are not available to the trigger.

If it was a static value you can use trigger_variables (like in my first post on this thread), but trigger variables cannot access the state machine, so they aren’t usable for this case. You could probably create some sort of uber-template trigger, by stringing together clauses with or’s but I think the end product would be less readable and require even more complicated templating.

How frequently? Like every day, every three days, or every week?

The Local Calendar integration, and the Schedule integration, is another useful thing to learn.