Determining week for automations?

Hi all,
I would like to setup an automation to run every second week for putting out the bins
I would want two automation, one for waste and green, and another automation for waste and recyclables.
How can I determine the week, whether it is an odd or even week so I can run the correct automation?

If it’s as simple as one week = A, next week = B, next week = A…

Create an input_boolean, have an automation that toggles it once a week, have your green bin automation fire when the boolean is switched on, have the black bin automation fire when the boolean is switched off. Set the boolean to the correct week today and then hide it.

Should be good to go.

1 Like

Hmm… but how will i toggle the boolean? How will it know which week it is?
its like I need to tell it for odd weeks, turn on boolean, and for even weeks turn off boolean.
I only want it to be on when on an odd week?

A separate automation that triggers every seven days, with an action to toggle the boolean :thumbsup:

still struggling on this one. If the server is restarted, the boolean will change not be based on absolute level but relative to when the server restarted, so it would tell me to but my green waste out when i should be putting out the recyclables LOL
Thoughts?

If you do not set initial state for the boolean, then it will remember its previous state on reboot. I’m presuming that you’re not expecting a reboot to take over a week, so unless the server is down at the exact day and time you specify for the boolean to change every week there shouldn’t be an issue.

Another option is to use the Google calendar component, set up a repeating entry every fortnight for each bin on your calendar and use the event as a trigger for your automations.

This is what I would have suggested if it was more complicated than a one week this, next week that affair.

Oh!! perfect thanks. I’ll give it a go. many thanks for the gentle push in the right direction. Only issue is now i need to wait a week as the bins went out last night :slight_smile: LOL

1 Like

Have a look here.
Not mine, but I use it a lot for inspiration and he has set up exactly what you’re looking for.
https://github.com/skalavala/smarthome/tree/master/packages

1 Like

I use a template_sensor which I got the inspiration from this post:

My Home Assistant code looks like

value_template: '{% if (now().isocalendar()[1]/2)|round(0)==(now().isocalendar()[1]/2)%}true{%else%}false{%endif%}'

Hope that helps

2 Likes

perfect thanks guys!

Here’s another variation. I have an automation that triggers every day but this condition prevents it running on odd weekends.

    - condition: template
      value_template: >
        {% set val = now() %}
        {{ not( ((val.strftime("%a")=="Sat" ) or (val.strftime("%a")=="Sun" )) and (((val.isocalendar()[1])%2)==0))  }}

To test a specific date, change now() to strptime("2017-09-07", "%Y-%m-%d"). Change the 0 to a 1 if you want even.

1 Like

For some reason this isn’t working for me. I made the following sensor in my configuration.yaml:

      even_week:
        friendly_name: Even Week
        value_template: '{%if (now.isocalendar()[1]/2)|round(0)!=(now.isocalendar()[1]/2)%}true{%else%}false{%endif%}'

I get the following error in my log. I am using hassio version 0.62.0

2018-01-29 21:17:34 ERROR (MainThread) [homeassistant.components.sensor.template] Could not render template Even Week: UndefinedError: 'function object' has no attribute 'isocalendar'

I got the same error, can anybody help?

I have an automation that triggers every 4th Sunday as I get up early and it makes sure my coffee maker is ready for me. You could modify it for your purpose…

- action:
  - data:
      entity_id: switch.coffeemaker
    service: switch.turn_on
  alias: Every 4th Sunday Switch Coffee Maker On at 7:15am
  condition:
  - condition: template
    value_template: '{{((as_timestamp(now()) - as_timestamp("2017-09-24 00:00:00"))/86400)
      | int % 28  == 0}}'
  id: '1506314379417'
  trigger:
  - at: 07:15:00
    platform: time

The 24-09-2017 was when I started it and it just works out if it’s been 4 weeks (MOD28=0)

It’s worked without missing a beat since september :slight_smile:

3 Likes

What version of home assistant are you running? I am running HassIO, maybe that explains the difference in what we are seeing. It definitely doesn’t work for me.

Here’s another one. :slightly_smiling_face:

That last one seems to work for me, thanks!

Thanks!
Would like to apply for odd or even days of the month

To get 0 for even weeks and 1 for odd weeks, use {{((now().strftime("%W"))|int)%2}}

If you need to compensate for wrong week numbers (there are several ways to calculate week numbers in the world), you can use something like {{((now().strftime("%W"))|int+1)%2}} for Sweden.

1 Like