Jinja help

Can anyone recommend a tutorial for Jinja where they explain the data structures? None of the tutorials seem to have information on syntax and how to populate structures. For example, i am trying to create a list of device IDs so that i can later iterate through them to determine which device triggered, but i can’t even figure out how to get the data into the list. Thinking Jinja would do substitutions, I’ve tried:

{% set up_bath =
[ 
  device_id("switch.upstairs_bathroom_fan"), 
  device_id("light.upstairs_bathroom_light"),  
  device_id("light.upstairs_bathroom_vanity_light")
]  
%}

and

{% set up_bath =
[ 
  {{ device_id("switch.upstairs_bathroom_fan") }}, 
  {{ device_id("light.upstairs_bathroom_light") }},  
  {{ device_id("light.upstairs_bathroom_vanity_light") }}
]  
%}

the logic part of things is easy, i am just having issues with basic syntax and would welcome any pointers or reading recommendations. Sorry for the super basic question

I am not sure what the issue is…
I have this and seems OK to me, or?
You can clearly see that not all devices have an ID, is that maybe your problem?

1 Like

well, i am not sure what happened, but i moved the code in the dev tools to the very top so i could do a screen capture like yours and magically now it is working…

FYI this also works and it’s less configuring

{% set up_bath = [
   "switch.upstairs_bathroom_fan", 
   "light.upstairs_bathroom_light",  
   "light.upstairs_bathroom_vanity_light"
] | map('device_id') | list %}
2 Likes

thank you! so, aside from years of doing templates, where do you find information such as this so i don’t have to pepper the forum with questions every 5 minutes?

It’s all in the docs but I also keep up with all the template PRs that get delivered.

thank you all for the help, i really appreciate it. maybe you can help me push this over the goal line. I’ve been trying to create an automation to turn on all lights and switches in any of the bathrooms if any of the lights are double tapped. when the device is double tapped, i get the device-id of the device that triggered it. i then put all these into a dictionary so i can determine which bathroom was the one that was triggered. the final step that i can’t really figure out is how to turn on the devices from the device id’s that i have. they could be either switches or lights, so i was hoping I could find a generic turn_on event, but have yet to find it. Is there a way to set the state to on via a template? here is the code i’ve been messing with so far:

#This is the trigger value i expect to receive i hard coded one for testing.  
{% set trigger_id = '1297c3624136259c6d4237f1b9dbd99e' %}

#Set up of the groups
{% set up_bath = [
   "switch.upstairs_bathroom_fan", 
   "light.upstairs_bathroom_light",  
   "light.upstairs_bathroom_vanity_light"
] | map('device_id') | list %}
{% set master_bath = [
   "switch.master_bathroom_fan", 
   "light.master_bathroom_light",  
   "light.master_bathroom_vanity_light"
] | map('device_id') | list %}
{% set guest_bath = [
   "switch.guest_bathroom_fan_2", 
   "light.guest_bathroom_light",  
   "light.guest_bathroom_vanity_light"
] | map('device_id') | list %}

#Load into dictionary
{% set all_baths = { 'up_bath' : up_bath, 
                    'master_bath':   master_bath,  
                     'guest_bath' : guest_bath
}  %}

# loop through dictionary to find the group
{% for key, value in all_baths.items() %}
  {% if trigger_id in value %}
    # found the device, this is the group
    {% for dev in value %}
      #
      # HERE IS WHERE I NEED TO TURN THE DEVICES ON
      # I HAVE THE DEVICE ID FOR EACH OF THE DEVICES
      # 
    {% endfor %}  
  {% endif %}
{% endfor %}

i may have found it!

service: homeassistant.turn_on
data: {}
target:
  area_id: upstairs_bathroom
  1. You can get the Area of the device that’s double-tapped.
  2. Given the Area, you can get all switch and light entities in it.
  3. Given a list of lights and switches, you can determine which ones are currently off and turn them on (using homeassistant.turn_on).

No mapping required (unless you only want to turn on a subset of all lights/switches in a given Area).

1 Like

i had just gone down that route… here’s the automation:

description: ""
mode: single
trigger:
  - platform: event
    event_type: zwave_js_value_notification
    event_data:
      command_class: 32
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ trigger.event.data.value_raw == 0 }}'
        sequence:
          - service: homeassistant.turn_on
            data: {}
            target:
              area_id: '{{ area_name(trigger.entity_id) }}'
      - conditions:
          - condition: template
            value_template: '{{ trigger.event.data.value_raw == 255 }}'
        sequence:
          - service: homeassistant.turn_off
            data: {}
            target:
              area_id: '{{ area_name(trigger.entity_id) }}'

so much simpler!

alias: example 
description: ""
mode: single
trigger:
  - id: 'on'
    platform: event
    event_type: zwave_js_value_notification
    event_data:
      command_class: 32
      value_raw: 0
  - id: 'off'
    platform: event
    event_type: zwave_js_value_notification
    event_data:
      command_class: 32
      value_raw: 25
condition: []
action:
  - service: 'homeassistant.turn_{{ trigger.id }}'
    target:
      area_id: '{{ area_name(trigger.entity_id) }}'

NOTE

I’m making an assumption that the trigger variable for the Event Trigger you created has an entity_id property. Its availability depends on the trigger type. According to the documentation for the Event Trigger, it’s not present.

1 Like

even better than mine. you are right, it does not have an entity_id, it has a device_id, but since all the entities associated with it are in the same area, could i use something like:

"{{ area_name(trigger.device_entities(device_id)[0]) }}"

EDIT: welp, that didn’t work, but maybe pulling the entity_id as avariable?

action:
  - variables:
      trigger_ent_id: "{{ device_entities(trigger.event.data.device_id)[0] }}"
  - service: homeassistant.turn_{{ trigger.id }}
    target:
      area_id: "{{ area_name(trigger_ent_id }}"
mode: single

welp, that didn’t work either… strange because in the templates, it works. This template:

{% set device_id = "1297c3624136259c6d4237f1b9dbd99e" %}
Device entities:           {{ device_entities(device_id) }}
First Device entity:       {{ device_entities(device_id)[0] }}
Area:                      {{ area_name(device_entities(device_id)[0]) }}

Produces these results:

Device entities:           ['switch.upstairs_bathroom_light', 'sensor.upstairs_bathroom_light_node_status', 'button.upstairs_bathroom_light_ping', 'light.upstairs_bathroom_light']
First Device entity:       switch.upstairs_bathroom_light
Area:                      Upstairs Bathroom

this is what the event produces:

event_type: zwave_js_value_notification
data:
  domain: zwave_js
  node_id: 7
  home_id: 3311958128
  endpoint: 0
  device_id: a71d760281a3963644128c4f4d8b2baf
  command_class: 32
  command_class_name: Basic
  label: Event value
  property: event
  property_name: event
  property_key: null
  property_key_name: null
  value: 0
  value_raw: 0
origin: LOCAL
time_fired: "2022-12-09T01:54:12.988371+00:00"
context:
  id: 01GKT9E5BWB1NBMVERE8CY80ZJ
  parent_id: null
  user_id: null

ok, thank you very much @123, @vingerha, and @petro… i think i am getting the hang of this and all your help has definitely made it easier… here is the final and WORKING automation:

alias: Double taps
description: ""
trigger:
  - id: "off"
    platform: event
    event_type: zwave_js_value_notification
    event_data:
      command_class: 32
      value_raw: 0
  - id: "on"
    platform: event
    event_type: zwave_js_value_notification
    event_data:
      command_class: 32
      value_raw: 255
condition: []
action:
  - service: homeassistant.turn_{{ trigger.id }}
    target:
      area_id: "{{ area_id(device_entities(trigger.event.data.device_id)[0]) }}"
mode: single