I am trying to show the sunset and sunrise in hadashboard, but having a difficult time formatting it. I created two sensors based on the sun.sun component and strptime isn’t working for me. I tried it like this as well as like next_setting.strptime(’%I:%M’)
sunset:
friendly_name: Sunset
value_template: '{{ strptime(states.sun.sun.attributes.next_setting, "%I:%M %p") }}'
sunrise:
friendly_name: Sunrise
value_template: '{{ strptime(states.sun.sun.attributes.next_rising, "%I:%M %p") }}'
both in hadashboard and in ha, the value shows as: 2017-09-17T23:34:09+00:00
Any suggestions?
Hi,
you can format the time by using “timestamp_custom” command.
See:
value_template: “{{ as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom(‘%d.%m.%Y %-H:%M’) }}”
or
value_template: “{{ as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom(‘%d.%m.%Y %-H:%M’) }}”
Timeformat also see: time — Time access and conversions — Python 3.12.2 documentation
Hmm… Tried that and the config check is blowing up on them…
Also tried:
sunset:
friendly_name: Sunset
value_template: ‘{{ strptime(states.sun.sun.attributes.next_setting, “%I:%M %p”) }}’
sunrise:
friendly_name: Sunrise
value_template: ‘{{ states.sun.sun.attributes.next_rising.strftime("%I:%M %p") }}’
and I get these values:
tried to validate config with your suggestion, I get the following:
value_template: {{ states.remote.harmony_main.attributes.current_activity }}
sunrise: [source /home/homeassistant/.homeassistant/devices/sensors.yaml:28]
friendly_name: Sunrise
value_template: {{ as_timestamp(states.sun.sun.attributes.next_rising) |
Am I missing something?
Here is my full config of the two sensors plus two screenshots.
Maybe this helps:
nextsunrise:
friendly_name: 'Nächster Sonnenaufgang'
value_template: "{{ as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom('%d.%m.%Y %-H:%M') }}"
icon_template: mdi:weather-sunset-up
nextsunset:
friendly_name: 'Nächster Sonnenuntergang'
value_template: "{{ as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom('%d.%m.%Y %-H:%M') }}"
icon_template: mdi:weather-sunset-down
My code:
What I get:
Got my config from thist post: https://community.home-assistant.io/t/date-formatting/17461
4 Likes
Thank you so much! Copied yours directly and it works (then changed the formatting).
You rock!
Great to hear!
You´r welcome.