Hi. I´m trying to figure out the code to add to an automation so that it runs lifetime (any year), but only if the current day is between 25th June and 14th september (regardless the year number). I´m not having success, any ideas? Thank you very much in advance
Here are two examples that report a message at 08:00
but only if the current date is within the desired range (June 25 to September 14).
Time Trigger with Template Condition
alias: example 1
trigger:
- platform: time
at: '08:00:00'
condition: "{{ (6,25) <= (now().month, now().day) <= (9,14) }}"
action:
- service: notify.persistent_notification
data:
message: 'Today is {{ now().timestamp() | timestamp_custom() }}'
Template Trigger
alias: example 2
trigger:
- platform: template
value_template: "{{ now().hour == 8 and (6,25) <= (now().month, now().day) <= (9,14) }}"
action:
- service: notify.persistent_notification
data:
message: 'Today is {{ now().timestamp() | timestamp_custom() }}'
The part that you want, confirming the current date is within the range, is performed by this:
(6,25) <= (now().month, now().day) <= (9,14)
It creates a tuple consisting of the current month and day then checks if it’s greater or equal to (6,25)
and less than or equal to (9,14)
.
Did the example I posted work for you?
Hi Taras. I’m on business trip, but I’ll be back at home for the weekend. I’m looking forward to check if your code works, I’m sure it will. In any case I’ll keep you updated. Thank you very much for your help. Best regards
Hi. Do you think it would be possible to generate something like a binary sensor or a calendar (something with just “on” or “off” value), with the condition: (6,25) <= (now().month, now().day) <= (9,14)???
This is, if current date is between 6,25 and 9,14, it has a value, and the rest of the days, the other value. My idea is to call it e.g. “summer”, and in my current automations add a condition “summer”. If the condition “summer” has the value “on”, the automationy will run. They wont in the rest of cases. Thank you in advance
template:
- sensor:
- name: My Seasons
state: "{{ iif((6,25) <= (now().month, now().day) <= (9,14), 'Summer', 'Not Summer') }}"
or
template:
- binary_sensor:
- name: Is Summer
state: "{{ (6,25) <= (now().month, now().day) <= (9,14) }}"
Binary sensor used in the two examples I posted earlier:
Time Trigger with Template Condition
alias: example 1
trigger:
- platform: time
at: '08:00:00'
condition: "{{ is_state('binary_sensor.is_summer', 'on') }}"
action:
- service: notify.persistent_notification
data:
message: 'Today is {{ now().timestamp() | timestamp_custom() }}'
Template Trigger
alias: example 2
trigger:
- platform: template
value_template: "{{ now().hour == 8 and is_state('binary_sensor.is_summer', 'on') }}"
action:
- service: notify.persistent_notification
data:
message: 'Today is {{ now().timestamp() | timestamp_custom() }}'
Taras’ direction is spot-on; you could also use the Season integration:
Thank you very much Taras. Works perfect!!!
Also thanks to Terry for the season integration
Great to have experts helping,
Best
Glad to hear it meets your requirements.
Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.
For more information, refer to guideline 21 in the FAQ.
Done. Thank you
You marked your own post which contains no information explaining how to solve the problem.
The purpose of the Solution tag is to mark the first post that answers your question. That’s why I suggested you mark my post containing examples of how to achieve what you requested.
OK, I think now is wright