Moon platform with moon phases pictures

I couldn’t add an update to this older post : Moon Platform, so I created this new topic. Maybe not the way to go, but I saw no other option.

More than a year later, the component moon python code did change a little bit, so that the given solution did not work anymore out of the box.
(https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/sensor/moon.py#L68)

Working configuration at the moment of writing: add this to your configuration.yaml file under the sensor entry:
See the older post for more pre-information.

sensor:
  # the original moon component
  - platform: moon
  # the template for the moon phase pictures using the original moon component
  - platform: template
    sensors:
      moon_phases:
        entity_id: sensor.moon
        friendly_name: 'FillHereYourOwnSensorName'
        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 %}
5 Likes

Hi. I’m new to this forum, and to HA, so please be gentle with me if I’ve made some obvious error.
Anyway.
What I did to try and simplify the entity_picture_template a bit is to rename the jpg files to match the state of the sensor, and then insert the state directly into the file path
e.g.
indent preformatted text by 4 spaces

  moon_phases:
    entity_id: sensor.moon
    friendly_name: 'Moon'
    value_template: '{{ states.sensor.moon.state }}'
    entity_picture_template: /local/moon_phases/{{ states.sensor.moon.state }}.jpg

This seems to be working a treat for me.

4 Likes

Hi, you should propose it to the creator of the moon component.
It’s much less code to write in the configuration file. Good job.

I dealt with this in a slightly different way. I created a new sensor with an icon template, and a set of custom svg icons:

- platform: template
  sensors:
    moon_phase:
      friendly_name: Moon
      value_template: >
        {{ states.sensor.moon.state }}
      icon_template: >
        moon:{{ states.sensor.moon.state }}

configuration.yaml:

frontend:
  extra_html_url:
  - /local/cwm-moon-icons.html

/www/cwm-moon-icons.html:

<ha-iconset-svg name="moon" size="24">
<svg>
<defs>
  <g id="new_moon"><path d="m12,1.150499c-5.982,0 -10.85,4.866 -10.85,10.849c0,5.981 4.867,10.85 10.85,10.85c5.981,0 10.85,-4.868 10.85,-10.85c0,-5.983 -4.868,-10.849 -10.85,-10.849z"/></g>
  <g id="waxing_crescent"><path d="m12.0005,1.151c-5.981,0 -10.851,4.866 -10.851,10.847c0,5.983 4.869,10.851 10.851,10.851c5.98,0 10.85,-4.867 10.85,-10.851c0,-5.981 -4.869,-10.847 -10.85,-10.847zm0,19.452c-0.306,0 -0.605,-0.018 -0.903,-0.048c3.042,-1.646 5.113,-4.854 5.113,-8.558c0,-3.699 -2.071,-6.91 -5.111,-8.555c0.296,-0.032 0.596,-0.049 0.901,-0.049c4.745,0 8.604,3.861 8.604,8.604c0,4.749 -3.858,8.606 -8.604,8.606z"/></g>
  <g id="first_quarter"><path d="m12.000999,1.1505c-5.983,0 -10.85,4.868 -10.85,10.849c0,5.982 4.867,10.85 10.85,10.85c5.981,0 10.848,-4.867 10.848,-10.85c0,-5.981 -4.867,-10.849 -10.848,-10.849zm0,19.454l0,-17.21c4.745,0 8.604,3.86 8.604,8.604c0,4.748 -3.859,8.606 -8.604,8.606z"/></g>
  <g id="waxing_gibbous"><path d="m12.000499,1.151c-5.983,0 -10.85,4.865 -10.85,10.847c0,5.983 4.867,10.851 10.85,10.851c5.981,0 10.849,-4.867 10.849,-10.851c0,-5.982 -4.868,-10.847 -10.849,-10.847zm0.614,19.423c-3.054,-1.641 -5.132,-4.864 -5.132,-8.576c0,-3.707 2.078,-6.931 5.132,-8.571c4.459,0.316 7.991,4.036 7.991,8.571c0,4.538 -3.531,8.255 -7.991,8.576z"/></g>
  <g id="full_moon"><path d="m22.8485,11.999499c0,-5.982 -4.868,-10.849 -10.849,-10.849c-5.983,0 -10.848,4.866 -10.848,10.849c0,5.981 4.865,10.85 10.848,10.85c5.981,0 10.849,-4.868 10.849,-10.85zm-10.849,8.606c-4.744,0 -8.605,-3.859 -8.605,-8.605c0,-4.745 3.861,-8.604 8.605,-8.604c4.745,0 8.604,3.859 8.604,8.604c0.001,4.745 -3.859,8.605 -8.604,8.605z"/></g>
  <g id="waning_gibbous"><path d="m12.000499,1.1515c-5.981,0 -10.85,4.866 -10.85,10.848c0,5.981 4.868,10.849 10.85,10.849c5.981,0 10.849,-4.868 10.849,-10.849c0,-5.982 -4.868,-10.848 -10.849,-10.848zm-0.612,19.421c-4.46,-0.317 -7.993,-4.037 -7.993,-8.574c0,-4.538 3.532,-8.256 7.991,-8.574c3.054,1.641 5.132,4.863 5.132,8.574c0,3.71 -2.081,6.935 -5.13,8.574z"/></g>
  <g id="third_quarter"><path d="m12.001,1.151c-5.984,0 -10.85,4.867 -10.85,10.85c0,5.981 4.865,10.848 10.85,10.848c5.981,0 10.848,-4.867 10.848,-10.848c0,-5.983 -4.867,-10.85 -10.848,-10.85zm0,19.453c-4.746,0 -8.605,-3.858 -8.605,-8.604s3.859,-8.605 8.605,-8.605l0,17.209z"/></g>
  <g id="waning_crescent"><path d="m12.0005,22.848501c5.98,0 10.85,-4.867 10.85,-10.848c0,-5.984 -4.869,-10.849 -10.85,-10.849c-5.984,0 -10.851,4.865 -10.851,10.849c0.001,5.981 4.867,10.848 10.851,10.848zm0,-19.453c0.304,0 0.604,0.015 0.902,0.048c-3.039,1.644 -5.113,4.855 -5.113,8.557c0,3.7 2.069,6.912 5.111,8.556c-0.296,0.031 -0.596,0.048 -0.9,0.048c-4.746,0 -8.605,-3.857 -8.605,-8.604s3.859,-8.605 8.605,-8.605z"/></g>
</defs>
</svg>
</ha-iconset-svg>

This keeps the icons nicely consistent in appearance with others in the entity card:

moon-entity

6 Likes

Thanks a lot. I used your Moon photos to crop the black background and make the background transparent (thus become .png files) so that it will appear on HA more realistic
image


  - platform: template
    sensors:
      moon_phases:
        entity_id: sensor.moon
        friendly_name: 'Moon'
        value_template: '{{ states.sensor.moon.state }}'
        entity_picture_template: /local/moon_phases/{{ states.sensor.moon.state }}.png

That looks nice! Do you mind sharing the .png’s with transparent background that you’ve created ?

Here it is…https://drive.google.com/open?id=12th-vVWDsCRSSY1ZEDqMIqv_eBsU5V1X

8 Likes

Thank you very much bthoven! They look awesome - great work.

1 Like

Thank you for sharing your icons cumpstey !

I’ve used your code but the way I understand them they seem to be “the other way around” - full moon looks like new moon, Waning gibbous looks like waxing gibbous and so forth; I’ve changed the icons order in the format that makes more sense to me:

<ha-iconset-svg name="moon" size="24">
<svg>
<defs>
  <g id="new_moon"><path d="m22.8485,11.999499c0,-5.982 -4.868,-10.849 -10.849,-10.849c-5.983,0 -10.848,4.866 -10.848,10.849c0,5.981 4.865,10.85 10.848,10.85c5.981,0 10.849,-4.868 10.849,-10.85zm-10.849,8.606c-4.744,0 -8.605,-3.859 -8.605,-8.605c0,-4.745 3.861,-8.604 8.605,-8.604c4.745,0 8.604,3.859 8.604,8.604c0.001,4.745 -3.859,8.605 -8.604,8.605z"/></g>
  <g id="waxing_crescent"><path d="m12.0005,22.848501c5.98,0 10.85,-4.867 10.85,-10.848c0,-5.984 -4.869,-10.849 -10.85,-10.849c-5.984,0 -10.851,4.865 -10.851,10.849c0.001,5.981 4.867,10.848 10.851,10.848zm0,-19.453c0.304,0 0.604,0.015 0.902,0.048c-3.039,1.644 -5.113,4.855 -5.113,8.557c0,3.7 2.069,6.912 5.111,8.556c-0.296,0.031 -0.596,0.048 -0.9,0.048c-4.746,0 -8.605,-3.857 -8.605,-8.604s3.859,-8.605 8.605,-8.605z"/></g>
  <g id="first_quarter"><path d="m12.001,1.151c-5.984,0 -10.85,4.867 -10.85,10.85c0,5.981 4.865,10.848 10.85,10.848c5.981,0 10.848,-4.867 10.848,-10.848c0,-5.983 -4.867,-10.85 -10.848,-10.85zm0,19.453c-4.746,0 -8.605,-3.858 -8.605,-8.604s3.859,-8.605 8.605,-8.605l0,17.209z"/></g>
  <g id="waxing_gibbous"><path d="m12.000499,1.151c-5.983,0 -10.85,4.865 -10.85,10.847c0,5.983 4.867,10.851 10.85,10.851c5.981,0 10.849,-4.867 10.849,-10.851c0,-5.982 -4.868,-10.847 -10.849,-10.847zm0.614,19.423c-3.054,-1.641 -5.132,-4.864 -5.132,-8.576c0,-3.707 2.078,-6.931 5.132,-8.571c4.459,0.316 7.991,4.036 7.991,8.571c0,4.538 -3.531,8.255 -7.991,8.576z"/></g>
  <g id="full_moon"><path d="m12,1.150499c-5.982,0 -10.85,4.866 -10.85,10.849c0,5.981 4.867,10.85 10.85,10.85c5.981,0 10.85,-4.868 10.85,-10.85c0,-5.983 -4.868,-10.849 -10.85,-10.849z"/></g>
  <g id="waning_gibbous"><path d="m12.0005,1.151c-5.981,0 -10.851,4.866 -10.851,10.847c0,5.983 4.869,10.851 10.851,10.851c5.98,0 10.85,-4.867 10.85,-10.851c0,-5.981 -4.869,-10.847 -10.85,-10.847zm0,19.452c-0.306,0 -0.605,-0.018 -0.903,-0.048c3.042,-1.646 5.113,-4.854 5.113,-8.558c0,-3.699 -2.071,-6.91 -5.111,-8.555c0.296,-0.032 0.596,-0.049 0.901,-0.049c4.745,0 8.604,3.861 8.604,8.604c0,4.749 -3.858,8.606 -8.604,8.606z"/></g>
  <g id="third_quarter"><path d="m12.000999,1.1505c-5.983,0 -10.85,4.868 -10.85,10.849c0,5.982 4.867,10.85 10.85,10.85c5.981,0 10.848,-4.867 10.848,-10.85c0,-5.981 -4.867,-10.849 -10.848,-10.849zm0,19.454l0,-17.21c4.745,0 8.604,3.86 8.604,8.604c0,4.748 -3.859,8.606 -8.604,8.606z"/></g>
  <g id="waning_crescent"><path d="m12.000499,1.151c-5.983,0 -10.85,4.865 -10.85,10.847c0,5.983 4.867,10.851 10.85,10.851c5.981,0 10.849,-4.867 10.849,-10.851c0,-5.982 -4.868,-10.847 -10.849,-10.847zm0.614,19.423c-3.054,-1.641 -5.132,-4.864 -5.132,-8.576c0,-3.707 2.078,-6.931 5.132,-8.571c4.459,0.316 7.991,4.036 7.991,8.571c0,4.538 -3.531,8.255 -7.991,8.576z"/></g>
</defs>
</svg>
</ha-iconset-svg>

Great work though, thanks for sharing it.

edit: I realize now why I saw it inverted - I’m using the dark theme! :slight_smile:

Hi, I am new and making my way through stuff slowley. i am trying to do a web scraping feature but battling trying to find the actual tags , i dont understand HTML so that makes its worse.
Basically im trying to fetch the dates of the moon phases from this website https://www.wunderground.com/weather/za/durban

i am then using the following code

#New Moon
sensor 19:
- platform: scrape
resource: https://www.wunderground.com/forecast/za/durban
name: New Moon
select: ".phases-date"
scan_interval: 240
the .phases-date is the same for all for moon states when i do an inspect webpage … i am lost surely they woudl have 4 variables for this ? how do i scrape those four dates to display in HA? any help would be appreciated.

Dumb question but what are you putting into the badges under Lovelace.yaml to make this work?

I’ve tried

- entity: sensor.Moon
- entity: Moon
- entity: custom:Moon

I know it will be simple/obvious but can’t figure it out.

Thanks

eoren1 try: sensor.moon_phases

Yup - that ended up being the answer. Thanks and sorry for not circling back when I found the solution in another thread here.

Thanks for this!

@FragMenthor I got a notification that you posted up a question about how to use these icons now, but I can’t see the question in this thread. Not sure where it’s gone.

You now need this in your configuration…

frontend:
  extra_module_url:
  - /local/moon-icons.js

…and this saved as www/moon-icons.js

const ICONS = {
  new_moon: { path: 'm12,1.150499c-5.982,0 -10.85,4.866 -10.85,10.849c0,5.981 4.867,10.85 10.85,10.85c5.981,0 10.85,-4.868 10.85,-10.85c0,-5.983 -4.868,-10.849 -10.85,-10.849z' },
  waxing_crescent: { path: 'm12.0005,1.151c-5.981,0 -10.851,4.866 -10.851,10.847c0,5.983 4.869,10.851 10.851,10.851c5.98,0 10.85,-4.867 10.85,-10.851c0,-5.981 -4.869,-10.847 -10.85,-10.847zm0,19.452c-0.306,0 -0.605,-0.018 -0.903,-0.048c3.042,-1.646 5.113,-4.854 5.113,-8.558c0,-3.699 -2.071,-6.91 -5.111,-8.555c0.296,-0.032 0.596,-0.049 0.901,-0.049c4.745,0 8.604,3.861 8.604,8.604c0,4.749 -3.858,8.606 -8.604,8.606z' },
  first_quarter: { path: 'm12.000999,1.1505c-5.983,0 -10.85,4.868 -10.85,10.849c0,5.982 4.867,10.85 10.85,10.85c5.981,0 10.848,-4.867 10.848,-10.85c0,-5.981 -4.867,-10.849 -10.848,-10.849zm0,19.454l0,-17.21c4.745,0 8.604,3.86 8.604,8.604c0,4.748 -3.859,8.606 -8.604,8.606z' },
  waxing_gibbous: { path: 'm12.000499,1.151c-5.983,0 -10.85,4.865 -10.85,10.847c0,5.983 4.867,10.851 10.85,10.851c5.981,0 10.849,-4.867 10.849,-10.851c0,-5.982 -4.868,-10.847 -10.849,-10.847zm0.614,19.423c-3.054,-1.641 -5.132,-4.864 -5.132,-8.576c0,-3.707 2.078,-6.931 5.132,-8.571c4.459,0.316 7.991,4.036 7.991,8.571c0,4.538 -3.531,8.255 -7.991,8.576z' },
  full_moon: { path: 'm22.8485,11.999499c0,-5.982 -4.868,-10.849 -10.849,-10.849c-5.983,0 -10.848,4.866 -10.848,10.849c0,5.981 4.865,10.85 10.848,10.85c5.981,0 10.849,-4.868 10.849,-10.85zm-10.849,8.606c-4.744,0 -8.605,-3.859 -8.605,-8.605c0,-4.745 3.861,-8.604 8.605,-8.604c4.745,0 8.604,3.859 8.604,8.604c0.001,4.745 -3.859,8.605 -8.604,8.605z' },
  waning_gibbous: { path: 'm12.000499,1.1515c-5.981,0 -10.85,4.866 -10.85,10.848c0,5.981 4.868,10.849 10.85,10.849c5.981,0 10.849,-4.868 10.849,-10.849c0,-5.982 -4.868,-10.848 -10.849,-10.848zm-0.612,19.421c-4.46,-0.317 -7.993,-4.037 -7.993,-8.574c0,-4.538 3.532,-8.256 7.991,-8.574c3.054,1.641 5.132,4.863 5.132,8.574c0,3.71 -2.081,6.935 -5.13,8.574z' },
  third_quarter: { path: 'm12.001,1.151c-5.984,0 -10.85,4.867 -10.85,10.85c0,5.981 4.865,10.848 10.85,10.848c5.981,0 10.848,-4.867 10.848,-10.848c0,-5.983 -4.867,-10.85 -10.848,-10.85zm0,19.453c-4.746,0 -8.605,-3.858 -8.605,-8.604s3.859,-8.605 8.605,-8.605l0,17.209z' },
  waning_crescent: { path: 'm12.0005,22.848501c5.98,0 10.85,-4.867 10.85,-10.848c0,-5.984 -4.869,-10.849 -10.85,-10.849c-5.984,0 -10.851,4.865 -10.851,10.849c0.001,5.981 4.867,10.848 10.851,10.848zm0,-19.453c0.304,0 0.604,0.015 0.902,0.048c-3.039,1.644 -5.113,4.855 -5.113,8.557c0,3.7 2.069,6.912 5.111,8.556c-0.296,0.031 -0.596,0.048 -0.9,0.048c-4.746,0 -8.605,-3.857 -8.605,-8.604s3.859,-8.605 8.605,-8.605z' },
}

async function getIcon(name) {
  return ICONS[name];
}

window.customIconsets = window.customIconsets || {};
window.customIconsets["moon"] = getIcon;

They’re then available with the moon: prefix. I have this template sensor to use them:

- platform: template
  sensors:
    moon_phase:
      friendly_name: Moon
      value_template: >
        {{ states.sensor.moon.state[0] | upper }}{{ states.sensor.moon.state[1:] | replace('_', ' ') }}
      icon_template: >
        moon:{{ states.sensor.moon.state }}
2 Likes

Thanks for the reply! I removed the post since I found adequate mdi icons already available, but it’s always good to know how to do things. Thanks again, not only for me, but because the answer stays here as a reference! :slight_smile:

Hello, is it still working? Because I add moon sensor to my configuration.yaml and there’s no entity?
Regards.

The integration changed with the Update to Core 2021.10.6, there are some miror changes/improvements.
All is described in the release notes.

1 Like

I could also show the moon phase in a lovelace with the Markdown element with this code:
<img src='{{ state_attr('sensor.moon_phases', 'entity_picture') }}' height='70px'/>
moon_phase

I see, when I place the sensor.moon in an entity card I get the translated value of the moon phase. Can I get this also in Markdown? It’s not a value of one of the sensor.moon properties.

1 Like

I wish I could get this going. I am not sure what is missing. When selecting the entitiy, the custom picture shows. The final result shows and “eye” icon.

My config yaml looks like this:

sensor:

  • platform: moon
  • platform: template
    sensors:
    moon_phases:
    entity_id: sensor.moon
    friendly_name: ‘Moon Phases’
    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 %}

I have copied the pictures with the same naming to the correct folder.(www/MoonPhases).
Anything I missed