Automatically examine a sensor and execute function

I took the entire text of my question with Ctrl-A Ctrl-C and pasted it into ChatGPT.

Wow. Just wow. No wonder they say computer programming isn’t going to be the career it used to be.


Consider the hx711 sensor in this component. From experience, if no weight is on the scale, the sensor reports anything from 0.01 to 3 lbs. How can this be modified so that the component continuously examines the average of the last 5 readings, and if:

 0.05 <  A  < 3.00

then the Tare button should be “pressed”. Yes, there might be a better auto-tare function out there, but i am more interested on how to implement it in this fashion.

esphome:
  name: "funky-scale"
  on_boot:
    - delay: 1s
# to change password, uncomment on lambda and password, comment out ota_password and run. then restofe comments
  #   - lambda: |-
  #       id(my_ota).set_auth_password("xxx");

ota:
  - platform: esphome
    password: !secret ota_password
    id: my_ota
    # on_abort: 
    #   - delay: 15sec
    #   - lambda: id(my_ota).clean_rtc();

# safe_mode:
#   id: my_safe_mode
#   disabled: false
#   boot_is_good_after: 45s
#   num_attempts: 10
#   reboot_timeout: 5min
#   # on_safe_mode: SOME ACTION
  
  # # ... other safe_mode configuration
    # on_boot:
    #   - delay: 1s
    #   - lambda: id(my_ota).clean_rtc();

esp8266:
  board: d1_mini
  
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  reboot_timeout: 1min
  
  fast_connect: true

  manual_ip:
    static_ip: 172.16.68.72
    gateway: 172.16.68.1
    subnet: 255.255.255.0
  
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "hx711B Fallback Hotspot"
    password: "3IexvKFhv5qd"

# Enable logging
logger:
  level: DEBUG

# Enable Home Assistant API
api:
  encryption:
    key: !secret api_key
  reboot_timeout: 15sec 

captive_portal:

sensor:

  - platform: hx711
    name: "HX711 Value"
    dout_pin: D2
    clk_pin: D3
    # ... Other HX711 options
    gain: 128
    update_interval: 2s
    accuracy_decimals: 2
    unit_of_measurement: lbs
    filters:
      - calibrate_linear:
          - -4516200 -> 0
          - -4579200 -> 10
      - lambda: |-
          id(weight_no_tare).publish_state(x);
          return (x - id(weight_tare));

  - platform: template
    id: weight_no_tare
    internal: True
    accuracy_decimals: 2

globals:
  - id: weight_tare
    type: float
    restore_value: False
    initial_value: '0.0'

button:
  - platform: template
    id: weight_tare_set
    name: 'Tare'
    on_press:
      - lambda: id(weight_tare) = id(weight_no_tare).state;
      - logger.log: "Tare pressed"

  - platform: restart
    id: scale_restart
    name: "Restart Button"
    on_press: 
      - logger.log: "Restart Button"

switch:
- platform: restart
  id: scale_switch
  name: "Restart Switch" 
  on_turn_on: 
    - logger.log: "Restart Switch"

The BLS say this about programming
https://www.bls.gov/ooh/computer-and-information-technology/computer-programmers.htm

Computer programmers write, modify, and test code and scripts that allow computer software and applications to function properly. They turn the designs created by software developers and engineers into instructions that a computer can follow.

They say this about software developers
https://www.bls.gov/ooh/computer-and-information-technology/software-developers.htm

Software developers create the computer applications that allow users to do specific tasks and the underlying systems that run the devices or control networks. Software quality assurance analysts and testers design and execute software tests to identify problems and learn how the software works.

The industry is always changing, but I do agree this change feels more impactful than the ones that occurred in the previous decades. Only time will tell what the impact will really be.

I’m not saying I agree with a particular POV, but I can understand why some people feel this. I knew AI could write code, but I’m amazed it was able to answer a forum post. It’s suggestions had some bugs however lol!

This is why the forum rules are no AI generated code. It is particularly bad at doing esphome and in the hands of someone who doesn’t know better it just leads to confusion and typically really bad configs and code.

I still find it’s ability to understand my question amazing.