When I have multiple sensors in template/sensors
, can one sensor reference another when paying attention to ordering? If sensors at the top get evaluated and stored first, then following sensors could reference them in their state
. But that’s an assumption - I’ve not found documentation about this.
More specifically, I am creating flow sensors for GitHub - reptilex/tesla-style-solar-power-card: Home assistant power card mimicking the one tesla provides for the powerwall app.. I get solar power, battery charging/discharging, grid in/out, house consumption as real measurements. With some assumptions, I can guess what the individual flows should be:
- name: "Flow Solar to House"
device_class: power
state_class: measurement
unit_of_measurement: "W"
# Assumption: all produced power is used first for the house.
state: >
{{ min( int(states('sensor.house_consumption'), 0), int(states('sensor.solar_power'), 0) ) }}
- name: "Flow Solar to Battery"
device_class: power
state_class: measurement
unit_of_measurement: "W"
# Assumption: all produced power is used first for the house, so what remains can be used for charging (but might not be used for it).
state: >
{{ max(0, min(int(states('sensor.battery_charging_power'), 0), int(states('sensor.solar_power'), 0) - int(states('sensor.house_consumption'), 0))) }}
- name: "Flow Solar to Grid"
device_class: power
state_class: measurement
unit_of_measurement: "W"
# Assumption: all produced power is used first for the house, then the battery. What remains is feed to the grid.
state: >
{{ max(0, int(states('sensor.solar_power'), 0) - int(states('sensor.house_consumption'), 0) - int(states('sensor.battery_charging_power'), 0)) }}
- name: "Flow Grid to Battery"
device_class: power
state_class: measurement
unit_of_measurement: "W"
# Assumption: The missing enery for charging the battery must come from the grid.
state: >
{{ max(0, int(states('sensor.battery_charging_power'), 0) - int(states('sensor.flow_solar_to_battery'), 0)) }}
- name: "Flow Grid to House"
device_class: power
state_class: measurement
unit_of_measurement: "W"
# Assumption: The missing enery for the house must come first from the battery.
state: >
{{ max(0, int(states('sensor.house_consumption'), 0) - int(states('sensor.solar_power'), 0) - int(states('sensor.battery_discharging_power'), 0)) }}
- name: "Flow Battery to House"
device_class: power
state_class: measurement
unit_of_measurement: "W"
# Assumption: The remaining missing enery for the house must come from the grid.
state: >
{{ max(0, int(states('sensor.house_consumption'), 0) - int(states('sensor.solar_power'), 0) - int(states('sensor.flow_grid_to_house'), 0)) }}
- name: "Flow Battery to Grid"
device_class: power
state_class: measurement
unit_of_measurement: "W"
# Assumption: any unused battery power must go to the grid.
state: >
{{ max(0, int(states('sensor.battery_discharging_power'), 0) - int(states('sensor.flow_battery_to_house'), 0)) }}
Note that for example, “Flow Battery to Grid” uses sensor.flow_battery_to_house
which was computed earlier. It seems to work, but perhaps only because it is using some previous value instead of the latest one. I could repeat the expression, but that would make the entire config harder to read and comprehend.