Help with template - unix time convert

Hi

Can someone help with the following template please:

I’ve got a sensor with an attribute which contains the unix time when the shutter has been executed last time.
It’s possible to convert / and show the output in a template?

for instance:

sensor.shutter
attribute:
  unix_time:1696855171.914279

Text

Use the timestamp_custom filter (or timestamp_local).

Reference:

Templating - Time

I don’t get it working

my template looks like this:

template:
  - sensor:
    - name: "Somfy unix date"
      state: "{{ state_attr('cover.somfy','cmd_fired') }}"

which creates a sensor with the unix time (as expected). In my case:

1701077095.18554

but I’m trying to convert this to a readable date/time

  - sensor:
    - name: "Somfy normal date"
      value_template: "{{  now()|as_timestamp - states('sensor.somfy_unix_date')|int < 0 }}"

but it gives me:

Invalid config for [template]: [value_template] is an invalid option for [template]. Check: template->sensor->0->value_template. (See /config/packages/esprolluik.yaml, line 1).

hm this gives me the same template error

  - sensor:
    - name: "Somfy normal date"
      value_template: "{{  now()|as_timestamp - states("sensor.somfy_unix_date"), 0)) |timestamp_custom("%B %d, %Y") }}"
1 Like

Did you try reading the error message and the documentation?

You’re using the modern format (template: at the top level), so you use state not value_template.

Both of your templates are unlikely to do what you want: I’m guessing you’ve copy/pasted them without understanding?

Your first post template only returns true or false depending if your UNIX timestamp is in the past or future. If that is what you want, it would be better to define a binary sensor.

Your second post template has other errors in it. You seem to be trying to convert the difference between two times into a date?

Try this:

  - sensor:
    - name: "Somfy normal date"
      state: "{{ states('sensor.somfy_unix_date')|int(0) | timestamp_custom('%B %d, %Y') }}"

1 Like

no, what I want to achieve with “somfy normal date sensor” is a convert from the unix time (somfy unix date sensor) to a readable date/time (so day, month, year, hour and minute).

Ok seems like I’m mixing templates, very confusing

Hellow!
I have same problem. I my config have:

- platform: template
  sensors:
     unixtime_normtime:
       friendly_name: 'Unixtime2Time'
       device_class: timestamp
       icon_template: mdi:watch
       state: "{{ states('sensor.mqttunixtime') | int (0) | timestamp_custom('%H:%M:%S')}}"
#      state: {{ states.sensor.mqttunixtime.state | int | timestamp_custom("%H:%M:%S") }}
#      value_template: >-
#         {{ states('sensor.mqttunixtime') | int(0) | timestamp_custom('%H:%M:%S') }}
#         {{ states.sensor.mqttunixtime.state | int | timestamp_custom("%H:%M:%S") }}

but when i check configuration on developer tools, got this error:

Configuration invalid!
Invalid config for [sensor.template]: [states] is an invalid option for [sensor.template]. Check: sensor.template->sensors->unixtime_normtime->states. (See ?, line ?).

When try same code in different way in dev.tools template, it’s workig:

value_template: >-
         {{ states.sensor.mqttunixtime.state |int| timestamp_custom("%H:%M:%S") }}
state: {{ states.sensor.mqttunixtime.state | int | timestamp_custom('%H:%M:%S') }}

value_template: >-
         {{ states('sensor.mqttunixtime') | int(0) | timestamp_custom('%H:%M:%S')}}
state: {{ states('sensor.mqttunixtime') | int(0) | timestamp_custom('%H:%M:%S') }}

I got this error in log:

Logger: homeassistant.components.sensor.helpers
Source: components/sensor/helpers.py:22
Integration: Sensor (documentation, issues)
First occurred: 2:35:17 PM (45 occurrences)
Last logged: 3:18:05 PM

sensor.unixtime_normtime rendered invalid timestamp: 29.11.2023 15:14
sensor.unixtime_normtime rendered invalid timestamp: 29.11.2023 15:15
sensor.unixtime_normtime rendered invalid timestamp: 29.11.2023 15:16
sensor.unixtime_normtime rendered invalid timestamp: 29.11.2023 15:17
sensor.unixtime_normtime rendered invalid timestamp: 29.11.2023 15:18

Two problems there:

Firstly, you’re using the legacy template sensor configuration — that needs value_template not state.

Secondly, a sensor with device_class: timestamp must have a state that is either a datetime object or an ISO8601 string (docs). The timestamp in your error message is neither of those, nor is it produced by the sensor configuration you posted.

Explain what you want and we’ll sort you out :slight_smile: .

Soo… a have a value of time (unix time) from weather station which is sended in HA trought mqtt (sensor.mqttunixtime) and it’s ok.
I want to transform this value in standard time (just hh:mm:ss) trought another sensor (sensor.unixtime_normtime) and shown in HA.
Thank’s!

Your first example will work fine if you use value_template instead of state, and remove the device_class.

Using modern configuration:

template:
  - sensor:
      - name: "UNIXtime normtime"
        state: "{{ states('sensor.mqttunixtime')|as_timestamp|int(0)|timestamp_custom('%H:%M:%S') }}"
        icon: mdi:watch

Solved!!!
Thank’s!
Already discovered with trial & error… :crazy_face: :hugs: :handshake:
Thank’s for your time! :hugs: :hugs: :hugs: