Custom sensor unavailable

Hello! I am trying to create a customer sensor to calculate days until I need to change my HVAC filter. Unfortunately the newly created sensor shows as unavailable.

First, I created two Helpers to output values that will be used in my customer sensor:

input_datetime:
  air_filter_installed:
    name: Air Filter Installed
    icon: mdi:air-filter
    has_date: true
    has_time: false

input_number:
  air_filter_lifespan:
    name: Air Filter Lifespan
    initial: 90
    min: 0
    max: 365
    step: 1
    mode: box
    unit_of_measurement: days
    icon: mdi:history

Then I created the sensor:

sensor:
  - platform: template
    sensors:
      air_filter_days_remaining:
        friendly_name: "Air Filter Days Remaining"
        unique_id: "air filter timer"
        unit_of_measurement: "days"
        icon_template: 'mdi:calendar-clock'
        value_template: >-
         {%- set inst = as_timestamp(states('input_datetime.air_filter_installed')) -%}
         {%- set dif = states('input_number.air_filter_lifespan') | int * 86400 -%}
         {%- set nt = as_timestamp(states('sensor.date')) -%}
         {{ (((inst + dif) - nt) / 86400) | round(0) }}

Everything seems to work, I get no config errors and I can see the newly created entities but the custom sensor shows as unavailable.

Where am I going wrong?

Thanks,

Hi, @jamesball - have you perchance tried the template statements in the developer template tool? You can print out the actual values - specifically the values for inst, dif, nt, and then the answer. I’m not in front of a computer at the moment, but will try tomorrow.

I suspect your issue is because your input_datetime is not a datetime object, it is a string. datetime object methods are unavailable unless one first converts the string via strptime. Check this in the template editor:

Then vote here:

From the templating docs:

  • as_timestamp() converts datetime object or string to UNIX timestamp.

  • strptime(string, format) parses a string based on a format and returns a datetime object.

1 Like

I didn’t know that this tool existed - thanks for pointing it out! I found the error… I didn’t have a sensor.date entity so that is why it wouldn’t work.

I added these lines into my config file and then it worked:

  - platform: time_date
    display_options:
      - 'date'

Thank you!

I got it to work so it seems that it didn’t need to be a datetime object. The UNIX timestamp format seems to work…

Glad you figured it out! The developer template tool is one of the most frequent things I use.

Hi, @tom_l - I checked this in the template editor and found that, as long as it’s cast using as_timestamp, it can then be used in math calcs. I added the results from the template editor on the feature-request page you referenced here: Make typed states available for entities - #5 by KSC

@jamesball - one other thing I wanted to mention is that you don’t need to use the - in the template, as in: {%- ssssss -%}. Those confused me in the beginning. They are, I think, for formatting output - which we’re not doing in a template. Just makes the code a little cleaner.

Again, glad you got it working!

you could also eliminate the ambiguity of what might or might not work depending on the string syntax by using the “timestamp” attribute of the input_datetime.

{{ state_attr('input_datetime.air_filter_installed', 'timestamp') }}