Passing trigger state to script always empty

Dear Home Assistant community!

I’m struggling with the following, I have a script that needs a ‘trigger state’ so in my script I have this:

    alias: "[Switch] Do stuff"
        name: Trigger event
        description: Trigger event passtrough
        required: true

Later on convert it to a variable:

- variables:
    trigger_action: "{{ trigger_action_state }}"

And use it as a test to log:

- service: system_log.write
    message: "{{ trigger_action }}"
    logger: walkin.script.yaml

In my automation I pass the following:

    - variables:
        trigger_data: "{{ }}"
    - service: system_log.write
        message: "{{ trigger_data }}"
        logger: walkin.lights.yaml
    - service: script.switch_pressed
        trigger_action_state: "{{ trigger_data }}"

As you can see, I do a log here as well. That one works and gives the correct output. But the log in the script where I need it remains empty. Am I doing something wrong? Or is it not possible to go this route? is an optional variable.
Are you sure the switch is actually passing any data, or is it just passing its id for instance?

Where is the automation’s trigger? I assume it employs an Event Trigger?

1 Like


  - platform: event
    event_type: state_changed
      entity_id: sensor.walk_in_switch_action

The switch is passing data, like on-press for example (what i see in the first log)

It’s passing its id, but I see no event data (apart from the id)
That’s probably your issue.

trigger_data is empty, because you’re passing empty data to it.

Why not just use a State Trigger?

  - platform: state
    entity_id: sensor.walk_in_switch_action
  - service: system_log.write
      message: "{{ trigger.to_state.state }}"
      logger: walkin.lights.yaml
  - service: script.switch_pressed
      trigger_action_state: "{{ trigger.to_state.state }}"

Have you tracing it? I see that you are logging the one variable but you’d probably get a lot more insight into what is going wrong from the trace. Both scripts and automations are traceable and then let you see exactly what happened step by step including all the data that was set into variables along the way.

All scripts are traceable (defined in YAML or defined in the UI) so you should be able to see a traces for your scripts already. Automations are traceable as long as you provide a value for id. They don’t have to be defined in the UI, yaml ones are still traceable as long as you put a value in id like so:

- id: my_traceable_automation
  alias: My traceable automation

Thanks for the suggestion! However this results unfortunately in trigger.to_state.state always being empty.

Trace shows me stuff like this:

  message: ''

But not more for me to grasp an idea of what’s wrong.

Are you testing this with the run automaton button or are you waiting for It to trigger?

Edit: using the run automation button will always net an empty variable because it doesn’t have a trigger because it didn’t trigger.

Testing every time with actually pushing the Hue Switch in real life :slight_smile:

Then it sounds like the issue is with your sensor. Which the trace would show.

Then you’re doing something wrong because trigger.to_state is an available object for a State Trigger.

Found it! log shows the on press in the automation system_log.write but it doesn’t get passed to the script as that log remains empty.