Home AssistantSettings

Tracking Sun and Moon in Home Assistant

You can track the Sun in the standard Home Assistant integration. The integration creates an object with two possible states: above_horizon, bellow_horizon, and several other attributes such as elevation, azimuth, next_dawn, next_dusk. You can use these states as a trigger or condition for automations in your smart home. An example of an automation idea is “turn on the light at the front door 30 minutes after sunset”.

You can also track the Moon in Home Assistant. This integration creates an entity that provides you with the current moon phase, such as new_moon, full_moon, waxing_crescent, waning_crescent, etc. But whether this information is useful for the average user is a good question. Probably not, but some people may find it as such. For someone who is into home automation, the best thing they can do is to create a dashboard and maybe set up a few automations

Setting up the Sun Integration.

  • Go to Home Assistant.
  • Click Settings on the side panel.
  • Choose “Devices and Settings” in the configuration.
  • In the lower right corner, click “Add Integration”.
  • Find “Sun” in the list.
  • Follow the instructions on the screen to finish.

Or, to finish the setup, click the button below.

Open your Home Assistant instance and start setting up a new integration.

Setting up the Moon integration.

  • Go to Home Assistant.
  • Click Settings on the side panel.
  • Choose “Devices and Settings” in the configuration.
  • In the lower right corner, click “Add Integration”.
  • Find “Moon” in the list.
  • Follow the instructions on the screen to finish.

Or, to finish setup, click the button below.

Open your Home Assistant instance and start setting up a new integration.

Usage of the Sun integration.

Sun integration can be very useful. You can use it in many automations as a trigger. For example, we can use it as a condition to turn on the light on the terrace, but only after sunset. We would not want the light to turn on when we go outside during the day. The trigger can be a motion detector signal.

alias: Terrace Lights ON
description: 'Turn ON Terrace Lights After Sunset'
trigger:
  - platform: state
    entity_id: binary_sensor.terrace
    from: 'off'
    to: 'on'
condition:
  - condition: sun
    after: sunset
    after_offset: '20'
action:
  - service: light.turn_on
    target:
      entity_id: light.terrace_lights

We can also use it directly as a trigger to turn on the backlights and turn them off at sunrise:

alias: Walkway Lights
description: 'Turn on Porch Lights After Sunset'
trigger:
  - platform: sun
    event: sunset
action:
  - service: light.turn_on
    target:
      entity_id: light.porch_lights
alias: Walkway Lights
description: 'Turn Off Porch Lights At Sunrise'
trigger:
  - platform: sun
    event: sunrise
action:
  - service: light.turn_off
    target:
      entity_id: light.porch_lights

Sun display on the information panel.

There is a wonderful Sun Card. We can use it to depict the Sun’s position in the sky. The card can also show some additional information. You can find this card in HACS, and it is most convenient to install it from there.

custom_sun-card

The card’s code looks like this:

type: custom:sun-card
darkMode: true
showAzimuth: true
showElevation: true

Using the Moon integration.

Similarly, the moon phases are also usable as trigger automations. Although the average user may not find the integration useful, some people will come up with a good use case for it. As an example, we will create an automation that will notify astronomers when there is a full moon so that they can go outside and enjoy this spectacle:

alias: Full Moon
description: 'Notify Me When It`s a Full Moon'
trigger:
  - platform: state
    entity_id:
      - sensor.moon
    to: full_moon
action:
  - service: notify.mobile_app
    data:
      title: Full Moon!
      message: It's a Full Moon Outside!

This automation will notify you with a notification on your phone

Moon display on the information panel.

Do you use Mushroom Cards? If so, the Sun and Moon information can look good on its Mushrooms-Chips Card. For example:

chips-card-weather

To create a more visually appealing card we will use custom:stack-in-card. We will also download .png images that HA user “bthoven” provided in the topic..

First, download the .png images here and upload them to your Home Assistant server. I have placed them in the folder www/images/MoonPhases, which corresponds to /local/images/MoonPhases in your browser. You can use Samba Share and Studio Code Server to upload them to the folder. Then we create a custom entity image card that will show the corresponding moon image during each phase.

But first, let’s create a sensor that, as a parameter, will transmit the image of the moon phase depending on the state of the main sensor (sensor.moon_phase).

- platform: template
  sensors:
    moon_phase_image:
      #entity_id: sensor.moon
      friendly_name: "Moon Phase image"
      value_template: "{{ states.sensor.moon_phase.state }}"
      icon_template: >
        moon:{{ states.sensor.moon.state }}
      entity_picture_template: >-
        {% if is_state('sensor.moon_phase', 'new_moon') -%}
        /local/images/MoonPhases/new_moon.png
        {% elif is_state('sensor.moon_phase', 'waxing_crescent') -%}
        /local/images/MoonPhases/waxing_crescent.png
        {% elif is_state('sensor.moon_phase', 'first_quarter') -%}
        /local/images/MoonPhases/first_quarter.png
        {% elif is_state('sensor.moon_phase', 'waxing_gibbous') -%}
        /local/images/MoonPhases/waxing_gibbous.png
        {% elif is_state('sensor.moon_phase', 'full_moon') -%}
        /local/images/MoonPhases/full_moon.png
        {% elif is_state('sensor.moon_phase', 'waning_gibbous') -%}
        /local/images/MoonPhases/waning_gibbous.png
        {% elif is_state('sensor.moon_phase', 'last_quarter') -%}
        /local/images/MoonPhases/last_quarter.png
        {% elif is_state('sensor.moon_phase', 'waning_crescent') -%}
        /local/images/MoonPhases/waning_crescent.png
        {%- endif %}

For more appeal, upload the image moon_bg.jpg to the same folder. This will be the background of the card with moon phases. I have an image of a tree against the evening sky, but you can choose an image of your choice.

sensor.moon_phase_image

Now create a card in your dashboard. You can see the sample code in my GitHub here.

As you can see, the card shows the parameter {{ state_attr(entity, ‘entity_picture’) }}, which passes the sensor as the image address. Below, there are two Mushrooms – Chips elements on the card – the time to the nearest moonrise and moonset. I take these parameters from the AstroWeather integration, which can also be found in HACS.

Leave a Reply