Trigger null user_id in template condition returns true instead of false

I have an automation in Home Assistant

alias: Populate Terasa TV metadata when user stopped playing
description: ''
trigger:
  - platform: state
    entity_id:
      - media_player.terasa_tv
    from: playing
    to: idle
condition:
  - condition: template
    value_template: >-
      {{ trigger.to_state.context.user_id is defined }}
action:
  - service: input_text.set_value
    data:
      value: 'true'
    target:
      entity_id: input_text.terasa_tv_stopped_by_user
mode: single

The template condition returns true for null values of trigger’s user_id

The trigger is below:

this:
  entity_id: automation.populate_terasa_tv_metadata_when_user_stopped_playing
  state: 'on'
  attributes:
    last_triggered: '2022-06-21T22:19:46.937425+00:00'
    mode: single
    current: 0
    id: '1652250758913'
    friendly_name: Populate Terasa TV metadata when user stopped playing
  last_changed: '2022-06-22T11:00:01.680321+00:00'
  last_updated: '2022-06-22T11:00:01.680321+00:00'
  context:
    id: 01G65H3CGGECPQ7DNQA4FGSH0Q
    parent_id: null
    user_id: null
trigger:
  id: '0'
  idx: '0'
  platform: state
  entity_id: media_player.terasa_tv
  from_state:
    entity_id: media_player.terasa_tv
    state: playing
    attributes:
      volume_level: 0.4000000059604645
      is_volume_muted: false
      media_content_id: http://radio.local:8000/md.plai
      media_duration: 19081.560816
      media_position: 19080.914255
      media_position_updated_at: '2022-06-22T19:24:12.926543+00:00'
      media_title: '[MD] RADIO PLAI'
      app_id: CC1AD845
      app_name: Default Media Receiver
      entity_picture_local: null
      friendly_name: Terasa TV
      supported_features: 152463
    last_changed: '2022-06-22T19:24:12.927085+00:00'
    last_updated: '2022-06-22T19:24:12.927085+00:00'
    context:
      id: 01G66DYJQZSYSWBQ0VB3NEZGFX
      parent_id: null
      user_id: null
  to_state:
    entity_id: media_player.terasa_tv
    state: idle
    attributes:
      volume_level: 0.4000000059604645
      is_volume_muted: false
      media_content_id: http://radio.local:8000/md.plai
      media_duration: 19081.560816
      media_position: 0
      media_position_updated_at: '2022-06-22T19:24:13.602723+00:00'
      media_title: '[MD] RADIO PLAI'
      app_id: CC1AD845
      app_name: Default Media Receiver
      entity_picture_local: null
      friendly_name: Terasa TV
      supported_features: 152463
    last_changed: '2022-06-22T19:24:13.603218+00:00'
    last_updated: '2022-06-22T19:24:13.603218+00:00'
    context:
      id: 01G66DYKD3KJEY3ZGX66D3C8N7
      parent_id: null
      user_id: null
  for: null
  attribute: null
  description: state of media_player.terasa_tv



What would the appropriate way to compare user_id null values?

1 Like
condition:
  - condition: template
    value_template: >
      {{ trigger.to_state.context.user_id != none }}

I have tried something similar and it didn’t work

condition : not
conditions:
  - condition: template
    value_template: '''{{ trigger.to_state.context.user_id == none }}'''
this:
  entity_id: automation.populate_terasa_tv_metadata_when_user_stopped_playing
  state: 'on'
  attributes:
    last_triggered: '2022-06-11T11:35:36.402398+00:00'
    mode: single
    current: 0
    id: '1652250758913'
    friendly_name: Populate Terasa TV metadata when user stopped playing
  last_changed: '2022-06-13T18:05:17.971933+00:00'
  last_updated: '2022-06-13T18:05:17.971933+00:00'
  context:
    id: 01G5F3VKRKCMA85YR6V4BPGETT
    parent_id: null
    user_id: null
trigger:
  id: '0'
  idx: '0'
  platform: state
  entity_id: media_player.terasa_tv
  from_state:
    entity_id: media_player.terasa_tv
    state: playing
    attributes:
      volume_level: 0.33000001311302185
      is_volume_muted: false
      media_content_id: http://radio.moraru.online:8000/md.bucovina
      media_duration: 51128.868571
      media_position: 51122.408977
      media_position_updated_at: '2022-06-14T02:48:18.544342+00:00'
      media_title: '[MD] RADIO BUCOVINA'
      app_id: CC1AD845
      app_name: Default Media Receiver
      entity_picture_local: null
      friendly_name: Terasa TV
      supported_features: 152463
    last_changed: '2022-06-13T12:36:15.035437+00:00'
    last_updated: '2022-06-14T02:48:18.544912+00:00'
    context:
      id: 01G5G1S8VGYJFERVAZBZMXK8F8
      parent_id: null
      user_id: null
  to_state:
    entity_id: media_player.terasa_tv
    state: idle
    attributes:
      volume_level: 0.33000001311302185
      is_volume_muted: false
      media_content_id: http://radio.moraru.online:8000/md.bucovina
      media_duration: 51128.868571
      media_position: 0
      media_position_updated_at: '2022-06-14T02:48:24.819354+00:00'
      media_title: '[MD] RADIO BUCOVINA'
      app_id: CC1AD845
      app_name: Default Media Receiver
      entity_picture_local: null
      friendly_name: Terasa TV
      supported_features: 152463
    last_changed: '2022-06-14T02:48:24.819909+00:00'
    last_updated: '2022-06-14T02:48:24.819909+00:00'
    context:
      id: 01G5G1SEZKNEGCJY0J1VXDEH75
      parent_id: null
      user_id: null
  for: null
  attribute: null
  description: state of media_player.terasa_tv


That’s why I moved away from “!= none” and switched to “is defined”

Triple quoting like that will resolve to the literal string of characters, not a jinja template.

Try this:

value_template: '{{ trigger.to_state.context.user_id == none }}'

Not this:

value_template: '''{{ trigger.to_state.context.user_id == none }}'''

Thanks for the tip with triple quotes, i haven’t noticed it.

I think below are similar, aren’t it?

condition : not
conditions:
  - condition: template
    value_template: '{{ trigger.to_state.context.user_id == none }}'
condition: template
    value_template: '{{ trigger.to_state.context.user_id != none }}'
1 Like

I am trying to create an automation that only triggers when a certain person says the trigger phrase. So far i’m not having much luck. Should I change anything in this automation?
image

Get rid of the nested value_template: >

I deleted it still no luck. When I dig through traces it looks like the user_id is not part of the trigger info.

But on other scripts and automations it does get generated. Here is an example of a script I call with an alias.

I wonder if the user_id only gets through when using an alias?

Sentence triggers do not have to_state as part of their trigger variable so that entire template is invalid.

I haven’t been keeping up closely with Assist and Voice stuff lately, but, AFAIK, user recognition has not been added yet.