Home Assistant service data in ESPhome

Hello,

I’m using a D1mini connected to MCP23017 GPIO extender to control 8 channel relay along with switches. I’m trying to assign a ESPHome momentary switch (binary_sensor) to a light. I’ve created binary sensor to toggle the light, and wanted to increase the brightness to 100% by long press the button. Below is my config

  - platform: gpio
    name: "Kitchen Bulb"
    pin:
      mcp23017: mcp23017_hub
      # Use pin number 7
      number: 7
      mode: INPUT_PULLUP
      inverted: True
    id: db_01_d1
    on_press:
      - homeassistant.service:
          service: light.toggle
          data: {entity_id: light.kitchen_bulb}
    on_click:
      min_length: 50ms
      max_length: 1000ms
      then:
      - homeassistant.service:
          service: light.turn_on
          data: 
            entity_id: light.kitchen_bulb
            brightness: 255
    internal: True

Couldn’t able to get the right code. what did I miss?

I simply made scenes that I load worked right away https://github.com/phixion/Home-AssistantConfig/blob/fe92fe9b77d46336c53dcb25c92c5f3ce24316a9/esphome/switchwhz.yaml#L88-L99

here’s the corresponding scene https://github.com/phixion/Home-AssistantConfig/blob/fe92fe9b77d46336c53dcb25c92c5f3ce24316a9/scenes.yaml#L35-L56

It keeps your esp slim and gives you the freedom to add more devices withought having to flash.

1 Like

I haven’t thought about scenes. Good idea. I’ll try.
So, I can’t add more than one line of service data in esphome yaml? If I remove the brightness, it shows valid.

you cann add multiple, check my code (its nothing final btw im still playing around)

Edit: be warned: there’s no scene.turn_off - you can only turn them on. I usually just made scenes with the opposite values that I can turn on to achieve that.

I can’t able to validate the code with 2 lines. Asked for a close loop, so, mentioned } next to brightness, code accepted, but function not working as there is no open {.
I don’t know json coding, so it’s just trial and error method.

try this:

switch:
  - platform: gpio
    pin: GPIOxxx
    id: relay
  - platform: template
    name: Kitchen Relay
    optimistic: true
    restore_state: true
    id: kitchen_button
    turn_on_action:
      - switch.turn_on: relay
      - light.turn_on: kitchen_led
    turn_off_action:
      - switch.turn_off: relay
      - light.turn_off: kitchen_led
binary_sensor:
  - platform: gpio
    pin:
      number: GPIOxxx
      mode: INPUT_PULLUP
      inverted: true
    id: db_01_d1
    on_press:
      - switch.toggle: kitchen_button
    on_click:
      min_length: 50ms
      max_length: 1000ms
      then:
      - homeassistant.service:
          service: light.turn_on
          data: 
            entity_id: light.kitchen_bulb
            brightness: 100%

here is a simple dimmer (the longer you press the more you raise:

on_press:
- while:
    condition:
    - binary_sensor.is_on: kitchen_button
    then:
    - light.dim_relative:
        id: light.kitchen_bulb
        relative_brightness: -1%
        transition_length: 0.01s
    - delay: 0.01s
1 Like

Thanks, I have thoughts on dimming with switch and this function helps me.

I think you need a negative value to make it brighter - i have changed it in the code

Thanks, I’ve noticed it. I’ll do that :+1: