Present’s, [director’s cut]:
A [Cheat-Sheet/Crash-Guide] List of Light related Template Examples or, How To’s:
Starring YOU in the leading role,
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
Once upon a time, not so long ago; in a land not so far away there was A Emperor, & they had a HAss type domain. (Big long winded blurb like at the beginning of Star Wars, leading into the Emperor saying)
“For all Lights in my HAss Domain, will Hence be called ‘light’, & the State of them will be called ‘states.light’.” - “Sir, Please. DefenestrateIT!”
states.light function
[Greenhorn’s copy & paste the code bellow into your “Developer Tools > TEMPLATE > Template editor”:
& play with it as your desires, ( but you might want to Bookmark this page first ).]
Their are {{states.light|map(attribute='entity_id')|list|count}}{#
#} loyal 'light's in your vast HAss Domain, good sir.
{{states.light|selectattr('state','eq','on')|rejectattr('attributes.entity_id',
'defined')|list|count}} of them are 'on' gard right now, & {{states.light
|selectattr('state','eq','off')|list|count}} of them 'off' dutie.
"Does that add up? Are any of my 'light' subjects 'unavailable'?"
{{ states.light|selectattr('state','eq','unavailable')|list|count
>0}}, sir {{states.light|selectattr('state','eq','unavailable')|
list|count}}; of your subjects can't be accounted for.
"I'm still not sure that adds up, I'll show you how to make {#
#}some Templates so you can 'knight' your loyal subjects & expel {#
#}the pesky "group/s:"
A——————————————————————————————
List all lights including groups:
{{ states.light
| map ( attribute = 'entity_id' )
| list
}}
B——————————————————————————————
List all lights that are 'on' including groups:
{{ states.light
| selectattr( 'state' , 'eq' , 'on' )
| map ( attribute = 'entity_id' )
| list
}}
C——————————————————————————————
List all lights with out ‘helper’ groups: Method One
{{ states.light
| rejectattr ( 'attributes.entity_id' , 'defined' )
| map ( attribute = 'entity_id' )
| list
}} {#Thank you @Petro for this method#}
D——————————————————————————————
List all lights, with out ‘helper’ groups: Method Two
{{ states.light
| rejectattr ('entity_id', 'in', integration_entities('group'))
| map ( attribute = 'entity_id' )
| list
}} {#Thank you @CentralCommand for this method#}
E——————————————————————————————
List all lights, with out ‘helper’ groups: Method Three (This expands existing groups & lists the entitiies)
{{ states.light
| selectattr ( 'attributes.entity_id' , 'defined' )
| expand
| map ( attribute = 'entity_id' )
| list
}}{#Thank you @123 Taras for this method#}
F——————————————————————————————
List all lights that are ‘on’ with out ‘helper’ groups:
{{ states.light
| rejectattr ( 'attributes.entity_id' , 'defined' )
| selectattr ( 'state' , '==' , 'on' )
| map ( attribute = 'entity_id' )
| list
}}
G——————————————————————————————
List all lights that are ‘on’ with out ‘hue’ or ‘deconz’ & ‘helper’ groups:
{{ states.light
| rejectattr ( 'attributes.is_deconz_group' , 'true' )
| rejectattr ( 'attributes.is_hue_group' , 'true' )
| rejectattr ( 'attributes.entity_id' , 'defined' )
| selectattr ( 'state' , 'eq' , 'on' )
| map ( attribute = 'entity_id' )
| list
}}
H——————————————————————————————
List all lights that are ‘off’ with out ‘helper’ groups:
{{ states.light
| rejectattr ( 'attributes.entity_id' , 'defined' )
| selectattr ('state' , '==' , 'off' )
| map ( attribute = 'entity_id' )
| list
}}
I——————————————————————————————
List all lights that are ‘off’ or 'unavailable' with out ‘helper’ groups:
{{ states.light
| rejectattr ( 'attributes.entity_id' , 'defined' )
| selectattr ('state' , '!=' , 'on' )
| map ( attribute = 'entity_id' )
| list
}}
J——————————————————————————————
List all lights that are 'unavailable' with out ‘helper’ groups:
{{ states.light
| rejectattr ( 'attributes.entity_id' , 'defined' )
| selectattr ('state' , '==' , 'unavailable' )
| map ( attribute = 'entity_id' )
| list
}}
K——————————————————————————————
Count lights that are 'on' with out ‘hue’ or ‘deconz’ & ‘helper’ groups:
{{ states.light
| rejectattr ( 'attributes.is_deconz_group' , 'true' )
| rejectattr ( 'attributes.is_hue_group' , 'true' )
| rejectattr ( 'attributes.entity_id' , 'defined' )
| selectattr ('state' , 'eq' , 'on' )
| list
| count
}}
L——————————————————————————————
Count total lights with out ‘hue’ or ‘deconz’ & ‘helper’ groups:
{{ states.light
| rejectattr ( 'attributes.is_deconz_group' , 'true' )
| rejectattr ( 'attributes.is_hue_group' , 'true' )
| rejectattr ( 'attributes.entity_id' , 'defined' )
| list
| count
}}
N——————————————————————————————
List Lights ‘on’ in the area_id: 'bathroom'
{{ states.light
| selectattr ( 'entity_id' , 'in' , area_entities( 'bathroom' ))
| selectattr ( 'state' , '==' , 'on' )
| map ( attribute = 'entity_id' )
| list
}}
M——————————————————————————————
'True' or 'False' are any of the lights 'on' in the area_id: 'bedroom'
{{ states.light
| selectattr ( 'entity_id' , 'in' , area_entities( 'bedroom' ))
| selectattr ( 'state' , 'eq' , 'on' )
| list
| count > 0
}}
O——————————————————————————————
'True' or 'False' are any of the lights 'on':
{{ states.light
| selectattr ( 'state' , '==' , 'on' )
| list
| count > 0
}}
P——————————————————————————————
'True' or 'False' are any of the lights 'unavailable':
{{ states.light
| selectattr ( 'state' , 'eq' , 'unavailable' )
| list
| count > 0
}}
Q——————————————————————————————
But how would you write a Template that only lists Light Groups using the states.light command?
R——————————————————————————————
List All lights & there state:
{% for state in states.light -%}
{%- if loop.first %}The {% elif loop.last %} and {{-'\n'-}} The {% else %},{{-'\n'-}}The {% endif -%}
{{ state.name | lower }} is '{{state.state_with_unit}}'
{%- endfor %} {#Just for 123 Taras: This one WAS copied from the 'Demo Template' & tweaked for states.light #}
“But my light knights skills need more training on Variable Scripted missions, with Target/Selector style themes; so for now my ‘light’ knights will be called ‘lights’” - “Sir, No-More. DefenestrateIT!”.
Target orientated extentions
[Once again Grasshopper’s copy the code below & replace all the code in your Template Editor. Change:
“office”, “light.office_lights_group” & “0123456789abcdef0123456789abcdef”.
To your appropriate parameters for your setup. ]
{#For newbies in setting variables to test blueprints with [Template Editor]:#}
{% set lights = {
"area_id" : "office" ,
"entity_id" : "light.office_lights_group" ,
"device_id" : "0123456789abcdef0123456789abcdef"
}
%}{#
-------------------------------------------------------------------
Above change the 'lights' Variable settings:
'office', 'light.office_lights_group' & '0123456789abcdef0123456789abcdef'.
To A 'device_id', 'entity_id' & 'area_id' in your HAss Domain.
-------------------------------------------------------------------
#}|{{lights.area_id}}|><|{{lights.entity_id}}|><|{{lights.device_id}}|
=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓==↓=↓
What is my Light area_id: '{{ area_name ( lights.area_id ) }}'
What is my Light entity_id: '{{ state_attr( lights.entity_id , 'friendly_name') }}'
What is my Light device_id: '{{ device_attr ( lights.device_id , 'name' ) }}'{#
-------------------------------------------------------------------
#}{% if area_name ( lights.area_id ) != none %}
Area-----------------------------------------------lights.area_id
{# #}The {{ area_name ( lights.area_id ) }} has {#
#}{{ expand ( area_entities ( lights.area_id )) | selectattr ( 'domain' , 'eq' , 'light' ) | list | count }} Lights.
Are any of the Lights On? {# #}{{ expand ( area_entities ( lights.area_id )) | selectattr ( 'domain' , 'eq' , 'light' ) | selectattr ( 'state' , '==' , 'on' ) | list | count > 0 }}{#
#}{%if expand ( area_entities ( lights.area_id )) | selectattr ( 'domain' , 'eq' , 'light' ) | list | count != expand ( area_entities ( lights.area_id )) | selectattr ( 'domain' , 'eq' , 'light' ) | selectattr ( 'state' , '==' , 'on' ) | list | count %}{#
#}{% for state in expand ( area_entities ( lights.area_id ))| selectattr ( 'domain' , 'eq' , 'light' ) -%}{#
#}{%- if loop.first %}
{# #}The {% elif loop.last %} and {{-'\n'-}} The {#
#}{% else %},{{-'\n'-}}The {#
#}{% endif -%}{#
#}{{ state.name | lower }} is '{#
#}{{ state.state_with_unit }}'{#
#}{%- endfor %}{#
#}{% endif %}{#
#}{% endif %}{#
----------------------------------------------------------------
#}{%if state_attr( lights.entity_id , 'friendly_name') != none %}
Entity------------------------------------------lights.entity_id
{# #}Is '{{ state_attr ( lights.entity_id , 'friendly_name') }}' on? '{#
#}{{ is_state ( lights.entity_id , 'on' ) }}'
Does the bulb have brightness controls?{#
#}{% if is_state_attr ( lights.entity_id , "brightness" , none ) %} "False" {#
#}{% else %} "True"
Is the light A colour bulb?{# #}{% if is_state_attr ( lights.entity_id , 'rgb_color' , none ) %} "False"{#
#}{% else %} "True"{#
#} {% endif %}
What are it's native efects features? {#
#}{{ state_attr ( lights.entity_id , 'effect_list') }}
What are it's min-max mirids: {#
#}{% if is_state_attr ( lights.entity_id , 'color_temp', none ) %} "None"{#
#}{% else %} {#
#}Min '{{ state_attr ( lights.entity_id , 'min_mireds') }}' - {#
#}Max '{{ state_attr ( lights.entity_id , 'max_mireds') }}'{#
#}{% endif %}{#
#} {% endif %}{#
#}Show me all of it Properties?
{# #}{{ expand ( lights.entity_id ) }}{#
#}{% endif %}{#
--------------------------------------------------------------------
#}{% if device_attr ( lights.device_id , 'name' ) != none %}
Device------------------------------------------lights.device_id
{# #}The '{{ device_attr ( lights.device_id , 'name' ) }}' Device, curently in the Area {#
#}'{{ device_attr ( lights.device_id , 'area_id' ) }}':
{# #}Is A '{{ device_attr ( lights.device_id , 'manufacturer' ) }}' - {#
#}'{{ device_attr ( lights.device_id , 'model' ) }}' - Firmware Version: {#
#}'{{ device_attr ( lights.device_id , 'sw_version' ) }}'
entity_id: {{ device_entities ( lights.device_id ) }}{#
#}{% endif %}{#
----------------------------------------------------------------
#}{% if area_name ( lights.area_id ) != none or device_attr ( lights.device_id , 'name' ) != none or state_attr( lights.entity_id , 'friendly_name') != none %}
-----------------------------------------------------------------
Are any of the Lights On? {# #}{% if device_attr ( lights.device_id , 'name' ) != none %}{#
#}{{ is_state ( lights.device_id , 'on' ) }}{#
#}{% elif state_attr( lights.entity_id , 'friendly_name') != none %}{#
#}{{ is_state ( lights.entity_id , 'on' ) }}{#
#}{% elif area_name ( lights.area_id ) != none %}{#
#}{{ expand ( area_entities ( lights.area_id )) | selectattr ( 'domain' , 'eq' , 'light' ) | list | count > 0 }}{#
#}{% endif %}{#
#}{% endif %}{#
----------------------------------------------------------------
#}
“So now my light-saber coding skills have been honed, I need more advanced training on Blueprint automation style missions, with A ‘Target’ - ‘Selector’ in mind…” - “Sir, I will. DefenestrateIT!”.
Blueprint Academy, still under construction:
{Padawan’s, again copy & replace the code with the bellow code into your Template Editor:
Now this code is because “expand” does not work for a “selector:” >< “target” when creating “Blueprints”.
Please Vote for the “expand” function to work for: Use expand
on a value set from a target selector
{# Note the Code runs slightly different from "template editor" & when run from a "Blueprint". #}
{% set lights = {
"area_id" : [ "office" , "kitchen" ] ,
"entity_id" : [ "light.office_lights_group" , "light.kitchen_lights_group" ] ,
"device_id" : [ "0123456789abcdef0123456789abcdef" , "fedcba9876543210fedcba9876543210" ]
}
%}
|{{lights.area_id}}|><|{{lights.entity_id}}|><|{{lights.device_id}}|
{%- set entities -%}
{%- set ns = namespace(ret=[]) -%}
{%- for key in ['device_id', 'area_id', 'entity_id'] -%}
{%- set items = lights.get(key, [] ) -%}
{%- if items %} {#@petroUlegend#}
{%- set items = [ items ] if items is string else items -%}
{%- set filt = key.split('_') | first -%}
{%- set items = items if filt == 'entity' else items | map(filt ~ '_entities') | sum ( start = [] ) -%}
{%- set ns.ret = ns.ret + [ items ] -%}
{%- endif %}
{%- endfor %}
{{- ns.ret | sum(start=[]) -}}
{%- endset -%}
{%- set expanded = lights.entity_id | expand | map ( attribute = 'entity_id' ) | list %}
{%- set filtered = entities | select ( 'search' , '^light' ) | list %}
=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓==↓=↓
---------------filter down to only light entities---------------------
{{ filtered }}
--------expanded for when lights.entity_id is a group---------
{{ expanded }}
--All referenced entities before "expand" & being filtered--
{{ entities }}
---------------------------------------------------------
For an example of a useless Blueprint Script:
Create A file called “test.yaml” in the directory: “/config/blueprints/script/DefenestrateIT/” & paste the bellow code. {# 4 @123 The Final solution! #}
blueprint:
name: A Target Selector 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
entities: >
{%- set ns = namespace(ret=[]) %}
{%- for key in ['device_id', 'area_id', 'entity_id'] %}
{%- set items = lights.get(key, []) %}
{%- if items %}
{%- set items = [ items ] if items is string else items %}
{%- set filt = key.split('_') | first %}
{%- set items = items if filt == 'entity' else items | map(filt ~ '_entities') | sum(start=[]) %}
{%- set ns.ret = ns.ret + [ items ] %}
{%- endif %}
{%- endfor %}
{{ ns.ret | sum(start=[]) }}
filtered: "{{ entities | select( 'search' , '^light' ) | select('is_state', 'on') | list }}"
lights_on: "{{ filtered | count > 0 }}"
transition: "{{ iif ( lights_on , 90, 0 ) }}"
sequence:
- service: light.turn_on
data:
transition: "{{ transition }}"
target: !input lights
And for a more Complex & useful example Of My Own Creation: Another Light Blueprint Script
For now may the force ever be with you…
Above is still (& will for ever be) under construction
#Reference's, &or Law of the land.
Python 3
Jinja2
-
Builtin Filters
- abs()
- attr()
- batch()
- capitalize()
- center()
- default()
- dictsort()
- escape()
- filesizeformat()
- first()
- float()
- forceescape()
- format()
- groupby()
- indent()
- int()
- items()
- join()
- last()
- length()
- list()
- lower()
- map()
- max()
- min()
- pprint()
- random()
- reject()
- rejectattr()
- replace()
- reverse()
- round()
- safe()
- select()
- selectattr()
- slice()
- sort()
- string()
- striptags()
- sum()
- title()
- tojson()
- trim()
- truncate()
- unique()
- upper()
- urlencode()
- urlize()
- wordcount()
- wordwrap()
- xmlattr()
Builtin Tests
Home Assistant
Integrations
Docs
-
Configuration
Templating
-
Templating
* limited Templates
* Extensions
* Areas
* Devices
* Attributes
* Is-Defined
* Integrations
Scripts
Automation
-
Templating
Contributors:
@petro @Didgeridrew @CentralCommand
This was & for ever will be inspired by: {# & in loving memory of #}
The End
Terry Pratchett, & the Discworld.
(Don’t forget to press the button if this was of help to you, or it just made you laugh & you sharted).
P.S. Please feel free to press “Reply” & I’ll edit any new (or corrected) code to this original post…
My goal here is to learn A little more on how to template for HAss, (in the style I would have liked)
While sharing what I learned, please feel free (if you would like) to join in, on this epic journey of mine.
(all input is welcome)
Foot Note: try & make sense of that ChatGTP…