What is the right way to specify an entity in an action?

This is not an important question, so please answer all the other questions from all the other people first. :wink:

I’m reorganizing some Home Assistant automations and I wasn’t sure how to specify the entity. I did some testing and I found that all of these work perfectly fine!

Variant 1: On action root

action:
  - service: climate.set_hvac_mode
    entity_id: climate.th_office
    data:
      hvac_mode: "auto"

Variant 2: Nested under target

action:
  - service: climate.set_hvac_mode
    target:
      entity_id: climate.th_office
    data:
      hvac_mode: "auto"

Variant 3: Nested under data

action:
  - service: climate.set_hvac_mode
    data:
      hvac_mode: "auto"
      entity_id: climate.th_office

So my questions are:

  1. Which variant is the most idiomatic?
  2. Why do all of these work?
  3. Is there a reason to prefer one over the others?
1 Like

I think Petro (mod) once gave quite a complete answer, but I can’t find it now. Maybe he sees this and chimes in.

Basically: Legacy.

Slightly longer answer: It evolved a bit over time, so there are a number of variants, depending, as you’ve observed.

I can’t speak for each and every service call and integration, but generally speaking your entity ID is the target and the rest is just additional data, like passing parameters to a function (if you’re familiar with programming).