Use trigger event data in scripts called from esphome

Hello Community!

I have following setup to control my lights (on, off)

  • Detached Shelly Relays using Esphome
    • when connected to API and WiFi I used to call a
      homeassistant.service from Esphome to service: light.toggle and pass on the entity id of the light each Esphome relay controls in Homeassistant
    • this way I could just change the entity id as a variable and flash the corresponding Shelly
    • the controlled lights are mostly dimmable Zigbee lights

Recently I wanted to implement dimming when using long click; I could successfully test a blueprint from notherealmarco

  • because of a Zigbee limitation, this blueprint also needs to know the IEEE address of the Zigbee light.
  • I changed the blueprint after looking through the forums, and replaced the blueprint IEEE input with the following template
variables:
  l1ieee: "{{ device_attr('light.target_entity', 'identifiers') | selectattr(0,'eq','zha') | map(attribute=1) | first }}"
  • this worked well and motivated me to further work on it
  • the mentioned blueprint uses script calls from Esphome:
*Esphome on Shelly device, on_multi_click -> single click -> action toggle*
  - homeassistant.service:
      service: script.smart_dimmer_test_victor
      data:
        action: toggle
        controlled_light_id: "${controlled_light}"

controlled_light is the variable each Esphome device was given when flashing

Homeassistant receives the event and message, I can see this in the logs, but how can I use controlled_light_id in a script or blueprint ?

from notherealmarco blueprint 
sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ action == "toggle" }}'
          - condition: template
            value_template: '{{ is_state(main_light_eid, "off") }}'
        sequence:
          - service: light.turn_on
            target: !input light

I would like to change !input light to something like "{trigger.event.data.controlled_light_id}". Unfortunately however, this does not work…

Using the blueprint as is, would mean I have to create a script based on that blueprint for each of my light switch. That is the reason why I would like to use one script, that parses the data passed on from Esphome.

One reason I cannot make this work the way I want is maybe because I don’t really understand the way the blueprint from notherealmarco works.

I am also grateful if somebody can point out a better way to get esphome devices to work with Zigbee lights :slight_smile:
Any help is appreciated!
Best regards from Germany!

Scripts don’t have triggers or trigger variables. You need to pass the entity ID during the homeassistant.service action. Which you may already be doing… what is the actual value being passed with controlled_light_id? If it is the entity ID, you would replace !input light as follows:

        sequence:
          - service: light.turn_on
            target: 
              entity_id: "{{ controlled_light_id }}"

in Esphome I call it like this

                - homeassistant.service:
                    service: script.smart_dimmer
                    data:
                      action: toggle
                      controlled_light_id: "${controlled_light}"
and 2 more times with action "dimm_auto" and "stop"

If I follow the Esphome documentation and try to use

from Native API Component — ESPHome

# In some trigger
on_...:
  - homeassistant.action:
      action: script.set_light_rgb
      data:
        light_name: 'my_light'
        red: '255'
        green: '199'
        blue: '71'

I get

Unable to find action with the name ‘homeassistant.action’.
My Esphome version is 2024.04

What HA version are you running?

I just tested it on ESPHome 2024.08.0 and HA 2024.8.2 and the variable was passed as expected:

ESPHome Action
  - platform: gpio
    pin:
      number: GPIO37
      inverted: true
    name: ${upper_devicename} Button A
    on_press:
      then:
        - homeassistant.action:
            action: script.helper_do_nothing
            data:
              test: light.${devicename}_led
Script Trace

I wonder if there’s some kind of clash because of the recent change to use action instead of service…?

Thank you for your valuable clue! I got actions working with ESPHome 2024.08+

1 Like

I can now toggle lights based on the variable passed from ESPHome.
I cannot add dimming, because the script cannot be activated in homeassistant…

I forked the original blueprint and changed it with my current script version. I also added the esphome project files which I currently use.

Current gist with project files

Can somebody please give it a look, I get strange errors about the actions I want to use.

Thanks to the community for helping, I learned a lot (very slowly). Now I got it working.
Toggling and dimming up and down now works. Feel free to use code as you wish.