Cannot spot the error

Hi! So I tried creating a widget (quite new to this) myself and I though I had it working but alas… no. And I couldn’t for the life of me figure out why and so I had to start over trying to get ChatGPT to help me out and it wrote the following code for me:

uid: power_usage_widget_rrd4j
tags:
  - power
props:
  parameter:
    label: Power Consumption Item
    context: item
  barChartColor:
    label: Bar Chart Color
    context: visualization
    description: Set the color of the bar chart. Default is blue.
    default: blue
slots:
  content:
    - component: f7-card
      slots:
        header:
          - component: f7-card-header
            slots:
              default:
                - component: f7-row
                  slots:
                    left:
                      - component: f7-col
                        slots:
                          default:
                            - component: Label
                              config:
                                text: Daily Power Usage
                                style:
                                  font-weight: bold
                    right:
                      - component: f7-col
                        slots:
                          default:
                            - component: f7-button
                              config:
                                text: Refresh
                                raised: true
                                fill: true
                                iconF7: refresh
                                on:
                                  click:
                                    then:
                                      - component: oh-repeater
                                        config:
                                          fragment: true
                                          for: item in props.parameter
                                          sourceType: currentStates
                                          filter:
                                            - type: item
                                              item: =item.name
                                          transform:
                                            - name: calculateDailyUsage
                                              params:
                                                fieldName: usageDaily
                                                function: |
                                                  var startTime = now.toDateMidnight().toInstant();
                                                  var endTime = now.toInstant();
                                                  var data = items[item.name].getHistoricState(startTime, endTime, "rrd4j").getStateSeries();
                                                  var total = 0;
                                                  data.forEach(function(element) {
                                                    total += element.doubleValue();
                                                  });
                                                  return Math.round(total * 100) / 100;
                                          slots:
                                            default:
                                              - component: oh-list-item
                                                config:
                                                  title: =item.label
                                                  item: =item.name
                                                  icon: f7:bolt
                                                  action: none
                                                  badge: "={{Math.round(items[item.name].state)}} W"
                                                  badgeColor: =props.barChartColor
                                                  badgeStyle:
                                                    font-size: 14px
                                              - component: ChartCard
                                                config:
                                                  type: bar
                                                  label: Daily Power Usage
                                                  data:
                                                    datasets:
                                                      - label: Power Usage
                                                        backgroundColor: =props.barChartColor
                                                        data: =items[item.name].historicState(now.toDateMidnight, "rrd4j").filter(startDate > now.minusDays(1)).sortedByState.sumSince(startDate).groupBy("HOUR").averageByGroup
                                                  options:
                                                    scales:
                                                      yAxes:
                                                        - ticks:
                                                            beginAtZero: true
                                                          scaleLabel:
                                                            display: true
                                                            labelString: "Watts"
                                                    legend:
                                                      display: false
                                                    tooltips:
                                                      enabled: false
                                                    animation:
                                                      duration: 0
                                                    responsive: true
                                                    maintainAspectRatio: false
                                                    aspectRatio: 2
                                                  style:
                                                    height: 200px
                                                    margin-top: 5px
                                                    margin-bottom: 5px
                                        configRefresh:
                                          type: simple
                                          timeout: 300000

When I input this into the widget creator, nothing shows up. Its just blank and I cannot figure out why that is. What I’m trying to create is just a simple bar chart with the daily power consumption for one single item. Any ideas?

To be blunt: the error is here

That generated code is worthless, I’m afraid. It’s not even a starting point. There are so many problems with it that’s is not even instructive to go through them. For just one example, there isn’t even a root component (which at least is why you got a blank display instead of hundreds of errors, I suspect).

It’s going to be a very long time before a LLM is going to do a good job with OH widget coding. 1) with chatGPT’s 2021 date limitation there are not that many widget examples for it to work from 2) a large number of the widgets in its training set are probably posts about “this doesn’t work can someone help” which means it’s training from broken widget examples 3) the number of OH widget examples in it’s training data are vastly outnumbered by other yaml configurations both for OH and non OH situations. Honestly, I am a little surprised it did as well as it did (which I would grade at around a D or D- level).

At the least, this is a good demonstration that custom widgets still need to be coded the old-fashioned way.

The best way to start with a custom chart widget is to try and create what you want in the chart page wizard first and then copy the generated yaml and use that as the base of your widget.