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:
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.
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:
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.
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.