Day/Night theme switching automation

and it still triggers every minute :slight_smile: probably because its either always before sunrise or after sunset :slight_smile:

Go re-read my post. Your trigger is wrong. Change your trigger to what I suggested and it will not trigger every minute. It will only trigger at sunrise or sunset. Also there is no need for any conditions for this automation.

I changed it to the following code below this morning but tonight it was still on the day skin.

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

Have you double checked caching by going Incognito? I fought this for a while to only find out that the theme was being cached.

This is what I did:
Create a very simple automation that will trigger a theme change at a specific time.
Open my site incognito and watch it change.

Once I found that, I removed all cache specifically for my HASS site and everything works like a charm

ahh good tip thanks

yep clear the cache of the browser first. I didn’t think of it at first, but this might be better and a more robust animation:

  - 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
            {% endif %}
2 Likes

works perfect!

I’ve posted the wrong automation, The correct one is:

automation:
  - alias: Automatic Theme Change
    trigger:
      - platform: homeassistant
        event: start
    condition:
      condition: or
      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 %}

This will only trigger once and on HA start. There’s still no need for conditions in the automation as the condition can actually be a trigger. what I posted is what they are looking for. They want it to trigger on sunrise and sunset and load the theme accordingly.

Hi @squirtbrnr

using this and adding some feedback. Im struggling a bit though to have it display the set theme in the message with a template, cant find the frontend.theme states anywhere… Obviously i don’t want to hardcode the theme in the message but have it read the last set theme, form the automation. To give it time to do so i have added a 2 sec delay, after which it should be able to find the set theme through a template?

- alias: Automatic Theme Change
  id: '1511601478030'
  initial_state: 'on' 
  trigger:
    - platform: homeassistant
      event: start
    - 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 states.sun.sun.state == "above_horizon" %}
            default
          {% else %}
            darkred
          {% endif %}
    - delay: 00:00:02
    - service: notify.notify
      data_template:
        message: 'It is {{ as_timestamp(now()) | timestamp_custom("%X") }} and
                  Frontend is set to Darkred.'

hope to be able to do something like this:

data_template:
  message: 'It is {{ as_timestamp(now()) | timestamp_custom("%X") }}, 
               Sun is {{states.sun.sun.state}} and
                      Frontend is set to {{states.frontend.theme.friendly_name}}'

I don’t think frontend.theme is a component that you can get the state from. I think you’ll have to hardcode it. There are only two services exposed for frontend and those are .set_theme and .reload_themes.

I tried using the template validator on the frontend for things like {{ states.frontend.get_theme }} or {{ states.frontend.theme.friendly_name }} and they either returned an error, nothing, or “None”.

yes i noticed that, seems a bit odd.

I filed a request :wink:

enable Frontend in the component section to be able to use it in templating, example:

data_template:
  message: 'It is {{ as_timestamp(now()) | timestamp_custom("%X") }}, 
               Sun is {{states.sun.sun.state}} and
                      Frontend is set to {{states.frontend.theme.friendly_name}}'

HI,
since Backend-selected is out of reach in the set theme configuration page on my computer (and iPhone btw) ( it won’t scroll up past default::

i tried to use this input_select:

input_select:
  set_current_theme:
    name: 'Set Current Theme'
    options:
     - 'Backend-selected'
     - 'default'
     - 'teal'
     - 'darkorange'
     - 'darkred'
     - 'darkcyan'
     - 'minimal'
     - 'vintage'
    initial: 'Backend-selected'
    icon: 'mdi:palette'


automation:
  - id: change_current_theme
    alias: 'Change Current Theme'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: input_select.set_current_theme
    action:
      - service: frontend.set_theme
        data_template:
          name: '{{states.input_select.set_current_theme.state}}'

It does work…

Maybe the automation is incorrect?
What is the name of the backend-selected theme to call in this input_select?

please have a look?

Marius

Is there possibility to change theme remotely on e.g. particular unit. E.g. device_tracker.device_ten

not that I am aware off. but you can set a theme for each device.

as far as I know you cant change the data of say your phone or tablet from home assistant remotely. because the selected theme is saved on your phone/tablet and not in home assistant.

My automations…

# /config/automations/
# auto_theme.yaml
- alias: Automaticka zmena Temy
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: sun.sun
  action:
  - service_template: frontend.set_theme
    data_template:
      name: >
        {% if states.sun.sun.state == "above_horizon" %}
          {{ "default" }}
        {% else %}
          {{ "midnight" }}
        {% endif %}

hi all , after Home assistant update 0.113 , my theme automation based on the sun broke. can any help please.

alias: HA Theme based on day and night
trigger:

  • event: start
    platform: homeassistant
  • entity_id: sun.sun
    platform: state
    to: above_horizon
  • entity_id: sun.sun
    platform: state
    to: below_horizon
    action:
  • data:
    data_template:
    name: “{% if states.sun.sun.state == “above_horizon” %}\n Light - Light
    \ Red\n{% else %}\n Dark - Light Red\n{% endif %}\n”
    service: frontend.set_theme

It’s broken here also, did you find a way to fix it?

what is broken? post your automation in correctly formatted yaml, and we can have a look.
In any case these are the correct sun triggers

Sadly no , HA screwed up the automation, Temporary i just made two Automation one for Day and One night.