‘{% set tempvar = ((as_timestamp(states.sun.sun.attributes.next_setting)) | timestamp_local) %}{{ tempvar[11:16] }}’
thx.
now i feel stupid for turning it into a list before taking a part from the string
and i guess i did something wrong when i tried it with : because that didnt function at that moment.
i can be even more simple as i figured out now:
‘{{ ((as_timestamp(states.sun.sun.attributes.next_setting)) | timestamp_local) [11:16] }}’
That works nice, thanks.
I used it in a template sensor to display the rising and setting times directly in the GUI.
I found this is a more convenient way to do it:
{{ as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom("%H:%M") }}
Edit: removed wrong “(” at the start.
Thanks for this. Works perfectly! I did remove the first ‘(’
And if anyone’s wondering how it looks in a sensors.yaml file:
- platform: template
sensors:
sunset_time:
value_template: '{{ as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom("%H:%M") }}'
- platform: template
sensors:
sunrise_time:
value_template: '{{ as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom("%H:%M") }}'
i guess you are right.
that option wasnt there at the time i wrote it, but now that should be the way.
Yeah my bad. I copied it from my config where I use it to display a time which is offset by a certain amount by a slider to enable variable home automation.
See it the example here:
@ReneTode: I revived this topic since I was struggling as well with the same issue and found the timestamp_custom function very useful also for my automation (see the link above)
This is great, just what I was looking for
Is there any way to change the format of the time? For example from 05:59 to 5:59 AM, and from 18:24 to 6:24 PM?
I struggled with stripping out the leading 0, as well as the am/pm setting. I was finally able to get it working correctly using the following code in my sensors YAML file:
- platform: template
sensors:
sunset_time:
value_template: '{% set timestamp = as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom("%I:%M") %} {{ timestamp.lstrip("0") }}'
friendly_name: "Sunset"
- platform: template
sensors:
sunrise_time:
value_template: '{% set timestamp = as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom("%I:%M") %} {{ timestamp.lstrip("0") }}'
friendly_name: "Sunrise"
Great, thanks a lot for that!
fyi, @jeremyowens using %-I should remove the 0.
"%-I:%M %p"
should give you something like 9:05 PM, remove the %p if you don’t want am/pm.
more reference http://strftime.org/
Thanks Jeremy… using your sensor code. One little mod people may like… use customize to set the sensor badge unit of measurement to AM or PM and it displays nicely under the time
customize:
sensor.sunrise_time:
unit_of_measurement: AM
sensor.sunset_time:
unit_of_measurement: PM
Looks like this: https://www.screencast.com/t/K3hdqHq6YiXa
Hi
How do i get hold of these sensors:
sensor.sunrise_time
sensor.sunset_time
/Jerry
You mean this:
- platform: template
sensors:
sunset_time:
/Jerry
Yep, that’s where the two template sensors are defined.
Oki then, i will try this out.
If i paste this in config.yaml
-
platform: template
sensors:
sunset_time:
value_template: ‘{% set timestamp = as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom("%I:%M") %} {{ timestamp.lstrip(“0”) }}’
friendly_name: “Sunset” -
platform: template
sensors:
sunrise_time:
value_template: ‘{% set timestamp = as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom("%I:%M") %} {{ timestamp.lstrip(“0”) }}’
friendly_name: “Sunrise”
I get this error:
Error loading /config/configuration.yaml: while parsing a block mapping
in “/config/configuration.yaml”, line 5, column 1
expected , but found ‘-’
in “/config/configuration.yaml”, line 34, column 1
How come?
These are template sensors, and the line before these config entries must be:
sensor:
If you the get an error message saying something like “duplicate sensor” then you already have such a line somewhere in your config file, and need to move your above config entries underneath that existing list of sensors.
Oki, i will try that.