- binary_sensor:
    - name: Vedligehold Tesla
      unique_id: maintenance_tesla_needed
      state: >-
        {{
          states('sensor.snowywhite_battery') | float(100) < 20
          or is_state('update.snowywhite_software_update', 'on')
        }}

    - name: Vedligehold bil
      unique_id: maintenance_car_needed
      state: >-
        {{
          states('sensor.snowywhite_tpms_front_left') | float(99) < 39
          or states('sensor.snowywhite_tpms_front_right') | float(99) < 39
          or states('sensor.snowywhite_tpms_rear_left') | float(99) < 39
          or states('sensor.snowywhite_tpms_rear_right') | float(99) < 39
        }}

    - name: Vedligehold stovsuger
      unique_id: maintenance_vacuum_needed
      state: >-
        {{
          is_state('binary_sensor.roborock_s8_pro_ultra_dock_clean_water_box', 'on')
          or is_state('binary_sensor.roborock_s8_pro_ultra_dock_dirty_water_box', 'on')
          or (states('sensor.roborock_s8_pro_ultra_vacuum_error') not in ['none', 'unknown', 'unavailable'])
          or (states('sensor.roborock_s8_pro_ultra_dock_error') not in ['ok', 'unknown', 'unavailable'])
          or states('sensor.roborock_s8_pro_ultra_filter_restlevetid') | float(999) < 20
          or states('sensor.roborock_s8_pro_ultra_dock_maintenance_brush_time_left') | float(999) < 20
        }}

    - name: Vedligehold opvasker
      unique_id: maintenance_dishwasher_needed
      state: >-
        {{
          is_state('binary_sensor.dishwasher_info_2', 'on')
          or is_state('binary_sensor.dishwasher_svigt', 'on')
          or is_state('sensor.dishwasher_status_2', 'service')
          or states('sensor.dishwasher_powerdisk_level') | float(999) < 20
          or states('sensor.dishwasher_salt_level') | float(999) < 20
          or states('sensor.dishwasher_rinse_aid_level') | float(999) < 20
        }}

    - name: Vedligehold stovsuger fejl
      unique_id: maintenance_vacuum_error_active
      state: >-
        {{ states('sensor.roborock_s8_pro_ultra_vacuum_error') not in ['none', 'unknown', 'unavailable'] }}

    - name: Vedligehold dock fejl
      unique_id: maintenance_dock_error_active
      state: >-
        {{ states('sensor.roborock_s8_pro_ultra_dock_error') not in ['ok', 'unknown', 'unavailable'] }}

    - name: Vedligehold batterier
      unique_id: maintenance_batteries_needed
      state: >-
        {{
          [
            states('sensor.andreas_switch_battery') | float(100),
            states('sensor.daniel_switch_battery') | float(100),
            states('sensor.stue_switch_battery') | float(100),
            states('sensor.indkorsel_sensor_battery') | float(100),
            states('sensor.stue_motion_batteri') | float(100),
            states('sensor.hue_motion_sensor_1_battery') | float(100),
            states('sensor.hue_motion_sensor_1_battery_2') | float(100),
            states('sensor.hue_motion_sensor_2_battery') | float(100),
            states('sensor.hue_motion_sensor_2_battery_2') | float(100),
            states('sensor.bad_motion_sensor_battery') | float(100),
            states('sensor.gang_sensor_battery') | float(100),
            states('sensor.forgang_sensor_battery') | float(100),
            states('sensor.kontor_motion_batteri') | float(100),
            states('sensor.garage_sensor_battery') | float(100),
            states('sensor.anne_battery') | float(100),
            states('sensor.midt_battery') | float(100),
            states('sensor.terrasse_dor_battery') | float(100),
            states('sensor.venstre_battery') | float(100),
            states('sensor.syd_batteri') | float(100),
            states('sensor.hojre_battery') | float(100),
            states('sensor.sovevaerelse_vindue_batteri') | float(100),
            states('sensor.daniel_vindue_batteri') | float(100),
            states('sensor.andreas_vindue_batteri') | float(100),
            states('sensor.badevaerelse_vindue_batteri') | float(100),
            states('sensor.lille_bad_vindue_batteri') | float(100),
            states('sensor.terrassedor_batteri') | float(100),
          ] | min < 20
        }}

    - name: Vedligehold Home Assistant
      unique_id: maintenance_home_assistant_needed
      state: >-
        {{
          is_state('binary_sensor.home_assistant_website_update_available', 'on')
          or is_state('update.hacs_update', 'on')
          or states('sensor.hacs_opdateringer') | int(0) > 0
        }}

    - name: Vedligehold affald
      unique_id: maintenance_garbage_needed
      state: >-
        {{
          states('sensor.affalddk_norgardsvej_22_restaffald') | float(999) <= 1
          or states('sensor.affalddk_norgardsvej_22_papir_plast') | float(999) <= 1
          or states('sensor.affalddk_norgardsvej_22_haveaffald') | float(999) <= 1
        }}

    - name: Vedligehold Synology DSM
      unique_id: maintenance_synology_dsm_needed
      state: >-
        {{
          is_state('binary_sensor.dethlefsen_security_status', 'on')
          or is_state('update.dethlefsen_dsm_update', 'on')
          or states('sensor.dethlefsen_drive_1_status') | lower not in ['normal', 'unknown', 'unavailable', 'none']
          or states('sensor.dethlefsen_drive_2_status') | lower not in ['normal', 'unknown', 'unavailable', 'none']
          or states('sensor.dethlefsen_drive_3_status') | lower not in ['normal', 'unknown', 'unavailable', 'none']
          or states('sensor.dethlefsen_drive_4_status') | lower not in ['normal', 'unknown', 'unavailable', 'none']
          or is_state('binary_sensor.dethlefsen_drive_1_exceeded_max_bad_sectors', 'on')
          or is_state('binary_sensor.dethlefsen_drive_2_exceeded_max_bad_sectors', 'on')
          or is_state('binary_sensor.dethlefsen_drive_3_exceeded_max_bad_sectors', 'on')
          or is_state('binary_sensor.dethlefsen_drive_4_exceeded_max_bad_sectors', 'on')
          or is_state('binary_sensor.dethlefsen_drive_1_below_min_remaining_life', 'on')
          or is_state('binary_sensor.dethlefsen_drive_2_below_min_remaining_life', 'on')
          or is_state('binary_sensor.dethlefsen_drive_3_below_min_remaining_life', 'on')
          or is_state('binary_sensor.dethlefsen_drive_4_below_min_remaining_life', 'on')
        }}

    - name: Vedligehold Synology disk 1 status
      unique_id: maintenance_synology_drive_1_status_needed
      state: >-
        {{ states('sensor.dethlefsen_drive_1_status') | lower not in ['normal', 'unknown', 'unavailable', 'none'] }}

    - name: Vedligehold Synology disk 2 status
      unique_id: maintenance_synology_drive_2_status_needed
      state: >-
        {{ states('sensor.dethlefsen_drive_2_status') | lower not in ['normal', 'unknown', 'unavailable', 'none'] }}

    - name: Vedligehold Synology disk 3 status
      unique_id: maintenance_synology_drive_3_status_needed
      state: >-
        {{ states('sensor.dethlefsen_drive_3_status') | lower not in ['normal', 'unknown', 'unavailable', 'none'] }}

    - name: Vedligehold Synology disk 4 status
      unique_id: maintenance_synology_drive_4_status_needed
      state: >-
        {{ states('sensor.dethlefsen_drive_4_status') | lower not in ['normal', 'unknown', 'unavailable', 'none'] }}

- sensor:
    - name: HACS opdateringer
      unique_id: hacs_pending_updates
      unit_of_measurement: opdateringer
      icon: mdi:package-variant
      state: >-
        {{
          integration_entities('hacs')
          | select('search', '^update\.')
          | map('states')
          | select('eq', 'on')
          | list | count
        }}

    - name: Antal vedligeholdskort
      unique_id: maintenance_cards_count
      unit_of_measurement: kort
      icon: mdi:wrench-cog
      state: >-
        {{
          [
            1 if is_state('binary_sensor.home_assistant_website_update_available', 'on') else 0,
            1 if is_state('update.hacs_update', 'on') else 0,
            1 if states('sensor.hacs_opdateringer') | int(0) > 0 else 0,
            1 if is_state('binary_sensor.dethlefsen_security_status', 'on') else 0,
            1 if is_state('update.dethlefsen_dsm_update', 'on') else 0,
            1 if is_state('binary_sensor.vedligehold_synology_disk_1_status', 'on') else 0,
            1 if is_state('binary_sensor.vedligehold_synology_disk_2_status', 'on') else 0,
            1 if is_state('binary_sensor.vedligehold_synology_disk_3_status', 'on') else 0,
            1 if is_state('binary_sensor.vedligehold_synology_disk_4_status', 'on') else 0,
            1 if is_state('binary_sensor.dethlefsen_drive_1_exceeded_max_bad_sectors', 'on') else 0,
            1 if is_state('binary_sensor.dethlefsen_drive_2_exceeded_max_bad_sectors', 'on') else 0,
            1 if is_state('binary_sensor.dethlefsen_drive_3_exceeded_max_bad_sectors', 'on') else 0,
            1 if is_state('binary_sensor.dethlefsen_drive_4_exceeded_max_bad_sectors', 'on') else 0,
            1 if is_state('binary_sensor.dethlefsen_drive_1_below_min_remaining_life', 'on') else 0,
            1 if is_state('binary_sensor.dethlefsen_drive_2_below_min_remaining_life', 'on') else 0,
            1 if is_state('binary_sensor.dethlefsen_drive_3_below_min_remaining_life', 'on') else 0,
            1 if is_state('binary_sensor.dethlefsen_drive_4_below_min_remaining_life', 'on') else 0,
            1 if states('sensor.affalddk_norgardsvej_22_restaffald') | float(999) <= 1 else 0,
            1 if states('sensor.affalddk_norgardsvej_22_papir_plast') | float(999) <= 1 else 0,
            1 if states('sensor.affalddk_norgardsvej_22_haveaffald') | float(999) <= 1 else 0,
            1 if states('sensor.snowywhite_battery') | float(100) < 20 else 0,
            1 if is_state('update.snowywhite_software_update', 'on') else 0,
            1 if states('sensor.snowywhite_tpms_front_left') | float(99) < 39 else 0,
            1 if states('sensor.snowywhite_tpms_front_right') | float(99) < 39 else 0,
            1 if states('sensor.snowywhite_tpms_rear_left') | float(99) < 39 else 0,
            1 if states('sensor.snowywhite_tpms_rear_right') | float(99) < 39 else 0,
            1 if is_state('binary_sensor.roborock_s8_pro_ultra_dock_clean_water_box', 'on') else 0,
            1 if is_state('binary_sensor.roborock_s8_pro_ultra_dock_dirty_water_box', 'on') else 0,
            1 if states('sensor.roborock_s8_pro_ultra_filter_restlevetid') | float(999) < 20 else 0,
            1 if states('sensor.roborock_s8_pro_ultra_dock_maintenance_brush_time_left') | float(999) < 20 else 0,
            1 if is_state('binary_sensor.vedligehold_stovsuger_fejl', 'on') else 0,
            1 if is_state('binary_sensor.vedligehold_dock_fejl', 'on') else 0,
            1 if is_state('binary_sensor.dishwasher_info_2', 'on') else 0,
            1 if is_state('binary_sensor.dishwasher_svigt', 'on') else 0,
            1 if is_state('sensor.dishwasher_status_2', 'service') else 0,
            1 if states('sensor.dishwasher_powerdisk_level') | float(999) < 20 else 0,
            1 if states('sensor.dishwasher_salt_level') | float(999) < 20 else 0,
            1 if states('sensor.dishwasher_rinse_aid_level') | float(999) < 20 else 0,
            1 if states('sensor.andreas_switch_battery') | float(100) < 20 else 0,
            1 if states('sensor.daniel_switch_battery') | float(100) < 20 else 0,
            1 if states('sensor.stue_switch_battery') | float(100) < 20 else 0,
            1 if states('sensor.indkorsel_sensor_battery') | float(100) < 20 else 0,
            1 if states('sensor.stue_motion_batteri') | float(100) < 20 else 0,
            1 if states('sensor.hue_motion_sensor_1_battery') | float(100) < 20 else 0,
            1 if states('sensor.hue_motion_sensor_1_battery_2') | float(100) < 20 else 0,
            1 if states('sensor.hue_motion_sensor_2_battery') | float(100) < 20 else 0,
            1 if states('sensor.hue_motion_sensor_2_battery_2') | float(100) < 20 else 0,
            1 if states('sensor.bad_motion_sensor_battery') | float(100) < 20 else 0,
            1 if states('sensor.gang_sensor_battery') | float(100) < 20 else 0,
            1 if states('sensor.forgang_sensor_battery') | float(100) < 20 else 0,
            1 if states('sensor.kontor_motion_batteri') | float(100) < 20 else 0,
            1 if states('sensor.garage_sensor_battery') | float(100) < 20 else 0,
            1 if states('sensor.anne_battery') | float(100) < 20 else 0,
            1 if states('sensor.midt_battery') | float(100) < 20 else 0,
            1 if states('sensor.terrasse_dor_battery') | float(100) < 20 else 0,
            1 if states('sensor.venstre_battery') | float(100) < 20 else 0,
            1 if states('sensor.syd_batteri') | float(100) < 20 else 0,
            1 if states('sensor.hojre_battery') | float(100) < 20 else 0,
            1 if states('sensor.garageport_batteri') | float(100) < 20 else 0,
            1 if states('sensor.sovevaerelse_vindue_batteri') | float(100) < 20 else 0,
            1 if states('sensor.daniel_vindue_batteri') | float(100) < 20 else 0,
            1 if states('sensor.andreas_vindue_batteri') | float(100) < 20 else 0,
            1 if states('sensor.badevaerelse_vindue_batteri') | float(100) < 20 else 0,
            1 if states('sensor.lille_bad_vindue_batteri') | float(100) < 20 else 0,
            1 if states('sensor.terrassedor_batteri') | float(100) < 20 else 0,
            1 if is_state('lawn_mower.husqvarna_automower', 'error') else 0
          ] | sum
        }}