Calling HA service from ESPHome

Hi all. Try to make a service call so I can call ESPHome from HA. I add a service in the api: section of Doorbell.yaml from ESPHome but the service doesn’t showup in HA. Do I have to do anything more?
The ESPHome doorbell is working fine in HA, but now I want to call the doorbell from HA.
This is a part of the doorbell.yaml:

Enable Home Assistant API

api:
services:
- service: dingdong
then:
- script.execute: dingdong_script

script:
id: dingdong_script
then:
if:
condition:
- switch.is_on: chime_active
then:
- switch.turn_on: relay
- delay: 200ms
- switch.turn_off: relay
- delay: 600ms
- switch.turn_on: relay
- delay: 200ms
- switch.turn_off: relay
- delay: 600ms
- switch.turn_on: relay
- delay: 200ms
- switch.turn_off: relay

But the dingdong sevice doesn’t show up in HA!?

The ESPHome documentation says:

It is also possible to get data from Home Assistant to ESPHome with user-defined services. When you declare services in your ESPHome YAML file, they will automatically show up in Home Assistant and you can call them directly.

# Example configuration entry
api:

  • services:*
    • service: start_laundry*
  •  then:*
    
  •    - switch.turn_on: relay*
    
  •    - delay: 3h*
    
  •    - switch.turn_off: relay*
    

For example with the configuration seen above, after uploading you will see a service called esphome.livingroom_start_laundry (livingroom is the node name) which you can then call.

But my service doesn’t showup

After installing the ESPH firmware and if it’s an update of an existing, in HA, try to reload the ESPhome integration of that device. (ESPh integration, select device, reload).
But new service may need to reload service part of the HA also (I’m not sure but my 2 cents)

Tanks for the support, but unfortunately reload doesn’t solve the problem

Where did you looked for the service?

The service can be found : Developer Tools → Tab: Services and will start with “ESPHome:”

See also the screen print

Yes I know, but it doesn’t show up, not in the dev tools/services, not as a possible service call in the action of an automation.

The ESPHome documentation says:

It is also possible to get data from Home Assistant to ESPHome with user-defined services.
When you declare services in your ESPHome YAML file,
they will automatically show up in Home Assistant and you can call them directly.

Example configuration entry

api:
services:
- service: start_laundry
then:
- switch.turn_on: relay
- delay: 3h
- switch.turn_off: relay

For example with the configuration seen above,
after uploading you will see a service called
esphome.livingroom_start_laundry (livingroom is the node name) which you can then call.

Please please post your code properly. Yaml is useless and impossible to follow without indentation.

The code is just a copy paste from the ESPHome documentation: Native API Component — ESPHome
So you can look at that URL

But that won’t help if there’s an cut and paste error in your code. Posting your code properly would be helpful to those who can help you.

Yep as mentionned, better to post your own yaml. On ESPhome dashboard, you can connect (wireless) to see esphome debug logs. The answer is may be there :wink:

I try to post it properly but the chat disturbed it.
Try again using preformatted text:

api:
  services:
  - service: dingdong
    then:
    - script.execute:
        id: dingdong_script
    variables: {}
  port: 6053
  password: xxxxxxxxx
  reboot_timeout: 15min
script:
- id: dingdong_script
  then:
  - if:
      condition:
        and:
        - switch.is_on:
            id: chime_active
      then:
      - switch.turn_on:
          id: relay
      - delay: 200ms
      - switch.turn_off:
          id: relay
      - delay: 600ms
      - switch.turn_on:
          id: relay
      - delay: 200ms
      - switch.turn_off:
          id: relay
      - delay: 600ms
      - switch.turn_on:
          id: relay
      - delay: 200ms
      - switch.turn_off:
          id: relay
  mode: single
  parameters: {}

Comparing your code and the sample code, you have a pretty obvious indentation error.

api:
  services:
  - service: dingdong
    then:
    - switch.turn_on: switch1

switch:
  platform: template
  id: switch1
  name: 'switch1'
  turn_on_action:
    - repeat:
        count: 5
        then:
        - switch.turn_on: relay          
        - delay: 200ms
        - switch.turn_off: relay          
        - delay: 600ms

You really don’t even need to call a service in this situation as just using that template switch in an automation is the exact same thing. You’re really over complicating things when they dont need to be.

i forgot your condition. I used a template binary sensor but you just need to change that to gpio or whatever your currently using. I added the delayed off so your switch turn on/off actually has some time to run 5x or whatever.

switch:
- platform: template
  id: switch1
  name: 'switch1'
  turn_on_action:
    - repeat:
        count: 5
        then:
        - switch.turn_on: relay          
        - delay: 200ms
        - switch.turn_off: relay          
        - delay: 600ms  



binary_sensor:

- platform: template
  id: chime_active
  filters:
    - delayed_off: 1s
  on_state:
  - while:
      condition:
        binary_sensor.is_on: chime_active
      then:
        - switch.turn_on: switch1

As I said, obvious mistake, now I am on my laptop I can type more.

No No, this is what it says in the docs

api:
  services:
    - service: dingdong   
      then:
        - script.execute:
            id: dingdong_script

theres no “obvious” mistake with the indents…

boom

The OP referred to the documents, but his settings did not match the documents.

His problem must be something else I guess.

Hi all, thanks for all the support.
As I said at the beginning of the thread, I am an ESPHome-nono and because nobody told me to install the changed yaml, I did not do that :pensive:.
So after installing the changed yaml the dingdong service shows up in HA and now I can use my doorbell as an alarmbell or for whatever function!! :fireworks: :sparkler:

Again thanks for the support and a happy automated new year from the Netherlands