Scripts not appearing in automation editor after creating files

I have a handful of scripts that I’ve downloaded from Github that all work together to generate two tts automations; morning briefing and evening briefing. I added them to HA, but after rebooting, I’m getting errors that the automations can’t run because the scripts cannot be found.

Conversely, I have a few scripts in the same directory that do run as intended. They appear when I search states in Developer Tools (the new scripts do not). I tried comparing and contrasting and changing the formatting to align with the scripts that do show up, but no luck there.

I’ve tried creating a new script from scratch in automation editor to see how HA formats the code, but even after adjusting the code in these new scripts to match and rebooting, they still don’t show up in the automation editor when trying to add them from the call service dropdown. I’m baffled at this point, and don’t know where the problem is.

Script that appears as script.radio_play:

radio_play:
  alias: Play Stream
  sequence:
  - service: media_player.volume_set
    data:
      entity_id: >
        {% if is_state("input_select.radio_speaker", "speaker1") %} media_player.speaker1
        {% elif is_state("input_select.radio_speaker", "speaker2") %} media_player.speaker2
        {% elif is_state("input_select.radio_speaker", "speaker3") %} media_player.speaker3
        {% elif is_state("input_select.radio_speaker", "speaker4") %} media_player.speaker4
        {% else %}
          media_player.speaker2
        {% endif %}
      volume_level: "{{ states('input_number.radio_volume') }}"

Example of script (script.speech_engine) that does not appear anywhere:

speech_engine:
  mode: queued
  sequence:
  - service: script.speech_processing
    data:
      media_player: >-
          {% if media_player | length == 0 %}
            {% set media_player = [
              'media_player.speaker3',
              'media_player.apeaker4'
            ] %}
          {% endif %}
          {% if media_player is not string and media_player is sequence %}
            {% set media_player = media_player|join(', ') %}
          {% endif %}
          {{ media_player }}
      speech_message: >
        {{ speech_message }}

Another example (script.evening_briefing) not appearing:

evening_briefing:
  sequence:
  - service: script.speech_engine
    data:
      where: 'living room'
      when: 'evening'
      speech_message: !include ../templates/speech/briefing.yaml

For the scripts not appearing, I tried adding an alias and indenting as needed, but, again, nada. It has to be something in the formatting I’m seeing across these scripts, as they’re all similar; but they’re slightly different than the ones I have that do work.

Look in the homeassistant.log file in your config directory to see if there is anything in there about the two scripts that aren’t showing up.

Also check ownership and rights on the files.

@finity I don’t see anything in the log files for any of the scripts. It looks like they’re not even loading because of errors. The files reference one another, so I had to recursively follow the breadcrumbs. Copy/pasting each of the scripts into the template editor, I saw that most had errors. So, I’m not sure how they’re working for the people that created them.

I traced everything back to what I think is the first script in line. When I pasted the YAML into the template editor, I got:

TemplateSyntaxError: unexpected '<='

Don’t know what that means, and the editor doesn’t show what line the error is coming from.

@WallyR Permissions should be good, as I created the files from Studio Code Server, and pasted the raw github content in.

Looks like you have a change of special chars.

Check all quotation marks,both the single and double ones.
Sometimes they are changed on publishing, so you can not just copy paste

I looked the script over, and didn’t see any obvious errors. All the inequality signs are correct. I even took out the <= and replaced with < (i.e., entity <= 2 changed to < 3). Then it complained about TemplateSyntaxError: unexpected '<'. Here is the complete code. Sorry, it’s pretty long.

>-
  {# Daily Briefing #}

  {%- macro getGreeting() -%}
    {% if now().strftime('%H')|int < 12 %}
      Good morning.
    {% elif now().strftime('%H')|int >= 12 and now().strftime('%H')|int < 17 %}
      Good afternoon.
    {% elif now().strftime('%H')|int >= 12 and now().strftime('%H')|int < 20 %}
      Good evening.
    {% else %}
      Ugh.  I'd ask my grandfather, Dr. Sbaitso, to take over for me; but he's asleep and drooling in his rocker.  It's best to let him be.
    {% endif %}

    {% if when == 'morning'  %}
        Today is {{states.sensor.today_is.state }}, {{ as_timestamp(now()) | timestamp_custom('%B %d %Y') }}.
    {% else %}
        It is {{ now().strftime("%I:%M %p") }}.

    {% endif %}
  {%- endmacro -%}

  {%- macro getWeather() -%}
    The weather in town is {{states('sensor.openweathermap_temperature')|round}} degrees
    {% if is_state('weather.openweathermap', 'rainy') %}
      {{ [
      'with rain.', 
      'with showers.'
      ] | random }}
    {% elif is_state('weather.openweathermap', 'cloudy') %}
      {{ [
      'with clouds.', 
      'with cloudy skies.'
      ] | random }}
    {% elif is_state('weather.openweathermap', 'partlycloudy') %}
      {{ [
      'with some clouds.', 
      'with partly cloudy skies.',
      'with scattered clouds'
      ] | random }}
    {% elif is_state('weather.openweathermap', 'sunny') %}
      {% if is_state('sun.sun', 'above_horizon') %}
        {{ [
        'and sunny.', 
        'with sun.'
        ] | random }}
      {% else %}
        {{ [
        'and clear.', 
        'with clear skies.'
        ] | random }}
      {% endif %}
    {% else %}
      and {{ states.weather.openweathermap.state }}
    {% endif %}
  {%- endmacro -%}

  {%- macro getUVIndex() -%}
    Currently, the UV Index is {{states('sensor.openweathermap_uv_index')}}
    {% if is_number ('sensor.openweathermap_uv_index', <= 2) %}
      {{ [
      'which is low.  No protection is needed.', 
      'which is minimal.  No protection is needed.'
      ] | random }}
    {% elif is_number ('sensor.openweathermap_uv_index', > 3 && <= 5) %}
      {{ [
      'which is moderate.  Protection may be needed.', 
      'which is moderate.  Seek shade during the late morning through mid-afternoon.'
      ] | random }}
    {% elif is_number ('sensor.openweathermap_uv_index', > 6 && <= 7) %}
      {{ [
      'which is high.  Protection is needed.',
      'which is high.  Seek shade during the late morning through mid-afternoon.  When outside, generously apply broad-spectrum SPF-15 or higher sunscreen on exposed skin, and wear protective clothing'
      ] | random }}
    {% elif is_number ('sensor.openweathermap_uv_index', > 8 && <= 10) %}
      {{ [
      'which is very high.  Extra protection is needed.',
      'which is very high.  Be careful outside, especially during late morning through mid-afternoon. If your shadow is shorter than you, seek shade and wear protective clothing.  Generously apply a minimum of SPF-50, broad-spectrum sunscreen on exposed skin.'
      ] | random }}
    {% else %}
      {{ [
      'which is extreme.  Take an abundance of caution when going outside.  Please, stay indoors, close all blinds and curtains, and monitor the inside temperature closely.',
      'which is extreme.  Avoid going outside if at all possible, especially during late morning through mid-afternoon. If your shadow is shorter than you, seek shade and wear protective clothing, a wide-brimmed hat, and sunglasses, and generously apply a minimum of  SPF-50, broad-spectrum sunscreen on exposed skin.'
      ] | random }}
    {% endif %}
  {%- endmacro -%}

  {%- macro getWeatherAlerts() -%}
    {% if when == 'evening' %}
      The overnight forecast says we can expect {{ states.weather.openweathermap.state }}.
    {% else %}
      The current forecast says we should expect {{ states.weather.openweathermap.state }}.
      {% if is_state('input_boolean.freeze_warning','on') %}
      And based on the forecasted low, It will be near or below freezing.
        {{ [ 'So, Someone might want to bring the lemon tree in. ',
            'Like, the turn water solid kind of cold. Do not leave the lemon tree out to die.',
            'I suggest bringing in the plants, otherwise the temperature might kill them. And that will be on you, dawg.',
            'I would say winter is coming. But, based on the weather forecast it appears to be here.',
            'I would bring in the plants but I lack legs. And Arms. So I am forced to rely on you. Do not let me down.'
          ] | random }}
      {% endif %}
    {% endif %}
  {%- endmacro -%}

  {%- macro getSun() -%}
        {% if is_state('sun.sun', 'below_horizon') %}
          You have
          {% set seconds = as_timestamp(states.sun.sun.attributes.next_rising)-now().timestamp() %}
          {% set hours = seconds / 60 %}
          {% if seconds / ( 60 * 60 ) > 1 %}
            {{ (seconds //  ( 60 * 60 )) | int }} hours
          {% else %}
            {{ (seconds // 60) | int }} minutes
          {% endif %}
          {{ [
            'until the sun rises.',
            'until the sun is up.',
            'before the sun officially rises. '
          ]|random }}
        {% else %}
          You have
          {% set seconds = as_timestamp(states.sun.sun.attributes.next_setting)-now().timestamp() %}
          {% set hours = seconds / 60 %}
          {% if seconds / ( 60 * 60 ) > 1 %}
            {{ (seconds //  ( 60 * 60 )) | int }} hours
          {% else %}
            {{ (seconds // 60) | int }} minutes
          {% endif %}
          {{ [
            'until the sun sets for the day.',
            'until the sun slips below the horizon.',
            'before the sun officially sets. '
          ]|random }}
        {% endif %}
  {%- endmacro -%}

  {%- macro getClimate() -%}
    {%- if is_state('climate.living_room','off') %}
      The heat is off. The temperature inside is {{ states.climate.living_room.attributes.current_temperature }} degrees. 
    {%- elif is_state('climate.living_room','heat_cool') %}
      The air conditioning will try to keep the temperature between {{ states.climate.living_room.attributes.target_temp_low }} and {{ states.climate.living_room.attributes.target_temp_high }}.
    {% else %}
      The heat is set to {{ states.climate.living_room.state }} with a current temperature of {{ states.climate.living_room.attributes.current_temperature }} which is 
      
        {%- if states.climate.living_room.attributes.current_temperature | int - states.climate.living_room.attributes.temperature | int |round > 0 %}
          {{ states.climate.living_room.attributes.current_temperature | int - states.climate.living_room.attributes.temperature | int }} degrees above 
        {%- elif states.climate.living_room.attributes.current_temperature | int - states.climate.living_room.attributes.temperature | int |round < 0 %}
          {{ (states.climate.living_room.attributes.current_temperature | int - states.climate.living_room.attributes.temperature | int) | abs }} degrees below
        {% else %}
          right at 
        {% endif %}
        the set point of {{ states.climate.living_room.attributes.temperature }}
      degrees.
    {%- endif -%}
    And The internal humidity is currently {{ states.climate.living_room.attributes.current_humidity }} percent.
  {%- endmacro -%}

  {%- macro getWazeTime() -%}
    {%- if is_state('binary_sensor.workday_sensor','on') %}
      As of {{states.sensor.time.state}} the current drive time downtown is {{ states('sensor.waze_travel_time') }} minutes.
      The ideal route is {{ state_attr("sensor.waze_travel_time","route") }}
    {% else %}
      Today is either a weekend day or a holiday, so you may not be driving to work.  I'm skipping the travel time report for this morning.  If I am incorrect, please correct my code.
    {%- endif -%}
  {%- endmacro -%}

  {# a macro that removes all newline characters, empty spaces, and returns formatted text #}
    {%- macro cleanup(data) -%}
      {%- for item in data.split("\n")  if item | trim != "" -%}
        {{ item | trim }} {% endfor -%}
  {%- endmacro -%}

  {# a macro to call all macros :) IF NEW MACROS ARE CREATED, ENTER THEM BELOW INCLUDING PARENS!  #}
    {%- macro mother_of_all_macros() -%}
      {{ getGreeting() }}
      {{ getEvents() }}
      {{ getWeather() }}
      {{ getUVIndex() }}
      {{ getWeatherAlerts() }}
      {{ getClimate() }}
      {{ getSun() }}
      {{ getWazeTime() }}

      {%- endmacro -%}

    {# Call the macro  #}
    {{- cleanup(mother_of_all_macros()) -}}

Where did you found that code because it’s invalid. The is_number function is used incorrectly and && isn’t a valid Jinja2 operator for logical AND.

  • is_number takes only one argument and checks if it’s numeric. You can’t use it to compare with other values.

  • The logical AND operator in Jinja2 is simply the word and.

Hi Taras! I pulled the original code here, and I believe it may have been ported from here.

I follow a few people on the socials and try to re-use what they’ve posted to fit in my environment. Usually, I have to reconfigure code; and in the end it winds up working. But this adventure seems to be the proverbial rabbit hole.

The entire script have elements that are not standard home assistant jinja code.
This might be a script for an updated jinja engine, like GitHub - thomasloven/hass-lovelace_gen: 🔹 Improve the lovelace yaml parser for Home Assistant

The links you posted don’t contain a getUVIndex() macro nor do they contain the invalid Jinja2 code you have in yours. So I don’t know what you led you to believe that what you created has no “obvious errors” but it’s incorrect.

Fix the issues I mentioned in my previous post and the error messages will be eliminated.

Thanks, WallyR and Taras. After reviewing the referenced code, I see they’ve been updated since I last looked at them. One had some dramatic changes made since the last copy. Let me recreate the briefing code and see what I can come up with.

I recreated the briefing yaml and took out the uvindex macro as well. I believe it’s good now.

Since the beginning, I’ve also seen two additional errors requiring repair. I put them aside as I wanted to fix the other code first. But I have two errors here; ‘Good Evening Report uses an unknown service’ and ‘Good Morning Report uses an unknown service’. Both state that the automations call a unknown scripts. However, they are there. I have script: !include_dir_merge_named scripts/ declared in configuration.yaml so it should see them.

The automation "Good Evening Report" (automation.good_evening_report) has an action that calls an unknown service: script.evening_briefing.

This error prevents the automation from running correctly. Maybe this service is no longer available, or perhaps a typo caused it.

To fix this error, edit the automation and remove the action that calls this service.

Click on SUBMIT below to confirm you have fixed this automation

The good evening report is as follows:

- id: '1665734523366'
  alias: Good Evening Report
  description: "Good Evening Report"

  initial_state: true

  trigger:
  - platform: state
    entity_id:
      - binary_sensor.trisensor_motion_detection
    from: "off"
    to: "on"

  condition:
  - condition: time
    after: "19:55:00"
    before: "20:25:00"
  - condition: state
    entity_id: input_boolean.evening_report_done
    state: 'off'

  action:
    - service: script.evening_briefing
    - service: input_boolean.turn_on
      entity_id: input_boolean.evening_report_done

And the evening briefing yaml:

evening_briefing:
  sequence:
  - service: script.speech_engine
    data:
      where: 'living room'
      when: 'evening'
      speech_message: !include ../templates/speech/briefing.yaml

Glad to hear the original problem has been resolved.

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.


Regarding the new issue, go to Developer Tools > Services and enter script.evening_briefing in the list of services. If the script is not in the list then it means it was never loaded by Home Assistant. Let me know if it appears in the list or not.

I restarted again, and after pasting the briefing.yaml code into the template editor, it now says UndefinedError: 'when' is undefined. No other errors though. I assume that means “morning” and “evening” must be defined.

  {%- macro getGreeting() -%}
    {% if now().strftime('%H')|int < 12 %}
      Good morning.
    {% elif now().strftime('%H')|int >= 12 and now().strftime('%H')|int < 17 %}
      Good afternoon.
    {% else %}
      Good evening.
    {% endif %}

    {% if when == 'morning'  %}
        Today is {{states.sensor.today_is.state }}, {{ as_timestamp(now()) | timestamp_custom('%B %d %Y') }}.
    {% else %}
        It is {{ now().strftime("%I:%M %p") }}.

    {% endif %}
  {%- endmacro -%}

Is it safe to assume that the when is defined here in the briefing_evening.yaml file that HA still cannot find?

evening_briefing:
  sequence:
  - service: script.speech_engine
    data:
      where: 'living room'
      when: 'evening'
      speech_message: !include ../templates/speech/briefing.yaml

When I go to Developer Tools > Services and type in either script.evening_briefing or script.morning_briefing nothing comes up. I do, however, see the other yaml files in the dropdown list.