Script not saving after editing in yaml

Open it in any Text editor, copy/paste.

OK, here it is.

{
  "trace": {
    "last_step": "action/1",
    "run_id": "22861f803918616f64a8ed9620ca786d",
    "state": "stopped",
    "script_execution": "error",
    "timestamp": {
      "start": "2024-02-08T02:04:56.655184+00:00",
      "finish": "2024-02-08T02:04:56.677072+00:00"
    },
    "domain": "automation",
    "item_id": "1706944419680",
    "error": "Error rendering data template: UndefinedError: 'dict object' has no attribute 'value'",
    "trigger": null,
    "trace": {
      "trigger": [
        {
          "path": "trigger",
          "timestamp": "2024-02-08T02:04:56.655417+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.new_automation",
              "state": "on",
              "attributes": {
                "id": "1706944419680",
                "last_triggered": "2024-02-08T02:01:43.546816+00:00",
                "mode": "single",
                "current": 0,
                "friendly_name": "Kitchen Lights Dim"
              },
              "last_changed": "2024-02-08T02:01:39.573204+00:00",
              "last_updated": "2024-02-08T02:01:43.566434+00:00",
              "context": {
                "id": "01HP3763BTX1XAHCD6SJCSF1NB",
                "parent_id": "01HP3763BSYSSEV0Q07QK28HWJ",
                "user_id": null
              }
            },
            "trigger": {
              "platform": null
            }
          }
        }
      ],
      "action/0": [
        {
          "path": "action/0",
          "timestamp": "2024-02-08T02:04:56.659731+00:00",
          "child_id": {
            "domain": "script",
            "item_id": "1707096702958",
            "run_id": "bad3f10e3e3aa4a29389aa4896f88202"
          },
          "changed_variables": {
            "context": {
              "id": "01HP37BZYFR6SNWYWR01PX6G9R",
              "parent_id": "01HP37BZYEQCQMZKQ7AAGC0Y6C",
              "user_id": null
            },
            "brightness": {}
          },
          "result": {
            "params": {
              "domain": "script",
              "service": "1707096702958",
              "service_data": {},
              "target": {}
            },
            "running_script": true
          }
        }
      ],
      "action/1": [
        {
          "path": "action/1",
          "timestamp": "2024-02-08T02:04:56.669630+00:00",
          "error": "Error rendering data template: UndefinedError: 'dict object' has no attribute 'value'"
        }
      ]
    },
    "config": {
      "id": "1706944419680",
      "alias": "Kitchen Lights Dim",
      "description": "Dim LIghts gradually after 9 pm",
      "trigger": [
        {
          "platform": "state",
          "entity_id": [
            "light.kitchen_main_lights"
          ],
          "to": "on"
        }
      ],
      "condition": [
        {
          "condition": "time",
          "after": "17:00:00",
          "before": "05:00:00",
          "weekday": [
            "sun",
            "mon",
            "tue",
            "wed",
            "thu",
            "fri",
            "sat"
          ]
        }
      ],
      "action": [
        {
          "service": "script.1707096702958",
          "metadata": {},
          "data": {},
          "response_variable": "brightness"
        },
        {
          "service": "light.turn_on",
          "target": {
            "entity_id": "light.kitchen_main_lights"
          },
          "data": {
            "brightness_pct": "{{ brightness.value | float(0) }}"
          }
        }
      ],
      "mode": "single"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01HP37BZYFR6SNWYWR01PX6G9R",
      "parent_id": "01HP37BZYEQCQMZKQ7AAGC0Y6C",
      "user_id": null
    }
  },
  "logbookEntries": [
    {
      "name": "Kitchen Lights Dim",
      "message": "triggered",
      "source": null,
      "entity_id": "automation.new_automation",
      "context_id": "01HP37BZYFR6SNWYWR01PX6G9R",
      "when": 1707357896.655648,
      "domain": "automation"
    },
    {
      "name": "Set Dimmer level",
      "message": "started",
      "entity_id": "script.1707096702958",
      "context_id": "01HP37BZYFR6SNWYWR01PX6G9R",
      "when": 1707357896.660597,
      "domain": "script",
      "context_event_type": "automation_triggered",
      "context_domain": "automation",
      "context_name": "Kitchen Lights Dim",
      "context_message": "triggered",
      "context_entity_id": "automation.new_automation"
    },
    {
      "when": 1707357896.661963,
      "state": "on",
      "entity_id": "script.1707096702958",
      "context_event_type": "automation_triggered",
      "context_domain": "automation",
      "context_name": "Kitchen Lights Dim",
      "context_message": "triggered",
      "context_entity_id": "automation.new_automation"
    },
    {
      "when": 1707357896.666626,
      "state": "off",
      "entity_id": "script.1707096702958",
      "context_event_type": "automation_triggered",
      "context_domain": "automation",
      "context_name": "Kitchen Lights Dim",
      "context_message": "triggered",
      "context_entity_id": "automation.new_automation"
    }
  ]
}

That points to the script being the source of the problem. Double check that your script itself is correct and that your automation is calling the correct script. I have tested the both version of the script I posted previously as well as a slightly simplified version of the automation you posted above and it is working as expected. I will post them together here for reference:

Script

alias: Response - Dimmer Level
variables:
  brightness: >
    {% set t1 = "21:00" %}
    {% set t2 = now() %}
    {% set t3 = today_at(t1) %}
    {% set t4 = (t2 - t3).total_seconds() %} 
    {% set t5 = (t4/600)|round(0, floor) %}
    {% set brightness_pct = 100 - 4 * t5 %}
    {{ {'value': brightness_pct} }}
sequence:
  - stop: Return dimmer value
    response_variable: brightness
mode: queued

Automation

alias: Light - time based dimming
description: ""
trigger:
  - platform: state
    entity_id:
      - light.example
    from: "off"
    to: "on"
condition:
  - condition: time
    after: '21:00:00'
    before: '05:00:00'
action:
  - service: script.response_dimmer_level
    data: {}
    response_variable: brightness
  - service: light.turn_on
    metadata: {}
    data:
      brightness_pct: "{{ brightness.value | float(0) }}"
    target:
      entity_id: light.example
mode: queued

There are several small differences that may or may not mean anything. 1) in your script, you called response_variable: brightness, I called it brightness_pct. 2) your script mode is single, I had “queued”. 3) your automation had mode as “queued” and I had “single” I changed mine to yours and made sure the starting time is about 20:00, which is about 1 hour ago my time. The result is the same error message.

{
  "trace": {
    "last_step": "action/1",
    "run_id": "99bf3d5dc0fd4253f81b08b48cbd2a52",
    "state": "stopped",
    "script_execution": "error",
    "timestamp": {
      "start": "2024-02-08T05:02:23.971345+00:00",
      "finish": "2024-02-08T05:02:23.987824+00:00"
    },
    "domain": "automation",
    "item_id": "1706944419680",
    "error": "Error rendering data template: UndefinedError: 'dict object' has no attribute 'value'",
    "trigger": null,
    "trace": {
      "trigger": [
        {
          "path": "trigger",
          "timestamp": "2024-02-08T05:02:23.971541+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.new_automation",
              "state": "on",
              "attributes": {
                "id": "1706944419680",
                "last_triggered": "2024-02-08T04:59:14.135938+00:00",
                "mode": "queued",
                "current": 0,
                "max": 10,
                "friendly_name": "Kitchen Lights Dim"
              },
              "last_changed": "2024-02-08T04:59:07.005455+00:00",
              "last_updated": "2024-02-08T04:59:14.150568+00:00",
              "context": {
                "id": "01HP3HB4AQJSS07BKT77YT5G2C",
                "parent_id": "01HP3HB4AP1GA4VAR723DKJ23N",
                "user_id": null
              }
            },
            "trigger": {
              "platform": null
            }
          }
        }
      ],
      "action/0": [
        {
          "path": "action/0",
          "timestamp": "2024-02-08T05:02:23.974967+00:00",
          "child_id": {
            "domain": "script",
            "item_id": "1707096702958",
            "run_id": "b6f520a10fbf57e0806d5efd6c047468"
          },
          "changed_variables": {
            "context": {
              "id": "01HP3HGXQ3EKV7W6AC4BBA9TNH",
              "parent_id": "01HP3HGXQ283X533EEQW1KTE30",
              "user_id": null
            },
            "brightness": {}
          },
          "result": {
            "params": {
              "domain": "script",
              "service": "1707096702958",
              "service_data": {},
              "target": {}
            },
            "running_script": true
          }
        }
      ],
      "action/1": [
        {
          "path": "action/1",
          "timestamp": "2024-02-08T05:02:23.981913+00:00",
          "error": "Error rendering data template: UndefinedError: 'dict object' has no attribute 'value'"
        }
      ]
    },
    "config": {
      "id": "1706944419680",
      "alias": "Kitchen Lights Dim",
      "description": "Dim LIghts gradually after 9 pm",
      "trigger": [
        {
          "platform": "state",
          "entity_id": [
            "light.kitchen_main_lights"
          ],
          "to": "on"
        }
      ],
      "condition": [
        {
          "condition": "time",
          "after": "20:00:00",
          "before": "05:00:00",
          "weekday": [
            "sun",
            "mon",
            "tue",
            "wed",
            "thu",
            "fri",
            "sat"
          ]
        }
      ],
      "action": [
        {
          "service": "script.1707096702958",
          "metadata": {},
          "data": {},
          "response_variable": "brightness"
        },
        {
          "service": "light.turn_on",
          "target": {
            "entity_id": "light.kitchen_main_lights"
          },
          "data": {
            "brightness_pct": "{{ brightness.value | float(0) }}"
          }
        }
      ],
      "mode": "queued"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01HP3HGXQ3EKV7W6AC4BBA9TNH",
      "parent_id": "01HP3HGXQ283X533EEQW1KTE30",
      "user_id": null
    }
  },
  "logbookEntries": []
}

BTW, thank you for spending time with me on this. Much appreciated.

Post the trace from script too.

Here it is.

{
  "trace": {
    "last_step": "sequence/0",
    "run_id": "a9729e92d3dc7e8ec5831045a083fe41",
    "state": "stopped",
    "script_execution": "aborted",
    "timestamp": {
      "start": "2024-02-08T05:25:55.512356+00:00",
      "finish": "2024-02-08T05:25:55.518670+00:00"
    },
    "domain": "script",
    "item_id": "1707096702958",
    "trace": {
      "sequence/0": [
        {
          "path": "sequence/0",
          "timestamp": "2024-02-08T05:25:55.516452+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "script.1707096702958",
              "state": "off",
              "attributes": {
                "last_triggered": "2024-02-08T05:02:23.976653+00:00",
                "mode": "single",
                "current": 0,
                "friendly_name": "Set Dimmer level"
              },
              "last_changed": "2024-02-08T05:02:23.979844+00:00",
              "last_updated": "2024-02-08T05:02:23.979844+00:00",
              "context": {
                "id": "01HP3HGXQ3EKV7W6AC4BBA9TNH",
                "parent_id": "01HP3HGXQ283X533EEQW1KTE30",
                "user_id": null
              }
            },
            "brightness_pct": {
              "value": 64
            },
            "context": {
              "id": "01HP3JW05R2SWP46AVH4SH493T",
              "parent_id": null,
              "user_id": "c42bcf0d2fbe454fb7add7579494a8b9"
            }
          },
          "error": "'brightness'",
          "result": {
            "stop": "Return dimmer value",
            "error": false
          }
        }
      ]
    },
    "config": {
      "alias": "Set Dimmer level",
      "variables": {
        "brightness_pct": "{% set t1 = \"20:00\" %} {% set t2 = now() %} {% set t3 = today_at(t1) %} {% set t4 = (t2 - t3).total_seconds() %}  {% set t5 = (t4/600)|round(0, floor) %} {% set brightness_pct = 100 - 4 * t5 %} {{ { 'value': brightness_pct} }}\n"
      },
      "sequence": [
        {
          "stop": "Return dimmer value",
          "response_variable": "brightness"
        }
      ],
      "mode": "single"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01HP3JW05R2SWP46AVH4SH493T",
      "parent_id": null,
      "user_id": "c42bcf0d2fbe454fb7add7579494a8b9"
    }
  },
  "logbookEntries": [
    {
      "when": 1707369955.513564,
      "state": "on",
      "entity_id": "script.1707096702958",
      "context_user_id": "c42bcf0d2fbe454fb7add7579494a8b9"
    },
    {
      "when": 1707369955.516926,
      "state": "off",
      "entity_id": "script.1707096702958",
      "context_user_id": "c42bcf0d2fbe454fb7add7579494a8b9",
      "context_state": "on",
      "context_entity_id": "script.1707096702958"
    }
  ]
}

You didn’t change the brightness_pct in the variables section to brightness.

you mean in the script? I just changed them and ran it again. Same error.

Okay, one last test before it’s time for drastic action…

Go to the Services tool: Link to Developer tools: services – My Home Assistant

put script.1707096702958 into the “Service” field, then click the “Call Service” button. Let us know or share a screen shot of what the Response says.

the response field is empty.

Not sure if this is what you expected.

Nope, it should show the returned value like:

Delete the script, restart Home Assistant, and the set up a new script.

I deleted the script, restarted HA completely, then re-entered the script, with the same name (but new script ID) and called the service from developer tools. Still got empty field. When I ran the script after saving it, it didn’t indicate any issue. Ran the trace and everything seems fine. But calling the service somehow return no value. Weird.

I think I got it to work! Somehow calling the script from service doesn’t work. I built the action directly into automation by adding “define variable” field before turning on the light, and somehow it worked. Tonight I will test the automation in real time and see how it behaves. Keeping fingers crossed.