Custom Images not shown on moon sensor

Hello,

Custom Images not shown on moon sensor anymore (see attached screenshot).

Part of my config from configuration.yaml

    sensor:
  - platform: yr
    name: Weather
    forecast: 24
    monitored_conditions:
     - temperature
     - symbol

  - platform: moon
  - platform: template
    sensors:
        moonphases:
          entity_id: sensor.moon
          friendly_name: 'Moon'
          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 %}

The pics/jpegs are present in the /.ha-config-dir/www/moonphases directory.

The pictures were displayed in version 74.x and below without problems.

But with 77.x and above they just disapeared.

What’s wrong here?

ha_moon_no_icon

regards Tom

There was a breaking change in one of the updates in between.
You have to change the states to lowercase and underscore instead of spaces.

As stated in the docs https://www.home-assistant.io/components/sensor.moon/

      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 %}

FYI, you guys can straight convert the states into the path without if statements:

      entity_picture_template: >
        {% set state = states('sensor.moon').title().replace('_','').replace(' ','') %}
        {{ '/local/moonphases/N{}.jpg'.format(state) }}

This will account for the state prior to the breaking change, it will also account for the breaking change state.

3 Likes

nice!
also, the entity_id isnt necessary:

  - platform: template
    sensors:
      moon_phases:
        friendly_name: 'Moon phases'
        value_template: >
          {{ states('sensor.moon')}}
        entity_picture_template: >
          {% set state = states('sensor.moon').title().replace('_','').replace(' ','') %}
          {{ '/local/weather/moonphases/N{}.jpg'.format(state) }}
1 Like

This is definitely one of the best things I’ve seen here. Not the most important but it is just brilliant!

also of course the value template…

  moonphases:
    entity_id: sensor.moon
    friendly_name: 'Moon'
    value_template: >
      {{ states('sensor.moon').title().replace('_',' ') }}
    entity_picture_template: >
      {% set state = states('sensor.moon').title().replace('_','') %}
        {{ '/local/icons/moon_phases/{}.jpg'.format(state) }}

Thanks for this! Thirty five lines of code down to 5. And no loss of readability :slight_smile:

image

3 Likes

you can cut 1 extra :wink:

1 Like

Are you saying I don’t need entity_id: sensor.moon?

I don’t really understand why not.
Yes I could test it myseff but for one line I am not going to sit watching HA restart for 5 minutes! :stuck_out_tongue_winking_eye:

indeed, several moons ago HA changed the need for the entity_id. Its mentioned in the value_template which suffices.

3 Likes

I see what you did there :slight_smile:

Thanks for the info.

1 Like

Thank you to all.

It’s working perfectly! :slight_smile:

i have a related sensor so please allow me to ask if you can spot the error…

sensor:
  platform: template
  sensors:
    m_life360_state:
      friendly_name: M life360 state
      value_template: >
        {% set state = states('device_tracker.life360_m') %}
        {{ state|replace(' ','_')| lower }}
      entity_picture_template: >
        {{ '/local/tiles/family/m_{}.png'.format(state) }}

the sensor gets the correct state, and my Tiles are showing the pictures based on this sensor just fine. Have I made an obvious mistake here?

If I check it in the dev-state, the entity picture is set to m_unknown.png.…shouldnt ‘state’ be enough to get states('sensor.m_life360_state') ?

gotta repeat the variable set state. separate templates don’t talk with each other.

sensor:
  platform: template
  sensors:
    m_life360_state:
      friendly_name: M life360 state
      value_template: >
        {% set state = states('device_tracker.life360_m') %}
        {{ state|replace(' ','_')| lower }}
      entity_picture_template: >
        {% set state = states('device_tracker.life360_m').replace(' ','_').lower() %}
        {{ '/local/tiles/family/m_{}.png'.format(state) }}

Yes, I am aware of that thanks. But since this sensor’s state is already made as a template with replace(’ ‘,’_').lower() I would have thought calling this sensor wouldn’t need that again in the entity_picture_template.

Which in fact is true. I tried this:

  entity_picture_template: >
    {{ '/local/tiles/family/m_' + states('sensor.m_life360_state') +'.png'}}

and

  entity_picture_template: >
    {{ '/local/tiles/family/wm_{}.png'.format(states('sensor.m_life360_state')) }}

and that works fine. So the issue isn’t one of formatting, but referencing the mother sensor…

final draft:

sensor:
  platform: template
  sensors:
    m_life360_state:
      friendly_name_template: >
        {{states('device_tracker.life360_m')|title}}
      value_template: >
        {% set state = states('device_tracker.life360_m') %}
        {{ state|replace(' ','_')| lower }}
      entity_picture_template: >
        {{ '/local/tiles/family/m_' + states('sensor.m_life360_state') +'.png'}}

Well to answer your question then:

No, template sensors do not work that way.

they do in Tiles: ;=)

      - entity: device_tracker.life360_m
        style_template: >
          if (state === 'home' || state === 'not_home') return 'background-image: url(\"/local/tiles/family/m_' + state + '.png\")';
          return 'background-color: #643aac;background-image: url(\"/local/tiles/family/m_' + entities['sensor.m_life360_state'].state + '.png\")';

thanks for the helping hand!

Right, but that’s not the same template. That’s using Java Script, where what you posted up top in the picture_template is Jinja. 2 totally different interpreters.

Sure, confusing it is now and then.
Still, I’ve been forced to use this intermediary template_sensor for the Tiles because I couldn’t get the javascript to do that for me…No way I could get that in the correct syntax.

A mixed bag of Jinja and Javascript, and both can use Pythonesque techniques…

Well, you are doing stuff that isn’t standard, of course it’s going to get confusing. The standard coding for HA is yaml and jinja.

ok, I appreciate that, and indeed confess to take things in the frontend a bit further than the default HA configuration. But thats what makes HA sing for me, and adds to the WAF…
Luckily with the aid of this great community, many challenges are met with great solutions. I thank you for that most sincerely! :+1:

1 Like

I am not sure I fully understand this change to 0.81

  • Template sensors will no longer auto update if we can’t find relevant entities in the template. You’ll want to review your template sensors and consider adding relevant entity_id entries or use the new homeassistant.update_entity service.

Does this still hold true?

I’m guessing yes because the entity_id appears somewhere in the template?
I just want to be sure before I ugrade!