Passing variables to a script from an automation action, stumped

Scope of project: Using device_tracker, log an entry in a Google Spreadsheet with IFTTT every time a given entity is set to ‘not_home’

Additional considerations: Insert the trigger.from_state information (previous location of the entity) into one of the 3 available IFTTT values
at execution of the action. (not yet attempted.)

Problem: Current configuration is adding an entry to the spreadsheet, but none of the service data is being passed. I’m using this page as a reference: https://home-assistant.io/components/ifttt/

The script:

location_tracking:
  sequence:
    - service: ifttt.trigger
      data_template: {"event":"Location_Tracking", "value1":"", "value2":"", "value3":""}

The automation:

- alias: Jon Changed Locations
  trigger:
    platform: state
    entity_id: device_tracker.jons_iphone_7
    to: 'not_home'
  action:
    service: script.location_tracking
    data_template:
      value1: 'one'
      value2: 'two'
      value3: 'three'

None of the variable values are being passed to IFTTT. A new row is added to the spreadsheet entitled ‘Location Tracking’ with the first cell containing the OccurredAt values generated by IFTTT, but none of the variable values I’ve set are passed.

Any ideas?

how about trying in your ifttt service

"value1":{{value1}} etc.

I think {{}} is the correct syntax. Here is something I do

  send_trv_command:
    sequence:
      - service: mqtt.publish
        data_template:
          topic: '/energenie/eTRV/Command/{{command}}/{{id}}'
          payload: 'ON'
  
  send_report_voltage_office:
    sequence:
      - service: script.send_trv_command
        data:
          id: '296'
          command: 'Voltage'

If I don’t use quotes in the IFTTT data template I get:

invalid key: "OrderedDict([('value2', None)])"
  in "/config/config/scripts/location_tracking.yaml", line 5, column 0

in the error log. This particular error happened with no quotes for the 2nd value, i.e.:

location_tracking:
  alias: Location Tracking
  sequence:
    - service: ifttt.trigger
      data_template: {"event":"Location_Tracking", "value1":"{{value1}}", "value2":{{value2}}}

If I replace {{value1}} in the script with something like {{ states.sun.sun.state }} the value of the sun’s horizon attribute is recorded to the spreadsheet if I activate the script. But if I try to force a ‘not_home’ state for the device tracker in the automation, none of the data values are passed.

@arretx

Let’s try this then:

location_tracking:
  alias: Location Tracking
  sequence:
    - service: ifttt.trigger
      data_template: {"event":"Location_Tracking", "value1":\"{{value1}}\", "value2":\"{{value2}}\"}

I don’t need to escape anything. I figured it out. It was required to put {{value1}} in side of the quotes in the script, but the instructions on the IFTTT page show no mention of the {{value1}} variable templates.

In fact, however, if you edit the GitHub page, the variables are there. They just don’t display on the web.

1 Like

@arretx Awesome! Glad you figured it out. I have on my HA todo list to add a similar functionality so now I know what to do too. Thanks.

I’ve tried this:

{“event”:“test_value”, “value1”:"{{states.media_player.central.attributes.media_title}}"}

When I trigger an ifttt script (to record in a google sheet)

But the only thing that was transferred as value was: states.media_player.central.attributes.media_title

Not the correct value.

I’ve also tried to record the correct state of sun.sun (states.sun.sun.state) into my spreadsheet (for test reasons) But the only value that was transferred was --> states.sun.sun.state

The value that you’re sending in the “value1” pair is a string. You need to pass the {{states.media_player.central.attrubutes.media_title}} value to a variable through an automation, then in your script you would just use:

{"event":"test_value","value1":"storedvariable"}

Do it with a data_template in the automation:

data_template: >-  
  value1: "{{states etc etc }}"
  value2: etc...

It doesn’t work.

Where can I include the data_template syntax?

  • id: ‘1543394795462’
    alias: Temperaturlog Test
    trigger:
    • at: ‘12:00’
      platform: time
      condition: []
      action:
    • data:
      event: test_value
      value1: value1
      service: ifttt.trigger
      data_template: {{value1":"{{states.sensor.fibaro_system_fgms001zw5_motion_sensor_temperature_3.state}}"

What to put into the to be pass value1? I don’t really have a stored value?

I’ve got it. it works.