Relative_time limitations?

Hi,

As part of my effort to get live updates from my car, I run a js.node script that gets information from it.

Now part of the data that I retrieve from the car is a timestamp of when the information was last fetched from the car, so I’ve been trying to convert this timestamp to a relative time using the relative_time function - without much luck, however.

My sensor information currently looks like this:

{{ states.sensor.siste_oppdatering.state }}
2016-09-06 10:35:46.000000+00:00
{{ states.sensor.siste_oppdatering.last_updated }}
2016-09-07 21:44:29.718119+00:00

Even though I’ve made an effort to make these conform to the exact same format, the relative_time function will happily convert the last_updated, but will not convert the sensors state.

Is the relative_time function limited to objects last_updated field, or am I simply doing something wrong? From trying it out in the developer tools, I am unable to make it work with anything other than last_updated.

1 Like

Your description of the issue and what you are wanting to do is kind of confusing. Are these sensors something you created or are these sensors created by adding an OBD from your car through a Home Assistant component or something? I’m just not sure where these sensors came from.

Some examples of your code and exactly what it is you are trying to do would be helpful because we are not in your head, so we don’t quite know everything you are trying to do. More information would be helpful :smiley:

Sorry, I was probably a bit vague, although it was because I believe after a lot of testing that my problem lies within the relative_time component.

My sensor is a shell_command that outputs this to a .txt file (I added the text in the parentheses):

off (charging state)
71 (battery charge %)
disconnected (charging cable state)
84 (range in km)
3000 (outside temperature in C * 100)
2900 (inside temperature in C * 100)
2016-09-07T10:53:05.001+02:00 (time of last update from car)

And then I read these values into separate sensors using the command_line component like so:

  • platform: command_line
    name: Rekkevidde
    command: “sed -n 4p /home/pi/eup.txt”
    value_template: ‘{{ value | int }}’
    unit_of_measurement: “km”
  • platform: command_line
    name: Utetemperatur
    command: “sed -n 6p /home/pi/eup.txt”
    value_template: ‘{{ value | multiply(0.01) | round(1) }}’
    unit_of_measurement: “%”
  • platform: command_line
    name: Siste oppdatering
    command: “sed -n 7p /home/pi/eup.txt”
    value_template: ‘{{ as_timestamp(value) | timestamp_local}}’

(I skipped a few lines for brevity.)

This gives me sensors like this (with some customizations):

Now what I’m trying to do is convert the “Siste oppdatering” (Last update) to a relative time, but this seems impossible as the relative_time component will not process it whatever I do - I’ve tried many different iterations of the output, and it does not seem to work either from the config file or the development tools.

The relative_time component will not work on the following lines in the development tools either:

{{ relative_time(“2016-09-07 21:44:29.718119+00:00”) }}
{{ relative_time(1473238385.001) }}

In fact, the only way I’ve managed to get it to work is to specifically reference a last_updated field.

So my question is basically, is there any way to get a timestamp from a command line sensor and convert it to a relative time?

And as a quick bonus question, is there any way to read a multi-line response from a shell command without going the route through a text file like I have done? (My script to get data from the car gets all fields, so I would like to not have to run it 6 times every update as I want to avoid hammering the car data service.)

Thank you in advance:)
Preben

if you would use appdaemon https://github.com/acockburn/appdaemon/blob/master/API.md you could probably do everything you want.

you can run a script which reads the file once and makes 6 files out of it, which you then can read with commandline sensors.
you can also make sure that the time is rewritten to relative time before you split the file.
(i would probably even set the values through appdaemon)

the other way is just to run a py code which does that for you.

1 Like

Cool, I had a quick look at appdaemon, will try to get into it over the weekend and see if I can’t make it work:)

Thanks for the tip:)

Personally, I recommend AppDaemon for ALL of your automations. There is no reason to use built-in automation functionality at all. Compared to AppDaemon, Home Assistsnts automations are rudimentary at best.

1 Like

Doesn’t look like the original question was answered about the limitations of the relative_time function. Anyone have thoughts on how to handle the original post?

1 Like

yeah like said noy use it and use appdaemon instead :wink: