Help with new (default) templating in 2021.10

If sensor.sm_g986b_next_alarm is unavailable what do you expect sensor.minutes_next_alarm_ahmed_original should report?

is there a way where I could change the state of sensor.minutes_next_alarm_ahmed_original when sensor.sm_g986b_next_alarm is unavailable?

I tried it with automation but couldn’t.

When using your sensor coding I get the following template errors, so missing the defaults somewhere.

Template warning: 'timestamp_local' got invalid input '21 days, 0:00:00' when rendering template '{%- set ns = namespace(previous = 2, spring=none, fall=none) %} {%- set today = strptime(states('sensor.date'), '%Y-%m-%d').astimezone().replace(hour=ns.previous) %} {%- for i in range(365) %} {%- set day = (today + timedelta(days=i)).astimezone() %} {%- if ns.previous - day.hour == -1 %} {%- set ns.spring = today + timedelta(days=i) | timestamp_local %} {%- elif ns.previous - day.hour == 1 %} {%- set ns.fall = today + timedelta(days=i) | timestamp_local %} {%- endif %} {%- set ns.previous = day.hour %} {%- endfor %} {{ [ns.spring, ns.fall] | min }}' but no default was specified. Currently 'timestamp_local' will return '21 days, 0:00:00', however this template will fail to render in Home Assistant core 2021.12
Template warning: 'timestamp_local' got invalid input '168 days, 0:00:00' when rendering template '{%- set ns = namespace(previous = 2, spring=none, fall=none) %} {%- set today = strptime(states('sensor.date'), '%Y-%m-%d').astimezone().replace(hour=ns.previous) %} {%- for i in range(365) %} {%- set day = (today + timedelta(days=i)).astimezone() %} {%- if ns.previous - day.hour == -1 %} {%- set ns.spring = today + timedelta(days=i) | timestamp_local %} {%- elif ns.previous - day.hour == 1 %} {%- set ns.fall = today + timedelta(days=i) | timestamp_local %} {%- endif %} {%- set ns.previous = day.hour %} {%- endfor %} {{ [ns.spring, ns.fall] | min }}' but no default was specified. Currently 'timestamp_local' will return '168 days, 0:00:00', however this template will fail to render in Home Assistant core 2021.12

You overlooked to answer my question. What do you think the sensor’s state should be when sensor.sm_g986b_next_alarm is unavailable?

you must have copied the pre-updated version of that code. Click the link, it has the current code
I need to push the updates when I get home, apparently I did not do that.

Thanks for helping out but states(‘sensor.day_off_tomorrow_var’) results in 2021-10-12 00:00:00 and I only want 2021-10-12.

Sorry to have confused you but what I wanted was to have the sensor’s state as ‘Alarm not set’ when sensor.sm_g986b_next_alarm is unavailable

Is that possible?

When I have a calendar event with start_time: 2021-10-12 08:10:00 and I only need 08:10 how can I extract that with the new template rules?

Then just do this:

{% set dayOff = states('sensor.day_off_tomorrow_var')[:10] %}

Try this:

  - platform: template
    sensors:
      minutes_next_alarm_ahmed_original:
        friendly_name: "Minutes until Next Alarm Ahmed 5mins ahead"
        unit_of_measurement: "m"
        value_template: >-
          {% set na = states('sensor.sm_g986b_next_alarm') %}
          {{ 'Alarm not set' if na == 'unavailable' else ((na|as_timestamp(0)|int - now().timestamp()|int)/60)|int }}
        availability_template: "{{ not is_state('sensor.sm_g986b_next_alarm', 'Alarm not set yet') }}"
        attribute_templates:
          time: "{{ state_attr('sensor.sm_g986b_next_alarm','Local Time') }}"
1 Like

Thank you very much, that did the trick.

Check the link again and copy the code at it. For some reason, the forums changed the link when I originally posted. I just reverted the post back to its original edit.

Perfect, Thank you!

Thank you :slight_smile: works perfectly and I got to learn something new about templating.

Hi guys, I need your help again. Were do I need to place (default=0) in template below?

{% set trash = as_timestamp(strptime(state_attr(“sensor.afval_rest”,“date”), “%Y-%m-%d”)) | timestamp_custom("%d-%m") %}

take a look at this post and see if you can figure it out.

Thanks Petro, issue resolved.

Hey @123 its all working well for me but I see these error messages regarding the template:

[homeassistant.helpers.event] Error while processing template: Template("{{ not is_state('sensor.sm_g986b_next_alarm') }}")
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 397, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1604, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1304, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 925, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.9/site-packages/jinja2/sandbox.py", line 393, in call
    return __context.call(__obj, *args, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1734, in wrapper
    return func(hass, *args[1:], **kwargs)
TypeError: is_state() missing 1 required positional argument: 'state'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 513, in async_render_to_info
    render_info._result = self.async_render(variables, strict=strict, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 399, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TypeError: is_state() missing 1 required positional argument: 'state'
2021-10-19 16:16:53 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('TypeError: is_state() missing 1 required positional argument: 'state'') while processing template 'Template("{{ not is_state('sensor.sm_g986b_next_alarm') }}")' for attribute '_attr_available' in entity 'sensor.minutes_next_alarm_ahmed'

I haven’t come across these errors before, can you help please?

I can help but you don’t appear to be reading the error message but simply reaching out to me to read it for you. The answer to the problem is explained within the error message.

TemplateError(‘TypeError: is_state() missing 1 required positional argument: ‘state’’) while processing template ‘Template(“{{ not is_state(‘sensor.sm_g986b_next_alarm’) }}”)’ for attribute ‘_attr_available’ in entity 'sensor.minutes_next_alarm_ahmed

If you look at the documentation for the is_state function you will see it requires two arguments but you have only supplied one argument (that’s exactly what the error message said). You have used the is_state function incorrectly and must also supply the state argument (again, that’s what the error message said).

thanks for pointing out. I suppose I would have to use the is_state as any other doesn’t work for me in my case which means that I will have those error messages every time.