Moon platform with moon phases pictures

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

great, your template is working .

what’s probably happening, is that the entity_id of the new sensor is not containing ‘moon’, but the state, waning_gibbous.

try searching for that in the entity filter in dev states. Although, Ive just added the German sensor, and see

it all depends on whether you had this sensor before, on the unique_id you give it, and maybe if you had already edited the entity_id (to take out the ‘template’ bit) .
it must be there (if you at least did a Reload Templates), now just got to find it :wink:

because we set the unique_id, you can edit all of these in the UI clicking the Info icon

1 Like

Found by searching for moon in attributes


Was the name generated by line 23 in templates.yaml?

I changed the name of the sensor in the UI.
moon_11

Looks very good.
Thank you

1 Like

the entity_id was created upon first load of that entity. It is henceforth attached to the unique_id, so wont change anymore, no matter the name/state template changes you do after that.

As said, you can easily change it, as you have done.
great it’s working for you now.

1 Like

I hope my last question:
Can I delete the translation in brackets?

sure, just adjust the template, and take out ({{phase|replace('_',' ')|capitalize}})

1 Like