Automation state change: event condition

I need help with an automation condition that always returns ‘false’.

I want to have a condition that checks if a value in the triggered event is not set.

A wall switch, how is doing what?

Scenario: I press the wall switch and the state is changed. Run the automation action

Scenario: Home assistant adjust the light and the state is changed. Do not run the automation action

Solution: condition will check if the to_state has a user_id. This will be null if the phyical device is pressed.

My Automation

alias: "Button: Kitchen"
description: ""
### CHECK FOR STATE CHANGE ON THE WALL SWITCH THAT CONTROLS LIGHT
trigger:
  - platform: state
    entity_id:
      - switch.double_switch_2_2
      - switch.double_switch_2
condition:
  ## SHOULD CHECK THAT user_id IS NULL
  - condition: template
    value_template: |-
      {% if trigger.to_state.context.user_id is not defined %}
      true
      {% else %}
      false
      {% endif %}
action:
   #### DO THINGS
mode: restart

Issue:
The condition always returns ‘false’ Guess the value is always null because the ‘path’ is wrong?

Trying to debug:
Trace the automation. Look at the ‘changed variables’. Convert the data to json ( I stripped information not needed). Try dev tools template to check if it works, here it works.

{% set tevent = {
  "this": { },
  "trigger": {
    "from_state": { },
    "to_state": {
      "context": {
        "id": "01HC0CR54978KVE2W0B44DW60E",
        "parent_id": null,
        "user_id": null
      }
    }
  }
} %}

{% if tevent.trigger.to_state.context.user_id is not defined %}
true, it works
{% else %}
false
{% endif %}

Screenshot 2022-06-21 081430

If you mean a script or automation instead of the dashboard interface changes the switch then you need to look at the parent_id instead. The user_id will be none for both your physical and automated change. Though the parent_id being none could be due to a change by the dashboard interface or the physical device.

If you want to distinguish between physical device, or automation or dashboard you need to check all three contexts.

I want to check if the physical devices was used or anything else.

Do I write my template check wrong? Both these tests will fail.

This is the ‘changed variables’ that triggers the automation

# Turn on wallswitch from home asssitant 'devcices'
this:
  context:
    id: 01HC0H2PG0WP6071PJ15SKE414
    parent_id: null
    user_id: null
trigger:
  to_state:
    context:
      id: 01HC0H3N6YET3JXTAG046M9JHV
      parent_id: null
      user_id: 69df44825150485e896ba4e8cc8af97e
	  
# Physically pressed the wallswitch
this:
  context:
    id: 01HC0H2PG0WP6071PJ15SKE414
    parent_id: null
    user_id: null
trigger:
  to_state:
    context:
      id: 01HC0H4JTD51QR787MAWW4T1KA
      parent_id: null
      user_id: null

I’ve updated the template to:

condition: template
value_template: >-
  {% if trigger.to_state.context.user_id is not defined and
  trigger.to_state.context.parent_id is not defined and
  trigger.to_state.context.id is defined %}
  true
  {% else %}
  false
  {% endif %}

Try this

value_template: >
  {{ trigger.to_state.context.user_id == none and
     trigger.to_state.context.parent_id == none and
     trigger.to_state.context.id != none }}

That template condition will only pass for a change made to the physical device.

Same issue, didn’t work :cry:

What does the trace show?

This is the trace from when I pressed the physical device.

Automation is triggered
image

Template running and returns ‘false’
image

The triggered ‘changed variable’

Tried to use checks against null, Didn’t work either :frowning:

condition: template
value_template: |-
  value_template: >
   {{ trigger.to_state.context.user_id == null and
      trigger.to_state.context.parent_id == null and
      trigger.to_state.context.id != null }}

You doubled up on value_template.

If you are entering this via the UI just put this in the template box:

  {{ trigger.to_state.context.user_id == none and
     trigger.to_state.context.parent_id == none and
     trigger.to_state.context.id != none }}
1 Like

Should be:

condition: template
value_template: |-

See my post above.

You are correct… and ‘none’ is the correct value to use as well. Thanks @tom_l

1 Like

I’m posting the full automation yaml,

This is to be used if you want to do an action when a physical wallswitch is pressed. Not when some automation/gui thingy is doing something.

alias: "Button: Kitchen"
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.double_switch_2_2
      - switch.double_switch_2
condition:
  - condition: template
    value_template: |-
      {{ trigger.to_state.context.user_id == none and
         trigger.to_state.context.parent_id == none and
         trigger.to_state.context.id != none }}
action:
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id:
        - input_boolean.kitchen_lock
  - delay:
      hours: 0
      minutes: 45
      seconds: 0
      milliseconds: 0
  - service: input_boolean.turn_off
    data: {}
    target:
      entity_id:
        - input_boolean.kitchen_lock
mode: restart

for another action please refer to the guide posted by @tom_l
post with grid

Here’s another way to do it using a template sensor. It’s only useful if you use the context a lot and don’t want to keep writing out all the conditions: