Template formating - Best practice and some other questions

In this picture I have two working automations that include some templating.

A) This template is created by editing the YAML file. It looks good there, but are not visible in the automation editor. And I am unsure if it will be saved or lost if I do any changed in the Automation Editor?

B) This is created by input in the Automation Editor, and the template is visible both in editor and in the YAML file. However it is messy and not relay easy to read and understand in the YAML file. This will also be hard to share with other people that may want to dublicate something. How can this be done? And how can it bee cleaned?.

I also see that template in Automation A is inside ’ and '. but the Automation B is inside " and ", why is that?

Which way is prefererd?

This is quite confusing.
From the structure it seems these have been created with the automation editor, the id: randomlongnumber is a giveaway.
All such produced are as visible in yaml as one that is hand crafted.
The editor sometimes put quotes in arbitrarily, I’ve seen single, double and even triple quotes, sometimes where they are not strictly needed.
There are no hard and fast rules but “generally” coders within this forum prefer to have “double quotes” on the outside of jinja and ‘single quotes’ on the inside. You are required (sometimes) to have this so the interpreter can see quoted text as distinguished from keywords and then interpret the whole for output, so “{{ 'hello ’ ~ ‘there’ }}”
BUT
You appear to have 3 automations here, as distinguished by the 3 alias’s
But the 2nd one looks to be from a list (the other 2 dictionary)
This shouldn’t work, the config checker should throw a wobbler.
The preffered way of posting questions with yaml is to post the yaml in preformatted text rather than images (they are huge by comparison) but I can understand why you posted this.
There are a few people here trying to get a handle on the automation editor, so they can help newbies, but the vast majority prefer to use a simple text editor to craft their efforts. The automation editor (for example) will strip out any comments (marked by a leading #) from your code.

Thanks for your reply, here is btw the yaml in text:

Automation A

- id: '1569620878109'
  alias: zigbee_15 - bryter soverom (change color - right)
  trigger:
  - event_data:
      event: 5002
      id: zigbee_15
    event_type: deconz_event
    platform: event
  condition: []
  action:
  - alias: ''
    service: light.turn_on
    data_template:
      entity_id: light.zigbee_soverom
      color_name: '{% if states.light.zigbee_soverom.attributes.rgb_color == (255,0,0)
        %} blue {% elif states.light.zigbee_soverom.attributes.rgb_color == (10,0,255)
        %} green {% elif states.light.zigbee_soverom.attributes.rgb_color == (0,255,0)
        %} purple {% elif states.light.zigbee_soverom.attributes.rgb_color == (253,11,255)
        %} yellow {% else %} red {% endif %}

        '

Automation B

- id: '1570917544192'
  alias: Morgen Rutine
  trigger:
  - platform: template
    value_template: "  {% set alarm_time = as_timestamp(states('input_datetime.flemming_phone_next_alarm_date_and_time'))\
      \ %}\n  {% set time_now = as_timestamp(states('sensor.date_time').replace(',',\
      \ '')) %}\n  {{ time_now >= ( alarm_time - 900 ) }}"
  condition:
  - condition: state
    entity_id: input_boolean.flemming_phone_alarm_set
    state: 'on'
  action:
  - alias: ''
    data: {}
    service: script.wakeup_light_soverom

Both automations are created by the Automation Editor, I do this so it gets an ID. But the template in Automation A is added in the YAML file afterwords.

But automation B does not ???
The alias after action is wrong, you should delete that line, maybe that is your problem ?

Both of them works, and there is no “problem”

so I’m not sure I understand your question.
Do you mean that you can find where automation B is stored but you can’t find automation A ?
Given that you are playing with the AE then it will (AFAIK) only save automations in one of two places.
The actual configuration.yaml or more often in automation.yaml. Both these files live in your config directory.
Have I misunderstood ?

How can this be done? And how can it bee cleaned?.

Simple. Don’t use the automation editor. It’s an abomination.

1 Like

What I think you meant to say is, “… It’s not nearly as feature rich as it probably will be someday.” :wink:

4 Likes

Tom merely tarred and feathered it
Phil Hung it, Drew It, Quartered it … And then Tarred and Feathered it. :rofl:

You can have an “alias” (even an empty one) in the action section too. I don’t see the purpose (other than to confuse things even more) but it is legal and won’t throw a “wobbler”. :wink:

so there truly are only two automations in there.

But I don’t know why (aside from the aforementioned “abomination theory”) the editor would put an “alias” in the action of one automation and not in the other one. :man_shrugging:

If you don’t want to use the automation editor then there is absolutely no benefit to having an “id” in any of your automations. It’s strictly there so that the automation editor can “see” the automation and be able to edit it. It’s not used anywhere else at all.

OTOH, if you still want to use the “id” then just put one in yourself when you write your automation by hand. There is no magic behind the “id”. It’s just a randomly assigned value. You can make it anything you want. And the good thing about that is that you can make it a “not-so-random string of text” so you can even make it human readable so it will make sense to you.

That’s my vote.

Okay, I will take your word for it, I’ve never seen it but after a couple of tragic attempts at using the AE, I decided that pulling my own teeth would be preferable. :nauseated_face:

You don’t need to be using the automation editor to use the alias in the action section. It works using regular yaml in a text editor as well.

Sorry, I’ve gone from confused to completely mystified.
Why would ANYONE want to do that ?

AND why have I got a cake after my name ?

No idea…

I think that means it’s an anniversary for when you joined the forum

Thanks, I at least got one mystery solved (the cake) :crazy_face:

Okey, thanks.
I think I will use the yaml for future automations then

Do you mean why would anyone use the alias option for action steps? To make debugging easier. If you look in home-assistant.log, there are message lines written each time an action step is executed. Without an alias it says something generic, like:

2019-11-04 10:50:20 INFO (MainThread) [homeassistant.components.automation] Executing Foyer Snapshot
2019-11-04 10:50:20 INFO (MainThread) [homeassistant.helpers.script] Script Foyer Snapshot: Executing step call service
2019-11-04 10:50:20 INFO (MainThread) [homeassistant.helpers.script] Script Foyer Snapshot: Executing step call service

But if you add an alias to each step, that will be displayed instead, making it easier to see which step has executed. Same thing goes for sequence steps in scripts.

Honestly, I don’t use them, at least normally. But it can make debugging easier if things aren’t working as expected.

1 Like