- 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 %}
- 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 %}
{% 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.
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') %}