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.
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 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.
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)
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.
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?
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