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,
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
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.
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}}
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.
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.
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.