Moon phases

I have read a lot of moon posts in this forum but am confused.
According to the documentation … https://www.home-assistant.io/integrations/moon/

sensor:

  • platform: moon

should be in configuration.yaml … which I did.
But I don’t see any of the values that the sensors was supposed to return.
I just want to create an entity card that shows the moon phase and an image of it.
Appreciate help since I am not so good in yalm coding … an example of moon phase entity card would help.

in developers tools - states
search for entity - sensor.moon
If you find it you will see its state

in entity card you just fill the entity sensor.moon

See here: Moon platform with moon phases pictures

The images you need are two posts below that one.

1 Like

There is no entity - sensor.moon in developers tools - states …

Did you restart Home Assistant after adding the Moon integration? You need to restart before Home Assistant detects the addition of the Moon integration.

I restarted HA … no sensor.moon
I rebooted HA … no sensor.moon

So where is the moon?

I’m not sure but clearly you are doing something wrong because it works perfectly in version 0.117.0.

In Developer Tools > States:
Screenshot from 2020-11-01 13-20-29

In Configuration > Entities:

In your configuration.yaml file, do you have more than one section named sensor: because if you do that could be the cause of the problem.

For example, if I have this in the configuration.yaml file:

sensor:
  - platform: moon

and then farther down in the same file I have another sensor: section like this:

sensor:
 - platform: mqtt
    name: "Outdoor Temperature"
    state_topic: "outdoor/sensor1/temperature"
    unit_of_measurement: '°C'

  - platform: season
    type: astronomical

then Home Assistant will only register the last two sensors, not the first one.

1 Like

OK - I moved

sensor:
  - platform: moon

to another place in the ‘configuration.yalm’ and now the moon showed up … but I don’t see the moon images.

What I have is:

  1. In ‘/config/www/images/moonphases/’ there are the 8 files ‘NWaxingGibbous.jpg … NFirstQuarter.jpg’
  2. In ‘configuration.yalm’ I have following
    sensors:
      moon_phases:
        entity_id: sensor.moon
        friendly_name: 'Månfaser'
        value_template: '{{ states.sensor.moon.state }}'
        entity_picture_template: /config/www/images/moonphases/{{ states.sensor.moon.state }}.jpg
  1. Also have created an entity card as follows:
type: entities
entities:
  - entity: sensor.moon_phases
title: Måne
theme: Dark-Gray

but don’t see any images at all in the entity card.
Got the feeling I am close to the solution but what have I missed?

OK - I moved

sensor:
  - platform: moon

to another place in the ‘configuration.yalm’ and now the moon showed up

Good. That explains the original problem you presented in your first post: you probably have TWO instances of sensor: in your configuration.yaml file.

However, you indicated you moved everything, including the word sensor: so that means your configuration.yaml file may still contain TWO instances of the word sensor: and you definitely don’t want that. Ensure you only have ONE instance of sensor: in your file.

The issue with moon-phase images is a completely separate problem. The names of the image files must match the Moon sensor’s state exactly. For example, the current moon phase is waning_gibbous so, according to your template, the image file’s name must be waning_gibbous.jpg otherwise it will not be found.

You provided the names of two image files:

 NWaxingGibbous.jpg
 NFirstQuarter.jpg

As far as I know, the Moon sensor’s state will never be “NFirstQuarter” and so those image files will never be used by the card. The Moon sensor is likely to indicate first_quarter. Either rename the image files to match what the sensor reports, or create a more sophisticated template that converts the sensor’s state (first_quarter) to match the image file’s name (NFirstQuarter). My choice would be to rename the files.


EDIT

In addition, I believe the correct path begins with /local/ as opposed to /config/www/.

Assuming you rename the image files to match Moon sensor’s states, this should work:

    sensors:
      moon_phases:
        entity_id: sensor.moon
        friendly_name: 'Månfaser'
        value_template: "{{ states('sensor.moon') }}"
        entity_picture_template: >
          {{ '/local/images/moonphases/{}.jpg'.format(states('sensor.moon')) }}

Thanks for the clarification.
I understand where I missed in the moon images but I don’t get the configuration.yalm correct … tried several times but still gets error.
Here is my original configuration.yalm …

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

homeassistant:
  latitude: 59.372543
  longitude: 17.837789
  elevation: 41
  unit_system: metric
  time_zone: Europe/Stockholm
  name: Hässelby Strand

# Uncomment this if you are using SSL/TLS, running in Docker container, etc.
# http:
#   base_url: example.duckdns.org:8123

# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

spotify:
  client_id: !secret homeassistant_client_id
  client_secret: !secret homeassistant_client_secret
  
sensor:
  - platform: moon
  
sensor:
  - platform: time_date
    display_options:
      - 'date_time'
  - platform: template
    sensors:
      nextsunrise:
        entity_id: sun.sun
        friendly_name: 'Soluppgång'
        value_template: >
          {{ as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom(' %H:%M ') | replace(" 0", "") }}
        icon_template: mdi:weather-sunset-up
      nextsunset:
        entity_id: sun.sun
        friendly_name: 'Solnedgång'
        value_template: >
          {{ as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom(' %H:%M ') | replace(" 0", "") }}
        icon_template: mdi:weather-sunset-down
  - platform: template
  
    sensors:
      vardagsrum_motion_battery:
        friendly_name: 'Vardagsrum sensor battery'
        value_template: '{{state_attr("binary_sensor.vardagsrum_sensor_motion", "battery_level")}}'
        unit_of_measurement: percent
  - platform: template
  
    sensors:
      sovrum_raivo_motion_battery:
        friendly_name: 'Sovrum Raivo sensor battery'
        value_template: '{{state_attr("binary_sensor.sovrum_sensor_raivo_motion", "battery_level")}}'
        unit_of_measurement: percent
  - platform: template
  
    sensors:
      sovrum_anne_motion_battery:
        friendly_name: 'Sovrum Anne sensor battery'
        value_template: '{{state_attr("binary_sensor.sovrum_sensor_anne_motion", "battery_level")}}'
        unit_of_measurement: percent
        
    sensors:
      moon_phases:
        entity_id: sensor.moon
        friendly_name: 'Månfaser'
        value_template: '{{ states.sensor.moon.state }}'
        entity_picture_template: /config/www/images/moonphases/{{ states.sensor.moon.state }}.jpg

frontend:
  themes: !include_dir_merge_named ./themes

camera:
  - platform: local_file
    file_path: /config/www/images/Sailboats.jpg
    
hue:
  bridges:
    - host: 192.168.1.17
    - host: 192.168.1.18


    
    
 
    
    
    

    
    

        


    
# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

homeassistant:
  latitude: 59.372543
  longitude: 17.837789
  elevation: 41
  unit_system: metric
  time_zone: Europe/Stockholm
  name: Hässelby Strand

# Uncomment this if you are using SSL/TLS, running in Docker container, etc.
# http:
#   base_url: example.duckdns.org:8123

# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

spotify:
  client_id: !secret homeassistant_client_id
  client_secret: !secret homeassistant_client_secret
  
sensor:
  - platform: moon
  
  - platform: time_date
    display_options:
      - 'date_time'

  - platform: template
    sensors:
      nextsunrise:
        friendly_name: 'Soluppgång'
        value_template: >
          {{ as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom(' %H:%M ') | replace(" 0", "") }}
        icon_template: mdi:weather-sunset-up
      nextsunset:
        friendly_name: 'Solnedgång'
        value_template: >
          {{ as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom(' %H:%M ') | replace(" 0", "") }}
        icon_template: mdi:weather-sunset-down
      vardagsrum_motion_battery:
        friendly_name: 'Vardagsrum sensor battery'
        value_template: '{{state_attr("binary_sensor.vardagsrum_sensor_motion", "battery_level")}}'
        unit_of_measurement: percent
      sovrum_raivo_motion_battery:
        friendly_name: 'Sovrum Raivo sensor battery'
        value_template: '{{state_attr("binary_sensor.sovrum_sensor_raivo_motion", "battery_level")}}'
        unit_of_measurement: percent
      sovrum_anne_motion_battery:
        friendly_name: 'Sovrum Anne sensor battery'
        value_template: '{{state_attr("binary_sensor.sovrum_sensor_anne_motion", "battery_level")}}'
        unit_of_measurement: percent
      moon_phases:
        friendly_name: 'Månfaser'
        value_template: '{{ states("sensor.moon") }}'
        entity_picture_template: '/config/www/images/moonphases/{{ states("sensor.moon") }}.jpg'

frontend:
  themes: !include_dir_merge_named ./themes

camera:
  - platform: local_file
    file_path: /config/www/images/Sailboats.jpg
    
hue:
  bridges:
    - host: 192.168.1.17
    - host: 192.168.1.18

Thanks for your support … now everything works like a Swiss clock.
I got also the moon images to work … though I have a question.
The moon phase is displayed in English (‘waning_gibbous’) … I want to translate it to Swedish.
Not sure how that can be done in configuration.yalm … guess it has to be done in value_template

      moon_phases:
        friendly_name: 'Aktuell månfas'
        value_template: '{{ states("sensor.moon") }}'
        entity_picture_template: '/local/images/moonphases/{{ states("sensor.moon") }}.jpg'

Do you have any suggestion?

1 Like

I think I figured it out … does it seems OK to you?

      moon_phases:
        friendly_name: 'Aktuell månfas'
        value_template: >-
          {% if is_state('sensor.moon', 'first_quarter') %}
            Måne 1
          {% elif is_state('sensor.moon', 'full_moon') %}
            Måne 2
          {% elif is_state('sensor.moon', 'last_quarter') %}
            Måne 3
          {% elif is_state('sensor.moon', 'new_moon') %}
            Måne 4
          {% elif is_state('sensor.moon', 'waning_crescent') %}
            Måne 5
          {% elif is_state('sensor.moon', 'waning_gibbous') %}
            Måne 6
          {% elif is_state('sensor.moon', 'waxing_crescent') %}
            Måne 7
          {% elif is_state('sensor.moon', 'waxing_gibbous') %}
            Måne 8
          {% endif %}
        entity_picture_template: '/local/images/moonphases/{{ states("sensor.moon") }}.jpg'

I haven’t put in the correct Swedish translations … Måne 1-8 is just for testing purposes

Glad to hear that it works well. Please consider marking my post (above) with the Solution tag. By doing that, it will automatically place a check-mark next to the topic’s title which indicates, to other users, that this topic is resolved. It will also place a link under your first post that leads to the post containing the Solution. All of this helps other people find answers to similar questions.

I would do it like this (replace Måne with the actual Swedish terms) :

      moon_phases:
        friendly_name: 'Aktuell månfas'
        value_template: >-
          {% set phases = {'first_quarter': 'Måne 1', 'full_moon': 'Måne 2',
                           'last_quarter': 'Måne 3', 'new_moon': 'Måne 4',
                           'waning_crescent': 'Måne 5', 'waning_gibbous': 'Måne 6',
                           'waxing_crescent': 'Måne 7', 'waxing_gibbous': 'Måne 8'} %}
          {% set phase = states('sensor.moon') %}
          {{ phases[phase] if phase in phases.keys() else 'Error' }}
        entity_picture_template: '/local/images/moonphases/{{ states("sensor.moon") }}.jpg'

Sorry to revive a topic but how would you go about using multiple sensor entities in a single config?

# Season      
sensor:
  - platform: season
    type: astronomical   
# Moon Phase
sensor:  
  - platform: moon

Use this

# Season      
sensor:
  - platform: season
    type: astronomical   
# Moon Phase  
  - platform: moon
1 Like

Thank you!

Hej Rayan,

Thank you for the moon phase display

Do you know or if you have and idea how to validate what will be the next “moon.phase” tomorrow or in x days?

I’d like to do something like that:

IF in x days Moon Phase EQUAL Full Moon THEN
   Hey honey… Full moon is coming !
ELSE
   IF Today is the Full Moon THEN
       My sweet LOVER,  today is your lucky day 
   ELSE
       Just another day
   ENDIF
ENDIF

Thank you in advance

For those that are interested… I combined HACS moon phases and compass card
image

Yes, I’m interested. Can you share the card settings?