- id: '1626701115745'
alias: Green Light - On
description: ''
trigger:
- platform: time
at: sensor.greenlight_on_time
condition: []
action:
- service: light.turn_on
target:
entity_id: light.sonoff_green
If I don’t add device_class: timestamp it shows as a string in lovelace, with it, I have a proper “relative time” displayed, meaning to me that this does the job, as the documentation states “The Entity ID of a sensor with the “timestamp” device class.”
In the example I presented, I focused on the template itself and forgot to add device_class: timestamp to the Template Sensor’s configuration. Sorry about that.
Every timestamp sensor that I have seen, presents the date, time, andtimezone. For example:
I added as_local and it now works. As a developer, I find it a rather complicated way to calculate and format a date to get it working in automations …
It’s complicated because you made it complicated. There’s no need to convert from timestamps to datetime objects to time strings so many times. The calculation could have been done using timestamps and converted once at the very end.
When I suggested using as_datetime (recently introduced in version 2021.7) I didn’t suggest using it the way the calculation mixes UTC and local time (and loses the timezone in the process).
The following template’s output is in UTC with the timezone. Home Assistant has no trouble converting it to local time (assuming Home Assistant’s timezone has been set correctly).
Yes; I had left it in seconds but if the input_number’s value is actually hours then your modification is preferable.
Did you find that the addition of as_local was necessary? The template is being used in a Template Sensor whose device_class is timestamp therefore UTC with timezone ought to be sufficient without the added step of converting to local time.