Time_stamp and 1 hour before

This line was the solution. Thanks @tom_l to help me out.
For everyone who struggle as well:

"{{ as_timestamp(utcnow()) | int > (as_timestamp(states.calendar.werk.attributes.start_time)-3600) | int }}"

This is working!

I’m still trying to think of a way to trigger it a bit better than checking every minute. That’s inefficient.

Will get back to you later.

true, but this was the first start of new automation. I’m don’t now how to do this in a different way.

Something like this:
at midnight check what start time will be, then calculate how many hours from midnight to start_time it will be and then countdown something until start_time…

Like this:

- alias: "Notification - Goto Work"
  trigger:
    platform: template
    value_template: "{{ states('sensor.time') == as_timestamp(state_attr('calendar.werk', 'start_time') - 3600)|timestamp_custom('%H:%M') }}"
  action:
    service: script.pushover_engine
    data_template:
      message: "Met een uur moet je naar je werk"
      priority: "0"
      sound: "bike"
      title: "HA Wwerken"

It will require you to create a sensor.time if you have not already. Very handy for other things. See how here:

To explain, we convert your start time to seconds, subtract an hour’s worth of seconds then format the result the same as sensor.time, which has the format HH:MM (e.g. 03:21). We then compare the two strings (not numbers/times). They will only be equivalent (for one minute) once per day, thus only one trigger per day.

Wow, you’re fast. And many thanks for the explain… This help me a lot to understand how it work.
Let me implement this and see how it work for tomorrow!!

I must know when I do a restart of HA then this is gone for that day right?

this automation creates this error:

2020-03-25 12:46:01 ERROR (MainThread) [homeassistant.core] Error doing job: Exception in callback async_track_state_change.<locals>.state_change_listener(<Event state_...739747+01:00>>) at /usr/src/homeassistant/homeassistant/helpers/event.py:81
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 103, in state_change_listener
    event.data.get("new_state"),
  File "/usr/src/homeassistant/homeassistant/core.py", line 372, in async_run_job
    target(*args)
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 130, in template_condition_listener
    template_result = condition.async_template(hass, template, variables)
  File "/usr/src/homeassistant/homeassistant/helpers/condition.py", line 369, in async_template
    value = value_template.async_render(variables)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 221, in async_render
    return compiled.render(kwargs).strip()
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/usr/local/lib/python3.7/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
TypeError: unsupported operand type(s) for -: 'str' and 'int'

I add the new line in develop tool and get this output

"{{ states('sensor.time') == as_timestamp(state_attr('calendar.werk', 'start_time') - 3600)|timestamp_custom('%H:%M') }}"

Unknown error rendering template

Try these in the template editor:

{{ states('sensor.time') }}

{{ as_timestamp(state_attr('calendar.werk', 'start_time')) }}

{{ as_timestamp(state_attr('calendar.werk', 'start_time')) - 3600 }}

{{ (as_timestamp(state_attr('calendar.werk', 'start_time')) - 3600)|timestamp_custom('%H:%M') }}

{{ states(‘sensor.time’) }}

12:56

{{ as_timestamp(state_attr(‘calendar.werk’, ‘start_time’) - 3600 }} - yours with 1 typo

{{ as_timestamp(state_attr(‘calendar.werk’, ‘start_time’)) - 3600 }} - with extra )

1585137600.0

{{ (as_timestamp(state_attr(‘calendar.werk’, ‘start_time’) - 3600))|timestamp_custom(’%H:%M’) }}

Unknown error rendering template

Try:

{{ (as_timestamp(state_attr('calendar.werk', 'start_time')) - 3600)|timestamp_custom('%H:%M') }}

if that works, try:

{{ states('sensor.time') == (as_timestamp(state_attr('calendar.werk', 'start_time')) - 3600)|timestamp_custom('%H:%M') }}

Tried some things and this worked

{{ (as_timestamp(state_attr('calendar.werk', 'start_time')) - 3600)|timestamp_custom('%H:%M') }}

13:00

Right, try this:

{{ states('sensor.time') == (as_timestamp(state_attr('calendar.werk', 'start_time')) - 3600)|timestamp_custom('%H:%M') }}

Error log is gone!

will I lost the countdown thing when I do a HA restart?
Or will it run script also when HA is done after restart?

This will work across home assistant restarts as there is no countdown, just states that are evaluated.

Thanks !! clear to me…

It worked!!! right on time and just 1 notification !!

1 Like

Hi Tom,

A funny other thing happend today. There was no calendar event. So entity attribute is empty.
I see this error popup. Thats normal because it cant do the value_element part, no attribute start_time available …

Can we change something to check if attribute is not empty then do the rest?

2020-03-26 09:20:01 ERROR (MainThread) [homeassistant.core] Error doing job: Exception in callback async_track_state_change.<locals>.state_change_listener(<Event state_...006428+01:00>>) at /usr/src/homeassistant/homeassistant/helpers/event.py:81
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 103, in state_change_listener
    event.data.get("new_state"),
  File "/usr/src/homeassistant/homeassistant/core.py", line 372, in async_run_job
    target(*args)
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 130, in template_condition_listener
    template_result = condition.async_template(hass, template, variables)
  File "/usr/src/homeassistant/homeassistant/helpers/condition.py", line 369, in async_template
    value = value_template.async_render(variables)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 221, in async_render
    return compiled.render(kwargs).strip()
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/usr/local/lib/python3.7/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'

Using this format state_attr('calendar.werk', 'start_time') to get the state of the attribute should prevent errors if the attribute state is unknown, but if the attribute does not exist then there’s a problem.

{{ state_attr('calendar.werk', 'start_time') }}

gives

none

Not everyday there is something in calendar.werk. So output is correct.

1 Like

Ok I can work with that.

- alias: "Notification - Goto Work"
  trigger:
    platform: template
    value_template: >-
      {% if state_attr('calendar.werk', 'start_time') == none %}
        {{ false }}
      {% else %}
        {{ states('sensor.time') == (as_timestamp(state_attr('calendar.werk', 'start_time')) - 3600)|timestamp_custom('%H:%M') }}
      {% endif %}
  action:
    service: script.pushover_engine
    data_template:
      message: "Met een uur moet je naar je werk"
      priority: "0"
      sound: "bike"
      title: "HA Wwerken"

Do you have a great page where I can learn from? Beginners coding yaml :smiley:
I learn also lot of this, but would be nice to learn more. I think there is so many more possible when I learn coding better

I’ll keep record on all changes to GitHub