Should entity_id be in or out of 'data:'

Hello everyone. I have been a ‘reader’ of this forum for a long, long time and you guys helped me tremendously when I was setting my Home Assistant for the first time. There is one thing I am struggling to understand the difference. Could you help me understand the difference between this:

- id: midnight_turn_off
  alias: Turn things off at Midnight
  initial_state: True
  trigger:
    platform: time
    at: '00:00:00'
  action:
    service: light.turn_off
    data:
      entity_id: light.christmas_lights

and this:

- id: midnight_turn_off
  alias: Turn things off at Midnight
  initial_state: True
  trigger:
    platform: time
    at: '00:00:00'
  action:
    service: light.turn_off
    entity_id: light.christmas_lights

note the difference in location where entity_id is located. They both seem to work but I would like to know what is the difference in execution and what data: is used for exactly.

Thank you for your help.

They are both the same so far as homeassistant is concerned, the second one is basically a shortcut that you can use to make your code neater.

You have a service, and you have data that is passed to the service. So if you’re switching a light on and setting the colour to red then you are saying

service: light.turn_on 
data:
  entity_id: light.cabinet
  color: red

But lots of times services only need an entity_id (and more often than not do need an entity_id) , so to be neater you can shortcut it by just putting entity_id at the same level as the service call.

1 Like