Failing to create a simple automation -with a template reference- :(


I’m new at HA and I’m having an issue with a simple templated automation.

Any help would be much appreciated.

What I’m trying to achieve:
An http GET request gets sent to a server when a given light is turned off. Said request must contain a query parameter that only refers to that light and will be used by the server

What I have done:

1- In customize.yaml, I have associated a test lamp’s device_id with its specific query parameter:

# customize.yaml

# Test Lamp
  lamp_id: 1234567

2-In configuration.yaml, I have defined a generic action to be used by any light as it’s turned off:

    url: "{{ lamp_id }}&value=0"
    method: get
    content_type: "application/x-www-form-urlencoded"
    payload: ""

3- In the HA GUI, I have defined the corresponding automation for the same test lamp:
Trigger: when Test Lamp turned off
Conditions: none
Actions: Call a service → rest_command.flag_device_turned_off


When I turn off the light, the logs do indicate the automation has run.

The server does receive a GET request, however its lamp_id parameter value is empty.

Replacing lamp_id=‘{{ lamp_id }}’ by lamp_id=1234567 in the action definition causes the server to receive the expected query.

Obviously I don’t have a correct view of how to make lamp_id a variable that is set at action time using the corresponding customize.yaml entry I set for the lamp’s device_id.

Could anyone help?

Post the actual automation configuration. In the automation editor click the 3-dot menu at the top right, select “Edit in YAML”, copy the whole config then paste it here with 3 back ticks ``` on the lines above and below what you pasted.

alias: Flag Test Lamp turned off
description: ""
  - platform: device
    type: turned_off
    device_id: 923afee7182321e77693a17cd0120cd3
    entity_id: b4c28853cff9f4e72f0451b09af9cd38
    domain: light
condition: []
  - service: rest_command.flag_device_turned_off
    data: {}
mode: single
    action: call-service

I doubt the syntax is the issue since the GET request does get sent. Plus this snippet is auto-generated from the GUI.

Somehow the expression {{ lamp_id }} is interpreted as if lamp_id were empty. If it wasn’t interpreted at all the braces would find their way to the query string.

It is empty, because you haven’t passed the value to it in your service call.

The lamp_id key and it’s value should be in the data block of the service call:

  - service: rest_command.flag_device_turned_off
      lamp_id: "{{ state_attr(trigger.entity_id, 'lamp_id') }}"

Where does the value for lamp_id come from? Do you need to enter it manually or do you have a mapping of light entities to lamp IDs somewhere?

EDIT: Re-read top post, added attribute mapping template.

1 Like

Gotcha. It just didn’t look right and I pulled one of mine up.

Yes, I realize I had assumed that my entry in customize.yaml amounted to adding a permanent “tag” to the Test Lamp description. It obviously didn’t.

Thanks++ for the lead. I need to understand the fine print of what your proposed change means, though. As such it causes the automation to stop with the following log entry: “Error rendering data template: Result is not a Dictionary”.

Did you actually turn the light off to trigger the automation?

I did, yes.

Triggered by the state of light.wiz_rgbww_tunable_08aae2 at January 1, 2024 at 7:36:39 PM

Call a service ‘rest_command.flag_device_turned_off’ on

Stopped because an error was encountered at January 1, 2024 at 7:36:39 PM (runtime: 0.00 seconds)

Error rendering data template: Result is not a Dictionary

Not all shown logbook entries might be related to this automa

Double check that lamp_id is showing up as an attribute of the light entity in the States tab of Developer Tools, like:

The lamp_id attribute is not in that list.

Does this mean the customize.yaml file is not intended for such a purpose?

Drew, I think you nailed it!

I cleaned up the automation action part (quotes, crs, tabs and what not) and I confirm that your suggestion works!!

Thanks++ for your help and my apologies for my poor yaml syntax!!

You either figured this out or did it accidentally, but for anyone who finds themselves in a similar situation…

It means your customize.yaml file hasn’t been properly loaded since you added the lamp_id configuration. Restarting Home Assistant should reload all your YAML files and add the lamp_id attribute to the entity.

You’re right, I did restart HA prior to the “final” test.

Thanks for pointing that out.