WTH! 😤 Howto: reference A Device/Area's state (On/Off/unavailable) with A "Target Selector"?

The following will give you the required data, for a device_id but how do you scrape the current light state out of A Target selector when filtered down to a single entity?
Please vote for this Use expand on a value set from a target selector

{{ expand ( device_entities ( lights.device_id ) ) }}

I would image it would be something like:

{{ is_state ( device_entities ( lights.device_id ) | string , 'on' ) }}

This is from the perspective of creating a blueprint, with A input (“lights”) Target selector & the user selects A Device E.g.

blueprint:
  name: A Test 
  description: "A Variable State Test for Target Selector input"
  domain: script

  input:
    lights:
      name: Targeted Lights
      description: The Lights this Script is Targeted at.
      selector:
        target:
          entity:
            domain: light

variables:
  lights: !input lights
  lights_on: >-
    {% if lights.device_id is defined %} {{ is_state ( device_entities (
    lights.device_id ) | string , 'on' ) }} {% elif lights.entity_id is
    defined %} {{ is_state ( lights.entity_id , 'on' ) }} {% else %} {{ expand
    ( area_entities ( lights.area_id ) ) | selectattr ( 'domain' , 'eq' ,
    'light' ) | selectattr ( 'state' , 'eq' , 'on' ) | list | count > 0 }} {%
    endif %}
  transition: "{% if lights_on  %} 90 {%else%} 0 {%endif%}"

sequence:
  - service: light.turn_on
    data:
      transition: "{{ transition }}"
    target: !input lights

:sob: :sob: :sob: :sob: :sob: :sob: :sob: :sob: :sob: :sob: :sob: :sob: :sob:

Steer away from relying of device IDs. It’s not recommended for many reasons I’m too lazy to repeat, but to mention one, it’s a hassle should devices be replaced. Use entities and their states. HA is very much designed to work best using that.

1 Like

Yes I don’t disagree with you, but there are also advantages to using device as well.
Like A device is only ever one device, while an entity can be a group of devices (helpers). & this makes a big difference when designing blueprints E.g. trying to get the colour state doesn’t work very well on a group.
But the main objective here is to figure out how to write A

I am just replying to my own post so it gets bumped. If someone could help me with this I would be eternally grateful :pray:

You already have a solution, you’re not using it. You’re going to need the entity_id anyways to get the state from the device, it’ll just be extra code that literally does nothing because you’ll just be matching it to the entity_id.

Use the entity_id.

But I couldn’t figure out how to revert the output from a list?

{{ is_state ( device_entities ( lights.device_id ) | first , 'on' ) }}

But thank you very much for the nudge :pray:

What happens when there’s no lights on the device? what happens when there’s 2 lights on the device? If you’re making this for other people, that will not work, you’ll need the entity_id.

What do you mean by this? The target selector filters for only devices in the light domain?

I have thought about this, I think it is a limitation to the target selector that you can’t limit multiples (i think). but that will be my next challenge to deal with multiples (but devices should be safe because it is only ever going to react to the first one & not thro a error).

I mainly just want areas &or entities, as that is what I am normally targeting light scripts at. E.g.
https://community.home-assistant.io/t/how-to-check-the-current-on-off-state-from-an-areas-lights-in-a-blueprint-using-selector-target-entity-domain-lights/472256?u=defenestrateit

I don’t want two input selectors & i needed to deal with the fact that the user could select a device.

Simply use a light selector. Don’t bother with devices. What about users who use yaml? Cant create a device in yaml, making the blueprint useless for them. However entities work regardless.

Sorry for my ignorance, but what selector?
Can you give me an example?

Entity selector

But I like using area. I have a lot of lights in each area, & it’s easier to maintain. But sometimes i want to add an extra entity from another area E.g. “Back Garden” & the “Main light” from the “Patio”. Or I want to use the same script for each room, but I also want to use the same code for a single entity.

Devices is not that necessary, but the code has a huge whole in it if you don’t deal with the device option.

What? How so. You can get all that information from the entity_id…. You don’t need devices for any of that.

You know so much more than I about all of this stuff, but unless I have something fundamentally wrong, A target selector gives the user 3 options, “Area”, “Entity” & “Device”.

The idea behind that is to apply settings to all effected entities/devices. Not a single entity within a device. If you’re trying to filter to a single entity, your logic is flawed.

Why would it be flawed? I would think that if the user chooses the device option, they would be selecting A device that is a light? I realise that some devices may have multiple light entities (&or entities that are not A light) but 9/10 they would be selecting A bulb, (& mainly I just want the one selector to select either A “Area” or A “Entity” but needed to deal with the device option).

Sorry for my ignorance, but I have only been looking at this stuff for about 2 months now. But I very much appreciate you sharing your knowledge with me.

If you can show me a better way of doing this (target at either an area &or A entity), or A better way of how to deal with the device option I would very much appreciate it?

You aren’t sharing your endgoal. You’re asking a question that’s narrow sighted and expecting an answer for it. An area and a device can have more than 1 entity associated with it. The target selector can select multiples of everything. That means you should be tailoring your templates and actions to support more than 1 entity. Otherwise, use a different selector.

How would you suggest that I say that? I thought I did by saying:

I know I am the one wrong here, as no one understood what I was trying to do. Maybe I could have said I want the options of either area &or entity?

I dealt with that,

{{ expand ( area_entities ( lights.area_id ) ) | selectattr ( 'domain' , 'eq' , 'light' ) | selectattr ( 'state' , 'eq' , 'on' ) | list | count > 0 }}

I actually said this in my first post:
https://community.home-assistant.io/t/how-to-check-the-current-on-off-state-from-an-areas-lights-in-a-blueprint-using-selector-target-entity-domain-lights/472256/5?u=defenestrateit

@petro please don’t get annoyed with me, you have been such a huge help, & i very much appreciate your time, but I am still only a novice at this; & thus have some very stupid perceptions.

State what your endgoal is, not “how do I get the state of a device”. What do you plan on doing with the entities on the device?

So your script takes a target with lights. If any of the lights are on then it turns them off. If all of them are off it turns them on.

Sounds like a light group, why not just make one of those?