Sensor for first time binary sensor is tripped each day

I’m getting an error on the { % set opened line under attributes > datetime, saying it “found character ‘%’ that cannot start any token”

There should be no space between the brace and percent symbols. {% set opened

I tried this but got an error when running it (and after tripping the sensor):

Error rendering state template for sensor.great_room_sensor_daily_first_on_time: TypeError: '<' not supported between instances of 'datetime.datetime' and 'NoneType'

I apologize, that’s my formatting. This is the entirety of the code:

template:
  - trigger:
      - platform: state
        entity_id: binary_sensor.great_room_motion_sensor
        to: 'on'
    sensor:
      - name: Great Room Sensor Daily First On Time
        state: >
          {% set opened = states[trigger.entity_id].last_changed %}
          {% set previous = this.attributes.get('datetime', 'none') %}
          {% if as_datetime(previous) is none or as_datetime(previous).date() != opened.date() %}
            {{ (opened | as_local).strftime('%H:%M') }}
          {% else %}
            {{ this.state }}
          {% endif %}
        attributes:
          datetime:
            {% set opened = states[trigger.entity_id].last_changed %}
            {% set previous = this.attributes.get('datetime', 'none') %}
            {% if as_datetime(previous) is none or as_datetime(previous).date() != opened.date() %}
              {{ opened.isoformat() }}
            {% else %}
              {{ this.attributes.get('datetime', 'none') }}
            {% endif %}

it’s missing the multiline character after datetime

        attributes:
          datetime: >
2 Likes

Yep, added it in my post as well

This displayed the earliest trip of the sensor from yesterday, but when I tripped it this morning, it still displayed the result from yesterday.

I had restarted HA around 8 PM ET last night and then tripped the sensor after which I know is into the next day in UTC. Could it be related to that? In other words, could it be think that an evening trip of the sensor is the earliest time for the following day in UTC?

This should fix that

template:
  - trigger:
      - platform: state
        entity_id: binary_sensor.your_sensor_here
        to: 'on'
    sensor:
      - name: Sensor Daily First On Time
        state: >
          {% set opened = states[trigger.entity_id].last_changed | as_local %}
          {% set previous = this.attributes.get('datetime', 'none') %}
          {% if as_datetime(previous) is none or as_local(as_datetime(previous)).date() != opened.date() %}
            {{ (opened | as_local).strftime('%H:%M') }}
          {% else %}
            {{ this.state }}
          {% endif %}
        attributes:
          datetime: >
            {% set opened = states[trigger.entity_id].last_changed | as_local %}
            {% set previous = this.attributes.get('datetime', 'none') %}
            {% if as_datetime(previous) is none or as_local(as_datetime(previous)).date() != opened.date() %}
              {{ opened.isoformat() }}
            {% else %}
              {{ this.attributes.get('datetime', 'none') }}
            {% endif %}

Yes! I confirmed that it displayed the earliest time yesterday and the earliest time this morning. Thank you both so much. This is a level of expertise with strings that I aspire to. I’m really grateful for all of the help and patience!