- binary_sensor:
    - name: 3D Printer (K1C) Filament
      unique_id: "192.168.2.315ce32fc9-5e95-49e7-80e7-bfdd2cf1d1fd"
      device_class: motion
      state: >
        {% set attr = states.sensor.printer_3d_sensor.attributes.get("filament_switch_sensor Filament") %}
        {{ attr.filament_detected if attr is defined else false }}
      icon: mdi:printer-3d-nozzle-alert
      availability: >
        {% set items = ['sensor.printer_3d_sensor'] %}
        {{ expand(items) | rejectattr('state','in',['unknown','unavailable']) | list | count == items | count }}
      attributes:
        friendly_name: "Filament Detected"

- sensor:
    - name: 3D Printer (K1C) Printer Name
      unique_id: "192.168.2.313a0f3144-a801-422f-adb6-e2ed35796074"
      state: '{{ states.sensor.printer_3d_server.attributes["printername"] }}'
      availability: >
        {% set items = ['sensor.printer_3d_server'] %}
        {{ expand(items) | rejectattr('state','in',['unknown','unavailable']) | list | count == items | count }}
      icon: mdi:printer-3d
      attributes:
        friendly_name: "Printer Name"

    # === Sensor: Chamber temperature (actual) ===
    - name: 3D Printer (K1C) Chamber Temp
      unique_id: "192.168.2.31167e5dec-719c-42d3-9560-4f177573f741"
      state: '{{ states.sensor.printer_3d_sensor.attributes["temperature_sensor Chamber_Temp"]["temperature"] | float(0) | round(1) }}'
      unit_of_measurement: "°C"
      icon: mdi:thermometer
      availability: >
        {% set items = ['sensor.printer_3d_sensor'] %}
        {{ expand(items)|rejectattr('state','in',['unknown','unavailable'])|list|count == items|count }}
      attributes:
        friendly_name: "Chamber Actual"

    # === Sensor: Hotend target temperature ===
    - name: 3D Printer (K1C) Hotend Target
      unique_id: "192.168.2.31167e5dec-719c-42d3-9560-4f177573f740"
      state: '{{ states.sensor.printer_3d_sensor.attributes["extruder"]["target"] | float(0) | round(1) }}'
      unit_of_measurement: "°C"
      icon: mdi:thermometer
      availability: >
        {% set items = ['sensor.printer_3d_sensor'] %}
        {{ expand(items)|rejectattr('state','in',['unknown','unavailable'])|list|count == items|count }}
      attributes:
        friendly_name: "Hotend Target"

    # === Sensor: Hotend actual temperature ===
    - name: 3D Printer (K1C) Hotend Actual
      unique_id: "192.168.2.317c2593fb-a7dd-4ed7-a865-2e4dddddbb40"
      state: '{{ states.sensor.printer_3d_sensor.attributes["extruder"]["temperature"] | float(0) | round(1) }}'
      unit_of_measurement: "°C"
      icon: mdi:thermometer
      availability: >
        {% set items = ['sensor.printer_3d_sensor'] %}
        {{ expand(items)|rejectattr('state','in',['unknown','unavailable'])|list|count == items|count }}
      attributes:
        friendly_name: "Hotend Actual"

    - name: 3D Printer (K1C) Hotend Power
      unique_id: "192.168.2.31c57a1e35-92fa-403e-b3cf-ec1e97ef94cf"
      state: '{{ states.sensor.printer_3d_sensor.attributes["extruder"]["power"]  | float(0) | round(3) * 100}}'
      availability: >
        {% set items = ['sensor.printer_3d_sensor'] %}
        {{ expand(items)|rejectattr('state','in',['unknown','unavailable'])
          |list|count == items|count }}
      unit_of_measurement: "%"
      icon: mdi:flash
      attributes:
        friendly_name: "Hotend Power"

    - name: 3D Printer (K1C) Bed Target
      unique_id: "192.168.2.31eadec415-c281-4814-8fba-17c6c42670ec"
      state: '{{ states.sensor.printer_3d_sensor.attributes["heater_bed"]["target"] | float(0) | round(1) }}'
      unit_of_measurement: "°C"
      icon: mdi:thermometer
      availability: >
        {% set items = ['sensor.printer_3d_sensor'] %}
        {{ expand(items)|rejectattr('state','in',['unknown','unavailable']) | list | count == items | count }}
      attributes:
        friendly_name: "Bed Target"

    - name: 3D Printer (K1C) Bed Actual
      unique_id: "192.168.2.31cb33503c-c5d4-4e06-b79b-c1cd64ca2d7a"
      state: '{{ states.sensor.printer_3d_sensor.attributes["heater_bed"]["temperature"] | float(0) | round(1) }}'
      unit_of_measurement: "°C"
      icon: mdi:thermometer
      availability: >
        {% set items = ['sensor.printer_3d_sensor'] %}
        {{ expand(items)|rejectattr('state','in',['unknown','unavailable']) | list | count == items | count }}
      attributes:
        friendly_name: "Bed Actual"

    - name: 3D Printer (K1C) Bed Power
      unique_id: "192.168.2.31abb2fc5b-8173-4131-a942-fb11e30f4efa"
      state: '{{ states.sensor.printer_3d_sensor.attributes["heater_bed"]["power"] | float(0) | round(3) * 100 }}'
      unit_of_measurement: "%"
      icon: mdi:flash
      availability: >
        {% set items = ['sensor.printer_3d_sensor'] %}
        {{ expand(items)|rejectattr('state','in',['unknown','unavailable']) | list | count == items | count }}
      attributes:
        friendly_name: "Bed Power"

    - name: 3D Printer (K1C) Printer State
      unique_id: "192.168.2.31758ce84c-3210-4f29-b6b3-38139180de96"
      state: '{{ states.sensor.printer_3d_sensor.attributes["print_stats"]["state"] }}'
      icon: >
        {% set val = states.sensor.printer_3d_sensor.attributes["print_stats"]["state"] %}
        {% if val == 'standby' %}
          mdi:sleep
        {% elif val == 'error' %}
          mdi:alert-circle
        {% elif val == 'printing' %}
          mdi:printer-3d-nozzle
        {% elif val == 'paused' %}
          mdi:pause-circle
        {% elif val == 'complete' %}
          mdi:printer-3d
        {% else %}
          mdi:help-circle
        {% endif %}
      availability: >
        {% set items = ['sensor.printer_3d_sensor'] %}
        {{ expand(items) | rejectattr('state', 'in', ['unknown','unavailable']) | list | count == items | count }}
      attributes:
        friendly_name: "Printer State"

    - name: 3D Printer (K1C) Current Print
      unique_id: "192.168.2.313a0f3144-a801-422f-adb6-e2ed35796072"
      state: '{{ states.sensor.printer_3d_sensor.attributes["print_stats"]["filename"] }}'
      icon: mdi:file
      availability: >
        {% set items = ['sensor.printer_3d_sensor'] %}
        {{ expand(items) | rejectattr('state', 'in', ['unknown','unavailable']) | list | count == items | count }}
      attributes:
        friendly_name: "Current Print"

    - name: 3D Printer (K1C) Current Progress
      unique_id: "192.168.2.318ec7163b-be00-4a10-8051-48cf9a260a29"
      state: '{{ ((states.sensor.printer_3d_sensor.attributes["display_status"]["progress"]) * 100) | round(0, "floor") }}'
      unit_of_measurement: "%"
      icon: mdi:progress-clock
      availability: >
        {% set items = ['sensor.printer_3d_sensor'] %}
        {{ expand(items) | rejectattr('state', 'in', ['unknown','unavailable']) | list | count == items | count }}
      attributes:
        friendly_name: "Progress"

    - name: 3D Printer (K1C) Print time
      unique_id: "192.168.2.31e1ac0fac-e8ba-4e05-8fa0-c8d8076f9f63"
      state: '{{ states.sensor.printer_3d_sensor.attributes["print_stats"]["print_duration"] | timestamp_custom("%H:%M:%S", 0) }}'
      icon: mdi:camera-timer
      availability: >
        {% set items = ['sensor.printer_3d_sensor'] %}
        {{ expand(items) | rejectattr('state', 'in', ['unknown','unavailable']) | list | count == items | count }}
      attributes:
        friendly_name: "Time Elapsed"

    - name: 3D Printer (K1C) Time Remaining
      unique_id: "192.168.2.31e12c8def-5ebe-43a4-9f75-605a0c46fc0f"
      state: >
        {{
          (
            (
              states.sensor.printer_3d_sensor.attributes["print_stats"]["print_duration"]
              / states.sensor.printer_3d_sensor.attributes["display_status"]["progress"]
              - states.sensor.printer_3d_sensor.attributes["print_stats"]["print_duration"]
            ) if states.sensor.printer_3d_sensor.attributes["display_status"]["progress"] > 0 else 0
          ) | timestamp_custom("%H:%M:%S", 0)
        }}
      icon: mdi:timer-sand
      availability: >
        {% set items = ['sensor.printer_3d_sensor'] %}
        {{ expand(items) | rejectattr('state','in',['unknown','unavailable']) | list | count == items | count }}
      attributes:
        friendly_name: "Time Remaining"

    - name: 3d_printer_eta
      unique_id: "192.168.2.31a2b57068-9a32-4d2c-8cc9-57d2389a9082"
      state: >
        {{
          (
            as_timestamp(now()) +
            (
              (
                states.sensor.printer_3d_sensor.attributes["print_stats"]["print_duration"]
                / states.sensor.printer_3d_sensor.attributes["display_status"]["progress"]
                - states.sensor.printer_3d_sensor.attributes["print_stats"]["print_duration"]
              ) if states.sensor.printer_3d_sensor.attributes["display_status"]["progress"] > 0 else 0
            )
          ) | timestamp_custom("%H:%M:%S", 1)
        }}
      icon: mdi:av-timer
      availability: >
        {% set items = ['sensor.printer_3d_sensor'] %}
        {{ expand(items) | rejectattr('state','in',['unknown','unavailable']) | list | count == items | count }}
      attributes:
        friendly_name: "ETA"

    - name: 3D Printer (K1C) Message
      unique_id: "192.168.2.310c418c0d-e59e-4d4e-aa11-8fae53df58f8"
      state: '{{ states.sensor.printer_3d_sensor.attributes["display_status"]["message"] }}'
      icon: mdi:message-cog
      availability: >
        {% set items = ['sensor.printer_3d_sensor'] %}
        {{ expand(items) | rejectattr('state','in',['unknown','unavailable']) | list | count == items | count }}
      attributes:
        friendly_name: "Message"

    - name: 3D Printer (K1C) Layer Height
      unique_id: "192.168.2.316cca455d-c327-4567-b886-4d7f99714265"
      state: '{{ states.sensor.printer_3d_file_metadata.attributes["layer_height"] | float(0) }}'
      unit_of_measurement: "mm"
      icon: mdi:arrow-collapse-down
      availability: >
        {% set items = ['sensor.printer_3d_file_metadata'] %}
        {{ expand(items) | rejectattr('state','in',['unknown','unavailable']) | list | count == items | count }}
      attributes:
        friendly_name: "Layer Height"

    - name: 3D Printer (K1C) Object Height
      unique_id: "192.168.2.316d6d9dc0-9a02-4ce4-a797-c84b42e011a6"
      state: '{{ (states.sensor.printer_3d_file_metadata.attributes["object_height"] | float(0)) - (states.sensor.printer_3d_file_metadata.attributes["layer_height"] | float(0)) }}'
      unit_of_measurement: "mm"
      icon: mdi:arrow-expand-vertical
      availability: >
        {% set items = ['sensor.printer_3d_file_metadata'] %}
        {{ expand(items) | rejectattr('state','in',['unknown','unavailable']) | list | count == items | count }}
      attributes:
        friendly_name: "Object Height"

    - name: 3D Printer (K1C) Current Height
      unique_id: "192.168.2.31d440e568-d4d1-4b3f-85c4-fdacd68c0e1a"
      state: '{{ states.sensor.printer_3d_sensor.attributes["gcode_move"]["gcode_position"][2] | float(0) | round(2) }}'
      unit_of_measurement: "mm"
      icon: mdi:arrow-collapse-down
      availability: >
        {% set items = ['sensor.printer_3d_sensor'] %}
        {{ expand(items) | rejectattr('state','in',['unknown','unavailable']) | list | count == items | count }}
      attributes:
        friendly_name: "Current Height"

    - name: 3D Printer (K1C) Current Layer
      unique_id: "192.168.2.316a77acc1-8134-4354-b2f6-390adab81993"
      state: '{{ (states("sensor.3d_printer_current_height") | float(0) / states("sensor.3d_printer_layer_height") | float(0)) | round(0) }}'
      icon: mdi:counter
      availability: >
        {% set items = ['sensor.3d_printer_current_height','sensor.3d_printer_layer_height'] %}
        {{ expand(items) | rejectattr('state','in',['unknown','unavailable']) | list | count == items | count }}
      attributes:
        friendly_name: "Current Layer"

    - name: 3D Printer (K1C) Total Layers
      unique_id: "192.168.2.3153cfe906-28ec-44cd-926a-b08ffb8766e5"
      state: '{{ (states("sensor.3d_printer_object_height") | float(0) / states("sensor.3d_printer_layer_height") | float(0)) | round(0) }}'
      icon: mdi:counter
      availability: >
        {% set items = ['sensor.3d_printer_object_height','sensor.3d_printer_layer_height'] %}
        {{ expand(items) | rejectattr('state','in',['unknown','unavailable']) | list | count == items | count }}
      attributes:
        friendly_name: "Total Layers"

    - name: 3D Printer (K1C) Actual Layer
      unique_id: "192.168.2.31167e5dec-719c-42d3-9560-4f177573f742"
      state: '{{ states.sensor.printer_3d_sensor.attributes.print_stats["info"]["current_layer"] }} / {{ states.sensor.printer_3d_sensor.attributes.print_stats["info"]["total_layer"] }}'
      icon: mdi:layers-triple
      availability: >
        {% set items = ['sensor.printer_3d_sensor'] %}
        {{ expand(items) | rejectattr('state','in',['unknown','unavailable']) | list | count == items | count }}
      attributes:
        friendly_name: "Actual Layer"

    - name: 3D Printer (K1C) Object Thumbnails
      unique_id: "192.168.2.3159b37837-b751-4d31-98c2-516a52edf833"
      state: >
        {% set dir = states('sensor.3d_printer_current_print') %}
        {% set thumb = state_attr('sensor.printer_3d_file_metadata','thumbnails') %}
        {% set img = (thumb | last).relative_path if thumb else 'not available' %}
        {{ (dir.split('/')[:-1] + [img]) | join('/') }}
      icon: mdi:image
      availability: >
        {% set items = ['sensor.printer_3d_file_metadata'] %}
        {{ expand(items) | rejectattr('state','in',['unknown','unavailable']) | list | count == items | count }}
      attributes:
        friendly_name: "Object Thumbnails"

    - name: 3D Printer (K1C) State Message
      unique_id: "192.168.2.319a5184c9-ac5b-44a9-a691-2b67c243d197"
      state: '{{ states.sensor.printer_3d_info.attributes["state_message"] }}'
      icon: mdi:message-cog
      availability: >
        {% set items = ['sensor.printer_3d_info'] %}
        {{ expand(items) | rejectattr('state','in',['unknown','unavailable']) | list | count == items | count }}
      attributes:
        friendly_name: "State Message"

    - name: 3D Printer (K1C) Bed and Nozzle Power
      unique_id: "192.168.2.313988e9be-c910-4634-8205-38d53170d5a5"
      state: '{{ states("sensor.3d_printer_bed_power") | float(0) | round(1) }}% / {{ states("sensor.3d_printer_hotend_power") | float(0) | round(1) }}%'
      icon: mdi:meter-electric-outline
      availability: >
        {% set items = ['sensor.3d_printer_bed_power','sensor.3d_printer_hotend_power'] %}
        {{ expand(items) | rejectattr('state','in',['unknown','unavailable']) | list | count == items | count }}
      attributes:
        friendly_name: "Bed and Nozzle Power"
