Template or script in an automation

I want to set the charge level on my solar battery depending on the Solcast forecast. If Solcast predicts X kWh then set it to 80% if it predicts Y kWH then set it to 70%. I want to put this in an automation.

What is the best way to do this. Do I use a Template that has the IF states or a script?

I am not looking for the code, I want to work that out. I just want to know the best direction.

“Best” is a pretty vague term… Are you looking for “Most Concise”, “Easiest to Read”, “Even a 7 year old could follow the logic”, or maybe “Shortest”. It is a lot easier to give applicable suggestions if you share a more specific description of your goals and methods #8, #9

From the minimal information you have given, you can likely accomplish setting the level using either templates or Choose actions.

I didn’t want to Google the answer and go down the wrong route. The plan is, at 9pm at night look at the Solcast forecast for the next day. If the forecast is like the below, set the battery charge level accordingly.

  • 0-5 kWh - charge level 100%
  • 6-10 kWh - charge level 80%
  • 10+ - charge level 50%

The numbers are examples. I am still working on the real numbers.

To help you, we will need more information.

What are the entity_ids of the following entities you mentioned:

  • solar battery
  • Solcast forecast

What are the integrations that created these entities?

The solar batter is number.lux_ac_battery_charge_level and Solcast is sensor.solcast_forecast_tomorrow

I did write this button to set it to 80, but want to move on to some automation

show_name: true
show_icon: true
type: button
tap_action:
  action: call-service
  service: number.set_value
  data:
    value: '80'
  target:
    entity_id: number.lux_ac_battery_charge_level
name: Set AC Charge Level To 80%
icon: mdi:battery-clock-outline
show_state: false
entity: number.lux_ac_battery_charge_level

alias: example
trigger:
  - platform: time
    at: '21:00:00'
condition: []
action:
  - service: number.set_value
    data:
      value: >
        {% set x = states('sensor.solcast_forecast_tomorrow') | float(0) %}
        {{ 100 if 0 <= x <= 5 else 80 if 5 < x <= 10 else 50 }}
    target:
      entity_id: number.lux_ac_battery_charge_level

Thanks very much. This is what I ended up using. I still need to tweak the right numbers, but the logic is working great.

alias: Nightly Battery Charge Level
trigger:
  - platform: time
    at: "20:30:00"
condition: []
action:
  - service: number.set_value
    data:
      value: >
        {% set x = states('sensor.solcast_forecast_tomorrow') | int(0) %}  {{
        100 if 0 <= x <= 6  else 90 if 7 == x == 7  else 80 if 8 == x == 8  else
        75 if 9 == x == 9  else 70 if 10 == x == 10  else 60 if 11 == x == 11 
        else 40 if 12 == x == 12  else 40 if 13 == x == 13   else 20 }}
    target:
      entity_id: number.lux_ac_battery_charge_level
  - service: notify.notify
    data:
      message: >-
        The battery charge level has been set to {{
        states("number.lux_ac_battery_charge_level") }}
      title: Nightly Battery Charge Level
mode: single

If you are interested, you can shorten the value template like this:

alias: Nightly Battery Charge Level
trigger:
  - platform: time
    at: "20:30:00"
condition: []
action:
  - service: number.set_value
    data:
      value: >
        {% set x = states('sensor.solcast_forecast_tomorrow') | int(0) %}
        {% set levels = { 7: 90, 8: 80, 9: 75, 10: 70, 11: 60, 12: 40, 13: 40 } %}
        {{ 100 if 0 <= x <= 6 else levels.get(x, 20) }}
    target:
      entity_id: number.lux_ac_battery_charge_level
  - service: notify.notify
    data:
      message: >-
        The battery charge level has been set to {{
        states("number.lux_ac_battery_charge_level") }}
      title: Nightly Battery Charge Level
mode: single

Thanks. I am new to Home Assistant so any tips are welcome.

1 Like