'Holes' in Graphs

Hi.

Maybe that is a solved question already, but I couldn’t find it…

When creating a template from a group of entities, in this case four different solar panels to get a sum of what they produce in power, the graph I am getting is getting holes in it, whenever HA is restarted or the yaml reloaded. I tried the following code, which already helped to not ‘zero’ return in those momets, but now the graph has many holes in it. I

Is it possible to code, that the template returns (and stores for graph display) the last value used until it is available again. I tried ‘availability’ also, but that didn’t work (for me…)

An help appreciated.

here is my template, and an image of the returned graph. I tried two versions, one with ‘is number only’ and one with an if-then logic. both don’t fill the holes…

Regards,
yocup

sensor:
  # recalculate m3 to kWh from Gaszähler
  - name: "GAS Verbrauchswert kWh"
    unique_id: sensor.gas_verbrauch_kwh
    unit_of_measurement: "kWh"
    device_class: energy
    icon: mdi:meter-gas-outline
    state_class: "total_increasing"
    state: >
      {% if states('sensor.gas_verbrauch_m3') |is_number and 
            states('input_number.gas_brennwert') |is_number and 
            states('input_number.gas_zustandszahl') |is_number
      %}
        {{ (states('sensor.gas_verbrauch_m3') |float(0) * 
            states('input_number.gas_brennwert') |float(0) * 
            states('input_number.gas_zustandszahl') |float(0)) |round(2) }}
      {% else %}       
          {{ this.state }}
      {% endif %}

    # sum up PV Hausdach also when unavailable
  - name: "Power PV Hausdach available"
    unique_id: sensor.pv_hausdach_available
    state: >
      {{ (states('sensor.pv_dach_gaube_output_active_power_2') |float(0) + 
          states('sensor.pv_dach_links_oben_output_active_power') |float(0) +
          states('sensor.pv_dach_links_unten_output_active_power') |float(0) +
          states('sensor.pv_dach_rechts_output_active_power') |float(0)) }}
    unit_of_measurement: "W"
    device_class: power
    state_class: measurement
    availability: >
      {{ 
        [ states('sensor.pv_dach_gaube_output_active_power_2'), 
          states('sensor.pv_dach_links_oben_output_active_power'),
          states('sensor.pv_dach_links_unten_output_active_power'),
          states('sensor.pv_dach_rechts_output_active_power') ] |map('is_number') |min }}

The “hole” means your sensor was unavailable.

After a restart your template sensor will not have a value until at least one of the sensors it contains updates.

It’s actually nothing to worry about and will not mess anything up. If you want you could create a triggered template sensor. These are restored after a restart.

e.g.

template:
  - trigger:
      - platform: state
        entity_id:
          - sensor.gas_verbrauch_m3
          - input_number.gas_brennwert
          - input_number.gas_zustandszahl
        not_to:
          - unavailable
          - unknown
    sensor:
      - name: "GAS Verbrauchswert kWh"
        unique_id: sensor.gas_verbrauch_kwh
        unit_of_measurement: "kWh"
        device_class: energy
        icon: mdi:meter-gas-outline
        state_class: "total_increasing"
        state: >
          {% if states('sensor.gas_verbrauch_m3') |is_number and 
                states('input_number.gas_brennwert') |is_number and 
                states('input_number.gas_zustandszahl') |is_number
          %}
            {{ (states('sensor.gas_verbrauch_m3') |float(0) * 
                states('input_number.gas_brennwert') |float(0) * 
                states('input_number.gas_zustandszahl') |float(0)) |round(2) }}
          {% else %}       
              {{ this.state }}
          {% endif %}

However, as I said this is not needed.

Btw, can we use here a newly added “conditions” option?
Kind of “if source entities are unavailable” - then false.

Hi tom_l!

Thankyou! The code works. I understand, that it’s not necessary, but no holes looks better. :wink:
I do get a nag message in Studio Code Server for - platform: state saying 'String does not match the pattern of “LEGACY_SYNTAX” which seems to be new in version 10.24 and couldn’t correct it (I tried ‘trigger’ instead of 'platform as suggested here: 2024.10: Heading in the right direction - Home Assistant , but somehow it did not compute for me) But, anyways, legacy code does still work and my trigger triggers. So thanks for solving my problem!

Regards, yocup

Change this:

  - trigger:
      - platform: state

To this:

  - trigger:
      - trigger: state

:cowboy_hat_face:
thanks so much.

You can also use conditions so you don’t have to refer to this.state

template:
  - trigger:
      - platform: state
        entity_id:
          - sensor.gas_verbrauch_m3
          - input_number.gas_brennwert
          - input_number.gas_zustandszahl
        not_to:
          - unavailable
          - unknown
    condition:
      - condition: template
        value_template: >
          {{ states('sensor.gas_verbrauch_m3') |is_number and 
                states('input_number.gas_brennwert') |is_number and 
                states('input_number.gas_zustandszahl') |is_number }}
    sensor:
      - name: "GAS Verbrauchswert kWh"
        unique_id: sensor.gas_verbrauch_kwh
        unit_of_measurement: "kWh"
        device_class: energy
        icon: mdi:meter-gas-outline
        state_class: "total_increasing"
        state: >
          {{ (states('sensor.gas_verbrauch_m3') |float * 
              states('input_number.gas_brennwert') |float * 
              states('input_number.gas_zustandszahl') |float) |round(2) }}
1 Like

THANK YOU!

If I understand right, ‘condition: template’ would only pull information, if entity ‘|is_number’, instead of allways pulling (if-else) and defaulting to the last given state?

So, if an entitity is not available (restart of HA inavailabilty of my tasmota gas sensor.), ‘condition: template’ would just not ask for information, right?

But what, if e.g. my PV on roof is just not spreading news of how much watts it’s producing (at night) and becomes inavailable. I used ‘0’ instead of ‘this.state’, so it’s not showing the last produced power ((e.g. 8 watts) the whole night. Would ‘condition’ work here, too?

It will only generate the template for the state if the condition returns true, if not, it will just keep the old state (trigger based template sensors restore their state, also after a restart)

For your PV question, don’t check on that state in the condition, and use float(0) to default it to 0 in case it’s unavailable.

thanks for explaining and the help! Understood.