Formatting Sensor Milliseconds Timestamps

I’m using NodeRed to send a timestamp to and MQTT topic. This is being read as a sensor by HA:

sensor:
  - platform: mqtt
    name: "ping"
    state_topic: "home-assistant/ping"

This sensor works well, but when displaying in Lovelace I get the Linux/Epoch time:

1558025641214

  1. What value template should I use? In the dev template tester the following returns 1558025641214:

{{states.sensor.ping.state | timestamp_local}}

I presume because the state isn’t of the right type. However using as_timestamp() returns None.

  1. Once I get it working in the dev tool, how should I apply it in config? Things I have tried are:

{{state | timestamp_local}}

or even

{{timestamp_local}}

?

  1. A follow up question: presumably using a value template changes the sensor read out for anything downstream. Is it possible to keep the Linux time for the sensor value and only format it for the Lovelace card?

I’ve used the following to fix this in Node-RED:

But it would be interesting to know how I would have done it otherwise.

you were missing the conversion to a number

{{states.sensor.ping.state | int | timestamp_local}}

Thanks Petro! That makes sense.

Any help with 2 or 3? I tried using int in my value_template but it didn’t seem to work:

sensor:
  - platform: mqtt
    name: "ping"
    state_topic: "home-assistant/ping"
    value_template: {{state | int | timestamp_local}}

should just be

sensor:
  - platform: mqtt
    name: "ping"
    state_topic: "home-assistant/ping"
    value_template: {{ value | int | timestamp_local }}

The above didn’t pass validation, so I added quotes around the template (single and double).

Still no luck - the sensor still returns the raw epoch time.

I see the same behaviour in the template tool.

This is an invalid Unix timestamp: 1558025641214

For comparison, here’s today’s date, rounded to the nearest integer value: 1558046099

Attempting to do this in the Template Editor results in an error message:
{{ 1558025641214 | timestamp_local }}

EDIT: @sshaikh

probably microseconds, divide by 1000.

sensor:
  - platform: mqtt
    name: "ping"
    state_topic: "home-assistant/ping"
    value_template: "{{ ( value | int  / 1000 ) | timestamp_local }}"

What are you using in Node-Red to generate this timestamp (expressed in micromilliseconds, not seconds)?

FWIW, Javascript (JS) expresses timestamps in micromilliseconds whereas Unix timestamps are in seconds. If you’re using Date.now() to generate the JS timestamp, divide it by a 1000 before publishing it for use by Home Assistant. This will spare you having to convert it for each and every sensor that receives a JS timestamp. On the other hand, if you only plan to have one sensor receiving the value, I guess it makes no difference who performs the conversion (Home Assistant or Node-Red) and Petro’s suggestion will definitely do the trick. :+1:

1 Like

Do you all mean milliseconds? Either way the solution works, so thanks all.

@123 I used the simple Inject with default settings. I also agree with your assessments - Node-RED has a Moment node that does an equivalent “right thing” too. I just wanted to know for completion, partly because there will be sensors that I won’t have control over.

I still have the open (but related) question about raw data. Is there any way to recover the raw data for downstream requirements? The only solution I can think of is to create two sensors.

1 Like

Milli, yes! My mistake. I’ve updated my original post.

My knowledge of Lovelace’s capabilities is insufficient to answer your question (sensor state is a timestamp but displayed in Lovelace UI as date/time).

I’ve taken a step back and decided to use timestamp strings where I can of the type:

2019-05-17T15:07:57.410Z

HA has a device class that tells HA it’s a timestamp. Lovelace will then be smarter about how it displays it.

  • platform: mqtt
    name: “ping”
    state_topic: “home-assistant/ping”
    device_class: timestamp