Number of days to Full Moon this Month Automation

I have added moon sensor component as follows:

sensor:
  - platform: moon

After adding the above component & grouping the same I see the status of the moon as follows:
Moon

Question:
**I want to display a card in Home Automation Panel which says something like **
"XX Days to Full Moon This Month"
"XX Days to New Moon This Month"

How do I achieve it? Please help with sample code.

I don’t think the moon platform has any attributes that can be used to get the information you want.

Have you done a search…

It seems google calendars have the info, you would just have to retrieve it.

1 Like

Hello,
any news on this? I would love to have moon dates in HA as well. Thanks J

You can use HACS - Astroweather (GitHub - mawinkler/astroweather: Asynchronous Astro Weather Forecast for Home Assistant)

Thanks for the link!

So you’re saying that they have a sensor for “Date of next Full Moon”?

I feel confident saying “no.” I have the Astroweather integration and the Lovelace card, and there is no such sensor.

I have tried making a template for this, but no luck so far this is what I have, but but it reports Unavailable

sensor:
  - platform: template
    sensors:
      days_until_full_moon:
        friendly_name: "Days Until Full Moon"
        unit_of_measurement: 'days'
        value_template: >
          {% set today = now().date() %}
          {% set next_full_moon = states('sensor.moon') | as_datetime %}
          {% set full_moon_date = next_full_moon.date() %}
          {% set days_until_full_moon = (full_moon_date - today).days %}
          {{ days_until_full_moon }}

I use REST to pull the data from the US Naval Observatory API into a group of sensors, then the days until a particular phase can be found with a template. I have no idea whether the REST API will be available to users outside the US.

rest:
  - authentication: basic
    scan_interval: 5400
    resource_template: |-
      https://aa.usno.navy.mil/api/moon/phases/date?date={{now().date()|string}}&nump=4
    sensor:
      - name: "Next Moon Phase Date"
        unique_id: next_moon_phase_date_0001
        value_template: |
          {% set x = value_json.phasedata[0] %}
          {{x.year}}-{{x.month if x.month > 9 else '0'~x.month|string}}-{{x.day if x.day > 9 else '0'~x.day|string}}
        json_attributes_path: "$.phasedata[0]"
        json_attributes: 
          - "phase" 
          - "time"
      - name: "Next but One Moon Phase Date"
        unique_id: next_1_moon_phase_date_0001
        value_template: |
          {% set x = value_json.phasedata[1] %}
          {{x.year}}-{{x.month if x.month > 9 else '0'~x.month|string}}-{{x.day if x.day > 9 else '0'~x.day|string}}
        json_attributes_path: "$.phasedata[1]"
        json_attributes: 
          - "phase" 
          - "time"
      - name: "Next but Two Moon Phase Date"
        unique_id: next_2_moon_phase_date_0001
        value_template: |
          {% set x = value_json.phasedata[2] %}
          {{x.year}}-{{x.month if x.month > 9 else '0'~x.month|string}}-{{x.day if x.day > 9 else '0'~x.day|string}}
        json_attributes_path: "$.phasedata[2]"
        json_attributes: 
          - "phase" 
          - "time"
      - name: "Next but Three Moon Phase Date"
        unique_id: next_3_moon_phase_date_0001
        value_template: |
          {% set x = value_json.phasedata[3] %}
          {{x.year}}-{{x.month if x.month > 9 else '0'~x.month|string}}-{{x.day if x.day > 9 else '0'~x.day|string}}
        json_attributes_path: "$.phasedata[3]"
        json_attributes: 
          - "phase" 
          - "time"
template:
  - sensor:
      - name: Days Until Full Moon
        unit_of_measurement: days
        state: >
          {% set s_list = ['sensor.next_but_three_moon_phase_date','sensor.next_but_two_moon_phase_date',
          'sensor.next_but_one_moon_phase_date','sensor.next_moon_phase_date'] %}
          {% set next_full = expand(s_list) | selectattr('attributes.phase', 'eq', 'Full Moon')
          | map(attribute='state') | join | as_datetime | as_local %}
          {{ (next_full - today_at()).days }}

I found a website that appears to work for the whole world:

When you select a location the Next New Moon and Next Full Moon are shown in a table on the left.
When the URL is extended with the correct latitude, longtitude and current date and time it immediately returns the correct moon data.
So I have been trying to create a Scrape sensor to collect these date time values, but did not succeed up to now.
This is what I have now (for Paris):

scrape:
  - resource: https://www.mooncalc.org/#/48.8583,2.2945,17/2023.09.20/10:10/1/2
    sensor:
      - name: Next full moon
        select: "span.moontext.vollmond"

But it returns three dots …
Can someone with better Scrape skills have go with this?

Didgeridrew, worked prefect, thanks.

template:
  - sensor:
      - name: Days Until Full Moon
        unit_of_measurement: days
        state: >
          {% set s_list = ['sensor.next_but_three_moon_phase_date','sensor.next_but_two_moon_phase_date',
          'sensor.next_but_one_moon_phase_date','sensor.next_moon_phase_date'] %}
          {% set next_full = expand(s_list) | selectattr('attributes.phase', 'eq', 'Full Moon')
          | map(attribute='state') | join | as_datetime | as_local %}
          {{ (next_full - today_at()).days }}

When I put this in Developer tools Template: it works fine, now I just have to figure how to get it into my config.yaml without getting errors.

I’m still messing around with different ideas.

1 Like