Templating an entity_id

I have ordered myself a couple of NFC tags and start to get familiar with them.
First I created an automation toggling the office light, which worked fine.

Toggle Office Lights
- alias: NFC - Toggle the office light
  id: 'nfc_tag'
  trigger:
  - platform: tag
    tag_id: 08ooob1a-diic-4400-b449-b8xxx7ff07ec

  condition: []

  action:
  - service: light.toggle
    entity_id: light.office

I then extended the automation to check the device_id the NFC tag is read from to distinguish who should get a notification regarding the performed action.

Toggle Office Lights and notify the NFC tag reading device
- alias: NFC - Toggle the office light and send a notification
  id: 'nfc_tag_2'
  variables:
    his_nfc_reader: 9259982bbbbb3xxx #His phone
    her_nfc_reader: aa069aaaa7f0yyy8 #Her phone

  trigger:
  - platform: tag
    tag_id: 08ooob1a-diic-4400-b449-b8xxx7ff07ec

  condition: []

  action:
  - service: light.toggle
    entity_id: light.office
  - service: notify.mobile_app_{{'galaxy_a32' if trigger.event.data.device_id == his_nfc_reader else 'pixel_3a'}}    data:
      title: 'NFC Tag'
      message: The office light has been turned {{ states('light.office') }}
      data:
        channel: nfc
        tag: nfc
  - delay: 00:00:03
  - service: notify.mobile_app_{{'galaxy_a32' if trigger.event.data.device_id == his_nfc_reader else 'pixel_3a'}} 
    data:
      message: clear_notification
      data:
        channel: nfc
        tag: nfc

After successful testing I tried to use euqal templates to set the scanning persons sleep helper to true.

Set person asleep
- alias: NFC - Set Asleep
  id: 'nfc_tag_3'
  variables:
    his_nfc_reader: 9259982bbbbb3xxx #His phone
    her_nfc_reader: aa069aaaa7f0yyy8 #Her phone

  trigger:
  - platform: tag
    tag_id: 08ooob1a-diic-4400-b449-b8xxx7ff07ec

  condition: []

  action:
  - service: notify.mobile_app_{{'galaxy_a32' if trigger.event.data.device_id == his_nfc_reader else 'pixel_3a'}}
    data:
      title: 'NFC Tag'
      message: The office light has been turned {{ states('light.office') }}
      data:
        channel: nfc
        tag: nfc
  - service: input_boolean.turn_on
    entity_id: input_boolean.{{'him' if trigger.event.data.device_id == his_nfc_reader else 'her'}}_asleep 
  - service: notify.mobile_app_{{'galaxy_a32' if trigger.event.data.device_id == his_nfc_reader else 'pixel_3a'}}
    data:
      title: 'NFC Tag Scanned'
      message: Good night {{'his name' if trigger.event.data.device_id == markus_nfc_reader else 'her name'}} have a good sleep
      data:
        channel: nfc
        tag: nfc
  - delay: 00:00:03
  - service: notify.mobile_app_{{'galaxy_a32' if trigger.event.data.device_id == his_nfc_reader else 'pixel_3a'}} 
    data:
      message: clear_notification
      data:
        channel: nfc
        tag: nfc

However doing so I get the following error log:
Invalid config for [automation]: not a valid value for dictionary value @ data['action'][2]['entity_id']. Got None.

Any help is appreciated.

Quote your single line templates and put the entity_id under target

  - service: input_boolean.turn_on
    target:
      entity_id: "input_boolean.{{'him' if trigger.event.data.device_id == his_nfc_reader else 'her'}}_asleep"

You have a bunch of other single line templates there that need to be quoted too.

1 Like

Thanks very much. This helped.

1 Like