Sun Next Rising time in badge

Seems like this would be simple, but maybe I’m missing something.

I just want to have a badge the shows the time (hh:mm) of the next sunrise.

I see the next_rising attribute in the sun.sun entity. I have used the “Attributes extractor” I found in HACS and have created an entity sensor.sun_next_rising, which is a timestamp. If I use sensor.sun_next_rising in the badge, all I see is “2022-…”.

How do I get the badge to display hh:mm from a timestamp?

Create a template sensor with this template:

{{ as_timestamp(state_attr('sun.sun', 'next_rising')) | timestamp_custom("%H:%M", false) }}

Thanks, that helped. I had to do some research as to what “Create a template sensor” meant, but I think got something working, but not quite right yet. Sorry, really confused by HA at times.

In my configuration.yaml I have:

template:
  - sensor:
    - name: Sun - Next Rising
      state: >
        {{ as_timestamp(state_attr('sun.sun', 'next_rising')) | timestamp_custom("%-I:%M", false ) }}

Now how do I show this as the correct time for my time zone? What is showing is several hours off, which I assume is GMT.

I believe I should have used true instead of false.
Try that and see if that works.

Thank you. Changing false to true did fix it.

I spent some time learning about how to use templates. In the past I had found a few things in posts that I managed to copy and get to work, but really didn’t understand much. I still don’t claim to understand much more than the basic concept, but at least I have a bit of an understanding now. I have to say, templating is a really user unfriendly way to extract attribute values. I know templating is far more powerful than just extracting attributes, but having access to attribute values is so fundamental, I should think there would be a better way.

Anywho, on to the next.

I don’t understand why I get an indention error with this:

template:
  - sensor:
    - name: Sun - Next Rising
      state: '{{ as_timestamp(state_attr('sun.sun', 'next_rising')) | timestamp_custom("%-I:%M", false ) }}'
bad indentation of a mapping entry (18:43)

 15 |  ... 
 16 |  ... 
 17 |  ... Sun - Next Rising
 18 |  ...  '{{ as_timestamp(state_attr('sun.sun', 'next_rising')) | tim ...
-----------------------------------------^

Compare your example’s indentation to the example in the documentation and the error will become obvious.

I had tried that too, it just wasn’t the version I copy and pasted here. Sorry about that.

I get the same indentation error with this version:

template:
  - sensor:
      - name: Sun - Next Rising
        state: '{{ as_timestamp(state_attr('sun.sun', 'next_rising')) | timestamp_custom("%-I:%M", false ) }}'
bad indentation of a mapping entry (18:45)

 15 |  ... 
 16 |  ... 
 17 |  ... Sun - Next Rising
 18 |  ...  '{{ as_timestamp(state_attr('sun.sun', 'next_rising')) | tim ...
-----------------------------------------^

I believe the issue is that you use ' both as the main and inner enclosure.

I think:

template:
  - sensor:
      - name: Sun - Next Rising
        state: "{{ as_timestamp(state_attr('sun.sun', 'next_rising')) | timestamp_custom('%-I:%M', true ) }}"

Will work because now we have " around the template and use ' inside the template.

Thank you so much! I learned something today.