Day/Night theme switching automation

Hi, I configured the themes on my home assistant and thought of an automation to change the frontend theme depending on day and night (using the sun sensor and “sunrise” and “sunset” events).

Now I have two themes, default one and a “dark” one.

I also wrote an automation (that actually doesn’t work), this one:

- alias: "Default Theme after Sunrise"
  trigger:
    platform: sun
    event: sunrise
    offset: "+00:00:00"
  action:
    - service: frontend.set_theme
      data:
        name: default
- alias: "dark Theme after Sunset"
  trigger:
    platform: sun
    event: sunset
    offset: "+00:00:00"
  action:
    - service: frontend.set_theme
      data:
        name: dark

What could be the problem? Look forward to your answers

1 Like

Read this topic. It contains a possible solution

It works great :slightly_smiling_face:
Now this is my configuration of the automation:

Configuration.yaml

frontend:
  themes:
    dark:
      primary-color: "#396FB6"
      [...]

input_select:
      hass_template:
        name: Choose template
        options:
         - default
         - dark
        initial: default
        icon: mdi:theme-light-dark

Automation.yaml:

# IF: { input_select hass_template is changed by user OR home assistants boots up } THEN: { set theme to state given by input_select.hass_template }
- alias: switch_hass_template
  id: switch_hass_template
  trigger:
    - platform: state
      entity_id: input_select.hass_template
    - platform: homeassistant
      event: start
  action:
    - service: frontend.set_theme
      data_template:
        name: "{{ states.input_select.hass_template.state }}"

  # IF: { sun.sun goes under horizon } THEN: { set theme to night mode }
- alias: day_to_night_theme
  id: day_to_night_theme
  trigger:
    - platform: state
      entity_id: sun.sun
      from: 'above_horizon'
      to: 'below_horizon'
  action:
    - service: input_select.select_option
      data:
        entity_id: input_select.hass_template
        option: 'dark'

  # IF: { sun.sun is up } THEN: { set theme to day mode }
- alias: night_to_day_theme
  id: night_to_day_theme
  trigger:
    - platform: state
      entity_id: sun.sun
      from: 'below_horizon'
      to: 'above_horizon'
  action:
    - service: input_select.select_option
      data:
        entity_id: input_select.hass_template
        option: 'default'

Thank you @j.assuncao for helping me out :slight_smile:

7 Likes

Your welcome @Mike1!

so I am pretty new to this and I thought one of the easier things to try is to get a day/night theme implemented.

I tried some code from Skalavala and that wouldn’t work so I changed some stuff and now it gives me 0 errors.

automation:
  - alias: Automatic Theme Change
    trigger:
      - platform: homeassistant
        event: start
      - platform: state
        entity_id: sun.sun
    action:
      - service_template: frontend.set_theme
        data_template:
          name: >
            {% if states.sun.sun.state == "above_horizon" %}
              {{ "default" }}
            {% else %}
              {{ "darkred" }}
            {% endif %}

Would this be considered good code? or is there a better way of doing it?

Try changing into this:

automation:
  - alias: Automatic Theme Change
    trigger:
      - platform: homeassistant
        event: start
      - platform: state
        entity_id: sun.sun
    action:
      - service_template: frontend.set_theme
        data_template:
          name: >
            {% if states.sun.sun.state == "above_horizon" %}
              default
            {% else %}
              darkred
            {% endif %}
3 Likes

if I wanted to add a custom day/night icon, where would I put that?

Did the code work? Where do you want the icon to appear?

yea worked great!

this icon:
Capture

and how would I hide this from the home screen?

in addition I see in the log that every minute this script is run! is that normal?

Why change the icon if you want to hide it?

I don’t think that that is the normal behavior… the trigger should fire only on HA start. Can you please post part of the log where you see the script is beeping triggered?

I want to know where I could insert the icon: command for learning purposes :slight_smile:
also where to insert: hide: yes so it becomes invisible.

fyi I know you meant the actual .log file but I will sort that out tomorrow since its full of other errors and its already past 2 am :slight_smile:

Post the error log when you have time. It’s late here too…

You can change the icon via customize.yaml file. As for hidding it I’m not sure how to do it.

EDIT: The code to change the icon is:

customize:    
  sensor.energy_volt:
    icon: mdi:speedometer
    friendly_name: 'Volt'

I don’t think you have an option to hide the icon. What you can do is a trick like changing the color of the icon to the same color as the background or give the icon a name that doesn’t exist (like icon: mdi:noiconhere). Never used that but i believe it works.

log is empty its just like it is above. the script triggers every minute to see if the sun is still up/down. I think that’s just tweaking the triggers.

The automation is firing every minute because of this trigger:

  - platform: state
    entity_id: sun.sun

Because you do not specify what attribute or state to trigger on, the automation will trigger when any attribute of the sun component changes, which is every minute. Try this instead for the sun triggers

  - platform: sun
    event: sunset
  - platform: sun
    event: sunrise

Now the automation will only fire when HA starts, the sun sets, or the sun rises. the rest of the automation action will work accordingly.

@squirtbrnr is right. The problem is with the sun trigger. If his suggestion don’t work, try using sun state as a condition.

condition:
  condition: and
  conditions:
    - condition: sun
      after: sunset
    - condition: sun
      before: sunrise

nope didn’t work. any more ideas guys?

Are you sure you have tried this?

automation:
  - alias: Automatic Theme Change
    trigger:
      - platform: homeassistant
        event: start
      - platform: state
        entity_id: sun.sun
    condition:
      condition: and
      conditions:
       - condition: sun
         after: sunset
       - condition: sun
         before: sunrise
    action:
      - service_template: frontend.set_theme
        data_template:
          name: >
            {% if states.sun.sun.state == "above_horizon" %}
              default
            {% else %}
              darkred
            {% endif %}

yup but from that AND statement it means that it needs to be sunset and sunrise correct? maybe if I put OR?

You’re right! it’s an OR condition… Sorry…