Moon platform with moon phases pictures

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

thats a bit odd really… you template uses /local/moonphases/ so your folder should use that too. If you add the dash there, the path is no longer the same.

@CrYoGeNiC
here’s what I use, in the ‘new’ template format:

template:
  - sensor:
      - unique_id: moon_phases
        name: >
          {% set phase = states('sensor.moon') %}
          {% set mapper = {'new_moon':'Nieuwe Maan',
                           'waxing_crescent':'Jonge Maansikkel',
                           'first_quarter':'Eerste Kwartier',
                           'waxing_gibbous':'Wassende Maan',
                           'full_moon':'Volle Maan',
                           'waning_gibbous':'Afnemende Maan',
                           'last_quarter':'Laatste Kwartier'} %}
          {{mapper.get(phase,'Asgrouwe Maan')}}
          ({{phase|replace('_',' ')|capitalize}})
        state: >
          {{states('sensor.moon').capitalize().replace('_',' ')}}
        picture: >
          {% set state = states('sensor.moon') %}
          /local/weather/moonphases/{{state}}.jpg

the ‘get’ function takes care of the ‘else’ situation, where the state isnt available in the mapper. You could do what @peerkn does and add the extra ‘unavailable’ but Ive never seen it error.

no matter how, you should always have an else in all situations because otherwise the template could throw an error.
Alsoe, you need too use then {{states('sensor.moon')}} format, and not use {{states.sensor.moon.state}} as that can cause issues at startup/reload

if you format your post correctly, we can more easily see if there’s an issue in the yaml.

on your next post, please add the screenshot of the actual state in dev states, and the template in dev templates

@Mariusthvdbarius

Thank you for your hint!!!

I made a mistake. My foldername is moonphases.

Best regards.

@Mariusthvdb
Where should I put this code?

   - unique_id: moon_phases
        name: >
          {% set phase = states('sensor.moon') %}
          {% set mapper = {'new_moon':'Nieuwe Maan',
                           'waxing_crescent':'Jonge Maansikkel',
                           'first_quarter':'Eerste Kwartier',
                           'waxing_gibbous':'Wassende Maan',
                           'full_moon':'Volle Maan',
                           'waning_gibbous':'Afnemende Maan',
                           'last_quarter':'Laatste Kwartier'} %}
          {{mapper.get(phase,'Asgrouwe Maan')}}
          ({{phase|replace('_',' ')|capitalize}})
        state: >
          {{states('sensor.moon').capitalize().replace('_',' ')}}
        picture: >
          {% set state = states('sensor.moon') %}
          /local/weather/moonphases/{{state}}.jpg

Put the as a sensor (template) in the configuration yaml

@Mariusthvdb

I use a templates.yaml and copied your code into it.
Unfortunately I don’t have a Moon_Phases sensor.
The images (.png) are stored at www/moonphases.
Here is my templates.yaml:

#Sunrise and Sunset
    - sensor:
        - name: next sunrise
          state: >
            {{ as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom('%H:%M') | replace(" 0", "") }}
          icon: mdi:weather-sunset-up
        - name: next sunset
          state: >
            {{ as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom('%H:%M') | replace(" 0", "") }}
          icon: mdi:weather-sunset-down

#Moon Phases
        - unique_id: moon_phases
          name: >
            {% set phase = states('sensor.moon') %}
            {% set mapper = {'new_moon':'Neumond',
                             'waxing_crescent':'zunehmender Mond',
                             'first_quarter':'Erstes Viertel',
                             'waxing_gibbous':'Halbmond',
                             'full_moon':'Vollmond',
                             'waning_gibbous':'Abnehmender Mond',
                             'last_quarter':'Letztes Viertel'} %}
            {{mapper.get(phase,'Aschgrauer Mond')}}
            ({{phase|replace('_',' ')|capitalize}})
          state: >
            {{states('sensor.moon').capitalize().replace('_',' ')}}
          picture: >
            {% set state = states('sensor.moon') %}
            /local/moonphases/{{state}}.png

What am I doing wrong?

Well, without you telling me, I wouldn’t know :wink:
What is the actual problem? the state of the template sensor, or the images?

btw, please use the state_attr() format:

{{as_timestamp(state_attr('sun.sun','next_setting'))| timestamp_custom('%H:%M') }}
1 Like

Shouldn’t a senor moon_phases be available?
There is nothing.

there’s a lot more going on there, as you also have a very odd device_class…

are you sure this is the only yaml in your config related to the moon_phase? and no picture is set…

what happens when you enter the template in dev tools:

            {% set phase = states('sensor.moon') %}
            {% set mapper = {'new_moon':'Neumond',
                             'waxing_crescent':'zunehmender Mond',
                             'first_quarter':'Erstes Viertel',
                             'waxing_gibbous':'Halbmond',
                             'full_moon':'Vollmond',
                             'waning_gibbous':'Abnehmender Mond',
                             'last_quarter':'Letztes Viertel'} %}
            {{mapper.get(phase,'Aschgrauer Mond')}}
            ({{phase|replace('_',' ')|capitalize}})

because thats what the name should be

1 Like

The moon integration is installed, otherwise no entry in a yaml refers to the moon phase.
The template result:

seems there’s no sensor.moon? it defaults to the default, in stead of mapping the state.

what does {{states('sensor.moon')}} return in dev templates

you sure you have this in your integrations:

Scherm­afbeelding 2022-11-11 om 11.30.42

your template should return:

1 Like

The integration is installed.
moon_4

{{states('sensor.moon')}} returns:

I am very confused.

clicking that entity in the integration panel, what entity is shown?

also, please show the sensor.moon in dev states

and lo and behold, I now see that same device_class: moon__phase, whihc cant be right.

lets find out about that
seems intended, though I figure its a typo: core/sensor.py at 6517e3e21e5828a317ea8b814d0345f2536b9170 · home-assistant/core · GitHub

1 Like

How can I show the sensor.moon in dev states?

ok, thats what I figured… you dont have a sensor.moon, but sensor.moon_phase. this is completely new to me (my entity is from before the move to the ui integration, so that probably causes that entity_id)

change the template to use that entity_id, and you’re set.

{% set phase = states('sensor.moon_phase') %}

its what you did here: Moon platform with moon phases pictures - #29 by noob6741

and I hadn’t suspected that to be the new entity_id of the core integration…
on the device_class: moon__phases, Ive just learned:

It’s a custom device class that sensors can define to have translation for the states, and it’s intended

see also Backend Localization | Home Assistant Developer Docs

also:

New entities should get an entity_id like sensor.moon_phase from what I can read in the code. We’ve set the entity to follow our new entity naming guidelines.

so thats all by design.

1 Like

First of all thank you very much for your help.
I modified the templates.yaml as follows:

#Sunrise and Sunset
    - sensor:
        - name: next sunrise
          state: >
            {{as_timestamp(state_attr('sun.sun','next_rising'))| timestamp_custom('%H:%M') }}
          icon: mdi:weather-sunset-up
        - name: next sunset
          state: >
            {{as_timestamp(state_attr('sun.sun','next_setting'))| timestamp_custom('%H:%M') }}
          icon: mdi:weather-sunset-down

#Moon Phases
        - unique_id: moon_phases
          name: >
            {% set phase = states('sensor.moon_phase') %}
            {% set mapper = {'new_moon':'Neumond',
                             'waxing_crescent':'zunehmender Mond',
                             'first_quarter':'Erstes Viertel',
                             'waxing_gibbous':'Halbmond',
                             'full_moon':'Vollmond',
                             'waning_gibbous':'Abnehmender Mond',
                             'last_quarter':'Letztes Viertel'} %}
            {{mapper.get(phase,'Aschgrauer Mond')}}
            ({{phase|replace('_',' ')|capitalize}})
          state: >
            {{states('sensor.moon_phase').capitalize().replace('_',' ')}}
          picture: >
            {% set state = states('sensor.moon_phase') %}
            /local/moonphases/{{state}}.png

template result:

Unfortunately still no moon_phases sensor