Formatting Template Condition with user_id

I am attempting to add a Template condition to an automation so I can control when it executes.

This is the automation:

trigger:
  - platform: device
    type: turned_on
    device_id: a4d75dd37051429a97e599a78c0b3a13
    entity_id: light.office
    domain: light
condition:
  - condition: template
    value_template: >-
      "{{ trigger.to_state.context.user_id == 'd70c34161d8d4f7691b8a6566a8443c7'
      }}"
    enabled: true
action:
  - service: script.office_light_brightness
    data: {}
mode: single

This user_id (d70c34161d8d4f7691b8a6566a8443c7) is for me, and the automation should execute only when I turn on the light. Yet the automation never executes.

My formatting of the template is apparently not correct?

You are using the >- line continuation character to indicate the template starts on the next line. Therefore remove the double-quotes wrapping the template. They’re only needed if the template is on the same line as the value_template option.

trigger:
  - platform: device
    type: turned_on
    device_id: a4d75dd37051429a97e599a78c0b3a13
    entity_id: light.office
    domain: light
condition:
  - condition: template
    value_template: >-
      {{ trigger.to_state.context.user_id == 'd70c34161d8d4f7691b8a6566a8443c7'
      }}
    enabled: true
action:
  - service: script.office_light_brightness
    data: {}
mode: single
1 Like

Well that was ready. Thank you!

I entered the template using the Visual Editor in the UI and did not manually enter the “>-”, it is a little strange that it was added automatically…

1 Like