Automation During Date Range

Thank you for your answer.
I want to set my heating rules for
1.11. - 31.03.


  - condition: template
    value_template: >
      {% set n = now() %}
      {{ n.month <= 3 and n.day <=31
      or n.month >= 11 and n.day >= 1 }}

  - condition: template
    value_template: "{{ n.month >= 11 or n.month < 4 }}"

No need to use the day in the expression at all.

1 Like

But would it be also right with the days in it?
I want to adjust the days eventually a bit if the weather is cold.

It would be as it is, but not if you change them.

As it is “n.day <= 31” is always true, and “something and True” is the same as “something”, which is why that clause is not necessary. Same with “n.day >= 1” – it’s always true.

But if you change them, then they won’t always be true, and they won’t be true when you want them to be true.

If you really want to be able to use a range of X.11 - Y.03, then:

  - condition: template
    value_template: >
      {{ n.month == 11 and n.day >= X or n.month > 11
         or n.month == 3 and n.day <= Y or n.month < 3 }}
2 Likes

I need to have an automation on during 01 June to 31 August (ideally for every year) but the dates may be vary (eg 15 May to 30 Sep)

I found above the condition code and rearrange it to my needs

it is working but now I am wondering if there is a better way to get there with no need to change the year (2020) every year.
Is there a way?

#Covers Close in SummerTime
- id: Covers Close in SummerTime
  alias: Covers Close in SummerTime
  trigger:
    - platform: numeric_state
      entity_id: sun.sun
      value_template: "{{ state.attributes.elevation }}"
      below: 73
      above: 71

  condition:
    - condition: template # Only between these dates
      value_template: >
        {% set from = '1.6.2020' %}
        {% set to = '30.8.2020' %}
        {{ as_timestamp(strptime(from,"%d.%m.%Y")) 
          <= as_timestamp(now()) <
          (as_timestamp(strptime(to,"%d.%m.%Y"))+60*60*24) }}
  
  action:
    - service: cover.close_cover
      entity_id: cover.50758014840d8e91f036 # erker
    - delay: 
        seconds: '15'
      value_template: >-
        {% set fromts = '06-01' %}
        {% set tots = '08-30' %}
        {{ fromts <= now()|as_timestamp|timestamp_custom('%m-%d', False) <= tots }}

Uses the fact that properly-ordered date strings (I’m looking at you, USA) sort correctly. I’ve renamed your variables as names like from and to are likely to trip you up at some point.

You could enhance further by setting the dates via input_datetimes…

2 Likes

Thanks! I already replace my code.
I will look at the input_datetimes too. (never used it)
Is there a way to have a kind of calendar in lovelace and change the dates accordingly in automation?

Inspired by this thread I have this automation [see bellow] which suddenly is not working as it used to.

The idea is to switch off light sat different times on different time periods defined within conditions.

The code suddenly completely ignores the conditions and fires at any defined triggered time and switches the lights off regardless.

The only thing which has changed on my setup was upgrading to 0.113.x.

I first noticed the strange behavior on the 1st of the August. I first Thought I had a typo in the code but couldn’t find any.

I am wondering if all the new automation features introduced in 0.113 could have had any impact on existing code. Did I miss something? Is there perhaps a new way how to accomplish the same logic?

Could anyone pls check?

- alias: Pool lights off
  id: Pool lights off
  trigger:
  - platform: time
    at: '20:00'
  - platform: time
    at: '21:30'
  - platform: time
    at: '22:00'
  - platform: time
    at: '23:59'
  condition:
    condition: or
    conditions:
      - condition: template
        value_template: >-
          {% set n = now() %}
          {{ now().strftime('%H:%M') == '20:00' and 5 < now().month >= 10 }}
      - condition: template
        value_template: >-
          {% set n = now() %}
          {{ now().strftime('%H:%M') == '21:30' and n.month == 5 }}
      - condition: template
        value_template: >-
          {% set n = now() %}
          {{ now().strftime('%H:%M') == '22:00' and n.month == 6 or n.month == 9 }}
      - condition: template
        value_template: >-
          {% set n = now() %}
          {{ now().strftime('%H:%M') == '23:59' and n.month == 7 or n.month == 8 }}
  action:
  - service: switch.turn_off
    entity_id: switch.shenzhen_neo_electronics_co_ltd_wall_switch_2_channels_switch
  - service: notify.telegram
    data:
      message: Pool lights --> OFF

I use sensors to detect the date (and time) requirements to true or false and then use that as a condition in the automation. It means it’s re-usable (is it Summer?) and I can check it’s state quickly and easily to see if I have it working as expected outside of the automation(s).

This is my Christmas sensor in configuration.yaml;

  - platform: template
    sensors:
      christmas_time:
        value_template: >
          {% set today = states('sensor.date').split('-') %}
          {% set month = today[1]|int %}
          {% set day = today[2]|int %}
          {{ month == 12 and day >= 5 or
             month == 1 and day <= 6 }}
1 Like

Why do you set n and then not use it in the strftime calls?

5 < now().month >= 10 is the same as now().month >= 10. I think you probably mean 5 > now().month >= 10 for “is it between June and October inclusive?”.

The real cause of your problem though, is this:

{{ now().strftime('%H:%M') == '23:59' and n.month == 7 or n.month == 8 }}

The and and or statements are not prioritised in the order you think: this is evaluating as true because it is August (blah or n.month == 8). You want:

{{ n.strftime('%H:%M') == '23:59' and (n.month == 7 or n.month == 8) }}

Do the same for the June & September condition or you’ll have the same problem next month too.

Thanks for your help.

Unfortunately I am still not having any luck still even with the fixes [brackets for correct and and or statements evaluation . Automation will fire but thew switch will not turn off at the time.

Strangest thing is that template editor evaluates the following as True assuming the correct time is present in the statement for the moment of evaluation.

          {% set n = now() %}
          {{ n.strftime('%H:%M') == '23:59' and (n.month == 7 or n.month == 8) }}

Hi
I have a sensor like this:

- platform: template
  sensors:
    makis_azimuth_permonth:
        friendly_name: makis azimuth per month
        #unit_of_measurement: '€'
        value_template: >
          {% if now().month in (1,) %} 100
           {% elif now().month in (2,) %} 110
            {% elif now().month in (3,) %} 120
             {% elif now().month in (4,) %} 130
               {% elif now().month in (5,) %} 140
                 {% elif now().month in (6,) %} 150
                   {% elif now().month in (7,) %} 160
                     {% elif now().month in (8,) %} 170
                       {% elif now().month in (9,) %} 180
                         {% elif now().month in (10,) %} 140
                           {% elif now().month in (11,) %} 200
                             {% elif now().month in (12,) %} 210
                             {% endif %}

Ideally I would like to change the values 3 times per month.

something like this - but I don’t know if it could be done, and for sure I cannot figure out the syntax (if this is the problem)

              {% set oct = now() %}
              {{% if oct.month == 10 and 01 <= oct.day <= 10 %}} 135
              {% set oct = now() %}
              {{ % elif oct.month == 10 and 11 <= oct.day <= 20 }} 140
              {% set oct = now() %}
              {{ % elif oct.month == 10 and 11 <= oct.day <= 31 }} 145  

Any ideas if I can adjust it?

1 Like

Based on @Troon’s solution, here’s an alternative that uses date objects instead of strings for the comparisons. It allows me to express the from/to dates in a more european-style format (day.month). Of course, this doesn’t work across year boundaries either.

value_template: >-
  {% set from = '23.03' %}
  {% set to = '30.09' %}
  {{ strptime(from ~ '.' ~ now().year, "%d.%m.%Y").date()
     <= now().date() <= 
     strptime(to ~ '.' ~ now().year, "%d.%m.%Y").date() }}

Does anyone have a clue how to make a sensor that shows the date for every x day of every month?

So for instance i want to show every 4th Saturday of the month as a sensor?

I’m sure someone will reply with a much more elegant solution, but here’s mine:

{# fom is the first day of the month #}
{% set fom = strptime(now().year|string+"-"+now().month|string+"-01","%Y-%m-%d") %}

{# wof is weekday of fom #}
{% set wof = fom.weekday() %}

{# work out fourth saturday #}
{% if wof == 6 %}
  {% set sat4 = 28 %}
{% else %}
  {% set sat4 = 27 - wof %}
{% endif %}

{# return datetime object for fourth saturday #}
{{ strptime(now().year|string+"-"+now().month|string+"-"+sat4|string,"%Y-%m-%d") }}

I will try it out. Tnx :smiley:

guys can anyone help ? Id like to set a date range between 11 of january to 14 of november all months between included how can i do that? and also one for 15 of november to 10 of january

See my post above:

      value_template: >-
        {% set fromts = '01-11' %}
        {% set tots = '11-14' %}
        {{ fromts <= now()|as_timestamp|timestamp_custom('%m-%d', False) <= tots }}

That will give you a condition that is true for your 11 Jan – 14 Nov date range and false for your second range — so you could use it in a NOT condition to work around the problem of the second range crossing the year boundary.

It works because properly-ordered date/time strings are sortable.

thank you so much for your answer ill test it tommorow and see if it works

sorry bout the latency but i just found some time to test the automation and unfortunately it does not work here it is

trigger:
  - platform: sun
    event: sunset
    offset: +00:21:00
  condition:
  - condition: not
    conditions:
    - condition: template
      value_template: "{% set fromts = '01-11' %}\n        {% set tots = '11-14' %}\n\
        \        {{ fromts <= now()|as_timestamp|timestamp_custom('%m-%d', False)\
        \ <= tots }}"
  action:
  - entity_id: switch.sonoff_1000321709
    service: switch.turn_on
  - after: '23:20:00'
    condition: time
  - entity_id: switch.sonoff_1000321709
    service: switch.turn_off

hope you can tell me why it does not work