Stuck trying to use datetime helper in a template variable

In a mushroom template card I have it reporting the time my boiler was put on/off.
Until now I’ve been using the last_changed value of the toggle, but this updates whenever HA is reset, so I was looking to create and use a persistent value instead.
So I created a datetime helper which, whenever the boiler is switched on/off, it updates.
That part works fine.

Where I’m stuck is in the template logic where it looks to compare the date of the activity compared to now() and if the same then return just the time. If a mismatch, return date + time.

This worked fine before with the last_changed, but I’m struggling to get the formatting for the datetime helper in the set variable

Currently it looks like this:

{% set datenow = (as_timestamp(now()))| timestamp_custom('%y%m%d', True) | float() | round (0) %}
{% set lastheated = (as_timestamp(states.input_boolean.water_heating.last_changed)) | timestamp_custom('%y%m%d', True) | float() | round (0) %}

{% if states('input_boolean.water_heating')=='on' %}
On {{( states('input_datetime.boiler_activity') | as_timestamp| timestamp_custom('%H:%M', True))}}

{% elif (is_state('input_boolean.water_heating','off') and (datenow == lastheated)) %}
OFF {{( states('input_datetime.boiler_activity') | as_timestamp| timestamp_custom('%H:%M', True))}}
{% else %}

OFF {{( states('input_datetime.boiler_activity') | as_timestamp| timestamp_custom('%y/%m/%d %H:%M', True))}}
{% endif %}

The below returns the right value, but I’m struggling to figure our how to format it for the lastheated variable.

{{(states('input_datetime.boiler_activity') | as_timestamp| timestamp_custom('%y%m%d', True))}}
{% set last_activity = states('input_datetime.boiler_activity') | as_datetime | as_local %}
{% set fmat = iif(now().date() == last_activity.date(), "%H:%M", '%y/%m/%d %H:%M') %}
{{ states('input_boolean.water_heating') | title }} {{ last_activity.strftime(fmat) }}

EDIT: Changed iff to iif

This seems to work …

{% set lastheated = states('input_datetime.boiler_activity') | as_timestamp | timestamp_custom('%y%m%d', True) | float() | round (0) %}

Did you try what I wrote? Your template has tons of unnecessary conversions

The template I wrote replaces yours completely. No other code needed

Hi.
Yes, I tried it, but unfortunately it returned an error in Dev Tools that I couldn’t resolve.

UndefinedError: 'iff' is undefined

But seeing your version of last_activity gave me the inspiration to see that the “states…” didn’t need to be wrapped in brackets which I think was what was breaking my attempts.

oops, it should be iif, make that change and it should work.

As for your template, it really is bloated with code that does nothing but slow the template down.

One last change?

ln.3
→ {{ state(
{{ states(

oops, thanks :slight_smile: updated

1 Like

This is how it’s set up now.
It’s used in 2 tabs across multiple dashboards, so moved it to be a template sensor

    - name: boiler_activity_date
      unique_id: -123456-123456-123456-123456-123456
      icon: mdi:calendar
      state: >
        {% set boiler_activity = states('input_datetime.boiler_activity') | as_datetime | as_local %}
        {% set boiler_activity_date = iif(now().date() == boiler_activity.date(), "%d/%m/%y", '%d/%m/%y') %}
        {{ boiler_activity.strftime(boiler_activity_date) }}
    - name: boiler_activity_ymd
      unique_id: 234567-234567-234567-234567-234567
      icon: mdi:calendar
      state: >
        {% set boiler_activity = states('input_datetime.boiler_activity') | as_datetime | as_local %}
        {% set boiler_activity_ymd = iif(now().date() == boiler_activity.date(), "%y%m%d", '%y%m%d') %}
        {{ boiler_activity.strftime(boiler_activity_ymd) }}
    - name: boiler_activity_hms
      unique_id: -345678-345678-345678-345678-345678
      icon: mdi:calendar
      state: >
        {% set boiler_activity = states('input_datetime.boiler_activity') | as_datetime | as_local %}
        {% set boiler_activity_hms = iif(now().date() == boiler_activity.date(), "%H:%M", '%H:%M') %}
        {{ boiler_activity.strftime(boiler_activity_hms) }}

And how it’s used in the card (secondary info)

secondary: >-
  {% set datenow = (as_timestamp(now()))| timestamp_custom('%y%m%d', True) | float() | round (0) %}
  {% set lastheated = (states('sensor.boiler_activity_ymd') | float())  %}

  {% if states('input_boolean.water_heating')=='on' and (datenow == lastheated) %}
  {{ states('sensor.boiler_activity_hms') }}

  {% elif (is_state('input_boolean.water_heating','on') and (datenow > lastheated)) %}
  {{ states('sensor.boiler_activity_date') }} {{ states('sensor.boiler_activity_hms') }}

  {% elif (is_state('input_boolean.water_heating','off') and (datenow == lastheated)) %}
  {{ states('sensor.boiler_activity_hms') }}

  {% else %}
  {{ states('sensor.boiler_activity_date') }} {{ states('sensor.boiler_activity_hms') }}

  {% endif %}

In situ
image
image