Update failed 'api_apiserver' was not declared in this scope

Hello all , I am unable to do the latest update on one of my esp, because I have the following error

it worked perfectly before the latest package updates

can you help me to correct the error ? Thanks

substitutions:
  name: watermeter
  friendly_name: "Water meter"
  pulse_gpio: GPIO25



esphome:
  name: $name
  platform: ESP32
  board: esp32dev
  on_boot:
    priority: -10
    then:
      - script.execute: publish_states


# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "ms6DSls78KDmWo6rnwj1CDHh1aNyss3jdJLhBBQ4="

ota:
  password: "c25707519d34287b1d736fe3d3100"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Compteur-Eau Fallback Hotspot"
    password: "LjWcFvMoth3S"

captive_portal:


# Documentations:
# https://github.com/esphome/issues/issues/664
# https://esphome.io/components/sensor/integration.html
# https://community.home-assistant.io/t/using-esphome-to-build-a-water-flow-rate-meter/119380/64

globals:
 - id: main_counter_pulses
   type: int
   restore_value: yes
   initial_value: '0'
 - id: secondary_counter_pulses
   type: int
   restore_value: yes
   initial_value: '0'
 - id: daily_counter_pulses
   type: int
   restore_value: yes
   initial_value: '0'
 - id: weekly_counter_pulses
   type: int
   restore_value: yes
   initial_value: '0'
 - id: monthly_counter_pulses
   type: int
   restore_value: yes
   initial_value: '0'
 - id: yearly_counter_pulses
   type: int
   restore_value: yes
   initial_value: '0'

 - id: event_quantity
   type: int
   restore_value: no
   initial_value: '0'
 - id: last_event_quantity
   type: int
   restore_value: no
   initial_value: '0'
 - id: event_counter
   type: int
   restore_value: no
   initial_value: '0'
 - id: current_event_quantity
   type: int
   restore_value: no
   initial_value: '0'

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));

sensor:
  - platform: uptime
    name: "${friendly_name} uptime"

  - platform: wifi_signal
    name: "${friendly_name} WiFi signal"

  - platform: pulse_meter
    id: water_pulse_counter
    name: "${friendly_name} water consumption"
    pin:
      number: $pulse_gpio
      allow_other_uses: true
      inverted: true
      mode:
        input: true
        pullup: true
    internal_filter: 100ms
    unit_of_measurement: "L/min"
    accuracy_decimals: 2
    timeout: 30s
    icon: "mdi:water"





  # Value calculated are not consistent
  #- platform: integration
  #  id: water_integration_sensor
  #  name: "${friendly_name} integrated water consumption"
  #  sensor: water_pulse_counter
  #  time_unit: min
  #  unit_of_measurement: "L"
  #  accuracy_decimals: 0
  #  icon: "mdi:water"

  - platform: template
    id: water_main_consumption
    device_class: "water"
    name: "${friendly_name} main water consumption"
    unit_of_measurement: "L"
    accuracy_decimals: 0
    icon: "mdi:water"

    

  - platform: template
    id: water_secondary_consumption
    device_class: "water"
    name: "${friendly_name} secondary water consumption"
    unit_of_measurement: "L"
    accuracy_decimals: 0
    icon: "mdi:water"

  - platform: template
    id: water_daily_consumption
    device_class: "water"
    name: "${friendly_name} daily water consumption"
    unit_of_measurement: "L"
    accuracy_decimals: 0
    icon: "mdi:water"

  - platform: template
    id: water_weekly_consumption
    device_class: "water"
    name: "${friendly_name} weekly water consumption"
    unit_of_measurement: "L"
    accuracy_decimals: 0
    icon: "mdi:water"

  - platform: template
    id: water_monthly_consumption
    device_class: "water"
    name: "${friendly_name} monthly water consumption"
    unit_of_measurement: "L"
    accuracy_decimals: 0
    icon: "mdi:water"

  - platform: template
    id: water_yearly_consumption
    device_class: "water"
    name: "${friendly_name} yearly water consumption"
    unit_of_measurement: "L"
    accuracy_decimals: 0
    icon: "mdi:water"

  - platform: template
    id: current_water_consumption
    device_class: "water"
    name: "${friendly_name} current water consumption"
    unit_of_measurement: "L"
    accuracy_decimals: 0
    icon: "mdi:water"

  - platform: template
    id: last_water_consumption
    device_class: "water"
    name: "${friendly_name} last water consumption"
    unit_of_measurement: "L"
    accuracy_decimals: 0
    icon: "mdi:water"

binary_sensor:
  # TCRT5000 pulse counter
  - platform: gpio
    id: water_pulse
    pin:
      number: $pulse_gpio
      allow_other_uses: true
    internal: true
    filters:
       - delayed_on_off: 50ms
       - lambda: |-
          id(main_counter_pulses) += x;
          id(secondary_counter_pulses) += x;
          id(daily_counter_pulses) += x;
          id(weekly_counter_pulses) += x;
          id(monthly_counter_pulses) += x;
          id(yearly_counter_pulses) += x;
          id(event_quantity) += x;
          return x;
    on_state:
       - script.execute: publish_states

switch:
  - platform: template
    name: "${friendly_name} reset main counter button"
    icon: "mdi:restart"
    turn_on_action:
      - script.execute: reset_main_counter

  - platform: template
    name: "${friendly_name} reset secondary counter button"
    icon: "mdi:restart"
    turn_on_action:
      - script.execute: reset_secondary_counter

  - platform: restart
    name: "${friendly_name} restart"

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));

interval:
  # Save the last consumption
  #
  # An event is published when a water flow (>= 1L / 15 seconds) is
  # detected and followed by a stop of consumption for a defined time.
  - interval: 15sec
    then:
      - lambda: |-
          if (id(event_quantity) != id(last_event_quantity)) {
            // Water continues to flow

            // Reset event counter
            id(event_counter) = 0;
          } else {
            // Water no longer flows

            if (id(event_quantity)) {
              // 4 * 15 * 5 = 5min
              if (id(event_counter) < 4 * 5) {
                // Timeout is not reaches
                id(event_counter)++;
              } else {
                  // Timeout is reaches
                  id(last_water_consumption).publish_state(id(event_quantity));

                  // Send event to Home Assistant
                  api::HomeAssistantServiceCallAction<> *api;
                  api = new api::HomeAssistantServiceCallAction<>(api_apiserver, true);
                  // Event id length limit is 32 characters
                  api->set_service("esphome.last_consumption_changes");
                  api->play();

                  id(event_quantity) = 0;
              }
            }
          }

          id(last_event_quantity) = id(event_quantity);

  # Track the current consumption
  - interval: 2sec
    then:
      - lambda: |-
          if (id(event_quantity) != id(current_event_quantity)) {
            id(current_water_consumption).publish_state(id(event_quantity));
          }
          id(current_event_quantity) = id(event_quantity);

    

Just compiled on 2024.4.1 version - all fine.
See lot of complians about 2024.5.0 version - IMHO too early to upgrade to it.

got the same error while compiling, so stayed at 2024.4.2

substitutions:
  name: meterkast
  friendly_name: "Meterkast"
  
esphome:
  name: meterkast
  platform: ESP8266
  board: d1_mini
  on_boot:
    priority: -10
    then:
    - script.execute: publish_states
# Enable logging
logger:
# Enable Home Assistant API
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));
sensor:
  - platform: pulse_counter
    id: water_pulse_counter
    pin:
      number: GPIO04
      allow_other_uses: true
    name: "${friendly_name} water consumption" 
    update_interval: 2sec
    internal_filter: 10us
    unit_of_measurement: "L/min"
    accuracy_decimals: 0
    icon: "mdi:water"
    filters:
      # Divide by 60
      - multiply: 0.0167
      - lambda: return abs(x);
  - platform: template
    id: water_main_consumption
    name: "${friendly_name} main water consumption"
    unit_of_measurement: "L"
    accuracy_decimals: 0
    icon: "mdi:water"
  - platform: template
    id: water_secondary_consumption
    name: "${friendly_name} secondary water consumption"
    unit_of_measurement: "L"
    accuracy_decimals: 0
    icon: "mdi:water"
  - platform: template
    id: water_daily_consumption
    name: "${friendly_name} daily water consumption"
    unit_of_measurement: "L"
    accuracy_decimals: 0
    icon: "mdi:water"
  - platform: template
    id: water_weekly_consumption
    name: "${friendly_name} weekly water consumption"
    unit_of_measurement: "L"
    accuracy_decimals: 0
    icon: "mdi:water"
  - platform: template
    id: water_monthly_consumption
    name: "${friendly_name} monthly water consumption"
    unit_of_measurement: "L"
    accuracy_decimals: 0
    icon: "mdi:water"
  - platform: template
    id: water_yearly_consumption
    name: "${friendly_name} yearly water consumption"
    unit_of_measurement: "L"
    accuracy_decimals: 0
    icon: "mdi:water"
  - platform: template
    id: current_water_consumption
    name: "${friendly_name} current water consumption"
    unit_of_measurement: "L"
    accuracy_decimals: 0
    icon: "mdi:water"
  - platform: pulse_meter
    pin:
      number: GPIO04
      allow_other_uses: true
#      number: GPIO4
#      mode: INPUT_PULLUP
    name: "Water Pulse Meter"
    unit_of_measurement: "liter/min"
    icon: "mdi:water"
    total:
      name: "Water Meter Total"
      unit_of_measurement: "m³"
      id: water_meter_total
      accuracy_decimals: 3
      device_class: water
      state_class: total_increasing
      filters:
          - multiply: 0.001
      
  - platform: template
    id: last_water_consumption
    name: "${friendly_name} last water consumption"
    unit_of_measurement: "L"
    accuracy_decimals: 0
    icon: "mdi:water"
# ⬇ Watermeter stand bij benadering ⬇ #
  - platform: template
    id: watermeter_total
    name: "watermeter stand"
    state_class: "total_increasing"
    icon: mdi:water
    unit_of_measurement: "m³"
    accuracy_decimals: 3
    lambda: return id(initial_water_usage) + (id(total_water_pulses) * 0.001);
      
# ⬇ Waterpulses ⬇ #
  - platform: template
    id: watermeter_pulses
    name: "watermeter pulses totaal"
    state_class: "total_increasing"
    accuracy_decimals: 0
    lambda: return id(total_water_pulses);
  - platform: dallas
    address: 0x8E0416C005A0FF28
    name: "Temperatuur meterkast"
  - platform: dallas
    address: 0x730416381E33FF28
    name: "Temperatuur in serverkast"
  
binary_sensor:
  # Nabijheidssensor
  - platform: gpio
    id: water_pulse
#    pin: D2
    pin:
      number: GPIO04
      allow_other_uses: true
    internal: true
    filters:
       - delayed_on_off: 50ms
       - lambda: |-
          id(main_counter_pulses) += x;
          id(secondary_counter_pulses) += x;
          id(daily_counter_pulses) += x;
          id(weekly_counter_pulses) += x;
          id(monthly_counter_pulses) += x;
          id(yearly_counter_pulses) += x;
          id(event_quantity) += x;
          return x;
    on_state:
       - script.execute: publish_states
  - platform: gpio
    device_class: window
    pin:
      number: GPIO12
      mode: INPUT_PULLUP
      inverted: True
    name: "deurbel"  
  - platform: gpio
    pin:
      number: GPIO13
      mode: INPUT_PULLUP
    name: "meterkast open-dicht"
    device_class: door
    internal: true

       
switch:
  - platform: template
    name: "${friendly_name} reset main counter button"
    icon: "mdi:restart"
    turn_on_action:
      - script.execute: reset_main_counter
  - platform: template
    name: "${friendly_name} reset secondary counter button"
    icon: "mdi:restart"
    turn_on_action:
      - script.execute: reset_secondary_counter
  - platform: restart
    name: "${friendly_name} restart"
interval:
  # Save the last consumption
  #
  # An event is published when a water flow (>= 1L / 15 seconds) is
  # detected and followed by a stop of consumption for a defined time.
  - interval: 15sec
    then:
      - lambda: |-
          if (id(event_quantity) != id(last_event_quantity)) {
            // Water continues to flow
            // Reset event counter
            id(event_counter) = 0;
          } else {
            // Water no longer flows
            if (id(event_quantity)) {
              // 4 * 15 * 5 = 5min
              if (id(event_counter) < 4 * 5) {
                // Timeout is not reaches
                id(event_counter)++;
              } else {
                  // Timeout is reaches
                  id(last_water_consumption).publish_state(id(event_quantity));
                  // Send event to Home Assistant
                  api::HomeAssistantServiceCallAction<> *api;
                  api = new api::HomeAssistantServiceCallAction<>(api_apiserver, true);
                  // Event id length limit is 32 characters
                  api->set_service("esphome.last_consumption_changes");
                  api->play();
                  id(event_quantity) = 0;
              }
            }
          }
          id(last_event_quantity) = id(event_quantity);
  # Track the current consumption
  - interval: 2sec
    then:
      - lambda: |-
          if (id(event_quantity) != id(current_event_quantity)) {
            id(current_water_consumption).publish_state(id(event_quantity));
          }
          id(current_event_quantity) = id(event_quantity);
globals:
 - id: main_counter_pulses
   type: int
   restore_value: yes
   initial_value: '0'
 - id: secondary_counter_pulses
   type: int
   restore_value: yes
   initial_value: '0'
 - id: daily_counter_pulses
   type: int
   restore_value: yes
   initial_value: '0'
 - id: weekly_counter_pulses
   type: int
   restore_value: yes
   initial_value: '0'
 - id: monthly_counter_pulses
   type: int
   restore_value: yes
   initial_value: '0'
 - id: yearly_counter_pulses
   type: int
   restore_value: yes
   initial_value: '0'
 - id: event_quantity
   type: int
   restore_value: no
   initial_value: '0'
 - id: last_event_quantity
   type: int
   restore_value: no
   initial_value: '0'
 - id: event_counter
   type: int
   restore_value: no
   initial_value: '0'
 - id: current_event_quantity
   type: int
   restore_value: no
   initial_value: '0'
 - id: higher_resistance
   type: float
   restore_value: yes
   initial_value: '0'
 - id: initial_water_usage
   type: float
   restore_value: yes      
   initial_value: '0' 
 - id: total_water_pulses
   type: int
   restore_value: yes



web_server:
  port: 80 ```

Hi JeremC82,

The problem is that api_apiserver is not declared. The solution is to add id: api_apiserver in the api: declaration section

1 Like