I found a use for chat gpt

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.```

It may be correct but is not the best way to do it.

  1. It is using the outdated legacy template sensor format.

  2. All those sensors can be created directly, without the need for additional template sensors, by using the rest integration rather than the rest sensor platform (using one call to the resource).

Just use the value templates in this:

well its no doubt going to be flawed… was more saying its amazing that i can take something i struggled to get my head around, like the yaml code itself at times… and the whole api keys and arrays stuff…and produce a working result…

theres probably way better ways to do anything it suggests, and if i was doing a legit ha installation instead of just playing with things to learn,
i wouldnt be able to do it without annoying smarter people, who have better things to do with their time than walk a casual user through a very specific solution…

i feel like if this got better and maybe was trained specifically on all aspects of home assistant, we could see a day where the onboarding process could include conversing with home assistant via chatgpt to integrate everything you want, and even the ability to just verbally dictate and guide your way through all sorts of custom configurations

1 Like

Typical. :roll_eyes: The cutoff date for ChatGPT’s training material was September 2021 - it won’t know anything about developments since then.

It’s never going to do the work for you. Some software developers find it a useful tool for producing rough drafts quickly, but you have to be an expert yourself to refine that.

Bard is actually more up to date because it doesn’t have a cut off date. I just asked it how to convert old style templates to the new format and it gave me instructions and examples :slight_smile:

(All my template sensors are already in the new format however!)

This is not to blame ChatGPT… I‘m sure it‘s good for anything.

But to me it seems I need more time to explain ChatGPT what I want instead of writing the code by myself.
And if I need to check the correctness… well, then I need to know what’s correct. But when I know what’s correct…why asking ChatGPT?

If I do not know how to code it, I would never trust a tool like ChatGPT, but that’s my personal point of view.

AI is impressive no doubt but for writing trustworthy code?

Using chatGPT to explain how to use chatGPT = win.

No that is a lose actually. Instant account ban.

1 Like

I hate that sarcasm can’t be properly conveyed in text form. Apologies

1 Like

How incredibly interesting. /s

:wink:

1 Like