Did you know you can make an automation to change the theme of Home Assistant? This works great if you want to setup a day/night theme. Lets see how it's done!

Requirements

  • Home Assistant
  • Node-RED
  • node-red-contrib-home-assistant-websocket installed in Node-RED and configured to work with your Home Assistant install.

Setup

Downloading or Creating themes

You can get themes either off the Home Assistant Community or from the Home Assistant Community Themes repository on GitHub. I ended up getting the Midnight theme from the community repository for my night theme and I use the default theme for the day.

Node-RED Flow Setup

Here is the flow that we will be using in Node-RED:

[{"id":"4c88da9a.98ff74","type":"api-call-service","z":"ff717302.0c688","name":"","server":"233a9c63.e2baf4","service_domain":"frontend","service":"set_theme","data":"{ \"name\": \"midnight\" }","mergecontext":"","x":890,"y":2220,"wires":[[]]},{"id":"45dc11c1.ec6ae","type":"api-current-state","z":"ff717302.0c688","name":"","server":"233a9c63.e2baf4","outputs":1,"halt_if":"","override_topic":true,"entity_id":"sun.sun","override_payload":true,"x":520,"y":2220,"wires":[["c5ae391d.9c4ed8"]]},{"id":"c5ae391d.9c4ed8","type":"switch","z":"ff717302.0c688","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"below_horizon","vt":"str"},{"t":"eq","v":"above_horizon","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":702,"y":2250,"wires":[["4c88da9a.98ff74"],["ea411b21.a53488"]]},{"id":"ea411b21.a53488","type":"api-call-service","z":"ff717302.0c688","name":"","server":"233a9c63.e2baf4","service_domain":"frontend","service":"set_theme","data":"{ \"name\": \"default\" }","mergecontext":"","x":890,"y":2280,"wires":[[]]},{"id":"2bf1895b.a05896","type":"comment","z":"ff717302.0c688","name":"Home Assistant Day/Night Theme Changer","info":"","x":660,"y":2180,"wires":[]},{"id":"6fb09c71.87cc54","type":"server-state-changed","z":"ff717302.0c688","name":"","server":"233a9c63.e2baf4","version":1,"entityidfilter":"sun.sun","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":520,"y":2280,"wires":[["c5ae391d.9c4ed8"]]},{"id":"d228ffc7.e80c7","type":"inject","z":"ff717302.0c688","name":"","topic":"","payload":"","payloadType":"date","repeat":"900","crontab":"","once":true,"onceDelay":0.1,"x":330,"y":2220,"wires":[["45dc11c1.ec6ae"]]},{"id":"233a9c63.e2baf4","type":"server","z":"","name":"Home Assistant","legacy":true,"hassio":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

With this flow we have two inputs. One of them is a timer that fires every 15 minutes (feel free to change this) just to make sure Home Assistant stays synchronized. The last one is only fired whenever the sun.sun component changes state. These inputs then feed into the switch which then decides on what theme to use.

Home Assistant has no way to get the current backend selected theme. So we cannot check what theme is selected before setting it. This is why we continuously fire every 15 minutes to keep things synced.

My theme isn't changing

If your theme isn't changing it is probably because your theme isn't set to Backend-selected. Select this under your profile in HA.

Users can still override the backend-selected theme

This only changes the backend-selected theme for Home Assistant. If the user select any other theme from their profile it will override this setting. This way your users aren't forced into this.

Conclusion

I had this exact same thing setup in YAML before moving to Node-RED and I am happy it moved over and works perfectly. I've been considering using my light sensors instead of the sun component. This way it would detect overcast conditions and use the dark theme instead.

I notice that sometimes the theme doesn't update right away on clients. This is only an issue if the client has HA open before the theme change is issued and will usually fix itself within a couple minutes (HA should eventually refresh).

Anyways, I really like this automation and I hope you do as well. If you end up making any changes to this or feel you can improve it in any way feel free to drop a comment below. I love hearing back from my readers.