Automatic Day/Night Switching based on Sun

Moderators please give this a fitting spot close to the themes :slight_smile:

by: squirtbrnr (among others)

Automation:
  - alias: Automatic Theme Change
    trigger:
      - platform: homeassistant
        event: start
      - platform: state
        entity_id: sun.sun
        to: above_horizon
      - platform: state
        entity_id: sun.sun
        to: below_horizon
    action:
      - service_template: frontend.set_theme
        data_template:
          name: >
            {% if states.sun.sun.state == "above_horizon" %}
              default
            {% else %}
              darkred # <-- change to your dark theme name.
            {% endif %}
15 Likes

Hey OP,

I cant seem to get this working, I just get:

Invalid config for [automation]: required key not provided @ data['action']. Got None
required key not provided @ data['trigger']. Got None. (See /config/configuration.yaml, line 55). Please check the docs at https://home-assistant.io/components/automation/
Invalid config for [automation]: required key not provided @ data['action'][0]['delay']. Got None. (See /config/configuration.yaml, line 55). Please check the docs at https://home-assistant.io/components/automation/

It’s not this automation that is causing you a problem - the error message mentions a delay: paramater, which does not feature in the automation in the OP.

but if I remove this automation it works again…

Maybe when you’re pasting it in it is not aligned correctly with your other automations so homeassistant is reading another as part of this one.

Does this automation, as it appears in OPs post, work on the latest version of home assistant for you?

If I know it’s currently working for SOMEONEon the same as me it’s defs something I’m doing wrong :thinking:

Mine is slightly different, but code wise it is the exact same principle…

automation:
  - alias: Interface - Set UI theme
    initial_state: on
    trigger:
      - platform: homeassistant
        event: start
      - platform: state
        entity_id:
          - alarm_control_panel.alarm
          - input_boolean.maintenance_mode
      - platform: state
        entity_id: input_boolean.dark
      - platform: time
        at: '23:00:00'
      - platform: time
        at: '07:00:00'
    action:
      service: frontend.set_theme
      data_template:
        name: >
          {% if not is_state ('alarm_control_panel.alarm' , 'disarmed') %} darkred
          {% elif is_state ('input_boolean.maintenance_mode' , 'on') %} darkorange
          {% elif ((now().hour == 23) or (now().hour < 7)) %} darkcyan
          {% elif is_state('input_boolean.dark' , 'on') %} midnight
          {% else %} default {% endif %}
2 Likes

Works for me on 0.72, exact code as OP.

1 Like

I have been trying to firgure this out for the last 3 days why it wasn’t working. I am new to HASS and thought if i can’t figure this out then don’t go further. I copied mine from a youtube video and i have just realised that i needed to add a space infron of Name so the N lined up with the A in Data_template.

The N for name should be under the T of data in data_template. All spacing is in twos.

Hello, i make it alf working, all the “armed” stuf change the theme.
But the disarmed mod don’t change anything:
Can anyone help meplease ? i am completely stuck for 1week.

automation:
- alias: test3651685215
  description: ''
  trigger:
  - entity_id: alarm_control_panel.home_alarm
    platform: state
  condition: []
  action:
    service: frontend.set_theme
    data_template:
       name: >
        {% if is_state ('alarm_control_panel.alarm' , 'disarmed') %} clear-dark
        {% elif is_state ('alarm_control_panel.home_alarm' , 'armed_home') %} dark_orange
        {% elif is_state ('alarm_control_panel.home_alarm' , 'armed_away') %} material_dark_red
        {% endif %} 

I suspect the theme is called clear_dark not clear-dark