Change theme on switch.lights state change

Hi there. So i have the following automation that is meant to change the theme of lovelace when lights are switched on/off, problem is its not working for me properly… it will change the theme (of the cards only for some reason instead of the whole lovelace ui like it does if I manually select theme? Weird…) when lights are switched from on to off but does nothing when lights are switched from off to on. I have tried various other code examples from ha forums with no luck - as in I modify the examples to suit and check config which says everything is valid but the automation doesnt change the theme for some reason. This code is the only code so far that actually even does anything remotely close to what I am trying to achieve. but it still wont work how I want it to. any ideas?


- id: '1570461067566'

  alias: change theme at lights on

  trigger:

  - entity_id: switch.lights

    from: 'off'

    platform: state

    to: 'on'

  condition: []

  action:

  - data:

      entity_id: 'light'

    service: frontend.set_theme

- id: '1570461084660'

  alias: change theme at lights off

  trigger:

  - entity_id: switch.lights

    from: 'on'

    platform: state

    to: 'off'

  condition: []

  action:

  - data:

      entity_id: 'dark'

    service: frontend.set_theme

Have also tried this to no avail

- alias: Automatic Theme Change
  trigger: 
- platform: homeassistant 
  event: start 
- platform: state
  entity_id: switch.lights
  to: on 
- platform: state
  entity_id: switch.lights
  to: off
  action: 
- service_template: frontend.set_theme 
  data_template: 
  name: >  
       {% if states.switch.lights.state == "on" %} 
           light
       {% else %} 
           dark
       {% endif %}

Says valid. Doesn’t work

Have a look at these two examples in the documentation, which you should read and understand before copying any old code from the forums:

Nowhere does it mention an entity_id in the set theme service.

Also please read point 11 here then edit your posts accordingly:

Sorry about that. Didnt realize that they were back ticks so when I tried with 3 ’ it wasn’t working and I didnt know why lol

I will try using “name:” instead?

Yes. And watch your indentation:

- alias: Automatic Theme Change
  trigger: 
  - platform: homeassistant 
    event: start 
  - platform: state
    entity_id: switch.lights
  action: 
  - service_template: frontend.set_theme 
    data_template: 
      name: >  
        {% if is_state('switch.lights', 'on') %} 
          light
        {% else %} 
          dark
        {% endif %}

So its changing the theme of the cards now when I switch on/off lights. But still not changing the background like it does when I manually select the same theme

Ahhhhhh it was because I had the light theme set under my profile. Changed it to backend selected and all works how I would like. Awesome. Thanks for your help very much appreciated

1 Like