Creating a Sensor from Input Datetime Helper

Hello!

I am trying to create a sensor based off of a Datetime Helper but I am getting invalid config errors. My code is:

  - sensor:
    - Name: Manually Set Wake Up
      unique_id: manually_set_wake_up_test
      state: "{{ states('input_datetime.shacharit)|as_datetime - timedelta(hours = 1)}}"
      device_class: timestame

Thank you in Advance!

Please also share the full error.

Based on the snippet I guess it must be the capitalized Name key that should be fully lowercase.
Other things could be an incorrect indentation due to includes or placement in the correct file (we cannot see in the snippet).

Your example is missing a single quote after the word shacharit and timestamp is misspelled. In addition it should ideally report the date and time in ISO format in order to meet the needs of a timestamp sensor.

- sensor:
    - Name: Manually Set Wake Up
      unique_id: manually_set_wake_up_test
      state: "{{ (states('input_datetime.shacharit')|as_datetime - timedelta(hours = 1)).isoformat() }}"
      device_class: timestamp

Be advised that if the value of input_datetime.shacharit is just a time with no date then the template you created will not work.

If it only reports time, use this version:

- sensor:
    - Name: Manually Set Wake Up
      unique_id: manually_set_wake_up_test
      state: "{{ (today_at(states('input_datetime.shacharit')) - timedelta(hours = 1)).isoformat() }}"
      device_class: timestamp

@123 @basbrus
Adding @123 's recommended time only code caused a whole lot of errors in my other template sensors as well.

Home Assistant Core
2024-04-15 06:35:12.708 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template<template=({{ state_attr('weather.forecast_home_celsius_met', 'temperature') | float }}) renders=2>
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2218, in forgiving_float_filter
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 586, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2545, in _render_with_context
return template.render(**kwargs)
File "<template>", line 1, in top-level template code
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2221, in forgiving_float_filter
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1849, in raise_no_default
ValueError: Template error: float got invalid input 'None' when rendering template '{{ state_attr('weather.forecast_home_celsius_met', 'temperature') | float }}' but no default was specified
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 712, in async_render_to_info
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 588, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: float got invalid input 'None' when rendering template '{{ state_attr('weather.forecast_home_celsius_met', 'temperature') | float }}' but no default was specified
2024-04-15 06:35:12.725 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('ValueError: Template error: float got invalid input 'None' when rendering template '{{ state_attr('weather.forecast_home_celsius_met', 'temperature') | float }}' but no default was specified') while processing template 'Template<template=({{ state_attr('weather.forecast_home_celsius_met', 'temperature') | float }}) renders=4>' for attribute '_temperature' in entity 'weather.forecast_home_celsius'
2024-04-15 06:35:12.730 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template<template=({{ states('sensor.jewish_calendar_upcoming_candle_lighting_3')|as_datetime - timedelta(minutes = 15) }}) renders=2>
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 586, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2545, in _render_with_context
return template.render(**kwargs)
File "<template>", line 1, in top-level template code
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 712, in async_render_to_info
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 588, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TypeError: unsupported operand type(s) for -: 'NoneType' and 'datetime.timedelta'
2024-04-15 06:35:12.738 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('TypeError: unsupported operand type(s) for -: 'NoneType' and 'datetime.timedelta'') while processing template 'Template<template=({{ states('sensor.jewish_calendar_upcoming_candle_lighting_3')|as_datetime - timedelta(minutes = 15) }}) renders=4>' for attribute '_attr_native_value' in entity 'sensor.leave_for_mincha_friday'
2024-04-15 06:35:12.740 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template<template=({{ states('sensor.jewish_calendar_upcoming_candle_lighting_3')|as_datetime - timedelta(minutes = 45) }}) renders=2>
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 586, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2545, in _render_with_context
return template.render(**kwargs)
File "<template>", line 1, in top-level template code
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 712, in async_render_to_info
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 588, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TypeError: unsupported operand type(s) for -: 'NoneType' and 'datetime.timedelta'
2024-04-15 06:35:12.749 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('TypeError: unsupported operand type(s) for -: 'NoneType' and 'datetime.timedelta'') while processing template 'Template<template=({{ states('sensor.jewish_calendar_upcoming_candle_lighting_3')|as_datetime - timedelta(minutes = 45) }}) renders=4>' for attribute '_attr_native_value' in entity 'sensor.before_shabbat_preparations'
2024-04-15 06:35:12.750 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template<template=({{ states('sensor.jewish_calendar_upcoming_candle_lighting_3')|as_datetime - timedelta(hours = 72) }}) renders=2>
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 586, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2545, in _render_with_context
return template.render(**kwargs)
File "<template>", line 1, in top-level template code
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 712, in async_render_to_info
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 588, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TypeError: unsupported operand type(s) for -: 'NoneType' and 'datetime.timedelta'
2024-04-15 06:35:12.759 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('TypeError: unsupported operand type(s) for -: 'NoneType' and 'datetime.timedelta'') while processing template 'Template<template=({{ states('sensor.jewish_calendar_upcoming_candle_lighting_3')|as_datetime - timedelta(hours = 72) }}) renders=4>' for attribute '_attr_native_value' in entity 'sensor.to_do_list_shabbat_preparations'
2024-04-15 06:35:12.761 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template<template=({{ states('sensor.jewish_calendar_upcoming_candle_lighting_3')|as_datetime - timedelta(minutes = 5) }}) renders=2>
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 586, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2545, in _render_with_context
return template.render(**kwargs)
File "<template>", line 1, in top-level template code
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 712, in async_render_to_info
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 588, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TypeError: unsupported operand type(s) for -: 'NoneType' and 'datetime.timedelta'
2024-04-15 06:35:12.771 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('TypeError: unsupported operand type(s) for -: 'NoneType' and 'datetime.timedelta'') while processing template 'Template<template=({{ states('sensor.jewish_calendar_upcoming_candle_lighting_3')|as_datetime - timedelta(minutes = 5) }}) renders=4>' for attribute '_attr_native_value' in entity 'sensor.early_shabbat_mode'
2024-04-15 06:35:12.772 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template<template=({{ states('sensor.jewish_calendar_upcoming_havdalah_3')|as_datetime - timedelta(minutes = 120) }}) renders=2>
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 586, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2545, in _render_with_context
return template.render(**kwargs)
File "<template>", line 1, in top-level template code
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 712, in async_render_to_info
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 588, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TypeError: unsupported operand type(s) for -: 'NoneType' and 'datetime.timedelta'
2024-04-15 06:35:12.781 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('TypeError: unsupported operand type(s) for -: 'NoneType' and 'datetime.timedelta'') while processing template 'Template<template=({{ states('sensor.jewish_calendar_upcoming_havdalah_3')|as_datetime - timedelta(minutes = 120) }}) renders=4>' for attribute '_attr_native_value' in entity 'sensor.havdalah_checker'
2024-04-15 06:35:12.783 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template<template=({{ states('sensor.jewish_calendar_upcoming_candle_lighting_3')|as_datetime + timedelta(hours = 23) + timedelta(minutes = 30)}}) renders=2>
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 586, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2545, in _render_with_context
return template.render(**kwargs)
File "<template>", line 1, in top-level template code
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 712, in async_render_to_info
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 588, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TypeError: unsupported operand type(s) for +: 'NoneType' and 'datetime.timedelta'
2024-04-15 06:35:12.791 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('TypeError: unsupported operand type(s) for +: 'NoneType' and 'datetime.timedelta'') while processing template 'Template<template=({{ states('sensor.jewish_calendar_upcoming_candle_lighting_3')|as_datetime + timedelta(hours = 23) + timedelta(minutes = 30)}}) renders=4>' for attribute '_attr_native_value' in entity 'sensor.leave_for_mincha_saturday'
2024-04-15 06:35:12.793 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template<template=({{ states('sensor.jewish_calendar_upcoming_candle_lighting_3')|as_datetime + timedelta(hours = 24) }}) renders=2>
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 586, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2545, in _render_with_context
return template.render(**kwargs)
File "<template>", line 1, in top-level template code
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 712, in async_render_to_info
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 588, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TypeError: unsupported operand type(s) for +: 'NoneType' and 'datetime.timedelta'
2024-04-15 06:35:12.800 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('TypeError: unsupported operand type(s) for +: 'NoneType' and 'datetime.timedelta'') while processing template 'Template<template=({{ states('sensor.jewish_calendar_upcoming_candle_lighting_3')|as_datetime + timedelta(hours = 24) }}) renders=4>' for attribute '_attr_native_value' in entity 'sensor.mincha'
2024-04-15 06:35:12.806 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template<template=({{ states('sensor.jewish_calendar_upcoming_candle_lighting_3')|as_datetime > today_at('19:00:00') }}) renders=2>
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 586, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2545, in _render_with_context
return template.render(**kwargs)
File "<template>", line 1, in top-level template code
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 712, in async_render_to_info
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 588, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TypeError: '>' not supported between instances of 'NoneType' and 'datetime.datetime'
2024-04-15 06:35:12.815 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('TypeError: '>' not supported between instances of 'NoneType' and 'datetime.datetime'') while processing template 'Template<template=({{ states('sensor.jewish_calendar_upcoming_candle_lighting_3')|as_datetime > today_at('19:00:00') }}) renders=4>' for attribute '_state' in entity 'binary_sensor.next_candle_lighting_after_7pm'
2024-04-15 06:35:12.818 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template<template=(= {% set time = as_timestamp(states('sensor.leave_for_mincha_friday'))|timestamp_custom('%-H:%M %p') %} {% set hour = time[:2]|int %} {{ '{:02d}:{} {}'.format(hour if hour == 12 else hour % 12, time[3:5], 'PM' if hour>11 else 'AM') }}) renders=2>
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2044, in forgiving_as_timestamp
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 586, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2545, in _render_with_context
return template.render(**kwargs)
File "<template>", line 1, in top-level template code
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2047, in forgiving_as_timestamp
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1849, in raise_no_default
ValueError: Template error: as_timestamp got invalid input 'unavailable' when rendering template '= {% set time = as_timestamp(states('sensor.leave_for_mincha_friday'))|timestamp_custom('%-H:%M %p') %} {% set hour = time[:2]|int %} {{ '{:02d}:{} {}'.format(hour if hour == 12 else hour % 12, time[3:5], 'PM' if hour>11 else 'AM') }}' but no default was specified
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 712, in async_render_to_info
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 588, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: as_timestamp got invalid input 'unavailable' when rendering template '= {% set time = as_timestamp(states('sensor.leave_for_mincha_friday'))|timestamp_custom('%-H:%M %p') %} {% set hour = time[:2]|int %} {{ '{:02d}:{} {}'.format(hour if hour == 12 else hour % 12, time[3:5], 'PM' if hour>11 else 'AM') }}' but no default was specified
2024-04-15 06:35:12.830 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('ValueError: Template error: as_timestamp got invalid input 'unavailable' when rendering template '= {% set time = as_timestamp(states('sensor.leave_for_mincha_friday'))|timestamp_custom('%-H:%M %p') %} {% set hour = time[:2]|int %} {{ '{:02d}:{} {}'.format(hour if hour == 12 else hour % 12, time[3:5], 'PM' if hour>11 else 'AM') }}' but no default was specified') while processing template 'Template<template=(= {% set time = as_timestamp(states('sensor.leave_for_mincha_friday'))|timestamp_custom('%-H:%M %p') %} {% set hour = time[:2]|int %} {{ '{:02d}:{} {}'.format(hour if hour == 12 else hour % 12, time[3:5], 'PM' if hour>11 else 'AM') }}) renders=4>' for attribute '_attr_native_value' in entity 'sensor.leave_for_mincha_friday_am_pm'
2024-04-15 06:35:12.832 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template<template=(= {% set time = as_timestamp(states('sensor.to_do_list_shabbat_preparations'))|timestamp_custom('%-H:%M %p') %} {% set hour = time[:2]|int %} {{ '{:02d}:{} {}'.format(hour if hour == 12 else hour % 12, time[3:5], 'PM' if hour>11 else 'AM') }}) renders=2>
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2044, in forgiving_as_timestamp
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 586, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2545, in _render_with_context
return template.render(**kwargs)
File "<template>", line 1, in top-level template code
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2047, in forgiving_as_timestamp
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1849, in raise_no_default
ValueError: Template error: as_timestamp got invalid input 'unavailable' when rendering template '= {% set time = as_timestamp(states('sensor.to_do_list_shabbat_preparations'))|timestamp_custom('%-H:%M %p') %} {% set hour = time[:2]|int %} {{ '{:02d}:{} {}'.format(hour if hour == 12 else hour % 12, time[3:5], 'PM' if hour>11 else 'AM') }}' but no default was specified
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 712, in async_render_to_info
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 588, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: as_timestamp got invalid input 'unavailable' when rendering template '= {% set time = as_timestamp(states('sensor.to_do_list_shabbat_preparations'))|timestamp_custom('%-H:%M %p') %} {% set hour = time[:2]|int %} {{ '{:02d}:{} {}'.format(hour if hour == 12 else hour % 12, time[3:5], 'PM' if hour>11 else 'AM') }}' but no default was specified
2024-04-15 06:35:12.842 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('ValueError: Template error: as_timestamp got invalid input 'unavailable' when rendering template '= {% set time = as_timestamp(states('sensor.to_do_list_shabbat_preparations'))|timestamp_custom('%-H:%M %p') %} {% set hour = time[:2]|int %} {{ '{:02d}:{} {}'.format(hour if hour == 12 else hour % 12, time[3:5], 'PM' if hour>11 else 'AM') }}' but no default was specified') while processing template 'Template<template=(= {% set time = as_timestamp(states('sensor.to_do_list_shabbat_preparations'))|timestamp_custom('%-H:%M %p') %} {% set hour = time[:2]|int %} {{ '{:02d}:{} {}'.format(hour if hour == 12 else hour % 12, time[3:5], 'PM' if hour>11 else 'AM') }}) renders=4>' for attribute '_attr_native_value' in entity 'sensor.to_do_list_shabbat_preparations_am_pm'
2024-04-15 06:35:12.844 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template<template=(= {% set time = as_timestamp(states('sensor.jewish_calendar_upcoming_candle_lighting_3'))|timestamp_custom('%-H:%M %p') %} {% set hour = time[:2]|int %} {{ '{:02d}:{} {}'.format(hour if hour == 12 else hour % 12, time[3:5], 'PM' if hour>11 else 'AM') }}) renders=2>
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2044, in forgiving_as_timestamp
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 586, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2545, in _render_with_context
return template.render(**kwargs)
File "<template>", line 1, in top-level template code
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2047, in forgiving_as_timestamp
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1849, in raise_no_default
ValueError: Template error: as_timestamp got invalid input 'unknown' when rendering template '= {% set time = as_timestamp(states('sensor.jewish_calendar_

The error messages refer to templates that aren’t in the example I posted above.

For example:

float got invalid input ‘None’ when rendering template
{{ state_attr(‘weather.forecast_home_celsius_met’, ‘temperature’) | float }}
but no default was specified.


Here’s proof that the suggested template works properly. The Input Datetime’s value is 07:00:00 and the template subtracts one hour and reports it as a valid datetime string in ISO format.

If you want to display it as 06:00:00 and not as an ISO datetime string, then you can’t declare its device_class is timestamp.

Sorry, I didn’t think that my question was clear. Why do these errors for other template sensors only show up if I have the code for this sensor in my Yaml?

I removed the device class and the sensor is still not showing in my entities. Any idea why this is happening?

There’s a very good chance that all your other errors are impacting the loading of additional template entities. Fix your errors, you have many.

1 Like

I cleared up my errors and now I am trying to get back to this. I want the end format to be 9:15. when I use today_at the end result is not the format I want but it does subtract an hour. I tried removing the today_at but i am unable to get the timedelta to work. Any recommendations?

The error: TypeError: unsupported operand type(s) for -: ‘str’ and 'datetime.timedelta

code:

{{ states('input_datetime.shacharit') - timedelta(hours = 1) }}

Don’t remove today_at().

Without it you’re not converting the Input Datetime’s value from string to datetime. That’s why you got that error message.

That’s definitely invalid for a timestamp sensor so your original decision to include device_class: timestamp was a mistake.

Try this version:

- sensor:
    - name: Manually Set Wake Up
      unique_id: manually_set_wake_up_test
      state: "{{ (today_at(states('input_datetime.shacharit')) - timedelta(hours = 1)).strftime('%-I:%M') }}"

thank you! that did the trick. can you please explain what the today_at() does?

1 Like

From the Templating documentation:

1 Like

Thank you again!