How to calculate time to Sunrise for Deep Sleep

Upon sunset I’d like to put a device into deep sleep and have it wake up at sunrise. It appears the way to do this is to use ‘deep_sleep.enter’ with ‘sleep_duration:’ set to the time to sunrise. How do you calculate time to sunrise? Will this work?

    - deep_sleep.enter:
        sleep_duration: !lambda 'return id(sun_component).sunrise(0).value().timestamp - id(homeassistant_time).now().timestamp;'

Thanks

The other option seems to fit your use case better

on_...:
  then:
    - deep_sleep.enter:
        id: deep_sleep_1
        until: !lambda 'return id(sun_next_rising)'
        time_id: sntp_id ## id of which ever time component you are using not the sunrise sensor

You should use the next sunrise sensor from HA rather than the full sun component. You will also need to have the deep sleep key without anything else in your config

deep_sleep:

I don’t believe until: is templatatable

It looks like sleep_duration is. I’d create a template sensor in ha with the time to next rising.

      - name: "Time to next sunrise"
        unique_id: xrrnsunrisingtemp
        state: >
          {% set next = state_attr('sun.sun', 'next_rising') %}
          {{ (next | as_datetime - now()).seconds | timestamp_custom('%H %M %S', False) }}

edit: changed the sensor template