Omitting 'then' block in triggers

Both below configs work as intended. Is there any difference defining with/without then: block?

With then: block:

esphome:
  on_boot:
    then:
      - light.control:
          id: light_1
          state: off
          brightness: 50%
      - light.control:
          id: light_2
          state: off
          brightness: 50%

Without then: block:

esphome:
  on_boot:
    - light.control:
        id: light_1
        state: off
        brightness: 50%
    - light.control:
        id: light_2
        state: off
        brightness: 50%

Both notations will produce the exact same code.
You need to use the then when you have configuration variables.

Eg, following would produce malformed yaml. Therefore that extra then level would then be needed. Just for best practice: always use the then, that way it’s much easier to extend later with config values.

esphome:
  on_boot:
    priority: 100
    - light.control:
        ...
1 Like

Ahh thanks for the apt answer! Visualising it in json syntax makes sense now:

array only (valid):

{
  []
}

with then (valid):

{
  key: value,
  then: []
}

without then (invalid as all child elements of a dict needs to be defined as a key:value pair):

{
  key: value,
  []
}
1 Like