Hi everybody,
I am trying to create a card that will allow me to toggle input_boolean
s that specify which entities ought to be triggered by an automation. Please stick with my, I’ll try to explain as well as I can…
input_boolean
s:
input_boolean.festlegen_gartenlicht_daslicht
input_boolean.festlegen_gartenlicht_fluter
input_boolean.festlegen_gartenlicht_lichterkette
I then want these entities to be mapped to actual light
/ switch
entities, so I can include/exclude each one from the automation by toggling them.
However, I cannot figure out the exact jinja2
code for this. The final automation should look something like this
automation:
- id: aaaaa
alias: "Motion activated garden lights"
trigger:
- platform: state
# (...)
action:
- service: homeassistant.turn_on
entity_id: >
{{ template needs to go here }}
I use homeassistant.turn_on
instead of light.turn_on
because two of those entities are actual lights, one is a switch.
This is what I came up with in the templates UI so far, but it won’t work properly.
{% set light_dictionary = [{
"input_boolean.festlegen_gartenlicht_daslicht": "light.daslicht",
"input_boolean.festlegen_gartenlicht_fluter": "light.fluter",
"input_boolean.festlegen_gartenlicht_lichterkette": "switch.draussen_garten_lichterkette"
}] -%}
{%- for state in light_dictionary -%}
{% if state.state == "on" -%}
{{ light_dictionary["state"] }}
{% endif -%}
{%- endfor -%}
Expected behavior:
- check, which of those
input_boolean
s is set to on - return each dictionary_value for those entities with that state
So if input_boolean.festlegen_gartenlicht_fluter
is set to on, I want to return light.fluter
, if it is set to off, I don’t want to return anything. Same goes for those other two input_boolean
s.
To simplify my tests, I also tried this
{% set myd = [{'1': 'eins'}, {'2': 'zwei'}] %}
{{ myd['1'] }}
But it will only return UndefinedError: 'list object' has no attribute '1'
. I then tried other variations, for example
{% set myd = {'1': 'eins'}, {'2': 'zwei'} %}
{{ myd['1'] }}
Which will return UndefinedError: 'tuple object' has no attribute '1'
.
What am I doing wrong? I tried researching how to create a dictionary in jinja2, but must have missed something.
Thank you in advance for your ideas