How to automate setting a number entity value

I am trying to set the value of the number.solax_battery_charge_max_current in below example.

I have tried various options read through numerous community posts but still can get it to work.

Setting the input_number in below example works perfectly but not the entity_id : number.solax_battery_charge_max_current.

I have also tried configuring with automation but got nowhere.

Any help will be appreciated.

All of the below is in a template.yaml. file which is included in the configuration.yaml.

- sensor:
    - name: Required Charge current
      unique_id: f45c8c8b-b494-4d9f-9988-278cedaa1e14
      icon: mdi:current-dc
      unit_of_measurement: A
      state: >
        {% set batvoltage = states('sensor.solax_battery_voltage_charge') | float(0) %}
        {% set chargebatcapacity = states('sensor.solax_chargeable_battery_capacity') | float(0) %}
        {{ ((chargebatcapacity / 3) / batvoltage) | round(1) }}

- trigger:
    - platform: time
      at: "18:47:00"

  action:
    - service: input_number.set_value
      target:
        entity_id: input_number.charge_current
      data:
        value: "{{ states('sensor.required_charge_current') | float(0) | round(1) }}"

    - service: number.set_value
      target:
        entity_id: number.solax_battery_charge_max_current
      data:
        value: "{{ states('input_number.charge_current') | float(0) | round(1) }}"

Just make a template number.

- number:
    - name: Required Charge current
      unique_id: f45c8c8b-b494-4d9f-9988-278cedaa1e14
      icon: mdi:current-dc
      unit_of_measurement: A
      availability: >
        {% set entities = 'sensor.solax_battery_voltage_charge', 'sensor.solax_chargeable_battery_capacity' %}
        {{ entities | map('has_value') | reject(False) | list | count == entities | count }}
      state: >
        {% set voltage, capacity = ['sensor.solax_battery_voltage_charge', 'sensor.solax_chargeable_battery_capacity'] | map('states') | map('float') | list %}
        {{ ((capacity / 3) / voltage) | round(1) }}
      set_value:
      - service: number.set_value
        target:
          entity_id: number.solax_battery_charge_max_current
        data:
          value: "{{ value }}"

Thank you for getting back to me. I am very new to HA so I appreciate the help.

I am trying to figure out how to action this on a trigger. I should have been cleared in my original post on what I am trying to achieve:

Each day at 12 pm, my solar system is set to charge the battery from the grid, and it stops charging at 3 pm. At 12 pm each day, I want to work out the maximum charge current to charge the battery fully by 3 p.m. This is calculated by dividing the charging capacity by 3 hours and then dividing the result by the charging voltage to get the required current. I want to use the result of this calc to set the entity number.solax_battery_charge_max_current.

the number entity “number.solax_battery_charge_max_current” is part of an integration I installed.

Baseds on your feedback above I have tried implementing it as follow.

- trigger:
    - platform: time
      at: "12:00:00"
  action:
    number:
      - name: Required Charge current
        unique_id: f45c8c8b-b494-4d9f-9988-278cedaa1e14
        icon: mdi:current-dc
        unit_of_measurement: A
        availability: >
          {% set entities = 'sensor.solax_battery_voltage_charge', 'sensor.solax_chargeable_battery_capacity' %}
          {{ entities | map('has_value') | reject(False) | list | count == entities | count }}
        state: >
          {% set voltage, capacity = ['sensor.solax_battery_voltage_charge', 'sensor.solax_chargeable_battery_capacity'] | map('states') | map('float') | list %}
          {{ ((capacity / 3) / voltage) | round(1) }}
        set_value:
          - service: number.set_value
            target:
              entity_id: number.solax_battery_charge_max_current
            data:
              value: "{{ value }}"

you’d make a separate automation that triggers at 12 and uses the number entity. You would not add the trigger to the template entity.

The number entity was missing the step condition and unit_of_measurement was invalid.

Template looks like this now

- number:
    - name: Required Charge current
      unique_id: 47c94e28-2f68-4666-97c7-4f13f17d464f
      icon: mdi:current-dc
      step: 0.1
      availability: >
        {% set entities = 'sensor.solax_battery_voltage_charge', 'sensor.solax_chargeable_battery_capacity' %}
        {{ entities | map('has_value') | reject(False) | list | count == entities | count }}
      state: >
        {% set voltage, capacity = ['sensor.solax_battery_voltage_charge', 'sensor.solax_chargeable_battery_capacity'] | map('states') | map('float') | list %}
        {{ ((capacity / 3) / voltage) | round(1) }}
      set_value:
        - service: number.set_value
          target:
            entity_id: number.solax_battery_charge_max_current
          data:
            value: "{{ value }}"

and the automation created is as follow but does not work:

- id: '1715348471973'
  alias: Set Max charge Current
  description: ''
  trigger:
  - platform: time
    at: '21:42:00'
  condition: []
  action:
  - service: number.set_value
    metadata: {}
    data: {}
    target:
      entity_id: number.required_charge_current
  mode: single

What’s not working? Logs?

2024-05-10 22:44:00.270 ERROR (MainThread) [homeassistant.components.automation.set_max_charge_current] Set Max charge Current: Error executing script. Invalid data for call_service at pos 1: required key not provided @ data[‘value’]
2024-05-10 22:44:00.270 ERROR (MainThread) [homeassistant.components.automation.set_max_charge_current] Error while executing automation automation.set_max_charge_current: required key not provided @ data[‘value’]

Well, the error says it all. Set it, but set it to what? you did not supply the new value in the (2nd) automation.

You need to supply a value… what value are you trying to put here?

I have a sneaking suspicion that there’s some communication issues here, and that you really want to set the value of this equation at a specific time.

        {% set batvoltage = states('sensor.solax_battery_voltage_charge') | float(0) %}
        {% set chargebatcapacity = states('sensor.solax_chargeable_battery_capacity') | float(0) %}
        {{ ((chargebatcapacity / 3) / batvoltage) | round(1) }}

And if that’s the case, you can just do this in a single automation without a helper entity.

- id: '1715348471973'
  alias: Set Max charge Current
  description: ''
  trigger:
  - platform: time
    at: '21:42:00'
  variables:
    voltage: "{{ states('sensor.solax_battery_voltage_charge') }}"
    capacity: "{{ states('sensor.solax_chargeable_battery_capacity') }}"
  condition: []
  action:
  - service: number.set_value
    metadata: {}
    data: {}
    target:
      entity_id: number.solax_battery_charge_max_current
    data:
      value: "{{ ((capacity / 3) / voltage) | round(1) }}"
  mode: single

Finally got it working. There was definitely a communication gap. Thanks for your persistence, petro. Below are a few small tweaks to what you have proposed. I am very new to YAML, and syntax is so critical. In this case, the float condition to be added to value:,

- id: '1715383332005'
  alias: Set Charge current 2
  description: ''
  trigger:
  - platform: time
    at: 07:53:00
  condition: []
  action:
  - variables:
      voltage: '{{ states(''sensor.solax_battery_voltage_charge'') | float(0) }}'
      capacity: '{{ states(''sensor.solax_chargeable_battery_capacity'')  | float(0)
        }}'
      current: '{{ ((capacity / 3) / voltage) | float(0) | round(1) }}'
  - service: number.set_value
    metadata: {}
    data:
      value: '{{ current | float(0) }}'
    target:
      entity_id: number.solax_battery_charge_max_current
  mode: single

If you default voltage to 0, but are dividing by voltage, it is mathematically forbidden in defaultcase and if voltage is 0 in other cases. You should define, what should happen in those cases as well.

Float isn’t needed anywhere, you should have been able to copy/paste exactly what I wrote

It gave me an error in the traces of the automtion as well as in the logs. It said Float required.

Did you use the ui? If yes, you should have removed the exterior quotes.

You are correct again :slightly_smiling_face:: I changed it back to exactly as you wrote it and it worked. However, the automation did not trigger if I edited the time trigger in the Studio code server. I had to go into the UI to setup the time trigger and save. In case you are wondering, I did in the Studio code server.