Calling HA services from within esphome lambda functions

In the yaml file define the id like this:

api:
  id: api_id
1 Like

Thanks, but I think I’m still misunderstanding. I have this in my ESPHome device code, and it will not compile:

# Enable Home Assistant API
api:
  encryption:
    key: !secret api_encryption_key
  id: api_id

Which error message do you have when compiling?
Could you please share that and also your whole yaml, please?
Please remove any sensitive information before sharing.

Wow - it must have been a temporary thing – I compiled again today, and it worked and installed. And, it does what I want it to do: I can control my house lights using the buttons on the m5stack.

1 Like

Topic kick :wink: i tried updating to the latest ESPhome, and getting exact the same error. My ESPhome config looks like:

api:
services:
- service: meterstand_water
variables:
meter_value: float
then:
- globals.set:
id: initial_water_usage

id: totalWaterUsage

        value: !lambda "return ( meter_value ) ;"

ota:

dallas:

  • pin: GPIO14

wifi:
ssid: !secret SSID
password: !secret WiFi_Password
fast_connect: true

use_address: watermeter.local

Enable fallback hotspot (captive portal) in case wifi connection fails

ap:
ssid: “Watermeter Fallback Hotspot”
password: “4Ejkhjkhuixa4M”
captive_portal:
script:

  • id: reset_main_counter
    then:
    • lambda: |-
      id(main_counter_pulses) = 0;
      id(water_main_consumption).publish_state(id(main_counter_pulses));
  • id: reset_secondary_counter
    then:
    • lambda: |-
      id(secondary_counter_pulses) = 0;
      id(water_secondary_consumption).publish_state(id(secondary_counter_pulses));
  • id: publish_states
    then:
    • lambda: |-
      id(water_main_consumption).publish_state(id(main_counter_pulses));
      id(water_secondary_consumption).publish_state(id(secondary_counter_pulses));
      id(water_daily_consumption).publish_state(id(daily_counter_pulses));
      id(water_weekly_consumption).publish_state(id(weekly_counter_pulses));
      id(water_monthly_consumption).publish_state(id(monthly_counter_pulses));
      id(water_yearly_consumption).publish_state(id(yearly_counter_pulses));
      id(current_water_consumption).publish_state(id(event_quantity));

time:

  • platform: sntp
    on_time:
    • seconds: 0
      minutes: 0
      hours: 0
      then:
      • globals.set:
        id: daily_counter_pulses
        value: ‘0’
      • lambda: id(water_daily_consumption).publish_state(id(daily_counter_pulses));
    • seconds: 0
      minutes: 0
      hours: 0
      days_of_week: MON
      then:
      • globals.set:
        id: weekly_counter_pulses
        value: ‘0’
      • lambda: id(water_weekly_consumption).publish_state(id(weekly_counter_pulses));
    • seconds: 0
      minutes: 0
      hours: 0
      days_of_month: 1
      then:
      • globals.set:
        id: monthly_counter_pulses
        value: ‘0’
      • lambda: id(water_monthly_consumption).publish_state(id(monthly_counter_pulses));
    • seconds: 0
      minutes: 0
      hours: 0
      days_of_month: 1
      months: JAN
      then:
      • globals.set:
        id: yearly_counter_pulses
        value: ‘0’
      • lambda: id(water_yearly_consumption).publish_state(id(yearly_counter_pulses))

So can anybody help me what’s “wrong”?