L’il Jinja help? How can I get an entity’s domain, such as whether it’s a light vs switch vs binary sensor?

I’m coding a script in YAML, and the script accepts an entity for one of its selectors. And that entity can be either an input_boolean, binary_sensor, light, or switch:

    name: Stop entity
          - input_boolean
          - binary_sensor
          - light
          - switch
    example: input_boolean.stop_everything
    required: false
    advanced: true

Within the body of the script, I’d like to branch based on what that entity’s domain may be—such as whether that entity might be an input_boolean vs binary_sensor—but after much googling, I can’t seem to figure out how to do that.

Does anyone know what Jinja to use to do that—to get the domain of a given entity?

Do you mean to have a condition that tests for the entity and then acts upon the result? In your script you can do a condition template to help with that. Combining that with IF…THEN…ELSE statements should get you over the finish line.

And you can use Jinja to feed your conditions:

{{ entity_name.startswith('input_boolean') }}
1 Like

Since domain is a property of the state object, another option is to use the state object method with bracket notation:

{{ states[stopEntity].domain == 'input_boolean'}}


I think that’ll do exactly what I need, @Didgeridrew—thanks very much for that!

(And thanks also to @CO_4X4—I definitely appreciate your input too!)

PS Just to ask—do either of you happen to have any links handy that spell out these sorts of topologies within Home Assistant, like which objects are children of which other objects and that sort of thing? Because I’d like to learn more about this sort of thing if I could.

State triggers e.g. carry the from and to state objects:

Also look a the states section here:

The hass object (not available everywhere) carries a states object. This is the full state engine. Under that sits the domains followed by the entity ID. From there it depends somewhat on the domain, but you’ll find a state there (the raw state value), as well as your attributes and other properties such as last_changed and friendly_name.

1 Like

@parautenbach That’s helpful info—thanks so much!

1 Like