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

1 Like

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