Alternating Every Second Weeks Boolean Conditions

Hey Everybody,

Been diving in to HASS ever since I fell upon it a couple months ago. It seems like I have something new I want to automate daily.

I share joint custody over my kids, and we have a bit of a convoluted schedule. One week I have them Monday, Tuesday, Friday, Saturday, and Sunday evenings, the next week I have them on Wednesday’s and Thursday’s.

I want to set up a few automations on the condition my kids are either with me or away.

There are two Helpers I set up … one for kids waking up at my place (AM) and one for them sleeping at my place (PM). I see this may be redundant, but figured I would work on that later.

In any case, I have been googling and browsing the forums trying to figure out how I can do this, and I’ve finally given up and asking for help.

I pulled some info to start from this thread: Condition every second week on wednesdays - #3 by datamonkey

In particular the Template code: {(as_timestamp(now())|timestamp_custom ('%U') | int % 2) == 0 }} which will tell me even or odd week.

Without setting up 14 automations, I was hoping I could do this in one well written, elegant code. If my old C++ or Excel experience tell me anything, maybe a lookup table/array?

I did see this thread, but it was of no help: The EPIC Time Conversion and Manipulation Thread!

assuming your week starts on Monday then the following should set up a service to toggle the input boolean every other week on Monday:

service: input_boolean.turn_{{'off' if (now().strftime('%w')| int == 1 and states('input_boolean.my_week') == 'on') else 'on' }}
entity_id: input_boolean.my_week

I am not sure what you want to do here?
A boolean that is TRUE on all the days the kids are at yours?
Maybe you can use a list (or two lists one for odd one for even weeks)

{% set odd_week= { 'Mon':'On', 'Tue':'On', 'Wed':'Off','Thu':'Off','Fri':'On','Sat':'On','Sun':'On'} %}
{% set even_week= { 'Mon':'Off', 'Tue':'Off', 'Wed':'On','Thu':'On','Fri':'Off','Sat':'Off','Sun':'Off'} %}

Use the above to set the Odd/Even week and then check the correct list.

{{ odd_week[‘Wed’] }} should return ‘Off’

I am not at home, can’t test it.

1 Like

I played a bit more, here is my option.
I am sure it could be made simpler.

{% set day_of_week = now().strftime('%a') %}
{%set is_even_week = as_timestamp(now())|timestamp_custom ('%U')  | int % 2 == 0 %} 

{% set odd_week = { 'Mon':'On', 'Tue':'On', 'Wed':'Off','Thu':'Off','Fri':'On','Sat':'On','Sun':'On'} %}
{% set even_week= { 'Mon':'Off', 'Tue':'Off', 'Wed':'On','Thu':'On','Fri':'Off','Sat':'Off','Sun':'Off'} %}
{% set fortnight = {0: odd_week, 1:even_week} %}

{% set KidsToday = fortnight[is_even_week][day_of_week] %}

day: {{day_of_week}}
week {{ as_timestamp(now())|timestamp_custom ('%U')}}
even: {{ is_even_week }}
KidsToday : {{KidsToday}}

paste this in the developer tool template editor and check it out

1 Like

Amazing! Thanks so much. That’s genius :smile:

So should I paste this into a YAML automation? If so, what’s the trigger.

You could make time the trigger. You only want to update once a day.
Pick a time just after midnight.

Since it might come in handy for multiple automations down the line, you should create a state-based template binary sensor which you can use as a trigger or condition in your automations.

Here’s another option for the template. It’s based on the isocalendar, so weeks are Mon-Sun. (If you start your weeks on a different day, let me know and I can alter the template)

  - binary_sensor:
      - name: "Kids Here Today"
        state: >
          {% set is_even = (now().date().isocalendar().week) % 2 == 0 %}
          {{ now().isoweekday() in [3, 4] if is_even == True else now().isoweekday() in [1, 2, 5, 6, 7]}}

For use in your automations, this sensor (binary_sensor.kids_here_today) will be “on” on days when the kids are supposed to be with you, and “off” when they are away.

If your “even weeks” are opposite the iso standard, just change the “0” in the first line of the template to a “1”. For reference, today is 2021-11-25 it is currently Week 47.

1 Like

I guess I was right ! :slight_smile:

@Didgeridrew’s answer is much more compact.

1 Like

Thanks to both of you!

This community is amazing. Now I’m on to figuring out how to do some math around a MQTT timestamp!

If you wish, you can trim off a few superfluous bits:

{% set is_even = now().isocalendar().week % 2 == 0 %}
{{ now().isoweekday() in [3, 4] if is_even else now().isoweekday() in [1, 2, 5, 6, 7]}}

or all in one line …

{{ now().isoweekday() in [3, 4] if (now().isocalendar().week % 2 == 0) else now().isoweekday() in [1, 2, 5, 6, 7]}}

but there is a fine balance between the readability/maintainability of the code and how compact it should be.

1 Like

Sorry to dig up an old thread but I am wanting to do a very similar thing as in I want to do the following.

My other half’s kids go to their Dad’s every Thursday and every other Fri, Sat, Sun so I want to have a routine that turns down their smart TRV’s on the alternating weeks when they are not here.

Can I ask within this code, where does it know which TRV the automation refers to?

Also, where do I put this code within HA?

Thank you.

The sensor configuration provided above is just for differentiating whether the current day is is a day matches the desired criteria, it does not address setting a TRV. That would be handled by an automation. Generally speaking, the sensor would be used as a condition, but exactly how someone would use it really depends on the overall design of their automation.

In a standard setup, this template sensor configuration would be placed in the configuration.yaml file. However it can be placed in any file as long as that file is properly included in the configuration. See Splitting Up the Config