Newbie asked ChatGPT for an automatization. Tell me why it doesn't work. One button Toggle on/off and dimming

So I’m trying to create a for me, advanced script, maybe its not even possible. Here’s what want to do:
I want my one button switch to toggle my light on/off and dim up and down. Since I’m new I thought hey let’s ask ChatGPT. I got a code that to me looks good but what do I know. Anyone wanna tell me what’s wrong with it? I might be a noob, but before anyone asks, of course I have changed the name of the light group entity to the real entity of the light group

input_boolean:
  dim_direction:
    initial: off

automation:
  - alias: Toggle Your Light Group On/Off with Single Press
    trigger:
      platform: state
      entity_id: switch.lampknapp_sovrum
    action:
      - service: script.toggle_light_group

  - alias: Dimming Your Group with Long Press and Change Direction with Double Press
    trigger:
      platform: state
      entity_id: switch.lampknapp_sovrum
    action:
      - service: script.start_dimming_group
        data:
          direction_entity: input_boolean.dim_direction

script:
  toggle_light_group:
    sequence:
      - service: light.toggle
        target:
          entity_id: light.your_light_group  # Replace with your light group entity_id

  start_dimming_group:
    sequence:
      - condition: state
        entity_id: input_boolean.dim_direction
        state: 'on'
      - service: input_boolean.turn_off
        target:
          entity_id: input_boolean.dim_direction
      - service: light.turn_on
        target:
          entity_id: light.your_light_group  # Replace with your light group entity_id
        data:
          brightness_step_pct: 5  # Adjust this value for the desired dimming level
      - wait_template: "{{ is_state('switch.lampknapp_sovrum', 'off') }}"
      - service: light.turn_off
        target:
          entity_id: light.your_light_group  # Replace with your light group entity_id
    default:
      - service: input_boolean.turn_on
        target:
          entity_id: input_boolean.dim_direction
      - service: light.turn_on
        target:
          entity_id: light.your_light_group  # Replace with your light group entity_id
        data:
          brightness_step_pct: -5  # Adjust this value for the desired dimming level

Tell its author, ChatGPT, that its suggestion failed to work and to correct it.


“What’s wrong with it” is that ChatGPT has an established track record of serving up malformed examples and that’s one of them. It’s got about 90% of it right and then it throws in something that is invalid within the given context.

Kind of what I expected. My expectation was to get somehow any kind of an idea how to do it.

What kind of “button switch” and how is it integrated to Home Assistant?


EDIT

ChatGPT produced an automation to control dimming via “Long Press” and “Double Press”. However, the example’s State Trigger is monitoring the state of a switch entity. A switch reports on or off and not button events like press/release/hold/etc.

Did it suggest to use a State Trigger with a switch or did you change its original example? Just trying to measure the extent of ChatGPT’s understanding of basic concepts.

First mistake! You expected that ChatGPT would do it for you. It has never done anything right unless given a lot of help before attempting it. That’s why a lot of students are failing their studies, relying on it instead of putting in the work.

So you now need to troubleshoot it your self. We are not your next AIs, to figure out the wrong part.

Hint: there are tons of examples in many threads here in the community. Get your fingers busy.

1 Like

basically, if you look at the two automations the first thing that pops out to me is that they both have exactly the same trigger. So that means that both scripts will be started at exactly the same time.

One script toggles the light group. Then nothing else.

And the other one’s first action if the boolean is on is to turn the boolean off and then turn light group on to 5%, waits for the switch to be turned off (forever?) and turns the lights back off. And if it’s not on then the script simply stops. Then nothing.

So which of those scripts get started first? You can never know as it’s totally determined by the system. I believe it’s what is referred to as a “race condition”. So will the light toggle first or will the dimming script run first? Who knows.

TBH i’m still not even sure where the input_boolean comes into play here. Are you turning it on somewhere else?

And as far as the “default:” is concerned I’m not sure what that would do since there is no branching behavior to decide whether to select some group of actions or a default group of actions.

Even if it could choose the “default” actions it just simply turns the light on to -5%. Once. Then stops.

Where exactly is the second automation looking for a “long press” and/or “double press”? I guess you could consider the forever wait for the switch to be turned back off as a “long press” but what if it never gets turned off?

As noted above the whole thing is a mess of words that seems like they should work if you have no idea what it should actually look like.

And therein lies the problem. ChatGPT tricks you into thinking that it’s “almost right” except for a tweak or two but in fact it’s almost entirely wrong. And since you don’t know what is right you have no idea how wrong it is. And then you come on here asking to tell you why it won’t work.

That’s the long answer.

The short answer is because it’s completely wrong.

I even have to keep looking at it to see if I’m completely missing something because the logic is so flawed I can’t believe how wrong it actually is. But I don’t think I am.

Can I just say that this is an absolutely crazy question?

Please don’t ask ChatGPT how to build a defibrillator and wonder why you killed someone!

1 Like

Opinion

As a software dev, I think there’s no harm consulting ChatGPT, but as others have said, it will reliably spew out incorrect stuff.

What will be more effective for you is to find a similar automation or blueprint available here in the community, look at their yaml, and read the documentation to understand what’s involved.

That way, you gain exposure quickly to what you’re trying to perform and at the same time understanding each of the pieces that is involved. Takes some time, not as easy as asking ChatGPT, but your bioGPT (read brain) will get the training it needs.


As for why the generated script didn’t work, it’s using totally incorrect keys for the yaml. Some of those keys do not belong in an automation yaml.

1 Like

“Code salad”

This helped me a lot. And have been able to make a few of the functions I’m looking for to work. Thanks!

1 Like

How exactly? It said what everyone else told you, that the example doesn’t work because it’s profoundly wrong.

The real “Solution” to this topic is to post an example that fulfills your requirements:

I want my one button switch to toggle my light on/off and dim up and down

If you have created such an example, post it so others can learn from it. Otherwise, it’s misleading to say this topic has been solved.

I don’t think I agree with that at all.

Every one of those keys are actually valid and the yaml syntax is generally correct. The only key that is nonsensical is the “default:” key that randomly got stuck in there. But even that yaml key is valid in an automation/script - just not there.

And again that’s the problem. To the uninitiated it looks like it SHOULD be right. And even the config checker will be likely fooled by it because it is mostly proper syntax.

Im choosing to to put the solution here since you were the only one who literally didn’t call me an idiot. So here is my solution for this.

some of you got very angry because of my OP, and got even more angrier of the jokes I put here in the beginning

Instead of yelling on some new user, I’m gonna write my solution below so other new users can get their answers

What I did was:
1. Create a Toggle helper. I named this Brightness direction (entity ID input_boolean.brightness_direction)

2. Create an automation that toggles the input_boolean on and off

alias: Input bolean vrum
description: ""
trigger:
  - platform: device
    device_id: eef6161ab643317b4ed2fe5697c6ab26
    domain: shelly
    type: double_push
    subtype: button1
condition: []
action:
  - service: input_boolean.toggle
    metadata: {}
    data: {}
    target:
      entity_id: input_boolean.brightness_direction
mode: single

3. Create the dim up automatization

alias: Vardagsrum dimmer upp
description: ""
trigger:
  - platform: device
    device_id: eef6161ab643317b4ed2fe5697c6ab26
    domain: shelly
    type: long_push
    subtype: button1
    id: dim-up-longpress
  - platform: device
    device_id: eef6161ab643317b4ed2fe5697c6ab26
    domain: shelly
    type: btn_up
    subtype: button1
    id: dim-up-release
condition:
  - condition: state
    entity_id: input_boolean.brightness_direction
    state: "on"
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - dim-up-longpress
        sequence:
          - repeat:
              sequence:
                - parallel:
                    - device_id: 66ae3e122f54e8736abaea3fe5aa1fa5
                      domain: light
                      entity_id: 84b3cc9394e801b8277e7446375fb8ed
                      type: brightness_increase
                    - device_id: d54b20204faa1b83a263d4d10e24f230
                      domain: light
                      entity_id: d85dea314a72c6e2614b2c1b84f6d2ca
                      type: brightness_increase
                    - device_id: d07161b2ecd417e9e33b7d92a1941c77
                      domain: light
                      entity_id: 22b3cf4c9ba874c4b98b5af35d5bcc09
                      type: brightness_increase
                    - delay:
                        hours: 0
                        minutes: 0
                        seconds: 0
                        milliseconds: 500
              until:
                - condition: trigger
                  id:
                    - dim-up-release
  - choose:
      - conditions:
          - condition: trigger
            id:
              - dim-down-doublepress
        sequence:
          - repeat:
              sequence:
                - parallel:
                    - device_id: ""
                      domain: ""
                      entity_id: ""
                    - device_id: ""
                      domain: ""
                      entity_id: ""
                    - device_id: ""
                      domain: ""
                      entity_id: ""
                    - delay:
                        hours: 0
                        minutes: 0
                        seconds: 0
                        milliseconds: 500
              until:
                - condition: trigger
                  id:
                    - dim-up-release
mode: restart

4. Create the dim down automatization

alias: Vardagsrum dimmer ner
description: ""
trigger:
  - platform: device
    device_id: eef6161ab643317b4ed2fe5697c6ab26
    domain: shelly
    type: long_push
    subtype: button1
    id: dim-up-longpress
  - platform: device
    device_id: eef6161ab643317b4ed2fe5697c6ab26
    domain: shelly
    type: btn_up
    subtype: button1
    id: dim-up-release
condition:
  - condition: state
    entity_id: input_boolean.brightness_direction
    state: "off"
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - dim-up-longpress
        sequence:
          - repeat:
              sequence:
                - parallel:
                    - device_id: 66ae3e122f54e8736abaea3fe5aa1fa5
                      domain: light
                      entity_id: 84b3cc9394e801b8277e7446375fb8ed
                      type: brightness_decrease
                    - device_id: d54b20204faa1b83a263d4d10e24f230
                      domain: light
                      entity_id: d85dea314a72c6e2614b2c1b84f6d2ca
                      type: brightness_decrease
                    - device_id: d07161b2ecd417e9e33b7d92a1941c77
                      domain: light
                      entity_id: 22b3cf4c9ba874c4b98b5af35d5bcc09
                      type: brightness_decrease
                    - delay:
                        hours: 0
                        minutes: 0
                        seconds: 0
                        milliseconds: 500
              until:
                - condition: trigger
                  id:
                    - dim-up-release
  - choose:
      - conditions:
          - condition: trigger
            id:
              - dim-down-doublepress
        sequence:
          - repeat:
              sequence:
                - parallel:
                    - device_id: ""
                      domain: ""
                      entity_id: ""
                    - device_id: ""
                      domain: ""
                      entity_id: ""
                    - device_id: ""
                      domain: ""
                      entity_id: ""
                    - delay:
                        hours: 0
                        minutes: 0
                        seconds: 0
                        milliseconds: 500
              until:
                - condition: trigger
                  id:
                    - dim-up-release
mode: restart
1 Like

Regarding this:

          - repeat:
                - parallel:
                    - device_id: ""
                      domain: ""
                      entity_id: ""
                    - device_id: ""
                      domain: ""
                      entity_id: ""
                    - device_id: ""
                      domain: ""
                      entity_id: ""
                    - delay:
                        hours: 0
                        minutes: 0
                        seconds: 0
                        milliseconds: 500
              until:
                - condition: trigger
                  id:
                    - dim-up-release

Did you intend to include empty Device Actions or did you overlook to complete them for the example? Because the Device Actions in the dim-up-longpress loop aren’t empty whereas they are in the loop for dim-down-doublepress.

For that matter, what’s the purpose of executing that repeat-until in parallel mode when the entire automation is set to restart mode?

Why use two separate choose statements when one will suffice?

For future reference, the examples rely on Device Triggers which have shortcomings in terms of portability and maintenance (see this community guide).

Have you considered browsing through Blueprints Exchange for an existing blueprint that supports your Shelly button?

https://community.home-assistant.io/search?q=Shelly%20button%20%23blueprints-exchange

Thanks for your feedback. The reason it’s set up with “choose” and “repeat” is because I could only get my ‘long_push’ to only dim 10% no matter how long I held the button pressed. I got some information and inspiration from this video (https://www.youtube.com/watch?v=L-bcabdaMxE&t=628s)

I will look at those links you posted to see if there’s anyway to make the dimming smoother instead of 10% steps. I mean, it works perfectly fine. but it want it to be perfect.[quote=“123, post:15, topic:670841, full:true”]

Regarding this:

          - repeat:
                - parallel:
                    - device_id: ""
                      domain: ""
                      entity_id: ""
                    - device_id: ""
                      domain: ""
                      entity_id: ""
                    - device_id: ""
                      domain: ""
                      entity_id: ""
                    - delay:
                        hours: 0
                        minutes: 0
                        seconds: 0
                        milliseconds: 500
              until:
                - condition: trigger
                  id:
                    - dim-up-release

Did you intend to include empty Device Actions or did you overlook to complete them for the example? Because the Device Actions in the dim-up-longpress loop aren’t empty whereas they are in the loop for dim-down-doublepress.

For that matter, what’s the purpose of executing that repeat-until in parallel mode when the entire automation is set to restart mode?

Why use two separate choose statements when one will suffice?

For future reference, the examples rely on Device Triggers which have shortcomings in terms of portability and maintenance (see this community guide).

Have you considered browsing through Blueprints Exchange for an existing blueprint that supports your Shelly button?

https://community.home-assistant.io/search?q=Shelly%20button%20%23blueprints-exchange
[/quote]

I don’t know if you are aware of it but that entire second choose can never execute because it’s looking for a trigger.id equal to dim-down-doublepress but neither of the two Device Triggers is configured to report that.

In other words, half of the action code, in both automations, does nothing. Is that intentional, because these examples are a ‘work-in-progress’, or not?

FWIW, those two automations can be combined into one, using some simple templating, but you would need to use service calls instead of a Device Actions (service calls support templates but Device Actions don’t .

Of course the OP is not aware of that. ChatGPT did not tell it to them. But, it did tell them to act like they have a girlfriend!!!

1 Like

Ah, I found what you meant and no, that was still there from my first try with this automatization. I removed it. I will take a look and see how I can out combine this into one. Would be needed since there’s a few rooms id like to have this for and it would be easier to have just one for each room and not five.

Thanks

You may wish to consider removing the Solution tag from that post. The example presented has far too many questionable aspects to recommend for use by new users. Wait until you have cleaned up the code.

BTW, have you had time to formulate an answer to the question of why you chose to use parallelize the repeat despite the automation’s mode being restart? Like the second choose that is never executed, the parallelization never occurs (i.e. it’s ‘dead code’).

Sounds like it could benefit from an automation blueprint but that’s a stretch goal.