Data_template help please

Here for more help again…

I’m trying to do something like the below but I get an error on the config check. I’ve looked all the ways I can think of to figure out why this isn’t working but I’m having no luck at all.

action:
  - service: camera.snapshot
    #data:
    #  entity_id: camera.living_room
    #  filename: /config/www/snapshot/livingroom_alarm_1.jpg
    data_template: >-
      {% if trigger.entity_id == 'binary_sensor.door_window_sensor_1_sensor' %}
        entity_id: camera.kitchen
        filename: /config/www/snapshot/kitchen_cam_1.jpg
      {% elif trigger.entity_id == 'binary_sensor.door_window_sensor_2_sensor' %}
        entity_id: camera.hallway
        filename: /config/www/snapshot/hallway_cam_1.jpg
      {% elif trigger.entity_id == 'binary_sensor.door_window_sensor_3_sensor' %}
        entity_id: camera.diningroom
        filename: /config/www/snapshot/diningroom_cam_1.jpg
      {% elif trigger.entity_id == 'binary_sensor.door_window_sensor_4_sensor' %}
        entity_id: camera.livingroom
        filename: /config/www/snapshot/livingroom_cam_1.jpg
      {% elif trigger.entity_id == 'sensor.sunroom_east_pe_beam' %}
        entity_id: camera.diningroom
        filename: /config/www/snapshot/diningroom_pe_1.jpg
      {% endif %}

the commented out part works but only for that single camera.

What I want to do is take snapshots from different cameras depending on the trigger that caused it and then save the snapshot to a different file name (again depending on the trigger).

Here is the error:

Invalid config for [automation]: expected a dictionary for dictionary value @ data['action'][0]['data_template']. Got None. (See /config/configuration.yaml, line 304). Please check the docs at https://home-assistant.io/components/automation/

Any help with this please?

Try %- and -%…

I was having a devil of a problem with data templates earlier in the week and that solved it. I just browsed the examples people had posted on github till I found one that worked. It was one by Arsaboo I thing where I saw that and it worked.

Thanks for the try but I still got the same error :frowning_face:

is trigger.entity_id valid?
One of the things I did was used the template editor until I knew I had got it right. Mine look like this:

  - service: light.turn_off
    data_template:
      entity_id: >-
        {%- if (as_timestamp(now()) - as_timestamp(states.light.lounge.last_changed) < 10) and (as_timestamp(now()) - as_timestamp(states.light.spots.last_changed) < 10) -%}
        light.lounge, light.spots
        {%- elif (as_timestamp(now()) - as_timestamp(states.light.office.last_changed) < 10) and (as_timestamp(now()) - as_timestamp(states.light.spots.last_changed) < 10) -%}
        light.office, light.spots
        {%- elif (as_timestamp(now()) - as_timestamp(states.light.spots.last_changed) < 10) -%}
        light.spots
        {%- elif (as_timestamp(now()) - as_timestamp(states.light.office.last_changed) < 10) -%}
        light.office
        {%- elif (as_timestamp(now()) - as_timestamp(states.light.lounge.last_changed) < 10) -%}
        light.lounge
        {%- else -%}
        light.dining
        {%- endif -%}

Which is different to you. Just need to play with it till you get it right or look for someone else’s example… or wait till someone else comes along here…

1 Like

I think so… :thinking:

I’m pretty sure it is from the ‘template’ section here:

I haven’t tried but I think I could split it up into a data template for the ‘entity_id:’ like you have it and it would probably be OK for that part but then I’m not sure how to handle the different ‘filename:’ parts based on the trigger too.

I found an example of something similar in another thread that used a different syntax for this but that didn’t work either.

data_template: >
  {% if trigger.event.data[‘entity_id’] == 'binary_sensor.door_window_sensor_1_sensor' %}
    entity_id: camera.kitchen
    filename: /config/www/snapshot/kitchen_cam_1.jpg
  {% elif trigger.event.data[‘entity_id’] == 'binary_sensor.door_window_sensor_2_sensor' %}
    entity_id: camera.hallway
    filename: /config/www/snapshot/hallway_cam_1.jpg
.
.

I just wonder if I am allowed to use two lines inside the different “if” statements? I’ve looked and haven’t found any examples of it but nothing that says you can’t either.

perhaps not…

What if you tried entity_id: , Filename: on the one line?
Also maybe use quotes??

The people who can really help are @petro and @pnbruckner

Tried:

data_template: >
  {%- if trigger.entity_id == 'binary_sensor.door_window_sensor_1_sensor' -%}
    entity_id: camera.kitchen, filename: /config/www/snapshot/kitchen_cam_1.jpg
  {%- elif trigger.entity_id == 'binary_sensor.door_window_sensor_2_sensor' -%}
    entity_id: camera.hallway, filename: /config/www/snapshot/hallway_cam_1.jpg
  .
  .

Still the same result.

Where are you suggesting I try to put the quotes?

wrapping the “entity_id: camera.hallway”, "filename
Or maybe the whole line
or maybe that line needs to be in {}?

If you use the template editor to try things it’s way less painful than constantly restarting… Eventually you’ll crack it

Could also try the support on Discord…

1 Like

So, unfortunately, what you are trying to do is not possible with yaml. You gotta have 2 templates, one for entity_id, one for filename. Super annoying but here is something that should work:

action:
  - service: camera.snapshot
    data_template: 
      entity_id: >
        {% set mapper = {
          'binary_sensor.door_window_sensor_1_sensor':'camera.kitchen',
          'binary_sensor.door_window_sensor_2_sensor':'camera.hallway',
          'binary_sensor.door_window_sensor_3_sensor':'camera.diningroom',
          'binary_sensor.door_window_sensor_4_sensor':'camera.livingroom',
          'sensor.sunroom_east_pe_beam':'camera.diningroom' } %}
        {{ mapper[trigger.entity_id] }}
      filename: >
        {% set mapper = {
          'binary_sensor.door_window_sensor_1_sensor':'/config/www/snapshot/kitchen_cam_1.jpg',
          'binary_sensor.door_window_sensor_2_sensor':'/config/www/snapshot/hallway_cam_1.jpg',
          'binary_sensor.door_window_sensor_3_sensor':'/config/www/snapshot/diningroom_cam_1.jpg',
          'binary_sensor.door_window_sensor_4_sensor':'/config/www/snapshot/livingroom_cam_1.jpg',
          'sensor.sunroom_east_pe_beam':'/config/www/snapshot/diningroom_pe_1.jpg' } %}
        {{ mapper[trigger.entity_id] }}

Now if you are feeling saucy, you could try json to get it working the way you originally tried. I’ve heard it works, but never tried it myself.

Here’s what it might look like (Disclaimer, this may not work):

action:
  - service: camera.snapshot
    #data:
    #  entity_id: camera.living_room
    #  filename: /config/www/snapshot/livingroom_alarm_1.jpg
    data_template: >
      {% if trigger.entity_id == 'binary_sensor.door_window_sensor_1_sensor' %}
        {'entity_id': 'camera.kitchen', 'filename': '/config/www/snapshot/kitchen_cam_1.jpg'}
      {% elif trigger.entity_id == 'binary_sensor.door_window_sensor_2_sensor' %}
        {'entity_id': 'camera.hallway', 'filename': '/config/www/snapshot/hallway_cam_1.jpg' }
      {% elif trigger.entity_id == 'binary_sensor.door_window_sensor_3_sensor' %}
        {'entity_id': 'camera.diningroom', 'filename': '/config/www/snapshot/diningroom_cam_1.jpg'}
      {% elif trigger.entity_id == 'binary_sensor.door_window_sensor_4_sensor' %}
        {'entity_id': 'camera.livingroom', 'filename': '/config/www/snapshot/livingroom_cam_1.jpg'}
      {% elif trigger.entity_id == 'sensor.sunroom_east_pe_beam' %}
        {'entity_id': 'camera.diningroom', 'filename': '/config/www/snapshot/diningroom_pe_1.jpg'}
      {% endif %}
3 Likes

thanks!

I’ll try both when I get another chance and let you know how it works.

I’m sure at least one way will work so I’ll give you solution point now. :smile:

Strangely, in playing around with different iterations the following didn’t give me any config errors:

data_template:
  entity_id: >-
    {%- if trigger.entity_id == 'binary_sensor.door_window_sensor_1_sensor' -%}
      camera.kitchen
      filename: /config/www/snapshot/kitchen_cam_1.jpg
    {%- elif trigger.entity_id == 'binary_sensor.door_window_sensor_2_sensor' -%}
      camera.hallway
      filename: /config/www/snapshot/hallway_cam_1.jpg
    {%- elif trigger.entity_id == 'binary_sensor.door_window_sensor_3_sensor' -%}
      camera.diningroom
      filename: /config/www/snapshot/diningroom_cam_1.jpg
    {%- elif trigger.entity_id == 'binary_sensor.door_window_sensor_4_sensor' -%}
      camera.livingroom
      filename: /config/www/snapshot/livingroom_cam_1.jpg
    {%- elif trigger.entity_id == 'sensor.sunroom_east_pe_beam' -%}
      camera.diningroom
      filename: /config/www/snapshot/diningroom_pe_1.jpg
    {%- endif -%}

I haven’t tried it to see if it ACTUALLY works (I doubt it will but you never know…) but it surprisingly didn’t complain about it.

That only passes the config test because you have an item inside the data_template. All the config checker cares about is that fields have the correct shape. before when you didn’t have entity_id after data_template:, the config checker was giving an error (basically) saying that it expected dictionary keys inside data_template. Now you have that, but it still won’t actually work when executing because you are feeding the entity_id attribute the following string:

"camera.kitchen/r/nfilename: /config/www/snapshot/kitchen_cam_1.jpg"

/r/n is carriage return. You’ll also note that you definitely don’t have that long ass string as an entity_id anywhere. So when the automation executes, you may get an error stating that it cannot find an entity_id.

That makes sense.

Thanks again.

As an FYI to finalize this part I tried both ways above and the “mapper” technique worked perfectly. The second way with the entity_id & filename on the same statement still gave me config errors.

Just as additional info, do triggers pass through from automation to any children scripts that are called from the automation?

As an example if I used the automation trigger to then call a script would I be able to use the “mapper” values in the script from the trigger in the automation?

kind of like:

  automation:
    - alias: Trespass Warning
      trigger:
        - platform: state
          entity_id: input_boolean.bool_7
          to: 'on'
        ## kitchen door
        - platform: state
          entity_id: binary_sensor.door_window_sensor_1_sensor
          to: 'on'
        ## main bath window
        - platform: state
          entity_id: binary_sensor.door_window_sensor_2_sensor
          to: 'on'
        ## sunroom door
        - platform: state
          entity_id: binary_sensor.door_window_sensor_3_sensor
          to: 'on'
        ## foyer door
        - platform: state
          entity_id: binary_sensor.door_window_sensor_4_sensor
          to: 'on'
        - platform: state
          entity_id: sensor.sunroom_east_pe_beam
          to: 'OFF'
      condition:
        condition: and
        conditions:
          - condition: state
            entity_id: input_boolean.bool_6
            state: 'on'
      action:
        - service: script.turn_on
          entity_id: script.snapshot


  script:
    sequence:
      - service: camera.snapshot
          data_template: 
            entity_id: >
              {% set mapper = {
                'binary_sensor.door_window_sensor_1_sensor':'camera.kitchen',
                'binary_sensor.door_window_sensor_2_sensor':'camera.hallway',
                'binary_sensor.door_window_sensor_3_sensor':'camera.dining_room',
                'binary_sensor.door_window_sensor_4_sensor':'camera.living_room',
                'sensor.sunroom_east_pe_beam':'camera.diningroom' } %}
              {{ mapper[trigger.entity_id] }}
            filename: >
              {% set mapper = {
                'binary_sensor.door_window_sensor_1_sensor':'/config/www/snapshots/kitchen_cam_1.jpg',
                'binary_sensor.door_window_sensor_2_sensor':'/config/www/snapshots/hallway_cam_1.jpg',
                'binary_sensor.door_window_sensor_3_sensor':'/config/www/snapshots/diningroom_cam_1.jpg',
                'binary_sensor.door_window_sensor_4_sensor':'/config/www/snapshots/livingroom_cam_1.jpg',
                'sensor.sunroom_east_pe_beam':'/config/www/snapshots/diningroom_pe_1.jpg' } %}
              {{ mapper[trigger.entity_id] }}

trigger stuff doesn’t, but there is a solution (I also simplified your automation). You need to create the script to use variables. It’s really simple, everyone over complicates it. All you need to do is use a made up variable inside a script. Then inside data_template, set that name equal to something. So in your example the script is using a made up variable: snapshot_entity_id. That is passed in the automation to the script.

  automation:
    - alias: Trespass Warning
      trigger:
        - platform: state
          entity_id: 
            - input_boolean.bool_7
              ## kitchen door
            - binary_sensor.door_window_sensor_1_sensor
              ## main bath window
            - binary_sensor.door_window_sensor_2_sensor
              ## sunroom door
            - binary_sensor.door_window_sensor_3_sensor
              ## foyer door
            - binary_sensor.door_window_sensor_4_sensor
          to: 'on'
        - platform: state
          entity_id: sensor.sunroom_east_pe_beam
          to: 'OFF'
      condition:
        - condition: state
          entity_id: input_boolean.bool_6
          state: 'on'
      action:
        - service: script.snapshot
          data_template:
            #place your variables here
            snapshot_entity_id: "{{ trigger.entity_id }}"

Thes script needs a check because you have some triggers that arn’t in your map. You don’t want errors, so you now need to verify that what triggered this is actually mapped to a camera snapshot. I.E. if you turn on input_boolean.bool_7, it won’t cause errors. If you remove that boolean, the condition isn’t needed because the trigger entity_id’s match what’s in the mapper.

snapshot script:

  script:
    snapshot:
      sequence:
        - condition: template
          value_template: >
            {% set snapshot_sensors = [
                  'binary_sensor.door_window_sensor_1_sensor',
                  'binary_sensor.door_window_sensor_2_sensor',
                  'binary_sensor.door_window_sensor_3_sensor',
                  'binary_sensor.door_window_sensor_4_sensor',
                  'sensor.sunroom_east_pe_beam'] %}
            # Is the entity_id inside the sensor map that displays the snapshot?
            # yes = move forward, no = stop
            {{ snapshot_entity_id in snapshot_sensors }}
        - service: camera.snapshot
            data_template: 
              entity_id: >
                {% set mapper = {
                  'binary_sensor.door_window_sensor_1_sensor':'camera.kitchen',
                  'binary_sensor.door_window_sensor_2_sensor':'camera.hallway',
                  'binary_sensor.door_window_sensor_3_sensor':'camera.dining_room',
                  'binary_sensor.door_window_sensor_4_sensor':'camera.living_room',
                  'sensor.sunroom_east_pe_beam':'camera.diningroom' } %}
                {{ mapper[snapshot_entity_id] }}
              filename: >
                {% set mapper = {
                  'binary_sensor.door_window_sensor_1_sensor':'/config/www/snapshots/kitchen_cam_1.jpg',
                  'binary_sensor.door_window_sensor_2_sensor':'/config/www/snapshots/hallway_cam_1.jpg',
                  'binary_sensor.door_window_sensor_3_sensor':'/config/www/snapshots/diningroom_cam_1.jpg',
                  'binary_sensor.door_window_sensor_4_sensor':'/config/www/snapshots/livingroom_cam_1.jpg',
                  'sensor.sunroom_east_pe_beam':'/config/www/snapshots/diningroom_pe_1.jpg' } %}
                {{ mapper[snapshot_entity_id] }}
1 Like

HI @petro
using this technique myself here, with, as you adviced before, the ‘else’ to prevent errors in the outcome.
Maybe thats superflous since already tested in the value_template condition, but let me ask anyway:

would you still need the else for security?

  - alias: Announce activity selection
    id: 'Announce activity selection'
   #hide_entity: True
   #initial_state: 'on'
    trigger:
      platform: state
      entity_id: input_select.activity
    condition:
      - condition: template
        value_template: >
          {{ states('input_select.activity') in 
              ['Opstaan','Aan de slag','Home theater',
               'Gym', 'Gaming', 'Selamat makan', 'Naar bed'] }}
      - condition: template
        value_template: >
          {{ is_state('input_boolean.notify_announcement', 'on')}}
    action:
      service: notify.ios_telefoonmhb
      data_template:
        title: >
          Activity {{states('input_select.activity')}} speech
        message: >
          {% set map = {'Opstaan': '{{"US-EN-Morgan-Freeman-Good-Morning.wav"[21:-4]|replace("-" , " ")}}',
                        'Aan de slag': '{{"US-EN-Morgan-Freeman-Coworker-Is-Arriving.wav"[21:-4]|replace("-" , " ")}}',
                        'Home theater': '{{"US-EN-Morgan-Freeman-Starting-Movie-Mode.wav"[21:-4]|replace("-" , " ")}}',
                        'Gym': '{{"US-EN-Morgan-Freeman-Motion-In-Game-Room.wav"[21:-4]|replace("-" , " ")}}',
                        'Gaming': '{{"US-EN-Morgan-Freeman-Motion-In-Game-Room.wav"[21:-4]|replace("-" , " ")}}',
                        'Selamat makan': '{{"US-EN-Morgan-Freeman-Motion-In-Kitchen.wav"[21:-4]|replace("-" , " ")}}'
                         } %}
          {% set state = states('input_select.activity') %}
            {{ map[state] if state in map else '{{"US-EN-Morgan-Freeman-Good-Night.wav"[21:-4]|replace("-" , " ")}}' }}
        data:
          push:
            sound: >
              {% set map = {'Opstaan': 'US-EN-Morgan-Freeman-Good-Morning.wav',
                            'Aan de slag': 'US-EN-Morgan-Freeman-Coworker-Is-Arriving.wav',
                            'Home theater': 'US-EN-Morgan-Freeman-Starting-Movie-Mode.wav',
                            'Gym': 'US-EN-Morgan-Freeman-Motion-In-Game-Room.wav',
                            'Gaming': 'US-EN-Morgan-Freeman-Motion-In-Game-Room.wav',
                            'Selamat makan': 'US-EN-Morgan-Freeman-Motion-In-Kitchen.wav'
                             } %}
              {% set state = states('input_select.activity') %}
                {{ map[state] if state in map else 'US-EN-Morgan-Freeman-Good-Night.wav' }}

or would this be preferable:

  - alias: Announce activity selection
    id: 'Announce activity selection'
#    hide_entity: True
#    initial_state: 'on'
    trigger:
      platform: state
      entity_id: input_select.activity
    condition:
      - condition: template
        value_template: >
          {{ states('input_select.activity') in 
              ['Opstaan','Aan de slag','Home theater',
               'Gym', 'Gaming', 'Selamat makan', 'Naar bed'] }}
      - condition: template
        value_template: >
          {{ is_state('input_boolean.notify_announcement', 'on')}}
    action:
      service: notify.ios_telefoonmhb
      data_template:
        title: >
          Activity {{states('input_select.activity')}} speech
        message: >
          {% set map = {'Opstaan': '{{"US-EN-Morgan-Freeman-Good-Morning.wav"[21:-4]|replace("-" , " ")}}',
                        'Aan de slag': '{{"US-EN-Morgan-Freeman-Coworker-Is-Arriving.wav"[21:-4]|replace("-" , " ")}}',
                        'Home theater': '{{"US-EN-Morgan-Freeman-Starting-Movie-Mode.wav"[21:-4]|replace("-" , " ")}}',
                        'Gym': '{{"US-EN-Morgan-Freeman-Motion-In-Game-Room.wav"[21:-4]|replace("-" , " ")}}',
                        'Gaming': '{{"US-EN-Morgan-Freeman-Motion-In-Game-Room.wav"[21:-4]|replace("-" , " ")}}',
                        'Selamat makan': '{{"US-EN-Morgan-Freeman-Motion-In-Kitchen.wav"[21:-4]|replace("-" , " ")}}',
                        'Naar bed': '{{"US-EN-Morgan-Freeman-Good-Night.wav"[21:-4]|replace("-" , " ")}}' }}
                         } %}
          {% set state = states('input_select.activity') %}
            {{ map[state] }}
        data:
          push:
            sound: >
              {% set map = {'Opstaan': 'US-EN-Morgan-Freeman-Good-Morning.wav',
                            'Aan de slag': 'US-EN-Morgan-Freeman-Coworker-Is-Arriving.wav',
                            'Home theater': 'US-EN-Morgan-Freeman-Starting-Movie-Mode.wav',
                            'Gym': 'US-EN-Morgan-Freeman-Motion-In-Game-Room.wav',
                            'Gaming': 'US-EN-Morgan-Freeman-Motion-In-Game-Room.wav',
                            'Selamat makan': 'US-EN-Morgan-Freeman-Motion-In-Kitchen.wav',
                            'Naar bed': '{{"US-EN-Morgan-Freeman-Good-Night.wav"[21:-4]|replace("-" , " ")}}' }}
                             } %}
              {% set state = states('input_select.activity') %}
                {{ map[state]}}

No, you are good because you provide an else statement in your output. That means you always have a resolution. In @finity’s automation, he doesn’t always want to display something, so he needs to stop the script instead of providing a default.

ok thanks. Was simply wondering if the else was still necessary because if passing this condition:

 - condition: template
   value_template: >
     {{ states('input_select.activity') in 
         ['Opstaan','Aan de slag','Home theater',
          'Gym', 'Gaming', 'Selamat makan', 'Naar bed'] }}

all errors would be prevented. If it didn’t pass here, the automation would stop anyhow?

In theory, you wouldn’t need the condition.

Do I need to install the custom component from here to use the variable:

Or is that functionality now built in to HA? I did a quick search and the link above was the only thing I found relating to variables.

And I didn’t see a specific variable declaration in the automation. Is that an oversight or not needed? is the “snapshot_entity_id: “{{ trigger.entity_id }}”” line recognized as the variable declaration?

It’s built in.

That is the declaration! It’s that simple!

EDIT: Variables are covered at the bottom of the script documentation:

1 Like