Now it is time for some simple automation. Lets say something like changing the theme based on whether the sun is above or below the horizon.
Home Assistant makes use of Devices and Entities. For example, a device might be a Multi Sensor, so it will have multiple entities, one for each sensor. Most motion sensors include a temperature and luminescence or humidity etc.
This automation will trigger whenever Home Assistant starts (or restarts) and on the state of the Sun (a built-in sensor).
- alias: change theme 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_template: name: > {% if states.sun.sun.state == "above_horizon" %} oxfordblue {% else %} darkorange {% endif %} service_template: frontend.set_theme
Let’s step through the elements of the automation script:
- alias – provide the automation a friendly name
- trigger – set what causes the automation to activate, in this case there are three:
- when your Home Assistant starts or restarts
- when the Sun is above the horizon
- when the Sun is below the horizon
- action – what to do when the automation is triggered, in this case an if statement checking the state of the sun to select the appropriate theme name
Unfortunately, this automation uses an action statement that is currently not understood by the Configuration Automation editor. Therefore, you need to use the Configurator to edit the ‘automation.yaml’ file directly.
To enable the Moon integration (Sun comes as default), you can simply add the following YAML to your ‘configuration.yaml’ file via the Configurator:
sensor: - platform: moon
This sensor will return one of the following values:
- new_moon
- waxing_crescent
- first_quarter
- waxing_gibbous
- full_moon
- waning_gibbous
- last_quarter
- waning_crescent
This means you could edit the original script to change the theme based on the current phase of the moon or of course any automation.
Complete: Stride 1: Getting Running