Help with new (default) templating in 2021.10

@petro full code is here:

  - platform: template
    sensors:
      recyclables_bintype:
        value_template: "{{ state_attr('sensor.bin_sensors_recyclables', 'BinType') }}"
      recyclables_eventdate:
        value_template: "{{ state_attr('sensor.bin_sensors_recyclables', 'EventDate') }}"

  - 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", 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 the state of date_in : 2021-10-20T00:00:00

Try this version:

  - 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", 0) %}
          {% set days = (bin.astimezone() - now() + timedelta(days=1)).days if bin is not integer else -1 %}
          {% if days in [0,1] %}
            {{ 'Today' if days == 1 else 'Tomorrow' }}
          {% elif days in [7,14] %}
            {{ '1 Week' if days == 7 else '2 Weeks' }}
          {% elif days == -1 %}
            Error
          {% else %}
            {{ days }} days
          {% endif %}

Hi @123 I tried it but now the state of the sensor is: Error. See below:

image

That means strptime failed to convert the value of sensor.recyclables_eventdate to a datetime object.

Post an example of the sensor’s value.

there you go:

sensor.recyclables_eventdate = 2021-10-20T00:00:00

That explains why strptime was unable to convert it using %a, %d %B %Y.

so what other way could I use to make it work?

Try this:

  - platform: template
    sensors:
      blue_bin_in_days:
        friendly_name: Blue Bin
        icon_template: 'mdi:recycle'
        value_template: >-
          {% set bin = as_datetime(states('sensor.recyclables_eventdate')) %}
          {% set days = (bin.astimezone() - now() + timedelta(days=1)).days if bin != None else -1 %}
          {% if days in [0,1] %}
            {{ 'Today' if days == 1 else 'Tomorrow' }}
          {% elif days in [7,14] %}
            {{ '1 Week' if days == 7 else '2 Weeks' }}
          {% elif days == -1 %}
            Error
          {% else %}
            {{ days }} days
          {% endif %}
1 Like

that worked perfectly thank you.

I am looking into my previous one where I added as_timestamp(0) in my code as suggested by @petro. I now get values of -27,232,644m.

Here is my code:

  - 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(0)|int - now()|as_timestamp(0)|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') }}"

before adding (0) to as_timestamp it all worked fine.

You are getting a negative result because as_timestamp is unable to convert the value of sensor.sm_g986b_next_alarm.

1 Like

Yes. Maybe it works because the automation was executed for the first time without this condition?

{% set dayOff = as_timestamp(strptime(states.sensor.day_off_tomorrow_var.state, “%Y-%m-%d”) |timestamp_custom("%Y-%m-%d") %}

gives a warning in the log. Please advice what I need to change to solve this warning.

I tried {% set dayOff = as_timestamp(strptime(states.sensor.day_off_tomorrow_var.state, “%Y-%m-%d”, default=0) |timestamp_custom("%Y-%m-%d") %} but that doesn’t work

the state of sensor.sm_g986b_next_alarm is unavailable. I suppose I could run an automation that when the state of sensor.sm_g986b_next_alarm is unavailable it should print Alarm not set yet.

what’s sensor.sm_g986b_next_alarm’s state?

Or just enhance the availability_template

        availability_template: "{{ states('sensor.sm_g986b_next_alarm') not in ['Alarm not set yet', 'unavailable'] }}"
1 Like

A closing bracket is missing.

Why is that template even necessary?

You are taking the sensor’s value, which is in YY-MM-DD format, using strptime to convert it into a datetime object, then into a timestamp with as_timestamp, and then back to YY-MM-DD format with timestamp_custom. Why?

It’s effectively just this:

{% set dayOff = states('sensor.day_off_tomorrow_var') %}
1 Like

the state is unavailable

well, that’s your problem. That template won’t resolve properly if the state is unavailable.

1 Like

that didnt do anything between, it still returns unavailable.