Stream record friendly name file from entity

My 1st post, so forgive my transgressions. I am also VERY new to HA and especially automation.

I wrote a script to record a camera stream given the entity and duration. I want the filename to use the friendly name instead of the full entity string. I have tried many suggestions but none have worked for whatever reasons. I have listed the latest version. It works except it names the stream “camera.c120_minorStream…” I want it to start “SideYard…” like I see in the logbook message I created. (I also don’t understand the nuances of quotes and spaces and such in the syntax, but that’s another topic.)

alias: Record Stream
sequence:
  - action: logbook.log
    metadata: {}
    data:
      name: RecordStream
      message: RecMessage {{ entity }}
  - action: camera.record
    metadata: {}
    data:
      duration: "{{duration}}"
      lookback: 0
      filename: /media/ext_media/{{entity}}-{{now().strftime("%Y%m%d-%H%M%S")}}.mp4
    target:
      entity_id: "{{ entity }}"
description: Record the specified stream
fields:
  entity:
    selector:
      entity: {}
    name: entity
    description: Stream entity
    required: true
  duration:
    selector:
      number:
        min: 1
        max: 3600
        step: 1
    name: duration
    description: Length of recording
    default: 2

Hello lancej,
Welcome to the HA forums.

One thing I see aside from whether the actual code is correct or not, all templates, things in {{ }} brackets need to be in quotes or proceeded by a designator in the prior line These are > | in certain cases

You have a couple of things that are not.

The next tricky thing is the path name. The path is usually not what you think it is.
Media source - Home Assistant.

Setting up local media sources - Home Assistant.

Thanks for the quick response.

The use ", ', >, | is part of my confusion. I see things done various ways in examples and posts and never really know what to do or exactly what the nuances are. I am having trouble finding a concise language syntax/semantic definition, so a link to one would be great. What I typically find are fairly narrow topics that assume I know everything else – somewhat like learning English from a dictionary.

Anyway, I took you suggestion and enclosed all {{}} in “” (there were only two where they were not already). The only change was the logbook message, which went from “SideYard” (when there were no quotes) to “camera.c120_minorStream” (when I added the quotes). (I didn’t post updated script because the change was small I wanted to avoid clutter. But I will surely post the final script once it works.)

Two things are puzzling (to me) about this. First, what is the semantic definition of the added quotes that caused the logbook message to change (between two “reasonable” values)? And why did the filename not change whether or not the quotes were there?

I read a little more and noodled around with the {{}} stuff and found something that works. I will list it below, but would appreciate telling me if the syntax is correct, especially with respect to the quotes and {{ }} stuff, or if this just works by happenstance. Also, to be clear, is it true that all instances of the {{ }} constructs are Templates (in the sense of Jinja)? And do all the HA references to Templates imply Jinja?

alias: Record Stream
sequence:
  - action: logbook.log
    metadata: {}
    data:
      name: RecordStream
      message: /media/ext_media/{{state_attr(entity,'friendly_name')}}-{{now().strftime('%Y%m%d-%H%M%S')}}.mp4
  - action: camera.record
    metadata: {}
    data:
      duration: "{{duration}}"
      lookback: 0
      filename: /media/ext_media/{{state_attr(entity,'friendly_name')}}-{{now().strftime("%Y%m%d-%H%M%S")}}.mp4
    target:
      entity_id: "{{ entity }}"
description: Record the specified stream
fields:
  entity:
    selector:
      entity: {}
    name: entity
    description: Stream entity
    required: true
  duration:
    selector:
      number:
        min: 1
        max: 3600
        step: 1
    name: duration
    description: Length of recording
    default: 2

Neither of those are quoted, and the path is still wrong.
I’m sorry I can’t help more, I’m traveling and don’t have all my tools at hand.
Look at those links and add what you need to to help with your path for media_source, local, and that stuff.

Best way to start here is do one without templates and get the path working then build a template with ~ in the middle to build a string that is the path, but you have to get the path right first.
If you look in here, I build an action command inside a template to make it vary according to need in the script.
Automations attached to my Youtube Episode 124 https://whatarewefixing.today/1797/tuya-lights-in-home-assistant/ · GitHub.

Thanks again for the help. Your development advice is exactly how I proceeded. I didn’t add templates until I had it working with direct entities. Then I added fields with hard-coded filenames. Then tried to make friendly (shorter) filenames, which is where I got stuck.

I’m not still sure how the path is wrong since the files are being created exactly where I expect them. This might be one of those “just happens to work even though its wrong” things. Still, I would like to understand what is wrong.

Thanks for your input and advice so far and I look forward to hear from you when you return. Meanwhile, the script now does what I wanted, so I can live it with for now.

@Sir_Goodenough, as I said in my last post, the original topic of the thread appears to be working – I able to name my recordings according to the friendly name. And the files are where I expect them to be. But I am still concerned that with your comment that “… the path is still wrong.” When you return, I would be grateful for any details about how it is wrong. Below is a relevant excerpt from my configuration.yaml file.

homeassistant:
  allowlist_external_dirs:
    - "/config/notify"
    - "/config/media"
    - "/media/ext_media"
  media_dirs:
    media: /config/media
    cams: /media/ext_media

Thanks for your insights and advice.

Well if it’s working you are ok then.

I tend to point to here in for calling my mp3 sound calls, for instance:
media-source://media_source/local/mp3
Here mp3 is a lower folder from the /local/.

This is the homeassistant.yaml:

  media_dirs:
    local: /media

The /local/ in the top one is internally replaced by the local: path listed in the bottom one.

For simple paths id complicates things,but for more complex ones…

  media_dirs:
    local: /media
    music: /media/int-sdb1/Music
    movies: /media/int-sdb2/Movies
    tv: /media/int-sdb2/TV

It is helpful. I should just add an mp3 one instead of adding that 50 times in my code, but… priorities…

Thanks for your help and advice, and especially thank you for making your production HA files available on github. One the things I find difficult is getting complete solutions understandable to a complete noob like me. Having a full working system as an example is helpful to overcome the “inside baseball” feeling I often get when reading some of the threads or documentation – somewhat akin to learning a new language by reading a dictionary.

1 Like