Template invisible despite (partially) working interactively?

As a newbie I could get some slap to my head. Who could do it, please?

within configuration.yaml, I added this to have the sun elevation angle, but only if over the horizon:

template:
  #--------------------------------------------------------------------------------------------------
  # Sun angle over Horizon
  #--------------------------------------------------------------------------------------------------
  - trigger:
      - platform: time_pattern
        minutes: "/10"
      - platform: event
        event_type: event_template_reloaded
      - platform: homeassistant
        event: start
    sensor:
      - name: "Sun Angle"
        unique_id: "sun_over_zero"
        unit_of_measurement: "°"
        value_template: >-
          {% if is_state('sun.sun', 'above_horizon') %}
          {{   '%+.1f' | format(states.sun.sun.attributes.elevation) }}
          {% else %}
          {{   '+0.0f' }}
          {% endif %}

Before, I tested the last 5 lines in “development tools” | “Template” | “Template Editor”, where it apparently works:

 {% if is_state('sun.sun', 'above_horizon') %}
 {{   '%.1f' | format(states.sun.sun.attributes.elevation) }}
 {% else %}
 {{   '0.0f' }}
 {% endif %}

… but I can not add any chart to my lovelace, when I type “add by entity name” and say “Sun Angle” or say “sun_over_zero”.

What’s my fault?
Ouch!
Thank you!

Your formatting is going to cause the sensor to fail. When you use unit_of_measurement the output needs to be numeric. Both the '+0.0f' returned by the else and the + prepended to the elevation are problematic.

template:
  #--------------------------------------------------------------------------------------------------
  # Sun angle over Horizon
  #--------------------------------------------------------------------------------------------------
  - trigger:
      - platform: time_pattern
        minutes: "/10"
      - platform: event
        event_type: event_template_reloaded
      - platform: homeassistant
        event: start
    sensor:
      - name: "Sun Angle"
        unique_id: "sun_over_zero"
        unit_of_measurement: "°"
        state: "{{ [0.0, state_attr('sun.sun','elevation')] | max | round(1) }}"

EDIT: Replaced incorrect key value_template with state

I understand your sample, a list of 0.0 and the revent elevation attribute, then taking the max - will result in no value less than 0.0. Okay. Nice.

But something else must be wrong with my code, the entity still does not appear. Also after a full restart of HA.

Could it perhaps come from having the older style templates some lines above?
this is above the sun_over_zero:

sensor:
  - platform: template
    sensors:
      ######## Frost Sensor:
      lowtemp36:
        unit_of_measurement: "°C"
        friendly_name: Min Temp 36h
        value_template: >
          {% set start = now().replace(hour=0,minute=0,second=0, microsecond=0) %}
          {% set end = (start + timedelta(hours=36)) %}
          {% set start = start.strftime("%Y-%m-%dT%H:%M:%S+00:00") %}
          {% set end = end.strftime("%Y-%m-%dT%H:%M:%S+00:00") %}
          {{ state_attr('weather.openweathermap', 'forecast') | selectattr('datetime', '>=', start) | selectattr('datetime','<=', end) | map(attribute='temperature') | list | min }}
      ######## Rain Sensor:
      rainy24:
        unit_of_measurement: "mm"
        friendly_name: Max Niederschlag 24h
        value_template: >
          {% set start = now().replace(hour=0,minute=0,second=0, microsecond=0) %}
          {% set end = (start + timedelta(days=1)) %}
          {% set start = start.strftime("%Y-%m-%dT%H:%M:%S+00:00") %}
          {% set end = end.strftime("%Y-%m-%dT%H:%M:%S+00:00") %}
          {{ state_attr('weather.openweathermap', 'forecast') | selectattr('datetime', '>=', start) | selectattr('datetime','<=', end) | map(attribute='precipitation') | list | max }}

finally, these are working. So I guess this is not the problem, or?

Working as well as some more of the newer template format, following the sun_over_zero thing:

 # --- Mom: sensor.mom_moto_g13_steps_Sensor
  - trigger:
      - platform: time_pattern
        minutes: "/5"
      - platform: event
        event_type: event_template_reloaded
      - platform: homeassistant
        event: start
    sensor:
      - name: "Daily Steps Mom"
        unique_id: "Daily Total Steps Mom"
        unit_of_measurement: "Steps"
        state: "{{ (states('sensor.mom_moto_g13_steps_Sensor')| int - states('sensor.total_steps_yesterday_mom')| int) | int }}"

  - trigger:
      - platform: time
        at: '00:00:02'
      - platform: event
        event_type: event_template_reloaded
      - platform: homeassistant
        event: start
    sensor:
      - name: "Total Steps Yesterday Mom"
        unique_id: "Yesterday Total Steps Mom"
        state: "{{ states('sensor.mom_moto_g13_steps_Sensor') | int }}"

I can see al those entities in HA / Lovelace configuration - except the “sun_over_zero” alias “Sun Angle”, which is between both.

Hmmm …
:cry:

That should not have any effect, as long as each “style” is set up under the correct integration key (sensor for older style, template for current).

Make sure you only have 1 top-level template key. If you have more than one, the final entry will over-write any earlier entries.

checked that.
It is not the case here.

Do you have any other ideas? Rules which get missed by noobs often? Or the like?

I missed it earlier, but your first post (which I copied) was using value_template instead of state. I have corrected it in my post above.

Now it is shown. 0.0° because the sun is below horizon in Germany now, as expected!

THANK YOU!!
I could hug you!

1 Like