Newbie Question - Variable for Time since Sunrise/Sunset?

I’m not sure if this already exists and I just haven’t found it. I am working on an automation that uses a service_template to decide to turn things on or off (Christmas Lights)

trigger:
- event: sunset
  platform: sun
- event: sunset
  offset: +05:00:00
  platform: sun 
action:
- service_template: >
    {%- if is_state('switch.outlet', 'off') -%}
      homeassistant.turn_on
    {%- else -%}
      homeassistant.turn_off
    {%- endif -%}
  entity_id: switch.outlet

I would like to also be able to check the time since sunset at the same time as checking whether they are on/off (I realize as it stands it is the same as homeassistant.toggle without ifs)
I did not see a variable in state.sun.sun as it has the next_setting, not the last_setting. But, with conditions you can test sunset with an offset, so I was hoping this is available somewhere…?

I think what you want to do is turn the switch on at sunset, and turn it off five hours later. Right? How about this:

- alias: Christmas lights
  trigger:
    platform: sun
    event: sunset
  action:
    - service: switch.turn_on
      entity_id: switch.outlet
    - delay: 05:00:00
    - service: switch.turn_off
      entity_id: switch.outlet

I thought of and tried that, it will work, but if I wanted something to also turn the lights on again an hour before sunrise, but only for an hour, then it doesn’t work so well.

Other things I have tried:

  trigger:
    platform: sun
    event: sunset
  action:
    - service: homeassistant.turn_on
      entity_id: switch.outlet
    - delay: 05:00:00
    - service: homeassistant.turn_off
      entity_id: switch.outlet

and

  trigger:
    - platform: sun
      event: sunset
    - platform: sun
      event: sunset
      offset: +05:00:00
  action:
    - service: homeassistant.turn_on
      entity_id: switch.outlet
    - condition:
      condition: sun
      after: sunset
      after_offset: "+01:00:00"
    - service: homeassistant.turn_off
      entity_id: switch.outlet

What I would really like to know is if I have access to time since or until sunrise or sunset on the current day. Especially if the sunrise or sunset is in the past, as I would like to use this in a service_template (or another template).

Thank you!

Wouldn’t that just be another automation? Why would that make this automation not work? Or maybe this was just a small part of what you’re trying to do and I’m reading too much into it?

Well, I tried to enhance the standard sun component to add these features, but it was rejected. However, I do have a custom version of the sun component that you can use that can have the attributes you’re looking for. Others have used it successfully. You can find information about it here.

If we use the basic configuration example that you use on your github docs will that breal any automations that use the current basic sun: component?

right now I just have "sun: " my config.

I will switch to yours as the example shows:

sun:
  monitored_conditions:
    - elevation
    - sunrise
    - sunset

It looks like from the description that should be all that is needed to mimic the original component.

Is that a good assumption?

If you just have sun:, then it will indeed act just like the standard component. If you want to specify monitored_conditions, and still have it act like the standard component, then you would need:

sun:
  monitored_conditions:
    - azimuth
    - elevation
    - next_dawn
    - next_dusk
    - next_midnight
    - next_noon

Of course, there would be no point in doing that. So to that list you would probably want to add other, new attributes, and possibly remove some of the standard, old attributes.

OK.

I just didn’t want to break any existing automations by inadvertently leaving out any existing attributes that I might be using somewhere.