How to handle unknown or unavailable states in template entities or automations

Do you want your sensor to return last good value until it changes to another good value? That should probably work (untested):

- trigger:
    - trigger: homeassistant
      event: start
      id: ha_start
    - trigger: event
      event_type: event_template_reloaded
      id: template_reload
    - trigger: state
      entity_id: 
        - sensor.pet_last_use_date
      id: change
  sensor:
    - name: "Last Valid Pet Litter Use Date"
      unique_id: "last_valid_pet_litter_use_date"
      device_class: timestamp
      state: >
        {% set last_value = states('sensor.last_valid_pet_litter_use_date') %}
        {% set last_date = as_datetime(last_value) if last_value is not none and as_datetime(last_value) is datetime else as_datetime('1970-01-01')|as_local %}
        {% if trigger.id == 'change' %}
          {% set value = states('sensor.pet_last_use_date') %}
          {% if value is not none and as_datetime(value) is datetime %}
            {{ as_datetime(value) }}
          {% else %}
            {{ last_date }}
          {% endif %}
        {% else %}
          {{ last_date }}
        {% endif %}

Important stuff:

  1. Return only valid timestamps from this sensor: no weird text, no ‘unavailable’. The line {% set last_date = ... %} is to produce 100% valid date, in any case (also if the sensor currently contains “garbage”).
  2. I added 2 more triggers, so that your sensor will be updated immediately on template reload, and it retains it’s last value on HA restart/template reload.

Edit: two more things :slight_smile:

  1. {{ 'none' }} is a string but {{ none }} is the null value: {% if states('some_sensor') in ['unknown','unavailable',none] %}
  2. You can output to logs but you’d have to repeat your logic within ‘action’ section. Or you can move your logic there. Top-level variables from ‘action’ section are visible to sensors.
Example
- trigger:
    - trigger: homeassistant
      event: start
      id: ha_start
    - trigger: event
      event_type: event_template_reloaded
      id: template_reload
    - trigger: state
      entity_id: 
        - sensor.pet_last_use_date
      id: change
  action:
    - variables:
        last_value: "{{ states('sensor.last_valid_pet_litter_use_date') }}"
        last_date: "{{ as_datetime(last_value) if last_value is not none and as_datetime(last_value) is datetime else as_datetime('1970-01-01')|as_local }}"
        value: "{{ states('sensor.pet_last_use_date') }}"
        new_sensor_value: >-
          {% if trigger.id == 'change' %}
            {% if value is not none and as_datetime(value) is datetime %}
              {{ as_datetime(value) }}
            {% else %}
              {{ last_date }}
            {% endif %}
          {% else %}
            {{ last_date }}
          {% endif %}
    - if: "{{ value in ['unknown','unavailable',none] }}"
      then:
        - action: system_log.write
          data:
            level: warning
            message: >-
              The variable 'value' is {{value}}, new sensor value is {{new_sensor_value}}
  sensor:
    - name: "Last Valid Pet Litter Use Date"
      unique_id: "last_valid_pet_litter_use_date"
      device_class: timestamp
      state: "{{ new_sensor_value }}"

Thank you very much and sorry for the delayed response - had some work issues that pulled me away from my much more fun Home Assistant work.

This is all really good feedback. I think the set last_date is a very useful line.

Thanks for the clarity on “none” - I actually didn’t realize it was a string but not the null value (not a native Python developer, came to it largely via HA).

And here’s the complete kicker. It turns out the Petkit integration returns some states with a capitalized first letter. Something I did not realize because Home Assistant capitalizes the first letter of a sensor’s state in the logbook and various automation displays as well (so the standard lowercase state appears with a capitalized first letter). It wasn’t until I did some more deep logging that I ascertained this was part of the problem. But I found your code to be very helpful as was the last_date “clean” code.

Thanks for your assistance

Thanks after a long break from HA due to work, I did try availability with some issues until I realized that the Petkit integration returns some states with a capitalized first letter, quite frustrating, I did not realize that due to HA automatically capitalizing states so I just didn’t’ assume that integration was atypical with it’s status returns. Thanks much for your suggestion though as you are right I wasn’t thinking of availability as a tool to reach for.