Newbie - trouble with code in moon phase

I am fighting the code. Any help appreciated. What am I doing wrong in this code?

I want the moon fase to appear in the picture as (not) shown

show_state: true
show_name: true
camera_view: auto
type: picture-entity
entity: sensor.moon_fase
image: /api/image/serve/8f1e1c919636a3f07b2c6aef8fd29293/512x512
sensor:
  - platform: template
    sensors:
      moon_fase_image:
        friendly_name: Maan
        value_template: '{{ states.sensor.moon_fase.state }}'
        icon_template: |
          moon:{{ states.sensor.moon.state }}
        entity_picture_template: |
          {% if is_state('sensor.moon_fase', 'New moon') %}
            /images/moonphases/xnew_moon.png
          {% elif is_state('sensor.moon_fase', 'Waxing crescent') %}
            /images/moonphases/xwaxing_crescent.png
          {% elif is_state('sensor.moon_fase', 'First quarter') %}
            /images/moonphases/xfirst_Quarter.png
          {% elif is_state('sensor.moon_fase', 'Waxing gibbous') %}
            /images/moonphases/xwaxing_gibbous.png
          {% elif is_state('sensor.moon_fase', 'Full moon') %}
            /images/moonphases/xfull_moon.png
          {% elif is_state('sensor.moon_fase', 'Waning gibbous') %}
            /images/moonphases/xwaning_gibbous.png
          {% elif is_state('sensor.moon_fase', 'Last quarter') %}
            /images/moonphases/xlast_quarter.png
          {% elif is_state('sensor.moon_fase', 'Waning crescent') %}
            /images/moonphases/xwaning_crescent.png
          {% endif %}

you’re putting sensor config code in your front end lovelace dashboard ui.
sensors should be defined in your configuration.yaml. then you might refer to it in your front end.

give this a good read:

define your sensor and make sure it shows up in your entities list and has the state values correct. then go build the front end card.

What @armedad said. Here’s the template sensor you want, in modern format. You cannot do this via the UI: you need to put it into your configuration.yaml (docs).

If you already have a line in configuration.yaml like:

template: !include templates.yaml

put the config below into that file (templates.yaml) without including the template: first line and moving the rest two spaces left.

If you already have a template: header in that file with configurations under it, put it underneath that, without repeating the first line header.

template:
  - sensor:
      - name: Moon fase image
        state: "{{ states('sensor.moon_fase') }}"
        icon: mdi:moon-{{ states('sensor.moon_fase')|lower|replace(' moon','')|replace(' ','-') }}
        picture: "/images/moonphases/x{{ states('sensor.moon_fase')|lower|replace(' ','_')|replace('rst_q','rst_Q') }}.png"

Restart HA to pick up the new sensor. In future, you can do Developer Tools / YAML / Template entities to reload without restart, but you must restart for the first one.

The two templates assumes the possible states you list are the correct ones: they then convert that state into the MDI icon name and filename; and the filename one assumes that the capital Q in xfirst_Quarter.png is correct.

In English, by the way, it’s “phase”. Doesn’t matter here though.

Thank you for the quick response.

This is what I have now in template .yaml
Not sure if everything under: “entity_picture_template” should be there?

template:
  - sensor:
      - name: Moon fase image
        state: "{{ states('sensor.moon_fase') }}"
        icon: mdi:moon-{{ states('sensor.moon_fase')|lower|replace(' moon','')|replace(' ','-') }}
        picture: "/images/moonphases/x{{ states('sensor.moon_fase')|lower|replace(' ','_')|replace('rst_q','rst_Q') }}.png"
        entity_picture_template: >-
          {% if is_state('sensor.moon_fase', 'New moon') %}
          /images/moonphases/xnew_moon.png
          {% elif is_state('sensor.moon_fase', 'Waxing crescent') %}
          /images/moonphases/xwaxing_crescent.png
          {% elif is_state('sensor.moon_fase', 'First quarter') %}
          /images/moonphases/xfirst_Quarter.png
          {% elif is_state('sensor.moon_fase', 'Waxing gibbous') %}
          /images/moonphases/xwaxing_gibbous.png
          {% elif is_state('sensor.moon_fase', 'Full moon') %}
          /images/moonphases/xfull_moon.png
          {% elif is_state('sensor.moon_fase', 'Waning gibbous') %}
          /images/moonphases/xwaning_gibbous.png
          {% elif is_state('sensor.moon_fase', 'Last quarter') %}
          /images/moonphases/xlast_quarter.png
          {% elif is_state('sensor.moon_fase', 'Waning crescent') %}
          /images/moonphases/xwaning_crescent.png
          {% endif %}

and this is the “front-end-card”

Must be “picture”.

The “entity_picture_template” is used in a legacy format - which you are NOT using.

It should not: I supplied the picture template that replaced it in modern template sensors. Remove the entity_picture_template completely — I know it looks much smaller without it, but all of the logic is there: your original was very inefficient.

You can paste it into Developer Tools / Template to see it resolve the icon and picture filenames.

Also, read what I wrote:

That file should start with

- sensor:

HOWEVER — I think this could be more easily achieved just by using the Picture Entity Card correctly. See the second example here:

So you’d have this as the card code:

type: picture-entity
entity: sensor.moon_fase
state_image:
  "New moon": /images/moonphases/xnew_moon.png
  "Waxing crescent": /images/moonphases/xwaxing_crescent.png
[and so on]

with no need for a template sensor. Just need to make sure those images are accessible, with the URL probably under /local/. See here:

Still no moonphase picture.
I made the changes: but … no moon top left corner.
Any suggestions?


or the code:

show_state: true
show_name: true
camera_view: auto
type: picture-entity
entity: sensor.moon_fase
image: /api/image/serve/8f1e1c919636a3f07b2c6aef8fd29293/512x512
state_image:
  New moon: /images/moonphases/xnew_moon.png
  Waxing crescent: /images/moonphases/xwaxing_crescent.png
  First quarter: /images/moonphases/xfirst_Quarter.png
  Waxing gibbous: /images/moonphases/xwaxing_gibbous.png
  Full Moon: /images/moonphases/xfull_moon.png
  Waning gibbous: /images/moonphases/xwaning_gibbous.png
  Last quarter: /images/moonphases/xlast_quarter.png
  Waning crescent: /images/moonphases/xwaning_crescent.png

Read the link in my last post. These are URLs under state_image. Does this work for you if you fill in the IP address and paste into your browser?

http://[YOUR HA IP]:8123/images/moonphases/xnew_moon.png

If it doesn’t display the image, then it won’t work in the picture card either. Read the link and work out how to display stored images via your browser.

Also, what is the actual state of the sensor? Is it “Waning crescent” or “Afnemende, sikkelvormige maan”? Go to Developer Tool / States and see what the result is for:

{{ states('sensor.moon_fase') }}

The states are wrong, it has to be:

  waxing_crescent
  first_quarter
  waxing_gibbous
  full_moon
  waning_gibbous
  last_quarter
  waning_crescent
1 Like

Yep thats the problem: I dont see it im all of my browser
404 error

/my-ip:8123/local/images/moonphases/waning_crescent.png
if i type the above in my browser i see the picture
But still not in the moon state

OK, so add the /local bit into the image URLs in the picture card, and update the states to match @walberjunior’s post:

show_state: true
show_name: true
camera_view: auto
type: picture-entity
entity: sensor.moon_fase
image: /api/image/serve/8f1e1c919636a3f07b2c6aef8fd29293/512x512
state_image:
  new_moon: /local/images/moonphases/xnew_moon.png
  waxing_crescent: /local/images/moonphases/xwaxing_crescent.png
  first_quarter: /local/images/moonphases/xfirst_Quarter.png
  waxing_gibbous: /local/images/moonphases/xwaxing_gibbous.png
  full_moon: /local/images/moonphases/xfull_moon.png
  waning_gibbous: /local/images/moonphases/xwaning_gibbous.png
  last_quarter: /local/images/moonphases/xlast_quarter.png
  waning_crescent: /local/images/moonphases/xwaning_crescent.png

That doesn’t work: I have to put my ip adres in front of the local/… so: my-ip/local/…
then it shows up in my browser
I removed the x in the names
still no moon picture
my sensor moon_fase says: see below (waning_cresent)

Any suggestions?

I made a typo error: Not a capital in the name.
I have a picture now of the waning_crescent
Only it’s to big!
Is there a way of down sizing it (with HA code) other than use photoshop and resize it to a smaller picture (100x100 px)?

This should be it;


now i want the sensor.moon_fase in it

Do I have the right card to make the above happen (picture-entity)?

That screenshot is a picture-elements card not a picture-entity.

Look at the State Label section of the Picture Elements card documentation (direct link).

I did know that (picture-element)
But why doesn’t it show in picture-entity
somewhere i am making a stupid mistake but where???
i am just messing around because i don’t see / know it what to do.
sorry for that

this is what i got now
The moon is too big and the bg-moon (api/…) is gone
I tried style: nothing what the heck …
I am confused

Sensor.moon_fase_image is correct as you can see

How to get this super imposed on the bg-moon image including the state
or i get the state without the moon, or i get the moon without te state
i would like to have both

I believe you cannot use the picture-entity “effect” within picture-elements. You can use something like this:

  - type: state-label
    entity: sensor.moon_phase
    prefix: "Moon Phase: "
    style:
      top: 90.0%
      left: 50.5%

image

Thank you but can you show me the entire code, doesn,t work for me