My first ESP attempt

This is my first attempt at writing an ESP sensor, and I’d like very much to get comments or have my errors pointed out before deploying it. The device (ESP8266 NodeMCU) will mount onto a totalizing gas flow meter measuring cubic feet of propane. That meter has a reed switch that closes once for each cubic foot of flow. The file below has a binary sensor on D2 to sense when the reed switch closes, and a totalizing sensor to sum the flow (dividing by 35.97 to get gallons). Here is the code:

esphome:
  name: gasmeter

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  
binary_sensor:
  - platform: gpio
    pin: D2
    mode:
      input: true
      pullup: true
    filters:
      - delayed_on: 10 ms
      - delayed_off: 10 ms
    name: "Reed Switch Sensor"
    unique_id: reed_switch_sensor
    device_class: reed_switch

sensor:
  - name: "Gas Flow Total"
    unique_id: gas_flow__total
    state: "{{ [ states('binary_sensor.reed_switch_sensor') ] | map('float') / 35.97 | sum }}"
    unit_of_measurement: gal
    icon: mdi:counter
    device_class: volume
    state_class: total_increasing
    attributes:
      last_reset: "1970-01-01T00:00:00+00:00"
    availability: "{{ [ states('binary_sensor.reed_switch_sensory') ] | map('is_number') | min }}"

Am I on the right track here? Thanks for any help.

Nope. This home assistant configuration not esphome:

Thanks; my first idea was to put was to put both the binary sensor and the totalizing sensor on the ESP but I see now that was wrong. This is what I flashed to the ESP:

substitutions:
  name: gasmeter
esphome:
  name: "${name}"
  platform: ESP8266
  board: nodemcuv2
  name_add_mac_suffix: false
  project:
    name: esphome.gasmeter
    version: "1.0"
logger:
api:
ota:
improv_serial:
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
captive_portal:
web_server:


binary_sensor:
  - platform: gpio
    name: "Gas Flow"
    pin:
      number: D4
      mode: INPUT_PULLUP
    filters:
    - invert:

This is working; the device and sensor show in HA and state toggles between off and on with the reed switch (using a momentary contact switch on breadboard for testing). Now I need a totalizing counter in HA for the number of gallons of propane used. It needs to sum the number of times the Gas Flow Sensor has toggled to on and back, and divide that number (which is in cubic feet) by 35.97 to get gallons. That sensor can be used by the Energy Dashboard to keep track of gas consumption. this is how such a sensor is implemented for my usage from the electrical grid:

  - name: "Grid Energy Total"
    unique_id: grid_energy_total
    state: >-
      {{ [ states('sensor.em3_channel_a_energy'), 
           states('sensor.em3_channel_b_energy'),
         ] | map('float') | sum }}
    unit_of_measurement: kWh
    icon: mdi:counter
    device_class: energy
    state_class: total_increasing
    attributes:
      last_reset: "1970-01-01T00:00:00+00:00"
    availability: >-
          {{ [ states('sensor.em3_channel_a_energy'), 
               states('sensor.em3_channel_b_energy'),
             ] | map('is_number') | min }}

This my first try at a gas flow totalizer. The conversion of cubic feet to gallons is not a part of this.

  - name: "Gas Flow Total"
    unique_id: gas_flow_total
    state: "{{ [ states('binary_sensor.gas_flow'),  ] | map('float') | sum }}"
    unit_of_measurement: gal
    icon: mdi:counter
    device_class: gas
    state_class: total_increasing
    attributes:
      last_reset: "1970-01-01T00:00:00+00:00"
    availability: "{{ [ states('binary_sensor.gas_flow'), ] | map('is_number') | min }}"

It loads but shows as unavailable and does not change when the binary sensory is toggled. Where did I go wrong here?

I realized I need to convert the binary sensor to an analog one, so used this:

  - name: "Gas Flow Analog"
    unique_id: gas_flow_analog
    device_class: gas
    state: >
      {% if is_state('binary_sensor.gas_flow', 'on') %}
        1
      {% else %}
        0
      {% endif %}

Then I wrote the totalizing sensor this way:

  - name: "Gas Flow Total"
    unique_id: gas_flow_total
    state: "{{ [ states('sensor.gas_flow_analog'),  ] | map('float') | sum }}"
    unit_of_measurement: gal
    icon: mdi:counter
#    device_class: gas
    state_class: total_increasing
    attributes:
      last_reset: "1970-01-01T00:00:00+00:00"
    availability: "{{ [ states('sensor.gas_flow_analog'), ] | map('is_number') | min }}"

When triggered, Gas Flow Analog shows 1, and Gas Flow Total shows 1.0. But when released, Gas Flow Total returns to 0.0. Why doesn’t it increase each time Gas Flow Analog is triggered? The state class is set to total increasing.