Condition every second week on wednesdays

Did this work for you?

I intergrade Google calender instead.

Hi Henrik,

I’m also using this component, but have you managed to do some sort of Automation based og events from your calendar?

Yes!

alias: Service - notifikation om soptunnan på dagen
trigger:
  platform: state
  entity_id: calendar.privat
  to: 'on'
condition:
  condition: template
  value_template: "{{states.calendar.privat.attributes.message == 'Ställ ut soporna' }}"
action:
  - service: notify.mypushbullet
    data:
      title: Kom ihåg
      message: Ställ ut soptunnan
      target: email/[email protected]
  - service: notify.mypushbullet
    data:
      title: Kom ihåg
      message: Ställ ut soptunnan
      target: email/[email protected]
  - service: notify.lg_webos_smart_tv
    data:
      message: Ställ ut soptunnan idag
1 Like

Thanks!
Works perfectly!

Thanks @datamonkey. That worked for me. For future searchers here are 2 automations that fire on alternate weeks using the value_template to identify the week number…

- id: '111' 
  alias: Email Bin Night Blue
  trigger:
    platform: time
    at: "17:00:00"
  condition:
    - condition: time
      weekday:
      - tue
    - condition: template
      value_template: "{{(as_timestamp(now())|timestamp_custom ('%U') | int % 2) == 1 }}"
  action:
  - data:
      message: Take out the bins. Tonight it is Recycling (Blue)
      title: Bin Night! (blue)
    service: notify.gmail

- id: '222' 
  alias: Email Bin Night Red
  trigger:
    platform: time
    at: "17:00:00"
  condition:
    - condition: time
      weekday:
      - tue
    - condition: template
      value_template: "{{(as_timestamp(now())|timestamp_custom ('%U') | int % 2) == 0 }}"
  action:
  - data:
      message: Take out the bins. Tonight it is Green Waste (Red)
      title: Bin Night! (red)
    service: notify.gmail

7 Likes

thank you using your template this works great.
YOU ROCK.

thank you for this template
you rock.

If you’re interested, you can reduce this:

value_template: "{{(as_timestamp(now())|timestamp_custom ('%U') | int % 2) == 1 }}"

to this:

value_template: '{{ now().isocalendar().week is odd }}'

Both templates do the same thing; they determine if the current week number is an odd number.

1 Like

here is mine for reminder to take cans and if both up. plays a video on tv and announcement on google mini.

- id: '1650599030716'
  alias: trash and recycle reminders calendar even days
  description: 'play video on TVS and announcement on google mini''s to take up green
    recycle and black trash can. '
  trigger:
  - platform: time
    at: '18:29:00'
  condition:
  - condition: time
    weekday:
    - wed
    before: 00:00:00
    after: 00:00:00
  - condition: template
    value_template: '{{(as_timestamp(now())|timestamp_custom (''%U'') | int % 2) ==
      0 }}'
  action:
  - service: media_player.play_media
    target:
      entity_id: media_player.nestmini7847
    data:
      media_content_id: media-source://tts/cloud?message=both+cans+need+to+go+up+this+week+see+alert+on+tv&language=en-US&gender=female
      media_content_type: provider
    metadata:
      title: both cans need to go up this week see alert on tv
      thumbnail: https://brands.home-assistant.io/_/cloud/logo.png
      media_class: app
      children_media_class:
      navigateIds:
      - {}
      - media_content_type: app
        media_content_id: media-source://tts
      - media_content_type: provider
        media_content_id: media-source://tts/cloud?message=both+cans+need+to+go+up+this+week+see+alert+on+tv&language=en-US&gender=female
  - service: media_player.play_media
    target:
      entity_id: media_player.livingroomtv
    data:
      media_content_id: media-source://media_source/local/bothcans.mov
      media_content_type: video/quicktime
    metadata:
      title: bothcans.mov
      thumbnail:
      media_class: video
      children_media_class:
      navigateIds:
      - {}
      - media_content_type: app
        media_content_id: media-source://media_source
  - service: media_player.play_media
    target:
      entity_id: media_player.bedroom_tv
    data:
      media_content_id: media-source://media_source/local/bothcans.mov
      media_content_type: video/quicktime
    metadata:
      title: bothcans.mov
      thumbnail:
      media_class: video
      children_media_class:
      navigateIds:
      - {}
      - media_content_type: app
        media_content_id: media-source://media_source
  mode: single
- id: '1650637963908'
  alias: 'trash  reminders  ODD week '
  description: 'play video on TVS and announcement on google mini''s to take up only
    black trash can. '
  trigger:
  - platform: time
    at: '18:29:00'
  condition:
  - condition: time
    weekday:
    - wed
    before: 00:00:00
    after: 00:00:00
  - condition: template
    value_template: '{{(as_timestamp(now())|timestamp_custom (''%U'') | int % 2) ==
      1 }}'
  action:
  - service: media_player.play_media
    target:
      entity_id: media_player.nestmini7847
    data:
      media_content_id: media-source://tts/cloud?message=Garbage+only++needs+to+go+up+this+week+see+alert+on+tv&language=en-US&gender=female
      media_content_type: provider
    metadata:
      title: Garbage only  needs to go up this week see alert on tv
      thumbnail: https://brands.home-assistant.io/_/cloud/logo.png
      media_class: app
      children_media_class:
      navigateIds:
      - {}
      - media_content_type: app
        media_content_id: media-source://tts
      - media_content_type: provider
        media_content_id: media-source://tts/cloud?message=Garbage+only++needs+to+go+up+this+week+see+alert+on+tv&language=en-US&gender=female
  - service: media_player.play_media
    target:
      entity_id: media_player.livingroomtv
    data:
      media_content_id: media-source://media_source/local/garbage.mov
      media_content_type: video/quicktime
    metadata:
      title: garbage.mov
      thumbnail:
      media_class: video
      children_media_class:
      navigateIds:
      - {}
      - media_content_type: app
        media_content_id: media-source://media_source
  - service: media_player.play_media
    target:
      entity_id: media_player.bedroom_tv
    data:
      media_content_id: media-source://media_source/local/garbage.mov
      media_content_type: video/quicktime
    metadata:
      title: garbage.mov
      thumbnail:
      media_class: video
      children_media_class:
      navigateIds:
      - {}
      - media_content_type: app
        media_content_id: media-source://media_source
  mode: single

Your first automation is named:

trash and recycle reminders calendar even days

If it’s meant to only run on even days, why is it using the Template Condition for determining odd weeks?

Or is the automation’s name not indicative of what it’s supposed to do?

well it looks like i copied my test YAML and not my working one. and did not change the == 1 to ==0

thanks for the catch

The problem with using week number is that if you have 53 weeks then you will get two odd weeks after each other.

                                                   | Because I'm 1 hours ahead of UTC (I disregard summertime because the int will flatten it out anyways), replace this with your timezone
{{ ((now().timestamp() | int-(86400*5)-(now().hour-1)*3600) / 604800) |int is odd}}
                                ^ because 1970-01-01 was a Thursday 

Calculates the number of weeks from UTC and thus should give you a correct every other week condition.

1 Like

The following template computes the number of days since the Unix Epoch and divides by 7.

{{ ((now() - '19700101T00Z' | as_datetime).days // 7) is odd }}

However, according to an online resource, there have been 13 leap years since 1970. Merely dividing the total days by 7 doesn’t take leap years into account therefore the computed result is a value that isn’t guaranteed to align with the boundaries of the current calendar week.

Nevertheless, it’s probably ‘good enough’ for this application because it performs the check on a Wednesday (i.e. at the middle of the calendar week and not at its boundaries which aren’t necessarily aligned with the formula’s concept of a week).


EDIT

Correction. Integer division operator is //

1 Like

I think this is the correct approach anyways. Personally, I don’t care if it’s an odd or even week. I just want every other week from when I start doing it. A date input makes sense and it would fire every other week from that date.

I think it should still work.
A leap day does not make the week longer, only the month. :slight_smile:

PS: Leap days are special to me, as that is my birthday :birthday:

1 Like

Today is Monday October 17.

The first template reports the start of the next week is in 7 days.

{{ (now() + timedelta(days=7)).isocalendar().week }}

The second template indicates that the next multiple of 7 days starts in 3 days.

{{ (t + timedelta(days=3)).days // 7 }}

The second template’s result is not aligned with calendar weeks (the Unix Epoch doesn’t start at the beginning of a calendar week plus there are 13 additional days due to leap years; elapsed days since the epoch divided by 7 doesn’t align with calendar weeks). As I mentioned previously, that offset can affect one’s application (it depends on the application).

Lastly, the original example I posted, of the Unix epoch calculation, used the wrong division operator. We want integer division so that’s // not %. I have corrected the original example posted above.


NOTE

If you want to experiment, copy-paste this into the Template Editor.

{{ (now() + timedelta(days=0)).isocalendar().week }}
{{ (now() + timedelta(days=1)).isocalendar().week }}
{{ (now() + timedelta(days=2)).isocalendar().week }}
{{ (now() + timedelta(days=3)).isocalendar().week }}
{{ (now() + timedelta(days=4)).isocalendar().week }}
{{ (now() + timedelta(days=5)).isocalendar().week }}
{{ (now() + timedelta(days=6)).isocalendar().week }}
{{ (now() + timedelta(days=7)).isocalendar().week }}
{{ (now() + timedelta(days=8)).isocalendar().week }} 

{% set t = now() - '19700101T00Z' | as_datetime %}
{{ (t + timedelta(days=0)).days // 7 }}
{{ (t + timedelta(days=1)).days // 7 }}
{{ (t + timedelta(days=2)).days // 7 }}
{{ (t + timedelta(days=3)).days // 7 }}
{{ (t + timedelta(days=4)).days // 7 }}
{{ (t + timedelta(days=5)).days // 7 }}
{{ (t + timedelta(days=6)).days // 7 }}
{{ (t + timedelta(days=7)).days // 7 }}
{{ (t + timedelta(days=8)).days // 7 }}

That is what I believe my template takes care of.
I believe if we set the template correctly now, then it will last for years before needing a calibration again since the week change is in the middle of the night.
I don’t believe too many of us get up at midnight to roll out the bins :wink:

I repeated the test I performed above using your template (adjusted for my timezone) and it starts a new ‘week’ tomorrow (i.e. the calculated value goes from odd to even on Tuesday).

Should be OK for this application because it checks on Wednesdays. However, as I explained previously, because it’s not aligned with a calendar week it may present a problem for some applications.

For example, if one application checks on a Monday and another on a Friday, the template will report odd on Monday and even on Friday despite the fact both days are within the same calendar week. That’s because its concept of a ‘week’, a period spanning 7 days, is not aligned with a calendar week.

Taking under advisement that it may not work for all situations (where the start of the week or year boundary matters)… You can use modulus to do other week skips as well:

# Every 2 weeks
{{ ((now() - '19700101T00Z' | as_datetime).days // 7) % 2 == 0 }}

# Every 3 weeks
{{ ((now() - '19700101T00Z' | as_datetime).days // 7) % 3 == 0 }}

# Every 7 weeks
{{ ((now() - '19700101T00Z' | as_datetime).days // 7) % 7 == 0 }}

1 Like