Date and time conditions

I am trying to create an automation based the alarm set on my iphone, and need help with creating the Home assistant automation.

I’ve created an iOS shortcut script created and modified on the basis of https://wltd.org/posts/how-to-automatically-sync-ios-alarms-with-home-assistant, allowing me to have three sensor - one with the time of day the alarm should normally go off (sensor.bedtime with e.g. a value of 6:00), one with the weekdays the alarms should sound (sensor.bedtime_repeat with a value of e.g. Monday Tuesday Wednesday… - each separated by a line break), and finally one that will give a date and time of an alarm to skip (sensor.bedtime_exception with a value of e.g. 21-01-2023, 06:00).

The automation I am looking to create basically needs to trigger, when there is a match between the current time and sensor.bedtime value, a match between the current weekday and any of the weekdays listed in sensor.bedtime_repeat and there is no match between the current date and the date in sensor.bedtime_exception.

I have the time_date integration enabled, but I really do not know where to start or end in terms of comparing the different values, and am looking for inspiration. I appreciate any input. Thanks.

You’re over thinking this. Use the time trigger, which supports entities as the at: field. And the time condition that allows you to filter based on the day. Plenty of examples in the trigger section of automations and the conditions section of automations.

You’re probably right. I can certainly see that I could point the time trigger to my time entity. And I can try to set a condition pointing to the exception. I think my issue is the sensor I have containing weekday names, and how to extract those and perhaps put them into at time condition in the automation. The format there is e.g. “- mon” for monday, but all I have is a long string with all the weekday names. How do I separate those?

Can you post a screen shot of the sensor entity’s data from the Developer Tools > States tool so we can better understand how the information is formatted to help you solve this issue? Community Guidelines #14

Sure. See below. I can see now that the weekdays are not separated by a space. In a lovelace card they will appear with a space in between, and in iOS the field is definitely with a line break between each day.

Ok, one way to handle that is to use a template condition:

trigger:
  - platform: time
    at:
      - sensor.bedtime
condition:
  - condition: template
    value_template: >
      {% set repeat_day = states('sensor.bedtime_repeat') is search(now().strftime("%A")) %}
      {% set exception_day = now().date() == (strptime(states('sensor.bedtime_exception')+"pm", "%d-%m-%Y, %I:%M%p", 0|as_datetime) | as_local).date() %}
      {{ repeat_day and not exception_day }}
action:
....

EDIT: Corrected missing states('

I did this

Yes, that definitly looks like what I was looking for, and I should be able to work from here. One question - you add ‘pm’ to the bedtime_exception time/date. I believe is it output in a 24h format - would that make a difference?

Thanks a lot for the help - this was exactly the kind of example I was looking for (to also help me learn how to work with string comparison, etc.)

Yes, I was just guessing with the “pm”… since it was a bed time and the value was “06:00”. If that was just a test case and the actual value is 24hr, you need to remove the +"pm" and change %I to %H. The template would then be as follows:

condition:
  - condition: template
    value_template: >
      {% set repeat_day = states('sensor.bedtime_repeat') is search(now().strftime("%A")) %}
      {% set exception_day = now().date() == (strptime(states('sensor.bedtime_exception'), "%d-%m-%Y, %H:%M", 0|as_datetime) | as_local).date() %}
      {{ repeat_day and not exception_day }}

This could also be done without all the time conversions using string manipulation and comparison… it just depends on how you want to approach it.

condition:
  - condition: template
    value_template: >
      {% set repeat_day = states('sensor.bedtime_repeat') is search(now().strftime("%A")) %}
      {% set d = states('sensor.bedtime_exception').split(',')[0].split('-') %}
      {% set exception_day = now().date() | string == d[::-1] | join('-') %}
      {{ repeat_day and not exception_day }}

Nice - thanks a bunch!