HELP! YAML Syntax Changes After Saving/Closing Automation that Plays Local MP3 Audio File on Echo

I am fairly new to automations and YAML and have done a lot of research trying to find an answer to my question. Unfortunately I’ve been unsuccessful therefore I have turned to this community to seek assistance.

My configuration: Home Assistant on a Raspberry PI4 with Home Assistant 2023.3.2; Supervisor 2023.03.1; Operating System 9.5; Frontend 20230306.0 - latest. Sensor is a Sengled Door/Window sensor; Alexa device is an Echo Dot 2nd Gen. My HA configuration includes HACs 1.31.0 with Alexa Media Player v4.6.1 installed.

Overview: I’ve created an automation that notifies me when mail is inserted into my mailbox. Notification comes through my mobile app using the HA Companion App as well as voice notification on my Echo device in my office. The automation was created following instruction from Ed on SmartHomeJunkie. I’ve also referenced Mark Watt Tech for information related to Notify.Alexa and playing local MP3 files on an Alexa device.

Problem: I’ve create the automation in HA using the visual editor and then switched to YAML view to see the YAML code that gets generated. I use the YAML view to fill in some additional detail for the automation as I find it easier to understand than the visual editor. Once I make the necessary updates and save the changes (but NOT exit the YAML view) the automation is able to be run and works as expected and the local MP3 file is played through the designated Echo device. The troubles begin once I close the YAML view/editor. Upon closing the view, the local MP3 file will no longer play and an error is spoken through the Echo device.

What I’ve observed is that when I go back into the editor to view the YAML file for the automation I see the syntax of the YAML file, in particular the line containing the ‘message: audio src=’ has been changed. This is what is causing the local MP3 file to no longer be able to be played – instead the error message is spoken through the device.

I realize changes could be made to have Alexa use TTS and speak a message however my goal is to be able to create additional automations that use messages and sounds in MP3 formatted files that I create. Furthermore, the issue I am experiencing isn’t in regard to getting the MP3 file to play but instead the issue is with how HA is changing the syntax of a ‘working’ automation in the service section calling Notify.Alexa into a format that does not work.

MY QUESTION: Has anyone experienced the ‘reformatting of YAML’ and how did you stop it from happening? I have included a copy of the automation (YAML format) below. Both the working version followed by the ‘non-working, reformatted’ version. I’ve changed the device ID & name and domain name for privacy. Sorry for the length of this post. This is my first post to the group, thanks in advance for the help!

WORKING VERSION

alias: "Mailbox: New mail notification"
description: ""
trigger:
  - type: opened
    platform: device
    device_id: e33333b43ad781f8ce34ef3309b65421
    entity_id: binary_sensor.mailbox_sensor_contact
    domain: binary_sensor
condition: []
action:
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id: input_boolean.mail_arrived
  - service: input_datetime.set_datetime
    data:
      timestamp: "{{ now().timestamp() }}"
    target:
      entity_id: input_datetime.mailbox_last_triggered
  - service: notify.mobile_app_sm_g988u1
    data:
      message: The mail has been delivered.
      title: Mail Delivered.
  - service: notify.alexa_media_jds_echo_dot
    data:
      message: 
        <audio src='https://myhome.duckdns.org/local/alexa_ready_youve-got-mail.mp3'/>
mode: single

NON-WORKING VERSION (syntax changed upon closing yaml in HA)

alias: "Mailbox: New mail notification"
description: ""
trigger:
  - type: opened
    platform: device
    device_id: e33333b43ad781f8ce34ef3309b65421
    entity_id: binary_sensor.mailbox_sensor_contact
    domain: binary_sensor
condition: []
action:
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id: input_boolean.mail_arrived
  - service: input_datetime.set_datetime
    data:
      timestamp: "{{ now().timestamp() }}"
    target:
      entity_id: input_datetime.mailbox_last_triggered
  - service: notify.mobile_app_sm_g988u1
    data:
      message: The mail has been delivered.
      title: Mail Delivered.
  - service: notify.alexa_media_jds_echo_dot
    data:
      message: >-
        <audio
        src='https://myhome.duckdns.org/local/alexa_ready_youve-got-mail.mp3'/>
mode: single

The reformatting is normal for yaml that is not completely manual like in automations.yaml and scripts.yaml. It should not break your automations or scripts.

Are you sure it’s the formatting that’s causing the issue? Both formats work for me… but I have been experiencing random failures of similar automations more than usual over the past week, especially mid-day. From previous forum discussions with the AMP dev, the cause of these issues are on Amazon’s side and not fixable client side.

Thank you for the reply and appreciate the info.!

From all the trial and error I have done it seems as though the break is due to the formatting. If I run the example I’ve labeled as non-working then the error message is spoken through the echo device. If I simply reformat it to match the example labeled working, save the changes and leave the editor open and run it then it works as expected and the MP3 file is spoken (crazy, I know…). It seems as though as soon as I close the YAML editor view and let HA run the automation by triggering the sensor I get the error message through the echo device opposed to the MP3 file. All of my testing has been done mid-day (EST)… I will see if I get different results at various times throughout the day over a greater period of time than the few days since I created the automation.

I don’t have an Alexa device, but try quoting the message:

  - service: notify.alexa_media_jds_echo_dot
    data:
      message: "<audio src='https://myhome.duckdns.org/local/alexa_ready_youve-got-mail.mp3'/>"

Thank you for the suggestion unfortunately, once the editor is closed and then the automation reopened the format of the script is changed and matches that of the script in my original inquiry. As Drew has identified, the ‘reformatting’ is normal and seems to be the case with my automation.

UPDATE
After two weeks of testing, things seem to be working as expected with playing local MP3 files through my Echo device.

I can not explain what the issue was however happy to explain what I had done and the problem seems to have resolved itself. As Didgeridrew mentioned in one response, YAML is automatically reformatted. This is true. Regardless of how many times I edited and saved my automation it was formatted and reformatted accordingly and I could not get it to work. As a last ditch effort, I copied the YAML that was generated for the automation and pasted it into a NEW automation that I created. Saved it… and wah-lah… it worked. Again, not sure why I met this challenge however, simply copying and pasting the YAML into an entirely new automation did the trick! Thanks to all in this community for the help and guidance you provide!