Looking for Solutions: MQTT Sensor Unavailable During HA Restart

I’m new to Home Assistant and currently trying to figure out a challenge with my PV inverter sensor. Every time Home Assistant restarts during the day, the sensor momentarily becomes unavailable. The issue resolves itself once the sensor.solar_generation_power receives an MQTT message from the PV inverter and becomes available again. However, during that brief period, I notice incorrect values being registered for GridPowerIn, as shown in the image below. For example, this happened during the day when the GridPowerIn value should have been zero.

Unfortunately, my PV inverter doesn’t retain MQTT values during a Home Assistant restart. If anyone has faced a similar issue and knows how to tackle it, I’d really appreciate your guidance!

Here is my code:

- sensor:   
  - name: "Grid Power In"
        unique_id: ca3babc7-e1f1-4b43-97a2-97aa0e66ce64
        unit_of_measurement: "W"
        state_class: measurement
        device_class: power
        state: >
          {% set GridPowerIn = ( (states('sensor.home_consumption_power') | float(default=0) | round (2)) - (states('sensor.solar_generation_power') | float (default=0) | round(2)) ) %}
          {% if GridPowerIn >= 0 %}
            {{GridPowerIn | float(2)}}  ## It means that the grid is feeding the house 
          {% else %}
            {{0 | float(2) }}  ## It means that the No power is coming from the grid to the house
          {% endif %}

Use an availability template.

- sensor:   
  - name: "Grid Power In"
        unique_id: ca3babc7-e1f1-4b43-97a2-97aa0e66ce64
        unit_of_measurement: "W"
        state_class: measurement
        device_class: power
        state: >
          {% set GridPowerIn = ( (states('sensor.home_consumption_power') | float(default=0) | round (2)) - (states('sensor.solar_generation_power') | float (default=0) | round(2)) ) %}
          {% if GridPowerIn >= 0 %}
            {{GridPowerIn | float(2)}}  ## It means that the grid is feeding the house 
          {% else %}
            {{0 | float(2) }}  ## It means that the No power is coming from the grid to the house
          {% endif %}
        availability: "{{ has_value('sensor.home_consumption_power') and has_value('sensor.solar_generation_power') }}"

I think I might have caused some confusion by renaming sensor.output_power to sensor.home_consumption_power. The sensor.output_power is always a positive value, representing the difference between solar_generation_power and home_consumption_power.

The logic I’m aiming for is something like this: If sensor.output_power is greater than sensor.solar_generation_power, it means power is being drawn from the grid. On the other hand, if sensor.output_power - sensor.solar_generation_power is negative, it indicates a surplus of power, so nothing is being drawn from the grid, and Grid Power In should be zero.

Your suggested approach works well in the morning when the PV inverter is operational. However, GridPowerIn will become unavailable if Home Assistant reboots after sunset when the PV inverter is powered off, which isn’t ideal because GridPowerIn after sunset should indicate the amount of power being drawn from the grid.

I’m looking for a solution that keeps GridPowerIn available in both scenarios and prevents incorrect values from being registered when sensor.solar_generation_power temporarily becomes unavailable during the day due to HA reboots.

    - name: "Grid Power In"
      unique_id: ca3babc7-e1f1-4b43-97a2-97aa0e66ce64
      unit_of_measurement: "W"
      state_class: measurement
      device_class: power
      state: >
        {% set GridPowerIn = ( (states('sensor.output_power') | float(default=0) | round (2)) - (states('sensor.solar_generation_power') | float (default=0) | round(2)) ) %}
        {% if GridPowerIn >= 0 %}
          {{GridPowerIn | float(2)}}
        {% else %}
          {{0 | float(2) }}
        {% endif %}
      availability: "{{ has_value('sensor.output_power') and has_value('sensor.solar_generation_power') }}"

Alternatively, create an automation than triggers on the MQTT message and have it send the same payload as a different topic, but this time with the retain flag set. Then base your sensor on that instead.