Need help with automation to preserve Powerwall charge

I recently got Home Assistant and am creating automations for my Powerwall 3. I’m able to create automations in the UI to set the battery reserve level to a fixed value without problems, including with more complicated triggers based on the current level. For instance “at time t, set battery reserve to N” or “when battery level is X, set reserve level to Y.”

However, when I want to set the reserve to level which is variable (based upon the current level), it’s not working (for example the first one I’m trying to do is to preserve charge: “at time t, read the current percentage charge and set backup reserve to that same level A”). It doesn’t help probably that the UI doesn’t have support for variables and I had to enter what chatGPT gave me in YAML as I tried to debug it.

If there was a way to do this the UI without needing to dig into YAML, I’d like to know what that is. I’m not finding great tutorials/instructions on how to understand all the options in the UI nor YAML. I’ve asked ChatGPT for help fixing this up, but it hasn’t solved it. I do some Python scripting at work and used to do Java, so I’m not new to programming, but I’m quite new to HA/YAML and all the syntax/options.

My current draft script is below, but it gives me an error. If I read it as a float (as shown below), it fails on the number.set_value saying “expected float for dictionary value @ data[‘value’]”. If I change it to I read it as an int, the variable line errors saying “int got invalid input ‘unknown’ when rending template…”

alias: At 12AM, set PW3 reserve to current charge
description: ""
triggers:
  - trigger: time
    at: "00:00:00"
conditions: []
actions:
  - variables:
      saved_battery_level: "{{ states('sensor.7e0ff971549062a82514bab32b634b3e') | float(none) }}"
  - action: number.set_value
    metadata: {}
    target:
      entity_id: number.my_home_backup_reserve
    data:
      value: "{{saved_battery_level}}"

For comparison, here is a trigger from another automation which checks when the battery level is at 80% and that automation and trigger work fine:

type: battery_level
device_id: 3fb17a1d1708357a714c17ba96fcc088
entity_id: 7e0ff971549062a82514bab32b634b3e
domain: sensor
trigger: device
above: 80
for:
  hours: 0
  minutes: 0
  seconds: 10

I got the answer based on some help from another forum (well, Reddit). I was using the right functions but I wasn’t referencing the sensor properly. I had to learn how to find the right name of the sensor. ChatGPT lead me astray on that, but the Home Assistant initial guides also didn’t really explain it.

I could also drop the explicit variable and also directly retrieve the value within the set call as you did. I expect the variable idea would have worked also but perhaps not… but I didn’t need to try it.

For anyone in the future, this is the script that worked:

alias: At 12AM, set PW3 reserve to current charge
description: ""
triggers:
  - trigger: time
    at: "00:00:00"
conditions: []
actions:
  - action: number.set_value
    metadata: {}
    target:
      entity_id: number.my_home_backup_reserve
    data:
      value: "{{ states('sensor.my_home_percentage_charged') | int }}"
  - action: notify.mobile_app_freds_iphone_15_pro
    metadata: {}
    data:
      message: "At midnight, PW3 reserve set to preserve charge "