Help with new (default) templating in 2021.10

FYI, if you just want to keep it in sync with mine, you’re more than welcome to.

EDIT: paired with this automation, you’d just have to change the notification.

- alias: Timed Event - DST Warning
  id: dst_warning
  trigger:
    - platform: time
      at: '10:00:00'
    - platform: time
      at: '19:00:00'
  condition:
    - condition: template
      value_template: >
        {{ state_attr('sensor.dst', 'next').days_to_event in [7,1] }}
  action:
    - service: script.notify
      data:
        message: >
          {%- set next = state_attr('sensor.dst', 'next') %}
          {%- set plural = 's' if next.days_to_event > 1 else '' %}
          "Daylight savings in {{ next.days_to_event }} day{{plural}}, you will {{ next.phrase }}!"

That reminds me 1 week before and 1 day before

2 Likes

thanks, I’ll checkup on that!

its just that I was comparing

{{states('sensor.date')|as_datetime}}
{{strptime(states('sensor.date'),'%Y-%m-%d').astimezone()}} #used in the Dst sensor

and the difference is

and these are identical:

{{states('sensor.date')|as_datetime}}
{{strptime(states('sensor.date'),'%Y-%m-%d')}}

so we could use:

{{(states('sensor.date')|as_datetime).astimezone()}}
{{strptime(states('sensor.date'),'%Y-%m-%d').astimezone()}}

on the automation, Ive adapted that somewhat, so need to re-figure out what the implications are here…

using:

    condition:
      condition: template
      value_template: >
        {{(trigger.now.strftime('%H:%M') == '10:00' and
              state_attr('sensor.dst','dst changed today') == true) or
          (trigger.now.strftime('%H:%M') == '19:00' and
              state_attr('sensor.dst','dst change tomorrow') == true)}}
    action:
      - service: notify.system
        data:
          title: >
            Dst {{'tomorrow' if state_attr('sensor.dst','dst change tomorrow') == true
                   else 'today'}}
          message: >
            {% set today = state_attr('sensor.dst','dst changed today') %}
            {% set change = 'ahead' if is_state_attr('sensor.dst','dst active',true)
                            else 'back' %}
            {% set phrase = state_attr('sensor.dst','next').phrase %}
            {% set suffix = 's' if today == false else 'ed' %}
            Daylight savings start{{suffix}} {{'today, please adjust all
              clocks 1 hour ' + change + ' manually if necessary.' if today == true else
              'tomorrow, you will ' + phrase + ' of sleep'}}

myself :wink:

I want automation to trigger on condition: if 10 seconds have passed since the last trigger of automation.heiman_last_action_emergency.

@petro @123 Could you guys please help, my log is piling up and I have used strptime in the past and with the new HA update its throwing warnings.

Here is my code:

  - platform: template
    sensors:
      minutes_next_alarm_ahmed:
        friendly_name: "Minutes until Next Alarm Ahmed"
        unit_of_measurement: "m"
        value_template: >-
          {% set dummy = states("sensor.time") %}
          {{((states('sensor.sm_g986b_next_alarm')|as_timestamp|int - now()|as_timestamp|int)/60 +4.59)|int}}
        availability_template: "{{ not is_state('sensor.sm_g986b_next_alarm','unavailable') }}"
        attribute_templates:
          time: "{{ state_attr('sensor.sm_g986b_next_alarm','Local Time') }}"


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

And here are the non-stop warnings:

2021-10-11 12:22:21 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'as_timestamp' got invalid input 'unavailable' when rendering template '{% set dummy = states("sensor.time") %} {{((states('sensor.sm_g986b_next_alarm')|as_timestamp|int - now()|as_timestamp|int)/60 +4.59)|int}}' but no default was specified. Currently 'as_timestamp' will return 'None', however this template will fail to render in Home Assistant core 2021.12
2021-10-11 12:22:21 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'as_timestamp' got invalid input 'unavailable' when rendering template '{% set dummy = states("sensor.time") %} {{((states('sensor.sm_g986b_next_alarm')|as_timestamp|int - now()|as_timestamp|int)/60)|int}}' but no default was specified. Currently 'as_timestamp' will return 'None', however this template will fail to render in Home Assistant core 2021.12
2021-10-11 12:22:21 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'as_timestamp' got invalid input 'unavailable' when rendering template '{% set dummy = states("sensor.time") %} {{((states('sensor.sm_g986b_next_alarm')|as_timestamp|int - now()|as_timestamp|int)/60)|int}}' but no default was specified. Currently 'as_timestamp' will return 'None', however this template will fail to render in Home Assistant core 2021.12

add a default to the as_timestamp.

as_timestamp(default=0)

or

as_timestamp(0)
1 Like

@petro ok that worked perfect… how about when I have some value after timestamp?

Like in this code:

  - platform: template
    sensors:
      blue_bin_in_days:
        friendly_name: Blue Bin
        icon_template: 'mdi:recycle'
        value_template: >-
          {% set date_in = states.sensor.recyclables_eventdate.state|replace('\n', '') %}
          {% set bin = strptime((date_in), "%a, %d %B %Y") %}
          {% set diff = as_timestamp(bin) - as_timestamp(now()) %}
          {% set days = ((diff / 86400)+1) | int %}
          {% if days == 0 %}
            Today
          {% elif days == 1 %}
            Tomorrow
          {% elif days == 7 %}
            1 Week
          {% elif days == 14 %}
            2 Weeks
          {% else %}
            {{ days }} days
          {% endif %}

As the above throws the following warnings:

 2021-10-11 12:22:21 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'strptime' got invalid input '2021-10-20T00:00:00' when rendering template '{% set date_in = states.sensor.recyclables_eventdate.state|replace('\n', $
  Today
{% elif days == 1 %}
  Tomorrow
{% elif days == 7 %}
  1 Week
{% elif days == 14 %}
  2 Weeks
{% else %}
  {{ days }} days
{% endif %}' but no default was specified. Currently 'strptime' will return '2021-10-20T00:00:00', however this template will fail to render in Home Assistant core 2021.12
2021-10-11 12:22:21 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'strptime' got invalid input '2021-10-20T00:00:00' when rendering template '{% set date_in = states.sensor.recyclables_eventdate.state|replace('\n', $
  Today
{% elif days == 1 %}
  Tomorrow
{% elif days == 7 %}
  1 Week
{% elif days == 14 %}
  2 Weeks
{% else %}
  {{ days }} days
{% endif %}' but no default was specified. Currently 'strptime' will return '2021-10-20T00:00:00', however this template will fail to render in Home Assistant core 2021.12

use as_timestamp(whatever_you_currently_have, default=0)

1 Like

Thank you, it works. Yes, it is what I want. I want the automation not to start again if the last trigger was less than 10 seconds ago.

sorry did you mean to say I should add default=0 to this line? like:

{% set diff = as_timestamp(bin, default=0) - as_timestamp(now(), default=0) %}

because I did that and still get the same warning. Unless you meant I should use this as_timestamp(whatever_you_currently_have, default=0) instead of strptime((date_in) in this line {% set bin = strptime((date_in), "%a, %d %B %Y") %}

well you also need it for strptime, basically adding the same default=0 after all your arugments inside the ()

ok so I will replace strptime with as_timestamp(default=0)

Small tip:

Wherever you currently do this:

as_timestamp(now())

replace it with:

now().timestamp()

and you won’t need to be concerned with supplying it with a default value.

1 Like

so I changed it to this:

  - platform: template
    sensors:
      blue_bin_in_days:
        friendly_name: Blue Bin
        icon_template: 'mdi:recycle'
        value_template: >-
          {% set date_in = states.sensor.recyclables_eventdate.state|replace('\n', '') %}
          {% set bin = as_timestamp((date_in), "%a, %d %B %Y", default=0) %}
          {% set diff = as_timestamp(bin, default=0) - as_timestamp(now(), default=0) %}
          {% set days = ((diff / 86400)+1) | int %}
          {% if days == 0 %}
            Today
          {% elif days == 1 %}
            Tomorrow
          {% elif days == 7 %}
            1 Week
          {% elif days == 14 %}
            2 Weeks
          {% else %}
            {{ days }} days
          {% endif %}

and now I get an error:

2021-10-11 12:57:44 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template("{% set date_in = states.sensor.recyclables_eventdate.state|replac$
  Today
{% elif days == 1 %}
  Tomorrow
{% elif days == 7 %}
  1 Week
{% elif days == 14 %}
  2 Weeks
{% else %}
  {{ days }} days
{% endif %}")

because you switched strftime to as_timestamp… Did I say to do that?

Isnt’t that what you meant?

no… you also need a default for strptime

1 Like

Why? It works for me now.

well that worked thanks but now the days for that sensor is miscalculated i.e. gives me -18910 days

that means the template never worked…

post the full code and the state of date_in = states.sensor.recyclables_eventdate.state|replace(’\n’, ‘’)