Convert UNIX Time to normal Time

Hi,

I am getting from a weatherstation the time of the latest values via unix time.

I am trying now to convert this via a template, but it doesnt work.

I have the following template, which delivers me the unix time:

sensors:
  weather_datetime_template:
    value_template: '{{ states.sensor.weather_datetime.state }}'  

What do I have to do to get a readable date/time?

Br,
Johannes

What do you mean by “unix time”? is it a epoch timestamp or something else?

Post the state of the sensor.weather_datetime.

Hi,

This is the state: 1547396700.0

Br,
Johannes

Try This:
‘{{ states.sensor.weather_datetime.state | int | timestamp_custom("%d.%m.%Y %H:%M")}}’

2 Likes

try:

{{ states.sensor.weather_datetime.state | timestamp_custom('%Y/%m/%d %H:%M:%S') }}

that will return the date & time in this format “2019/01/13 11:25:00” based on the state you gave me above.

And now for shameless self-promotion I created a thread on manipulating dates & times here:

Maybe you will find it helpful.

1 Like

Thanks daveyrb, that worked.

hello,
I’m using your method to convert an epoch time to update with the following config:

sensor:
  - platform: snmp
    scan_interval: 10
    name: ex4200
    unique_id: ex4200
    host: 192.168.1.254
     baseoid: 1.3.6.1.4.1.2636.3.1.5.0
    accept_errors: true
    value_template: '{{ states.sensor.juniper_ex4200 | int | timestamp_custom("%d.%m.%Y %H:%M")}}'

but I got the following error:

ValueError: Template error: int got invalid input '<template TemplateState(<state sensor.ex4200=unavailable; restored=True, friendly_name=ex4200, supported_features=0 @ 2024-05-15T13:08:31.746892+02:00>)>' when rendering template '{{ states.sensor.juniper_ex4200 | int | timestamp_custom("%d.%m.%Y %H:%M")}}' but no default was specified

removing the value_template is working with the second timer :frowning:
thanks for your help :wink:

    value_template: "{{ states('sensor.juniper_ex4200') | int(0) | timestamp_custom('%d.%m.%Y %H:%M') }}"

Read this carefully:

You were trying to convert a state object (first line in screenshot below) to a number. I’m using the states() function to get the state of the object. Another (less-preferred) option is the third line:

You’ll see I have also included a default value on the int filter. That is useful if the sensor state is ever not a number (e.g. unavailable): the output will then be Jan 1970 rather than an error message.