I’m trying to write a template helper that examines the (numeric) state of a sensor and depending on the value returns a time value (h:mm:ss). The intent is that this helper can be used as the comparison target in a time condition. Despite being a programmer for many years I have to say that I am struggling somewhat with the syntax and functionality of HA scripting, templates etc. as the documentation does not seem complete or detailed enough. Can someone maybe take a look at my code and suggest what might be wrong (the value of this helper is always ‘Unknown’):
The ‘fancy quotes’ are an artefact of pasting the text into the text entry field for thsi forum. My actual code has normal quotes. If I run the code via ‘Developer Tools → Template’ I can see that it is working and returning a (correct) string value. The problem is that I need the return type to be timestamp (actually time in reality) as I want to use the result in a time comparison condition (e.g. before hh:mm:ss) but it seems that either the fact that the helper is defined as type ‘Timestamp’ (there is surprisingly no ‘time’ type for helpers, though there is a ‘date’ type) or the fact that the return value is a string may be the issue.
I guess I have more questions:
In HA is a ‘timestamp’ just a time or is it a date-time value?
Is there a type for just a time (h:mm:ss[.sss])? and if so what is it?
Is there a way to cast/coerce the time string to something of type timestamp?
Is my approach (helper of type timestamp) the right one here or should I be doing something different?
Also, when replying to people, make sure to reply to them not the whole post. I did not get notified, so you replied to the whole post and not my comment.
The problem with the fancy quotes comes from using “blockquote” to insert your code, where you should have used the “code” insert. Blockquote does change the quotes, whereas “code” doesn’t! I know, details…
I thought, that’s what we have datetime for? I will never get these small differences that HA makes with all these time things… Can’t it just be a timestamp is a number (the seconds) and a datetime object is a datetime?
Problem with timestamps is that they don’t contain the timezone. It’s up to the user to figure that out, which never goes over well. If it were my choice, we’d just have datetime objects everywhere except for in the database, which would be a timestamp from UTC.