Sweet, thanks for that. That has heaps of useful stuff in it that helps heaps.
So once again thanks heaps for that EdwardTFNEdward Firmo But that doesn’t really answer my original Question.
For anyone else who finds this post. The problem with the above example is that the target selector; result (which can be found in the [Edit in yaml] of the saved script parameters). E.g.
input:
target_lights:
area_id: office
But the template calculation is expecting A [entity_id] not A [area_id]. So the answer to the problem is in either how we handle the input data with the [Variables] or by using another {Selector}.
So if we format the variable input data according to if the user selected {Area, Device or Entity} in the blueprint parameter {target_lights>selector>target>entity>domain: lights}.
input:
target_lights:
name: Targeted Lights
description: The Lights this Script is Targeted at.
selector:
target:
entity:
domain: light
variables:
lights: !input "target_lights"
lights.area_id: "{{ expand ( area_entities ( lights.area_id ) ) | selectattr ( 'domain' , 'eq' , 'light' ) | selectattr ( 'state' , 'eq' , 'on' ) | list | count > 0 }}"
# for entities:
lights.entity_id: "{{ is_state ( lights.entity_id , 'on' ) }}"
# for device:
lights.device_id: "{{ is_state ( device_entities ( lights.device_id ) | first , 'on' ) }}"
# combining them all together so that it can handle missing data:
lights_on: "{% if lights.device_id is defined %} {{ is_state ( device_entities ( lights.device_id ) | first , '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 %}"
- condition:
- "{{ lights_on }}"
Otherwise we choose another [Selector>Target] like [area] rather than [entity] & rewrite half of the script.
Please feel free to correct me if I’m wrong or if you have a better way of doing this?
Edit:
There is a much better way of doing this: WTH! triumph How do you reference A Device’s state (On/Off/unavailable) for A Target Selector?