How to set an entity with another entity

I’m not an expert on yaml. How do I set a value of an entity with another entity?

In my case I want to set number.inverter_backup_power_soc with the value of sensor.batteries_state_of_capacity. Both are percentage values ranging from 0 to 100.

I tried with the automation below but it doesn’t work. Any ideas what I’m doing wrong? Thank you! :pray:t2:

alias: Pause Home Battery When Charging Car
description: ""
triggers:
  - trigger: state
    id: wallbox_stopped
    entity_id:
      - sensor.wallbox_pulsar_max_charging_power
    to: "0"
    for:
      hours: 0
      minutes: 1
      seconds: 0
  - trigger: state
    id: wallbox_started
    entity_id:
      - sensor.wallbox_pulsar_max_charging_power
    from: "0"
    for:
      hours: 0
      minutes: 1
      seconds: 0
conditions: []
actions:
  - if:
      - condition: or
        conditions:
          - condition: trigger
            id:
              - wallbox_stopped
          - condition: numeric_state
            entity_id: sensor.batteries_state_of_capacity
            below: 20
    then:
      - action: number.set_value
        metadata: {}
        target:
          entity_id: number.inverter_backup_power_soc
        data:
          value: "20"
    else:
      - action: number.set_value
        metadata: {}
        target:
          entity_id: number.inverter_backup_power_soc
        data:
          value: "{{sensor.batteries_state_of_capacity}}"
mode: single
value: "{{ states('sensor.batteries_state_of_capacity') }}"

I would split that automation into two separate ones. There’s no real benefit having it as one.

2 Likes

Perfect. Thx!

Could I ask what are the pros of splitting the automation in two and the cons of keeping it as one?

It’s often personal preference, but I find it easier to maintain. In this case you can avoid the if statement, so you’re ending up with less code. Maybe try both versions and see how it works for you.

Certainly a personal preference.

I tend to go with fewer automations. Meaning more code in each. It keeps similar things in one place as opposed to multiple automations spreading the code out.

1 Like

You might be better off with using a numeric_state trigger instead of a state trigger if you’re waiting for 1 minute to trigger.
If your wallbox reports 0 > 1 > 2 in less than a minute, then your automation will not trigger.

Something like the below should do it. Note I also added a condition which prevents unnecessary triggers if your wallbox is unavailable:

triggers:
  - trigger: numeric_state
    id: wallbox_stopped
    entity_id:
      - sensor.wallbox_pulsar_max_charging_power
    below: 0.01
    for:
      hours: 0
      minutes: 1
      seconds: 0
   - trigger: numeric_state
    id: wallbox_started
    entity_id:
      - sensor.wallbox_pulsar_max_charging_power
    above: 0
    for:
      hours: 0
      minutes: 1
      seconds: 0   
conditions:
  - condition: template
    value_template: "{{ trigger.from_state.state not in ['unknown', 'unavailable'] }}"
actions:
  - if:
      - condition: or
        conditions:
          - condition: trigger
            id:
              - wallbox_stopped
          - condition: numeric_state
            entity_id: sensor.batteries_state_of_capacity
            below: 20
    then:
      - action: number.set_value
        metadata: {}
        target:
          entity_id: number.inverter_backup_power_soc
        data:
          value: "20"
    else:
      - action: number.set_value
        metadata: {}
        target:
          entity_id: number.inverter_backup_power_soc
        data:
          value: "{{sensor.batteries_state_of_capacity}}"
mode: single
1 Like

Thank you for your suggestions.

I think the wallbox reports the charging power in float, kW with 2 decimals (I added 1 minute wait just to prevent debouncing). Would a numeric_state still work in this case?

Could I use template triggers instead?

triggers:
  - trigger: template
    id: wallbox_stopped
    value_template: "{{ states('sensor.wallbox_pulsar_max_charging_power') | float == 0 }}"
    for:
      hours: 0
      minutes: 1
      seconds: 0
  - trigger: template
    id: wallbox_started
    value_template: "{{ states('sensor.wallbox_pulsar_max_charging_power') | float > 0 }}"
    for:
      hours: 0
      minutes: 1
      seconds: 0

Yes it will, but I noticed I had a typo in my suggestion. It should have said above: 0 for the second trigger. Silly copy paste mistake, sorry.

I’ll have edited my post by the time you read this, and will also be changing your below value to factor in a float between 0 & 1.

1 Like