Execution order of template.sensor

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.

I think the order is irrelevant.

The template sensor will get its value updated based on changes made by the underlying sensor.

So if you reference one template sensor in another, its possible one could be in an unknown or unavailable state.

Not sure what happens when HA restarts, someone else might have an idea on that.


Source: State-based Template Sensor

Make sure to include availability template to avoid spiking of your values due to unknown or unavailable source sensors.

So the sensor references should work already, great!

Regarding “availability template”: I think I am covering that by using 0 as default for each int conversion. Or is that still missing some error cases?

I’m a bit confused about states. Template - Home Assistant says:

returns the state and never results in unknown

But then Templating - Home Assistant says the opposite (?):

returns the state string (not the state object) of the given entity, unknown if it doesn’t exist, and unavailable if the object exists but is not available