Skip to content

Instantly share code, notes, and snippets.

@Fabaderheld
Created December 17, 2023 14:41
Show Gist options
  • Save Fabaderheld/87373c736662bb3ac7f1e265e64975cd to your computer and use it in GitHub Desktop.
Save Fabaderheld/87373c736662bb3ac7f1e265e64975cd to your computer and use it in GitHub Desktop.
Filamet esphome config backup

esphome: name: filament friendly_name: filament includes: - filament.h

esp8266: board: nodemcu

board: d1_mini

Enable logging

logger:

Enable Home Assistant API

api: encryption: key: "+3vxGeHwGfsiIqB3hbb0g3Z2QXDkDWwhXbZEsbO8uW0="

ota: password: "66cd52cdbd7b04b8e3a092cb3779aec1"

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

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

ap: ssid: "Filament Fallback Hotspot" password: !secret ap_password

captive_portal:

web_server: version: 2

font:

  • file: "fonts/comic.ttf" id: my_font size: 15

  • file: "fonts/comic.ttf" id: big_font size: 25

binary_sensor: #physical buttom

  • platform: gpio pin: D7 name: button1 filters:
    • delayed_off: 30ms

globals:

  • id: initial_zero type: float restore_value: yes

    NOTE: make sure to align this value to the one used in "calibrate_linear" below!

    initial_value: "-227236"

  • id: auto_tare_enabled type: bool restore_value: yes initial_value: "true"

  • id: auto_tare_difference type: float restore_value: yes initial_value: "0"

sensor:

template sensors from global variables

  • platform: template id: smart_scale_initial_zero name: "Smart Scale Initial Zero" lambda: |- return id(initial_zero); update_interval: 1s

  • platform: template id: smart_scale_auto_tare_difference name: "Smart Scale Auto Tare Difference" lambda: |- return id(auto_tare_difference); update_interval: 1s

Load cell

  • platform: hx711 id: smart_scale_hx711_value_raw dout_pin: D5 clk_pin: D6 gain: 128 update_interval: 0.2s filters:
    • quantile: window_size: 10 send_every: 1 send_first_at: 1 quantile: .9 on_value: then:
      • sensor.template.publish: id: smart_scale_hx711_value state: !lambda "return id(smart_scale_hx711_value_raw).state;"
      • if: condition: and: - lambda: "return id(auto_tare_enabled);" # current smart scale value is below approx. 10KG (raw value -275743) aka nobody is standing on the scale - lambda: "return id(smart_scale_hx711_value).state < 1.0;" then: - if: condition: # current raw scale value is below expected zero value - lambda: "return id(smart_scale_hx711_value_raw).state < (id(initial_zero) - id(auto_tare_difference));" then: # INcrease Auto-Tare offset to slowly align real zero value with expected zero value - lambda: |- id(auto_tare_difference) += 1; else: # DEcrease Auto-Tare offset to slowly align real zero value with expected zero value - lambda: |- id(auto_tare_difference) -= 1;

Mapped value to G

  • platform: template id: smart_scale_hx711_value name: "Smart Scale HX711 Value" internal: False filters:

    apply auto_tare difference

    • lambda: "return x + id(auto_tare_difference);"

    apply rough calibration

    • calibrate_linear:

      retrieve these values by evaluating the raw values with loads of known mass.

      note that a bigger difference between measurements usually results in higher resolution,

      so measure 0 G and the highest known mass you have (like f.ex. your own weight, measured by a normal scale with good accuracy)

      • "-227236 -> 0"
      • "-120101 -> 530"

    map values below 0.1 to 0 (to decrease value changes due to random fluctuation)

    • lambda: |- if (x <= 0.1) { return 0.0; } else { return x; }
  • platform: template name: "Spool Weight"

JOYA Blue

  • platform: homeassistant name: "Filament Spool Weight" id: joya_blue_spool_weight entity_id: sensor.joya_blue_pla attribute: "filament_spool_weight"
  • platform: homeassistant name: "Filament Weight" id: joya_blue_weight entity_id: sensor.joya_blue_pla attribute: "remaining_weight"

text_sensor:

# JOYA Blue

- platform: homeassistant

name: "Filament Name"

id: joya_blue_name

entity_id: sensor.joya_blue_pla

attribute: "friendly_name"

# JOYA Red

- platform: homeassistant

name: "Filament Name"

id: joya_red_name

entity_id: sensor.joya_red_pla

attribute: "friendly_name"

- platform: homeassistant

name: "Filament Weight"

id: joya_red_weight

entity_id: sensor.joya_red_pla

attribute: "remaining_weight"

# JOYA White

- platform: homeassistant

name: "Filament Name"

id: joya_white_name

entity_id: sensor.joya_white_pla

attribute: "friendly_name"

- platform: homeassistant

name: "Filament Weight"

id: joya_white_weight

entity_id: sensor.joya_white_pla

attribute: "remaining_weight"

# JOYA Black

- platform: homeassistant

name: "Filament Name"

id: joya_black_name

entity_id: sensor.joya_black_pla

attribute: "friendly_name"

- platform: homeassistant

name: "Filament Weight"

id: joya_black_weight

entity_id: sensor.joya_black_pla

attribute: "remaining_weight"

button:

  • platform: template name: "Next Page" id: next_page

    Optional variables:

    icon: "mdi:emoticon-outline" on_press:

    • display.page.show_next: my_display

switch:

Switch to enable/disable the auto tare feature

  • platform: template id: smart_scale_continuous_tare_enabled name: "Smart Scale Continuous Tare Enabled" lambda: |- return id(auto_tare_enabled); turn_on_action:
    • lambda: |- id(auto_tare_enabled) = true; turn_off_action:
    • lambda: |- id(auto_tare_enabled) = false;

i2c: sda: D1 scl: D2 frequency: 800kHz

display:

  • platform: ssd1306_i2c model: "SSD1306 128x32" reset_pin: D0 address: 0x3C id: my_display pages:
    • id: scale lambda: |- //it.printf(0, 0, id(my_font),"%.0f", id(smart_scale_hx711_value).state - id(joya_blue_spool_weight).state); it.printf(50, 0, id(big_font),"%.0f", id(smart_scale_hx711_value).state);
    • id: filament_scale lambda: |- //it.printf(0, 0, id(my_font),"%.0f", id(smart_scale_hx711_value).state - id(joya_blue_spool_weight).state); it.printf(50, 0, id(big_font),"%.0f", id(smart_scale_hx711_value).state);

- id: joya_red_page

lambda: |-

auto red = Color(255, 0, 0);

it.printf(0, 0, id(my_font),"%s", id(joya_red_name).state.c_str());

it.printf(0, 15, id(my_font),"%s", id(joya_red_weight).state.c_str());

// it.filled_rectangle(64, 15, 100, 32, red);

- id: joya_blue_page

lambda: |-

it.printf(0, 0, id(my_font),"%s", id(joya_blue_name).state.c_str());

it.printf(0, 15, id(my_font),"%.0f", id(joya_blue_weight).state);

- id: joya_white_page

lambda: |-

it.printf(0, 0, id(my_font),"%s", id(joya_white_name).state.c_str());

it.printf(0, 15, id(my_font),"%s", id(joya_white_weight).state.c_str());

- id: joya_black_page

lambda: |-

it.printf(0, 0, id(my_font),"%s", id(joya_black_name).state.c_str());

it.printf(0, 15, id(my_font),"%s", id(joya_black_weight).state.c_str());

it.print(0, 0, id(my_font), "%f", id(filament).state);

http_request: useragent: esphome/device timeout: 10s id: http_request_data interval:

  • interval: 1min then:

    • logger.log: format: "Filament weight with spool is is %1.0f" args: ["id(joya_blue_spool_weight).state + id(joya_blue_weight).state"]

    - homeassistant.service:

    service: input_select.set_options

    data_template:

    options: >

    [

    {% for entity in integration_entities('spoolman') %}

    "{{ state_attr(entity, 'friendly_name') }}",

    {% endfor -%}

    ]

    target:

    entity_id: input_select.filaments

  • interval: 5sec then:

    • logger.log: format: "current weight is %f" args: ["id(smart_scale_hx711_value).state"]

- http_request.get:

verify_ssl: false

on_response:

then:

- lambda: |-

json::parse_json(id(http_request_data).get_string(), [](JsonObject root) {

id(filament_id).publish_state(root["id"]);

});

- logger.log:

format: "Filament ID is %.1f"

args: [ 'id(filament_id).state' ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment