was kinda unimpressed at first… everything i tried to get it to make code for was a failure… but i dont know enough about coding to guide it in any usefull way
however… i have found chatgpt is quite usefull at just doing stuff for me when i use it properly
the following is how i got chatgpt to create code for me to import glances data
me to chatgpt:
this code imports glances api data about the cpu into home assistant as seperate entities
rest:
###############################################################
- resource: http://localhost:61208/api/3/all
sensor:
- name: cpu
value_template: '{{ value_json.cpu.total | default }}'
json_attributes_path: '$.cpu'
json_attributes:
- total
- user
- nice
- system
- idle
- iowait
- irq
- softirq
- steal
- guest
- guest_nice
- time_since_update
- cpucore
- ctx_switches
- interrupts
- soft_interrupts
- syscalls
sensor:
- platform: template
sensors:
hassio_cpu_total:
friendly_name: "CPU Total"
value_template: "{{ state_attr('sensor.cpu', 'total') }}"
availability_template: "{{ states('sensor.cpu') not in ['unknown', 'unavailable', 'none'] }}"
unit_of_measurement: '%'
hassio_cpu_user:
friendly_name: "CPU User"
value_template: "{{ state_attr('sensor.cpu', 'user') }}"
availability_template: "{{ states('sensor.cpu') not in ['unknown', 'unavailable', 'none'] }}"
unit_of_measurement: '%'
hassio_cpu_nice:
friendly_name: "CPU Nice"
value_template: "{{ state_attr('sensor.cpu', 'nice') }}"
availability_template: "{{ states('sensor.cpu') not in ['unknown', 'unavailable', 'none'] }}"
unit_of_measurement: '%'
hassio_cpu_system:
friendly_name: "CPU System"
value_template: "{{ state_attr('sensor.cpu', 'system') }}"
availability_template: "{{ states('sensor.cpu') not in ['unknown', 'unavailable', 'none'] }}"
unit_of_measurement: '%'
hassio_cpu_idle:
friendly_name: "CPU Idle"
value_template: "{{ state_attr('sensor.cpu', 'idle') }}"
availability_template: "{{ states('sensor.cpu') not in ['unknown', 'unavailable', 'none'] }}"
unit_of_measurement: '%'
hassio_cpu_iowait:
friendly_name: "CPU IOWait"
value_template: "{{ state_attr('sensor.cpu', 'iowait') }}"
availability_template: "{{ states('sensor.cpu') not in ['unknown', 'unavailable', 'none'] }}"
unit_of_measurement: '%'
hassio_cpu_irq:
friendly_name: "CPU IRQ"
value_template: "{{ state_attr('sensor.cpu', 'irq') }}"
availability_template: "{{ states('sensor.cpu') not in ['unknown', 'unavailable', 'none'] }}"
unit_of_measurement: '%'
hassio_cpu_softirq:
friendly_name: "CPU SoftIRQ"
value_template: "{{ state_attr('sensor.cpu', 'softirq') }}"
availability_template: "{{ states('sensor.cpu') not in ['unknown', 'unavailable', 'none'] }}"
unit_of_measurement: '%'
hassio_cpu_steal:
friendly_name: "CPU Steal"
value_template: "{{ state_attr('sensor.cpu', 'steal') }}"
availability_template: "{{ states('sensor.cpu') not in ['unknown', 'unavailable', 'none'] }}"
unit_of_measurement: '%'
hassio_cpu_guest:
friendly_name: "CPU Guest"
value_template: "{{ state_attr('sensor.cpu', 'guest') }}"
availability_template: "{{ states('sensor.cpu') not in ['unknown', 'unavailable', 'none'] }}"
unit_of_measurement: '%'
hassio_cpu_guest_nice:
friendly_name: "CPU Guest Nice"
value_template: "{{ state_attr('sensor.cpu', 'guest_nice') }}"
availability_template: "{{ states('sensor.cpu') not in ['unknown', 'unavailable', 'none'] }}"
unit_of_measurement: '%'
hassio_cpu_time_since_update:
friendly_name: "CPU Time Since Update"
value_template: "{{ state_attr('sensor.cpu', 'time_since_update') }}"
availability_template: "{{ states('sensor.cpu') not in ['unknown', 'unavailable', 'none'] }}"
hassio_cpu_cpucore:
friendly_name: "CPU Core"
value_template: "{{ state_attr('sensor.cpu', 'cpucore') }}"
availability_template: "{{ states('sensor.cpu') not in ['unknown', 'unavailable', 'none'] }}"
hassio_cpu_ctx_switches:
friendly_name: "CPU Context Switches"
value_template: "{{ state_attr('sensor.cpu', 'ctx_switches') }}"
availability_template: "{{ states('sensor.cpu') not in ['unknown', 'unavailable', 'none'] }}"
hassio_cpu_interrupts:
friendly_name: "CPU Interrupts"
value_template: "{{ state_attr('sensor.cpu', 'interrupts') }}"
availability_template: "{{ states('sensor.cpu') not in ['unknown', 'unavailable', 'none'] }}"
hassio_cpu_soft_interrupts:
friendly_name: "CPU Soft Interrupts"
value_template: "{{ state_attr('sensor.cpu', 'soft_interrupts') }}"
availability_template: "{{ states('sensor.cpu') not in ['unknown', 'unavailable', 'none'] }}"
show me a configuration using the same syntax and style as the code i just provided to import the following data from the mem branch of the glances api with friendly names and measurement units included and automatically converte all byte types to megabytes
total
available
percent
used
free
active
inactive
buffers
cached
shared
ChatGPT
response:
Sure, here's the configuration to import the memory data from the Glances API with the desired friendly names and measurement units:
yaml
rest:
###############################################################
- resource: http://localhost:61208/api/3/all
sensor:
- name: memory
value_template: '{{ value_json.mem.total | default }}'
json_attributes_path: '$.mem'
json_attributes:
- total
- available
- percent
- used
- free
- active
- inactive
- buffers
- cached
- shared
sensor:
- platform: template
sensors:
hassio_memory_total:
friendly_name: "Memory Total"
value_template: "{{ (state_attr('sensor.memory', 'total') | int / 1048576) | round(2) }}"
availability_template: "{{ states('sensor.memory') not in ['unknown', 'unavailable', 'none'] }}"
unit_of_measurement: 'MB'
hassio_memory_available:
friendly_name: "Memory Available"
value_template: "{{ (state_attr('sensor.memory', 'available') | int / 1048576) | round(2) }}"
availability_template: "{{ states('sensor.memory') not in ['unknown', 'unavailable', 'none'] }}"
unit_of_measurement: 'MB'
hassio_memory_percent:
friendly_name: "Memory Percent"
value_template: "{{ state_attr('sensor.memory', 'percent') }}"
availability_template: "{{ states('sensor.memory') not in ['unknown', 'unavailable', 'none'] }}"
unit_of_measurement: '%'
hassio_memory_used:
friendly_name: "Memory Used"
value_template: "{{ (state_attr('sensor.memory', 'used') | int / 1048576) | round(2) }}"
availability_template: "{{ states('sensor.memory') not in ['unknown', 'unavailable', 'none'] }}"
unit_of_measurement: 'MB'
hassio_memory_free:
friendly_name: "Memory Free"
value_template: "{{ (state_attr('sensor.memory', 'free') | int / 1048576) | round(2) }}"
availability_template: "{{ states('sensor.memory') not in ['unknown', 'unavailable', 'none'] }}"
unit_of_measurement: 'MB'
hassio_memory_active:
friendly_name: "Memory Active"
value_template: "{{ (state_attr('sensor.memory', 'active') | int / 1048576) | round(2) }}"
availability_template: "{{ states('sensor.memory') not in ['unknown', 'unavailable', 'none'] }}"
unit_of_measurement: 'MB'
hassio_memory_inactive:
friendly_name: "Memory Inactive"
value_template: "{{ (state_attr('sensor.memory', 'inactive') | int / 1048576) | round(2) }}"
availability_template: "{{ states('sensor.memory') not in ['unknown', 'unavailable', 'none'] }}"
unit_of_measurement: 'MB'
hassio_memory_buffers:
friendly_name: "Memory Buffers"
value_template: "{{ (state_attr('sensor.memory', 'buffers') | int / 1048576) | round(2) }}"
availability_template: "{{ states('sensor.memory') not in ['unknown', 'unavailable', 'none'] }}"
unit_of_measurement: 'MB'
hassio_memory_cached:
friendly_name: "Memory Cached"
value_template: "{{ (state_attr('sensor.memory', 'cached') | int / 1048576) | round(2) }}"
availability_template: "{{ states('sensor.memory') not in ['unknown', 'unavailable', 'none'] }}"
unit_of_measurement: 'MB'
hassio_memory_shared:
friendly_name: "Memory Shared"
value_template: "{{ (state_attr('sensor.memory', 'shared') | int / 1048576) | round(2) }}"
availability_template: "{{ states('sensor.memory') not in ['unknown', 'unavailable', 'none'] }}"
unit_of_measurement: 'MB'
In this configuration, we use the same style and syntax as before. The memory data is fetched from the Glances API's "mem" branch, and we convert all byte types to megabytes by dividing them by 1048576 (which is 1024^2). The round(2) function is used to round the values to two decimal places for a cleaner representation of the memory sizes. Each sensor is provided with a friendly name, unit of measurement, and an availability template to check if the data is available before displaying it.```