Value Template Syntax

Could someone tell me why this condition does not match.

trigger:
  - platform: state
    entity_id:
      - switch.office_fan
    from: null
    to: null
condition:
  - condition: template
    value_template: >-
      = {{ states.switch.office_fan_state.context.user_id == None and
      states.state.office_fan_state.context.parent_id == None }}
    enabled: true

You have an = character outside the template. Remove it.

This would have been a lot easier for us to spot if you had done this: https://community.home-assistant.io/t/how-to-help-us-help-you-or-how-to-ask-a-good-question/114371#oneone-format-it-properly-16

thank you. I formatted the original post.

should I move the = as removing it I have same results. Thank you so much for the fast response

The template should also be

{{ states.switch.office_fan_state.context.user_id is none and states.switch.office_fan_state.context.parent_id is none }}

Your template refers to states.state.office_fan. Change it to states.switch.office_fan.

condition:
  - condition: template
    value_template: >-
      {{ states.switch.office_fan_state.context.user_id == None and
        states.state.office_fan_state.context.parent_id == None }}
               ^^^^^
    enabled: true

You still should be using is none, not == None

alias: aaa
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.office_fan
condition:
  - condition: template
    value_template: >-
      {{ states.switch.office_fan_state.context.user_id is none and
      states.switch.office_fan_state.context.parent_id is none }}
    enabled: true

does that look correct?

cant get it to trigger. Tried so many combination now that I got the syntax correct with misplaced words

Look at your trace, see what’s going wrong. You likely have to swap to the trigger object’s context.

{{ trigger.to_state.context.user_id is none and  trigger.to_state.context.parent_id is none }}
1 Like

I dont understand the part about the Undefined error I dont have None

{
  "trace": {
    "last_step": "condition/0",
    "run_id": "57873ba1e511f118412fcb338b63c2ad",
    "state": "stopped",
    "script_execution": "failed_conditions",
    "timestamp": {
      "start": "2024-08-06T14:14:10.578386+00:00",
      "finish": "2024-08-06T14:14:10.578694+00:00"
    },
    "domain": "automation",
    "item_id": "1722953622884",
    "trigger": "state of switch.office_fan",
    "trace": {
      "trigger/0": [
        {
          "path": "trigger/0",
          "timestamp": "2024-08-06T14:14:10.578399+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.bbb",
              "state": "on",
              "attributes": {
                "id": "1722953622884",
                "last_triggered": "2024-08-06T14:12:45.572618+00:00",
                "mode": "single",
                "current": 0,
                "friendly_name": "bbb"
              },
              "last_changed": "2024-08-06T14:14:04.162801+00:00",
              "last_reported": "2024-08-06T14:14:04.162801+00:00",
              "last_updated": "2024-08-06T14:14:04.162801+00:00",
              "context": {
                "id": "01J4M0ME42GY7BTA60N51YN7Y7",
                "parent_id": null,
                "user_id": null
              }
            },
            "trigger": {
              "id": "0",
              "idx": "0",
              "alias": null,
              "platform": "state",
              "entity_id": "switch.office_fan",
              "from_state": {
                "entity_id": "switch.office_fan",
                "state": "on",
                "attributes": {
                  "friendly_name": "Office Fan"
                },
                "last_changed": "2024-08-06T14:13:55.575181+00:00",
                "last_reported": "2024-08-06T14:13:55.575181+00:00",
                "last_updated": "2024-08-06T14:13:55.575181+00:00",
                "context": {
                  "id": "01J4M0M5QQ0V1DAFT6EQGADEF4",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "to_state": {
                "entity_id": "switch.office_fan",
                "state": "off",
                "attributes": {
                  "friendly_name": "Office Fan"
                },
                "last_changed": "2024-08-06T14:14:10.577416+00:00",
                "last_reported": "2024-08-06T14:14:10.577416+00:00",
                "last_updated": "2024-08-06T14:14:10.577416+00:00",
                "context": {
                  "id": "01J4M0MMCHC3JQT0M1TQCKEJN6",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "for": null,
              "attribute": null,
              "description": "state of switch.office_fan"
            }
          }
        }
      ],
      "condition/0": [
        {
          "path": "condition/0",
          "timestamp": "2024-08-06T14:14:10.578406+00:00",
          "error": "In 'template' condition: UndefinedError: 'None' has no attribute 'context'"
        }
      ]
    },
    "config": {
      "id": "1722953622884",
      "alias": "bbb",
      "description": "",
      "trigger": [
        {
          "platform": "state",
          "entity_id": [
            "switch.office_fan"
          ],
          "from": null,
          "to": null
        }
      ],
      "condition": [
        {
          "condition": "template",
          "value_template": "{{ states.switch.office_fan_state.context.parent_id is none and states.state.office_fan_state.context.user_id is none }}",
          "enabled": true
        }
      ],
      "action": [],
      "mode": "single"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01J4M0MMCJ10ZNWJDK5FB0WV5S",
      "parent_id": "01J4M0MMCHC3JQT0M1TQCKEJN6",
      "user_id": null
    }
  },
  "logbookEntries": []

Your entity_id is incorrect, which is returning a None object.

Outside that, you have to switch to the trigger object because the state machine does not store context.

I.e. you have to use this template:

ok I give up thank you for your help. if you want to make $20 let me know.

  id: '1722953622884'
  alias: bbb
  description: ''
  trigger:
    - platform: state
      entity_id:
        - switch.office_fan
      from: null
      to: null
  condition:
    - condition: template
      value_template: >-
        {{ trigger.to_state.context.user_id is none and 
        trigger.to_state.context.parent_id is none }}
  action: 
    - #YOUR ACTION
  mode: single

wow that is amazing spent all day on that. works great! Thank you so much everyone!

I’m glad I could “help”… :smile:

FYI, I gave you the template he used 5 hours ago :wink: Not sure why you didn’t use it then.

Dang I guess I just eyeballed it and not copy paste geeze what a terrible student I am. Thank you petro!