Error: request for member 'state' trying to publish a state

The code below generates the error code at the bottom during compiling

esphome:
  name: d1minilolin-01
  friendly_name: D1MiniLOLIN-01

esp8266:
  board: esp01_1m

# Enable logging
logger:

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

ota:
  password: "f3605d46be86c79d06ca0ff3cf767bde"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "D1Minilolin-01 Fallback Hotspot"
    password: "KoLWfxrgkzkK"

captive_portal:

  - platform: adc
    pin: A0
    id: gasmeter_hall_measurement
    internal: true 
    update_interval: 100ms
    raw: true 
    name: "Gasmeter Hall measurement"
    filters:
    - lambda: return 1024-x; 
    - delta: 2.0

  - platform: uptime
    name: "Time Since Last On"
    id: time_since_last_on
    internal: True

  - platform: template
    name: "gasmeterpulsetijd on-on"
    id: gasmeterpulsetijd_on_on
    device_class: duration
    unit_of_measurement: "s"
    state_class: "measurement"
    icon: "mdi:clock-fast"
    accuracy_decimals: 2
    
  - platform: template
    name: "gasmeterpulsetijd on-off"
    id: gasmeterpulsetijd_on_off
    device_class: duration
    unit_of_measurement: "s"
    state_class: "measurement"
    icon: "mdi:clock-fast"
    accuracy_decimals: 3

binary_sensor:
  - platform: analog_threshold
    name: "Gasmeterteller 10liter"
    sensor_id: gasmeter_hall_measurement
    threshold:
      upper: 476 #was 480
      lower: 467 #was 470
   on_press:
      then:
        - lambda: |-
            id(pulsduur_onon) = id(time_since_last_on).state - id(starttijdpulse);
            id(starttijdpulse) = id(time_since_last_on).state;
            id(gasmeterpulsetijd_on_on).publish_state(id(pulsduur_onon).state);
    on_release:
      then:
        - lambda: |-
            id(pulsduur_onoff) = id(time_since_last_on).state - id(starttijdpulse);
            id(gasmeterpulsetijd_on_off).publish_state(id(pulsduur_onoff).state);
    
globals:
  - id: total_pulses
    type: int
    initial_value: '920' 

  - id: starttijdpulse
    type: long
    initial_value: '0'

  - id: pulsduur_onon
    type: long
    initial_value: '0'

  - id: pulsduur_onoff
    type: long
    initial_value: '0'

Error codes:

INFO ESPHome 2023.9.3
INFO Reading configuration /config/esphome/d1minilolin-01.yaml...
INFO Generating C++ source...
INFO Compiling app...
Processing d1minilolin-01 (board: esp01_1m; framework: arduino; platform: platformio/[email protected])
--------------------------------------------------------------------------------
HARDWARE: ESP8266 80MHz, 80KB RAM, 1MB Flash
Dependency Graph
|-- ESPAsyncTCP-esphome @ 2.0.0
|-- ESPAsyncWebServer-esphome @ 3.1.0
|-- DNSServer @ 1.1.1
|-- ESP8266WiFi @ 1.0
|-- ESP8266mDNS @ 1.2
|-- noise-c @ 0.1.4
Compiling .pioenvs/d1minilolin-01/src/main.cpp.o
/config/esphome/d1minilolin-01.yaml: In lambda function:
/config/esphome/d1minilolin-01.yaml:107:69: error: request for member 'state' in 'pulsduur_onon->esphome::globals::GlobalsComponent<int>::value()', which is of non-class type 'int'
  107 |             id(gasmeterpulsetijd_on_on).publish_state(id(pulsduur_onon).state);
      |                                                                     ^~~~~
/config/esphome/d1minilolin-01.yaml: In lambda function:
/config/esphome/d1minilolin-01.yaml:112:71: error: request for member 'state' in 'pulsduur_onoff->esphome::globals::GlobalsComponent<int>::value()', which is of non-class type 'int'
  112 |             id(gasmeterpulsetijd_on_off).publish_state(id(pulsduur_onoff).state);
      |                                                                       ^~~~~
*** [.pioenvs/d1minilolin-01/src/main.cpp.o] Error 1
========================== [FAILED] Took 7.58 seconds ==========================

I tried to change the data type of the globals to float and int, but without success.

What am I doing wrong?

Thx for any help.

Globals do not have a so-called state, so delete the .state, try

id(gasmeterpulsetijd_on_on).publish_state(id(pulsduur_onon));
2 Likes