Use automation to set an entity's value based on input from another template sensor

Hi,

I can’t seem to figure out how to do this. My objective is to set the overnight charge level of my house battery based on the following day’s predicted solar yield from my PV array.

I can directly set the value I want to charge to in the inverter entity, and I have written a function that works in the template editor to calculate what battery %age want to charge to based on the forecast.

I have created an automation to look at the predicted yield at 22:00 in the evening each day. I can specify which entity I want to change in the action for the automation, but I can’t figure out how to pass the action a variable as a value rather than just a discrete number which is what it seems it wants.

Am I going about this the wrong way or am I missing something? I’ve spent the best part of today googling for suggestions on this but can’t seem to find anything to help! If this has been covered I’d truly appreciate someone pointing me in that direction.

Many thanks,

Tom

Hello Tom,
It would help if you could to provide some code so we can see what you’ve done so far… We won’t be able to help if all we have is ‘I want to do this thing with this other thing’.
How to help us help you - or How to ask a good question.

Fair enough - but it’s pretty basic and very much work in progress.

Template sensor to deliver a target charge looks like this:

{{ ((states("sensor.energy_production_tomorrow")**(-1.324))*12315) }}

automation code (at the moment) looks like this:

description: ""
mode: single
trigger:
  - platform: time
    at: "22:00:00"
condition: []
action:
  - variables:
      target_charge: >-
        ((states("sensor.energy_production_tomorrow")
        |float(0))**(-1.324))*12315)
  - device_id: d436151015af999b29352f736399bafe
    domain: number
    entity_id: fc3862a52441ab291f5fcfd2d18a01ba
    type: set_value
  - choose:
      - conditions: []
        sequence: []

note that it’s untidy as I originally tried to use an automation variable so deliver the target charge number for the action. I’ve since moved on to trying to use a template sensor. I’m mostly trying to build the automation in the Visual editor, I’ve not tried explicitly adding the variable in YAML - I’d thought about it but can’t find the syntax do so.

Thanks,

tom

What is that entity specifically? (and always try to use entity ID to target it instead of device ID for reasons I’ll link to later)

If it’s not something you can set, maybe you just create your own template sensor to store the result and use that in your display /automation etc. Instead?

Hi, the entity I’m trying to change is:

number.sm_prog1_capacity

I can change it easily via e.g. a slider control.

Thank you!

Use a Service Call, not a Device Action, because it supports templates whereas Device Actions don’t.

action:
  - variables:
      target_charge: >-
        {{ ((states('sensor.energy_production_tomorrow') | float(0))**(-1.324)) * 12315 }}
  - service: number.set_value
    target: 
      entity_id: number.your_number
    data:
      value: '{{ target_charge }}'

Reference

Number - number.set_value

2 Likes

Then when you have your variable then issue a service call to number.set_value

(Device id’s change when a device is replaced. To make your automation survive device replacement use entity id’s instead and you can rename replacement device entities to use old names and the automation continues without edit.)

Edit: And if troon answers a template thread. Just do what he says.

That makes a lot of sense, especially as the device ID’s will probably change on reboot as they are USB RS485 adapters going directly into my HA server!

Thanks

Blinding. Thank you, I will give that a try!

Tom

See here :slight_smile:

Folks, thanks for the awesome help. everytime I try to do something in HA I learn more :slight_smile:

I just set up the automations (two independent inverters to change) as you suggested using the service call methodology and ran them manually and validated that they changed the target charge levels as expected!

Cheers!

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

1 Like

Here is another example where I had to deal with a PULSAR PLUS WALLBOX made by the manufacturer WALLBOX.

At the end it is a slider transformation of a given sensor (by the integration) and the slider with a measurement everyone is familiar with as for example here kW versus Ampere
image

The Wallbox integration offers a sensor which allows you to adjust the max. charging current.
I can adjust the slider from 6 to 16 Ampere.

For us nerds MAYBE OK,
but NO ONE ELSE IN THE FAMILY really wants to deal with Ampere and Volts and the number of phases. I am already quite happy that they have learned what Watt and kW means and how much our devices are using and that the homestorage battery is limited to 3 kW.

Here you can see the slider / number provided by the manufacturers integration (BOTTOM HALF) and in the upper half is the kW slider all family members are familiar with.
The genuine Ampere slider is here visible only for demonstration purposes (and to check that it works properly).

Why is it important ?
The average user is not aware how to calculate from Ampere the charging speed in kW cause therefore you have to be aware on how many phases your car is charging cause not all EVs have a 3 phase onboard charger (the smaller the battery the less phases = rule of thumb).

While the first line is doing the calculation for what most people expect

3 phases x 230 V x 10 A = 6900 W 
2 phases x 230 V x 10 A = 4600 W 

the 2nd line is showing the calculation for 2 phases, simple with 10A but gets challenging.

Hence this solution which offers a kW slider instead which is based on an inputnumber helper and the number of phases used to charge the car.

Any difference between the kW inputnumber and the wallbox number (after the inputnumber in kw has been transformed into Ampere) will trigger the automation to change the settings of the charging current number in Ampere.

In this example we use it only as a 2 phase charger with our cars to have a better solarpower charging efficiency (less from the grid). If the setting in kW has been changed the automation will do the math and the trigger will kick in and change the Amper settings in 1 second.

Be aware to set the input number helper right cause in this case the Wallbox expects to get values from 6 to 16 A and nothing in between, therefore the SLIDER is king to avoid the mess a 2.777 kW figure typed in manually could cause.

So you have to do your math first which means for 2 phases:
6A x 230 V x 2 phases = 2760 Watt

I suggest to set the lowest input number to 2,76 kW instead of 2760 W which had caused issues for me in the dashboard (not all figures were visible).

Then there is a step size which can is based on 1A as lowest step and is calculated this way
1A x 230V x 2 phases = 460 Watt

You have to use 0,46 kW in the input helper configuration as you can see below.

And in this case all the settings will later match up with each other as you can see here in the last column with
2,76 kW matching 6 A
3,22 kW matching 7A

 6 A * 2 * 230 = 2760 W = 2.76 kW ~  6 A
 7 A * 2 * 230 = 3220 W = 3.22 kW ~  7 A
 8 A * 2 * 230 = 3680 W = 3.68 kW ~  8 A
 9 A * 2 * 230 = 4140 W = 4.14 kW ~  9 A
10 A * 2 * 230 = 4600 W = 4.60 kW ~ 10 A
11 A * 2 * 230 = 5060 W = 5.06 kW ~ 11 A
12 A * 2 * 230 = 5520 W = 5.52 kW ~ 12 A
13 A * 2 * 230 = 5980 W = 6.98 kW ~ 13 A
14 A * 2 * 230 = 6440 W = 6.44 kW ~ 14 A
15 A * 2 * 230 = 6900 W = 6.90 kW ~ 15 A
16 A * 2 * 230 = 7360 W = 7.40 kW ~ 16 A

Here ist the automation

alias: WALLBOX - set charging current
description: calculate from the kW inputnumber the Ampere first and then set that as the max WALLBOX charging current
triggers:
  - trigger: template
    value_template: >-
      {{ ( states('input_number.wallbox_charging_kw_max') | float () * 1000 / 2
      / 230 - states('number.wallbox_pulsarplus_161183_max_chargecurrent') |
      float () ) != 0 }}
conditions: []
actions:
  - target:
      entity_id: number.wallbox_pulsarplus_161183_max_chargecurrent
    data:
      value: >-
        {{ states('input_number.wallbox_charging_kw_max') | float () * 1000 / 2
        / 230 }}
    action: number.set_value
mode: single

This works like a charm. All the unnecessary things I had to explain before are now hidden

image

as you can see here

image

well explained and worked out for me with a complex task.
thanks a lot :+1: