# Loads default set of integrations. Do not remove.
default_config:

assist_pipeline:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include_dir_merge_named scripts
scene: !include scenes.yaml

stream:
  ll_hls: true
  part_duration: 0.75
  segment_duration: 6

mqtt:
  sensor:
    - state_topic: "/meyer/pos"
      name: "Meyer Position Raw"
      unit_of_measurement: "thou"

    - state_topic: "/jc/height/log"
      name: "JC Height Raw"
      unit_of_measurement: "thou"

    - state_topic: "/jc/angle/log"
      name: "JC Angle Raw"
      unit_of_measurement: "thou"

    - state_topic: "/jc/feed/log"
      name: "JC Feed Angle Raw"
      unit_of_measurement: "degrees"
      value_template: >-
        {{ value | round(0) }}

    - state_topic: "/scale/cup1/tare"
      name: "Cup 1 Tare Weight"
      unique_id: "cup1_tare_weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup-outline"

    - state_topic: "/scale/cup2/tare"
      unique_id: "cup2_tare_weight"
      name: "Cup 2 Tare Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup-outline"
    - state_topic: "/scale/cup3/tare"
      unique_id: "cup3_tare_weight"
      name: "Cup 3 Tare Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup-outline"
    - state_topic: "/scale/cup4/tare"
      unique_id: "cup4_tare_weight"
      name: "Cup 4 Tare Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup-outline"
    - state_topic: "/scale/cup5/tare"
      unique_id: "cup5_tare_weight"
      name: "Cup 5 Tare Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup-outline"
    - state_topic: "/scale/cup6/tare"
      unique_id: "cup6_tare_weight"
      name: "Cup 6 Tare Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup-outline"
    - state_topic: "/scale/cup7/tare"
      unique_id: "cup7_tare_weight"
      name: "Cup 7 Tare Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup-outline"
    - state_topic: "/scale/cup8/tare"
      unique_id: "cup8_tare_weight"
      name: "Cup 8 Tare Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup-outline"
    - state_topic: "/scale/cup9/tare"
      unique_id: "cup9_tare_weight"
      name: "Cup 9 Tare Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup-outline"
    - state_topic: "/scale/cup10/tare"
      unique_id: "cup10_tare_weight"
      name: "Cup 10 Tare Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup-outline"
    - state_topic: "/scale/cup11/tare"
      unique_id: "cup11_tare_weight"
      name: "Cup 11 Tare Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup-outline"
    - state_topic: "/scale/cup12/tare"
      unique_id: "cup12_tare_weight"
      name: "Cup 12 Tare Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup-outline"
    - state_topic: "/scale/cup13/tare"
      unique_id: "cup13_tare_weight"
      name: "Cup 13 Tare Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup-outline"
    - state_topic: "/scale/cup14/tare"
      unique_id: "cup14_tare_weight"
      name: "Cup 14 Tare Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup-outline"
    - state_topic: "/scale/cup15/tare"
      unique_id: "cup15_tare_weight"
      name: "Cup 15 Tare Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup-outline"
    - state_topic: "/scale/cup16/tare"
      unique_id: "cup16_tare_weight"
      name: "Cup 16 Tare Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup-outline"

    - state_topic: "/scale/cup1/general"
      unique_id: "cup1_general_weight"
      name: "Cup 1 General Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup2/general"
      unique_id: "cup2_general_weight"
      name: "Cup 2 General Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup3/general"
      unique_id: "cup3_general_weight"
      name: "Cup 3 General Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup4/general"
      unique_id: "cup4_general_weight"
      name: "Cup 4 General Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup5/general"
      unique_id: "cup5_general_weight"
      name: "Cup 5 General Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup6/general"
      unique_id: "cup6_general_weight"
      name: "Cup 6 General Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup7/general"
      unique_id: "cup7_general_weight"
      name: "Cup 7 General Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup8/general"
      unique_id: "cup8_general_weight"
      name: "Cup 8 General Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup9/general"
      unique_id: "cup9_general_weight"
      name: "Cup 9 General Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup10/general"
      unique_id: "cup10_general_weight"
      name: "Cup 10 General Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup11/general"
      unique_id: "cup11_general_weight"
      name: "Cup 11 General Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup12/general"
      name: "Cup 12 General Sample Weight"
      unique_id: "cup12_general_weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup13/general"
      unique_id: "cup13_general_weight"
      name: "Cup 13 General Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup14/general"
      unique_id: "cup14_general_weight"
      name: "Cup 14 General Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup15/general"
      unique_id: "cup15_general_weight"
      name: "Cup 15 General Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup16/general"
      unique_id: "cup16_general_weight"
      name: "Cup 16 General Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"

    - state_topic: "/scale/cup1/half"
      unique_id: "cup1_half_weight"
      name: "Cup 1 Half Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup2/half"
      unique_id: "cup2_half_weight"
      name: "Cup 2 Half Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup3/half"
      unique_id: "cup3_half_weight"
      name: "Cup 3 Half Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup4/half"
      unique_id: "cup4_half_weight"
      name: "Cup 4 Half Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup5/half"
      unique_id: "cup5_half_weight"
      name: "Cup 5 Half Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup6/half"
      unique_id: "cup6_half_weight"
      name: "Cup 6 Half Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup7/half"
      unique_id: "cup7_half_weight"
      name: "Cup 7 Half Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup8/half"
      name: "Cup 8 Half Sample Weight"
      unique_id: "cup8_half_weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup9/half"
      unique_id: "cup9_half_weight"
      name: "Cup 9 Half Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup10/half"
      unique_id: "cup10_half_weight"
      name: "Cup 10 Half Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup11/half"
      unique_id: "cup11_half_weight"
      name: "Cup 11 Half Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup12/half"
      unique_id: "cup12_half_weight"
      name: "Cup 12 Half Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup13/half"
      unique_id: "cup13_half_weight"
      name: "Cup 13 Half Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup14/half"
      unique_id: "cup14_half_weight"
      name: "Cup 14 Half Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup15/half"
      unique_id: "cup15_half_weight"
      name: "Cup 15 Half Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"
    - state_topic: "/scale/cup16/half"
      unique_id: "cup16_half_weight"
      name: "Cup 16 Half Sample Weight"
      unit_of_measurement: "oz"
      icon: "mdi:cup"

    - state_topic: "/sheller/bin1"
      name: "Sheller Bin 1 Weight"
      unique_id: "bin1_weight"
      unit_of_measurement: "lb"
    - state_topic: "/sheller/bin2"
      name: "Sheller Bin 2 Weight"
      unique_id: "bin2_weight"
      unit_of_measurement: "lb"
    - state_topic: "/sheller/bin3"
      name: "Sheller Bin 3 Weight"
      unique_id: "bin3_weight"
      unit_of_measurement: "lb"
    - state_topic: "/sheller/bin4"
      name: "Sheller Bin 4 Weight"
      unique_id: "bin4_weight"
      unit_of_measurement: "lb"
    - state_topic: "/half_yield"
      unique_id: "half_yield_sample"
      name: "Latest Half Yield Sample"
      unit_of_measurement: "%"

  number:
    - command_topic: "/jc/angle"
      name: "JC Angle"
      unit_of_measurement: "thou"
      min: -500
      max: 500
    - command_topic: "/jc/height"
      name: "JC Height"
      unique_id: "jc_height"
      unit_of_measurement: "thou"
      min: 0
      max: 1665
    - command_topic: "/jc/feed"
      name: "JC Feed Angle"
      unit_of_measurement: "degrees"
      min: 0
      max: 90
    - command_topic: "/jc/feed_time"
      name: "JC Feed Time"
      unit_of_measurement: "seconds"
      min: 5
      max: 90
    - command_topic: "/sheller/angle"
      name: "Sheller Angle"
      unique_id: "sheller_angle"
      unit_of_measurement: "degrees"
      min: 2.5
      max: 5.5
      step: 0.5

  switch:
    - command_topic: "/jc/use_feed_time"
      name: "JC Limit Feed Duration"

sensor:
  - platform: tcp
    host: 172.22.113.200
    port: 23
    payload: ""
    name: "Precision Scale"
    scan_interval:
      seconds: 0.1
    value_template: >-
      {% if not value | contains('M') %}
        {% set temp_value = value | regex_findall_index('\d+\.\d+(?=oz)') | float %}
        {% if value | contains('-') %}
          {% set temp_value = temp_value * -1 %}
        {% endif %}
        {{ temp_value | round(3) }}
      {% else %}
        {{ states("sensor.precision_scale") | round(3) }}
      {% endif %}
    unit_of_measurement: "oz"
  - platform: tcp
    host: 172.22.113.200
    port: 26
    payload: ""
    name: "Sheller Scale"
    scan_interval:
      seconds: 0.1
    value_template: >-
      {% if value | contains('S') %}
        {% set temp_value = value | regex_findall_index('\d+\.\d+(?=  lb)') | float %}
        {% if value | contains('-') %}
          {% set temp_value = temp_value * -1 %}
        {% endif %}
        {{ ((temp_value * 50 - (0.5 if temp_value > 0 else -0.5)) | int | float / 50) }}
      {% else %}
        {{ states("sensor.sheller_scale") }}
      {% endif %}
    unit_of_measurement: "lb"
  - platform: tcp
    host: 172.22.113.201
    port: 23
    payload: ""
    name: "Moisture Scale"
    scan_interval:
      seconds: 1
    value_template: >-
      {% if value | contains('S') %}
        {% set temp_value = value | regex_findall_index('\d+(?=, g)') | float %}
        {% if value | contains('-') %}
          {% set temp_value = temp_value * -1 %}
        {% endif %}
        {{ temp_value }}
      {% else %}
        {{ states("sensor.moisture_scale") }}
      {% endif %}
    unit_of_measurement: "g"
  - platform: serial
    serial_port: /dev/steinlite
    name: "steinlite"

  - platform: pid_controller
    name: paddle_pid
    enabled: '{{ states("switch.shellyplus1_cc7b5c0d316c_switch_0") }}'
    set_point: '{{ states("input_number.sheller_paddle_rpm") }}'
    entity_id: sensor.shelling_machine_paddle_rpm
    precision: 0
    minimum: 0
    maximum: 253
    sample_time: 1
    invert: true
    p: 40
    i: 0
    d: 0
  - platform: pid_controller
    name: drum_pid
    enabled: '{{ states("switch.shellyplus1_cc7b5c0d0eb4_switch_0") }}'
    set_point: '{{ states("input_number.sheller_drum_rpm") }}'
    entity_id: sensor.shelling_machine_drum_rpm
    precision: 0
    minimum: 0
    maximum: 253
    sample_time: 2.5
    invert: true
    p: 5
    i: 0
    d: 0

template:
  - sensor:
      - name: Steinlite Moisture Content
        unique_id: steinlite_moisture
        unit_of_measurement: "%"
        state: >
          {% set sensor_val = states('sensor.steinlite') %}
          {% set moisture_match = sensor_val.find('Moisture: ') %}
          {% if moisture_match != -1 %} 
            {% set moisture_value = sensor_val[moisture_match + 10:sensor_val.find('%',moisture_match)] %}
            {% if moisture_value %} 
              {% set state = moisture_value | float %}
            {% endif %}
          {% endif %}

          {% if state is defined %} {{ state }}
          {% else %} {{ this.state }}
          {% endif %}
      - name: Steinlite Sample Temperature
        unique_id: steinlite_temperature
        unit_of_measurement: "°F"
        state: >
          {% set sensor_val = states('sensor.steinlite') %}
          {% set temp_match = sensor_val.find('Gr Temp: ') %}
          {% if temp_match != -1 %} 
            {% set temp_value = sensor_val[temp_match + 9:sensor_val.find(' Deg F',temp_match)] %}
            {% if temp_value %} 
              {% set state = temp_value | int %}
            {% endif %}
          {% endif %}

          {% if state is defined %} {{ state }}
          {% else %} {{ this.state }}
          {% endif %}

input_number:
  moisture_cold_soak_time:
    name: Cold Soak Time
    min: 0
    max: 75
    unit_of_measurement: hours
  moisture_hot_soak_time:
    name: Hot Soak Time
    min: 0
    max: 45
    unit_of_measurement: minutes
  moisture_ambient_temperature:
    name: Ambient Temperature
    min: 68
    max: 74
    unit_of_measurement: °F
  moisture_hot_bath_temperature:
    name: Hot Soak Temperature
    min: 180
    max: 200
    unit_of_measurement: °F
  jc_pecan_grade:
    name: Pecan Size
    min: 250
    max: 1250
    mode: box
    step: 0.5
    icon: mdi:peanut
    unit_of_measurement: thou
  jc_crush_amount:
    name: Crush Amount
    min: 31.25
    max: 93.75
    step: 31.25
    unit_of_measurement: thou
  jc_hopper_angle:
    name: Angle
    min: 0
    max: 90
    step: 1
    icon: mdi:angle-acute
    unit_of_measurement: °
  jc_plate_frequency:
    name: Impacting Plate Frequency
    min: 30
    max: 60
    step: 0.1
    unit_of_measurement: Hz
  jc_feeder_frequency:
    name: Vibratory Feeder Frequency
    min: 30
    max: 90
    step: 0.1
    unit_of_measurement: Hz

  sheller_paddle_rpm:
    name: Paddle Shaft RPM
    min: 400
    max: 800
    step: 50
    unit_of_measurement: RPM
  sheller_drum_rpm:
    name: Drum RPM
    min: 30
    max: 40
    step: 2
    unit_of_measurement: RPM
  sheller_ring_gap:
    name: Sheller Ring Gap
    min: 0
    max: 1.125
    mode: box
    unit_of_measurement: in

  meyer_preload_stiffness:
    name: Preload Spring Stiffness
    min: 1800
    max: 2200
    mode: box
    unit_of_measurement: N/m
  meyer_motor_speed:
    name: Motor Speed
    min: 30
    max: 60
    step: 0.1
    unit_of_measurement: Hz

  batch_weight:
    name: Batch Weight
    unit_of_measurement: lb
    min: 0
    max: 45
    step: 0.01
    mode: box

remote_homeassistant:
  instances:

shell_command:
  lsdev: ls /dev/

#ltss:
#    db_url: postgresql://postgres@factory.uga.edu/postgres
#    chunk_time_interval: 86400
#    commit_interval: 5
#    include:
#      domains:
#      - sensor
#      - number
#      - input_number
#    exclude:
#      entity_globs:
#      - 'sensor.sun*'

mqtt_eventstream:
  publish_topic: pecan/hass

modbus:
  - name: modbus_hub
    type: tcp
    host: 192.168.1.190
    port: 502
    sensors:
      - name: JC Status
        slave: 1
        address: 680
        input_type: holding

recorder:
  db_url: !secret connection_string
  auto_purge: false
