Moon Platform

Looking at the code, it appears to support 8 phase states:

 @property
    def state(self):
        """Return the state of the device."""
        if self._state == 0:
            return 'New moon'
        elif self._state < 7:
            return 'Waxing crescent'
        elif self._state == 7:
            return 'First quarter'
        elif self._state < 14:
            return 'Waxing gibbous'
        elif self._state == 14:
            return 'Full moon'
        elif self._state < 21:
            return 'Waning gibbous'
        elif self._state == 21:
            return 'Last quarter'
        else:
            return 'Waning crescent'

I just found a set of eight moon phase icons; would be cool to use the icon template property to set the icon on the card to match…

1 Like

^that would be better, I’m surprised it doesn’t already. I think Sun does. Maybe fire off the icons to whoever wrote the moon component?

I think that was @fabaff - but he’s a busy busy guy. I’m going to take a look at doing this from a template perspective but it’s going to take me a bit of time.

Look forward to seeing what you come up with. I guess if it works well then we can leave the component as is and just update the component configuration page with a suggestion.

#LabourSaving :smile:

The proper way to do it would be to extent the frontend.

I have added a feature request for this, if anyone is interested in posting their vote in favor of it: https://community.home-assistant.io/t/moon-sensor-more-information/

I finished my template sensor with moon phase pictures, based on the sensor.moon, this evening.

Have to test out two automations hiding and displaying the sensor when sunrise and sunsett.

Ill post the code after testing for a couple of days.

1 Like

Here is the sensor code for displaying lunar phases.
Create this folder structure and use your own icons or pictures. Attached are pictures for the northern hemisphere.

HA_Moon_filestruc

  • platform: moon

  • platform: template
    sensors:
    moonphases:
    entity_id: sensor.moon
    friendly_name: “Moon”
    value_template: “{{ states.sensor.moon.state }}”
    entity_picture_template: >-
    {% if is_state(‘sensor.moon’, ‘New moon’) %}
    /local/MoonPhases/NNewMoon.jpg
    {% elif is_state(‘sensor.moon’, ‘Waxing crescent’) %}
    /local/MoonPhases/NWaxingCrescent.jpg
    {% elif is_state(‘sensor.moon’, ‘First quarter’) %}
    /local/MoonPhases/NFirstQuarter.jpg
    {% elif is_state(‘sensor.moon’, ‘Waxing gibbous’) %}
    /local/MoonPhases/NWaxingGibbous.jpg
    {% elif is_state(‘sensor.moon’, ‘Full moon’) %}
    /local/MoonPhases/NFullMoon.jpg
    {% elif is_state(‘sensor.moon’, ‘Waning gibbous’) %}
    /local/MoonPhases/NWaningGibbous.jpg
    {% elif is_state(‘sensor.moon’, ‘Last quarter’) %}
    /local/MoonPhases/NLastQuarter.jpg
    {% elif is_state(‘sensor.moon’, ‘Waning crescent’) %}
    /local/MoonPhases/NWaningCrescent.jpg
    {% endif %}

If you want the moon sensor do disappear when the sun is up, and display when the sun is under the horizon, use this code, with the following addon.

in your customize.yaml add:

###################################################

MOON PHASES GUI DISPLAY/HIDE

###################################################

sensor.moonphases:
templates:
hidden: “return (entities[‘sun.sun’].state === ‘above_horizon’);”

And use the add on CustomUI

NFirstQuarterNFullMoonNLastQuarterNWaningGibbousNNewMoonNWaningCrescentNWaxingCrescentNWaxingGibbous

3 Likes

This is fantastic you reckon you can package this all into a zip file?

For limited time only.

Cheers.

10 Likes

Thank you very much. Your pic is so nice and clean ^^

Thanks, working great.

Some issues I had to fix:

  • indentation is wrong, moonphases is indented, and entity_id is indented under maanphases
  • all quotes should be replaced to ‘normal’ quotes, not fancy quotes
  • the entity_picture_template should have a ‘>’, not a ‘>-’ (not sure if this needs to be changed)
  • remember to copy the MoonPhases directory under your config/www directory

Im trying to set this up, and i see some errors, i tried what you’ve mentioned about indents and quotes, getting this error on running config check , im not able to find the error causing syntax issue.

Configuration invalid
Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected char ‘‘’ at 15) for dictionary value @ data[‘sensors’][‘moonphases’][‘entity_picture_template’]. Got '{% if is_state(‘sensor.moon’, ‘New moon’) %} /local/MoonPhases/NNewMoon.jpg {% elif is_state(‘sensor.moon’, ‘Waxing crescent’) %} /local/MoonPhases/NWaxingCrescent.jpg {% elif is_state(‘sensor.moon’, ‘First quarter’) %} /local/MoonPhases/NFirstQuarter.jpg {% elif is_state(‘sensor.moon’, ‘Waxing gibbous’) %} /local/MoonPhases/NWaxingGibbous.jpg {% elif is_state(‘sensor.moon’, ‘Full moon’) %} /local/MoonPhases/NFullMoon.jpg {% elif is_state(‘sensor.moon’, ‘Waning gibbous’) %} /local/MoonPhases… (See ?, line ?). Please check the docs at Template - Home Assistant

- platform: template
  sensors:
    moonphases:
      entity_id: sensor.moon
      friendly_name: 'Moon'
      value_template: '{ states.sensor.moon.state }'
      entity_picture_template: >
        {% if is_state(‘sensor.moon’, ‘New moon’) %}
          /local/MoonPhases/NNewMoon.jpg
        {% elif is_state(‘sensor.moon’, ‘Waxing crescent’) %}
          /local/MoonPhases/NWaxingCrescent.jpg
        {% elif is_state(‘sensor.moon’, ‘First quarter’) %}
          /local/MoonPhases/NFirstQuarter.jpg
        {% elif is_state(‘sensor.moon’, ‘Waxing gibbous’) %}
          /local/MoonPhases/NWaxingGibbous.jpg
        {% elif is_state(‘sensor.moon’, ‘Full moon’) %}
          /local/MoonPhases/NFullMoon.jpg
        {% elif is_state(‘sensor.moon’, ‘Waning gibbous’) %}
          /local/MoonPhases/NWaningGibbous.jpg
        {% elif is_state(‘sensor.moon’, ‘Last quarter’) %}
          /local/MoonPhases/NLastQuarter.jpg
        {% elif is_state(‘sensor.moon’, ‘Waning crescent’) %}
          /local/MoonPhases/NWaningCrescent.jpg
        {% endif %}

Figured it out, thanks to you all!

The single quotes around your if test statements are the wrong kind. Look closely at them and compare them to the single quotes around the value_template expression.

1 Like

Can you quote the correct template?

Ok thanks, but due to my poor english what are fancy sloped single quotes and vertical one?

Thanks a bunch Tom, that fixed it, i ought to pay more attention to details.

For the rest, i have corrected below.

  - platform: template
      sensors:
        moonphases:
          entity_id: sensor.moon
          friendly_name: 'Moon'
          value_template: '{{ states.sensor.moon.state }}'
          entity_picture_template: >
            {% if is_state('sensor.moon', 'New moon') %}
              /local/moonphases/NNewMoon.jpg
            {% elif is_state('sensor.moon', 'Waxing crescent') %}
              /local/moonphases/NWaxingCrescent.jpg
            {% elif is_state('sensor.moon', 'First quarter') %}
              /local/moonphases/NFirstQuarter.jpg
            {% elif is_state('sensor.moon', 'Waxing gibbous') %}
              /local/moonphases/NWaxingGibbous.jpg
            {% elif is_state('sensor.moon', 'Full moon') %}
              /local/moonphases/NFullMoon.jpg
            {% elif is_state('sensor.moon', 'Waning gibbous') %}
              /local/moonphases/NWaningGibbous.jpg
            {% elif is_state('sensor.moon', 'Last quarter') %}
              /local/moonphases/NLastQuarter.jpg
            {% elif is_state('sensor.moon', 'Waning crescent') %}
              /local/moonphases/NWaningCrescent.jpg
            {% endif %}

EDIT: i missed the double {{ }} on the value template, corrected now.

5 Likes

Thanks…