HELP Nedded: automation to notify before set time

Hi, I have a timestamp from another sensor (jewish_calendar integration) and I want to get notified before that time which is changing every day - but a set time before, like 10 minutes before that time stamp.
How can I achieve that?

Thanks

how often does it change?

one of the options is to use timer:

  • create an automation that fires on your sensor’s state, calculate when you need a notification (calc), start a timer with duration = calc - now, store calc in input_datetime.
  • create an automation that fires on timer’s finished event and in action call your notification service.
  • create another automation that fires on event homeassistant.start and re-calc timer’s duration, if it’s > 0 => start the timer.

What is the format of your sensor’s ‘timestamp’? A full unix timestamp? Just an HH:MM:SS?

  1. Create a time sensor if you don’t have one.
  1. Create a template binary sensor. This is your ‘alarm’ sensor.
binary_sensor:
  - platform: template
    sensors:
      # Turns 'on' when time is between 0 and 10 minutes until another sensor time.
      special_calendar_alarm:
        friendly_name: "Time to Party"
        # Tell it to update when either of these sensors change.
        entity_id: 
          - sensor.time
          - sensor.jewish_calendar_integration_time
        # Convert now to minutes (in a day). Convert other sensor to minutes (in a day). Compare.
        # You'll have to change the %H:%M to the exact output of your sensor time. Google 
        # python strptime to see all of the special letters. Should be all you have to change...
        value_template: >-
          {% set now = strptime(states('sensor.time'), '%H:%M') %}
          {% set now_min = now.hour * 60 + now.minute %}
          {% set then = strptime(states('sensor.jewish_calendar_integration_time'), '%H:%M') %}
          {% set then_min = then.hour * 60 + then.minute %}
          {% set timediff = then_min - now_min %}
          {{ timediff <= 10 and timediff >= 0 }}

Now that sensor is on only if current time is within 10 minutes of the target time. This should handle any cases where the other sensor time updated to within 10 minutes of the current time (no idea how often it changes).

Now just use your alarm sensor like normal


- alias: Alarm
  trigger:
    platform: state
    entity_id: binary_sensor.specal_calendar_alarm
    to: 'on'
  ...
  ...
2 Likes

It is a unix time stamp, and changes every week or so BUT somtimes twice or more in a week, I see why I need to use the input time but I am not sure I fully understant your solutions.
Will try and report, thank you!

I didn’t check the whole template but this could be much more natural imho

{{ 0 <= timediff <= 10 }}

Well, I actually did and in the light of the TS’s comment " changes every week or so BUT somtimes twice or more in a week" the template won’t work unless we use now() and carefully calculate timedelta (as sensor.time does not have day/month).

2 Likes

After a bit of checking it seems that I was wrong and its not a unix timestamp (althogh it has an attribute of it) and it is an iso timestamp.
So as far as i understand i need to create a time date sensor in that same format and compare with “timediff”?
I am trynig to understand the root of it so I can debug it myself and learn on the way

a template sensor which would return True if the current time is within 10 mins from your sensor’s time.

an advice: as your sensor changes rarely, sensor.time won’t do. take tour time checking the docs about time paying attention to this as I believe it’s a good way to learn.
or you can try my initial strategy with timers :wink:

when you have some code and questions, welcome back :wink:

2 Likes

which sensor are you using to get notified for? what time zone are you actually in?

Ended up creating a reformatted time sensor from the original sensor as advised and comparing the two with a template binary as @AhmadK showed.
will report if it workd.

thank you

So it works flawlessly for the past couple of months and it is also very scalable for other cases.
However it keeps giving me that log error and i can see why but I do not understant how to fix it.
please help?

      friendly_name: 'Shabat Half hour Notification Trigger'
      value_template: >-
        {% set now = strptime(states('sensor.time'), '%H:%M') %}
        {% set now_min = now.hour * 60 + now.minute %}
        {% set then = strptime(states('sensor.upcoming_candle_lighting_formatted'), '%H:%M') %}
        {% set then_min = then.hour * 60 + then.minute %}
        {% set timediff = then_min - now_min %}
        {{ 0 <= timediff <= 30 }}
2020-08-12 20:20:00 ERROR (MainThread) [homeassistant.components.template.binary_sensor] Could not render template Shabat Half hour Notification Trigger: UndefinedError: 'str object' has no attribute 'hour'

It gives the error on every sensor that is created this way.
how do I define the “hour”?