Sunset not working

I’m not sure. You should be watching your events. Plus if you have that sun based theme selector, why not base your automation off that changing?

Given that this is a template_sensor, and if you are using version 0.81 or higher, maybe Home Assistant’s template analyzer can’t identify the entity, sun.sun, responsible for triggering this sensor. If that’s the case then it may help to explicitly identify it:

        friendly_name: Sun based theme
        entity_id: sun.sun
        value_template: >

His problem isn’t with the template he posted, it’s with this method for automations:

  trigger:
    - platform: sun
      event: sunset

I read “based on this sensor” as meaning the posted example didn’t work:

I now see the very last line says: “If I use above_horizon and below_horizon, it immediately works” which suggests the posted example does work. At least that gives me confidence in the template analyzer’s ability to identify sun.sun. :slight_smile:

Yeah, what you posted is correct if it was his problem. For some reason, his automations aren’t triggering off sunset or sunrise states. I don’t use the conventional automation enough to be able to debug it. Have you had sunset/sunrise triggers fail to trigger?

Short answer: No.

Long answer: No because I don’t use it.

Strange as it may sound but, for all my interest and participation in Home Assistant’s community forum, I don’t use it to control my home automation.

Home Assistant currently serves as a frontend to my existing system, Premise, which handles all control logic. The two systems communicate via MQTT.

Truth be told, I also use openHAB as a frontend as well, mostly because it gives me free remote-access. However, I have a greater interest in Home Assistant.

Having said all that, I’ll create sunset/sunrise automations and see how they behave.

hi,

no its not an issue of the sun.sun being able to trigger the automation or change template sensors, that works just fine.

I must conclude that both triggers work, but the event trigger happens sooner than the change of the sensor state = above/below_horizon, in which case the automation triggers to set the theme it already is…

That could be an option indeed, a bit of a workaround maybe, because the true trigger is the sun changing state, or the event happening.

will try both alternatives, with a delay to be sure the sensor has changed state, and trigger based on state change of the sensor.

thanks !

Ah Ok, I miss understood your original problem. Honestly, I don’t think it’s a work around to build the automation off theme selector changing. If the intent of the automation is to properly set the theme, then base it off the selector changing.

lol, thanks, I understand what you are saying, and I do have that automation too.

this is my scenario:

set themes for both sunset and sunrise.

I have these input_selects:

  set_sunset_theme:
    name: 'Select Sunset theme'
    icon: mdi:weather-night
    options:
     - 'darkblue'
     - 'darkcyan'
     - 'darkorange'
     - 'darkred'
     - 'default'
     - 'done'
     - 'matrix'
     - 'midnight'
     - 'minimal'
     - 'PmxMononight'
     - 'stormy_hues'
     - 'teal'
     - 'vintage'
#    initial: 'darkblue'

  set_sunrise_theme:
    name: 'Select Sunrise theme'
    icon: mdi:weather-sunny
    options:
     - 'darkblue'
     - 'darkcyan'
     - 'darkorange'
     - 'darkred'
     - 'default'
     - 'done'
     - 'matrix'
     - 'midnight'
     - 'minimal'
     - 'PmxMononight'
     - 'stormy_hues'
     - 'teal'
     - 'vintage'
#    initial: 'minimal'

the automation triggers on HA start, changing of these input_selects (manually) and on the sun events automatically:

  - alias: Sun based theme change
    id: 'Sun based theme change'
#    initial_state: 'on'
    trigger:
      - platform: homeassistant
        event: start
      - platform: state
        entity_id: input_select.set_sunset_theme
      - platform: state
        entity_id: input_select.set_sunrise_theme
      - platform: sun
        event: sunrise
      - platform: sun
        event: sunset
#      - platform: state
#        entity_id: sun.sun
#        to: above_horizon
#      - platform: state
#        entity_id: sun.sun
#        to: below_horizon
    condition: []
    action:
      - delay:
          seconds: 10
      - service_template: frontend.set_theme
        data_template:
          name: >
            {{ states('sensor.sun_based_theme') }}
      - condition: template
        value_template: >
          {{ is_state('input_boolean.notify_system', 'on')}}
      - service: notify.notify
        data_template:
          message: >
            {{ as_timestamp(now()) | timestamp_custom("%X") }}: 
            Sun is {{states('sun.sun')}} and Frontend is set to '{{ states('sensor.sun_based_theme') }}'

It uses the sensor (which reads the state of the sun.sun) to decide which theme it needs to set.

sensor:
  - platform: template
    sensors:
      sun_based_theme:
        friendly_name: Sun based theme
        value_template: >
          {% if is_state('sun.sun','above_horizon') %}
           {{states('input_select.set_sunrise_theme')}}
          {% else %}
            {{states('input_select.set_sunset_theme')}}
          {% endif %}
        icon_template: >
          {% if is_state('sun.sun', 'above_horizon') %}
            mdi:weather-sunny
          {% else %}
            mdi:weather-night
          {% endif %}

So I can’t use the sensor in its current form to trigger the automation?
Using the state in the trigger brings both the trigger and sensor on the same level, so works as desired.
Using the event seems to happen sooner than the state change of the sensor, and then nothing happens. Hence the built-in delay I will be able to test in a few hours.

Else I should change the sensor somehow, but now that Ive lined this all up again, Im not sure how to follow your suggestion to base the trigger in the sensor other than:

  - alias: Sun based theme change
    id: 'Sun based theme change'
#    initial_state: 'on'
    trigger:
      - platform: homeassistant
        event: start
      - platform: state
        entity_id: input_select.set_sunset_theme
      - platform: state
        entity_id: input_select.set_sunrise_theme
      - platform: state
        entity_id: sensor.sun_based_theme # this is the new line?
#      - platform: sun
#        event: sunrise
#      - platform: sun
#        event: sunset
#      - platform: state
#        entity_id: sun.sun
#        to: above_horizon
#      - platform: state
#        entity_id: sun.sun
#        to: below_horizon
    condition: []
    action:
      - service_template: frontend.set_theme
        data_template:
          name: >
            {{ states('sensor.sun_based_theme') }}

I think we’ve lost the OP.

Just change your frontend.set_theme for those triggers

      - service_template: frontend.set_theme
        data_template:
          name: >
            {% if trigger.platform == 'sun' %}
              input_select.{{ 'set_sunset_theme' if trigger.event == 'sunset' else 'set_sunrise_theme' }}
            {% else %}
              {{ states('sensor.sun_based_theme') }}
            {% endif %}
1 Like

clever!

will implement and see in 49 minutes :wink:

—update—

really sorry to report, this doesn’t work. Had my system notifications turned on, and indeed I got a notification, so I know 100% sure the automation was triggered and passed all steps in the action.

No theme change though…

for completeness sake and documentation this is the changed automation:

  - alias: Sun based theme change
    id: 'Sun based theme change'
#    initial_state: 'on'
    trigger:
      - platform: homeassistant
        event: start
      - platform: state
        entity_id: input_select.set_sunset_theme
      - platform: state
        entity_id: input_select.set_sunrise_theme
      - platform: sun
        event: sunrise
      - platform: sun
        event: sunset
#      - platform: state
#        entity_id: sun.sun
#        to: above_horizon
#      - platform: state
#        entity_id: sun.sun
#        to: below_horizon
    condition: []
    action:
      - service_template: frontend.set_theme
        data_template:
          name: >
            {% if trigger.platform == 'sun' %}
              input_select.{{ 'set_sunset_theme' if trigger.event == 'sunset' else 'set_sunrise_theme' }}
            {% else %}
              {{ states('sensor.sun_based_theme') }}
            {% endif %}
      - condition: template
        value_template: >
          {{ is_state('input_boolean.notify_system', 'on')}}
      - service: notify.notify
        data_template:
          message: >
            {{ as_timestamp(now()) | timestamp_custom("%X") }}: 
            Sun is {{states('sun.sun')}} and Frontend is set to '{{ states('sensor.sun_based_theme') }}'

and see what the notification sent me…

42

the top message is from:

  - alias: Notify sun state change
    id: 'Notify sun state change'
    trigger:
      platform: state
      entity_id: sun.sun
    condition:
      - condition: template
        value_template: >
          {{ trigger.to_state.state is in ['above_horizon', 'below_horizon'] and
             trigger.to_state.state != trigger.from_state.state }}
      - condition: template
        value_template: >
          {{ is_state('input_boolean.notify_system', 'on')}}
    action:
      service: notify.notify
      data_template:
        message: >
         Sun state change - {{as_timestamp(now()) | timestamp_custom("%X") }} The Sun is 
         {{'up' if is_state('sun.sun','above_horizon') else 'down'}} and
         Solar angle is {{states('sensor.solar_angle')}}

I think the the template isn’t correct, it should set the name to

         {{states('input_select.set_sunset_theme')}}

or
{{states('input_select.set_sunrise_theme')}}

and not to input_select.set_sunset_theme which is what it does now.

testing this for tomorrows sunrise:

  - service_template: frontend.set_theme
    data_template:
      name: >
        {% if trigger.platform == 'sun' %}
          {{states('input_select.set_sunset_theme') if trigger.event == 'sunset' else 
            states('input_select.set_sunrise_theme') }}
        {% else %}
          {{ states('sensor.sun_based_theme') }}
        {% endif %}

Moreover still, the message is clear, it says Sun is above_horizon, indicating the state of the sun.sun isn’t yet synchronous with the event having taken place. Needs 2 seconds…so will add that also.

ok, to finalize this, I must conclude the platform: sun events trigger perfectly, but combined with entity sun.sun states this causes synchronization issues, needing at least a delay to have the state of the sensor being updated adequately.

I’ve solved my specific issue by resorting once again to a trigger based on the state, albeit indirectly this time, which makes the automation even smaller. I now use only the state of the sensor.sun_based_theme, which changes state on sun.sun changing state:

  - alias: Sun based theme change
    id: 'Sun based theme change'
#    initial_state: 'on'
    trigger:
      - platform: homeassistant
        event: start
#      - platform: state
#        entity_id: input_select.set_sunset_theme
#      - platform: state
#        entity_id: input_select.set_sunrise_theme
      - platform: state
        entity_id: sensor.sun_based_theme
#      - platform: sun
#        event: sunrise
#      - platform: sun
#        event: sunset
#      - platform: state
#        entity_id: sun.sun
#        to: above_horizon
#      - platform: state
#        entity_id: sun.sun
#        to: below_horizon
    condition: []
    action:
#      - delay:
#          seconds: 3
      - service_template: frontend.set_theme
        data_template:
          name: >
            {{ states('sensor.sun_based_theme') }}

result:

19

perfect synchronization up to the second, and correct state change and correct theme change…

1 Like

Just the other day I noticed that my automation for the same purpose (setting my theme at sunset) was firing on every state change of the sun (including attributes) because I didn’t specify a to: or from: state. So I decided to switch to the nifty built-in sunrise/sunset triggers. Same issue as you were having two years ago! I’ve added a three second delay and will see how it goes tomorrow.

Can anyone else coorborate on recently seeing this behavior? I’ll probably submit an issue if adding the delay works, because every other platform I’ve used this sort of logic on has worked fine. I’m using this logic in quite a few automations because the choose block makes it very easy to implement “on” and “off” automatons in one. :slight_smile: