Sensors in template unavailable

Hi, I’ve been struggling with custom sensors in template in yaml for a few days now. They are still unavailable even though they work correctly in Developers tools-templates.
This is the last one in the listing from the “PV power min” sensor.
The latter are dependent on “PV power min”. They work fine for me in the beginning. I can’t figure out what’s wrong.
I guess I’m making a mistake in the boolean operations.
Thanks for any help.

template:
  - sensor:
      - name: "PV suff"
        unit_of_measurement: "W"
        unique_id: pv_sufficient
        state: >
         {% set PV_suff = ( states('sensor.PV_power')|float(default=0) - states('sensor.house_consumption')|float(default=0) ) |float(default=0) %}
         {{ PV_suff|float(default=0) }}
  - sensor:
      - name: "On grid now"
        unit_of_measurement: "W"
        unique_id: on_grid_now
        state: >
         {% set On_grid_now = states('sensor.on_grid_l1_power')|float(default=0) + states('sensor.on_grid_l2_power')|float(default=0) + states('sensor.on_grid_l3_power')|float(default=0) %}
         {{ On_grid_now|float(default=0)|int }}
  - sensor:
      - name: "Shelly power 3f"
        unit_of_measurement: "W"
        unique_id: shelly_pw_3f
        state: >
         {% set Shelly_PW = states('sensor.shelly_boiler_channel_a_power')|float(default=0) + states('sensor.shelly_boiler_channel_b_power')|float(default=0) + states('sensor.shelly_boiler_channel_c_power')|float(default=0) %}
         {{ Shelly_PW|float(default=0) }}
  - sensor:
      - name: "PV power min"
        unique_id: pv_min_pw
        icon: mdi:water-boiler-auto
        state: >
         {% set B_2kW = 2000|int %}        
         {% set pv_min = PV_suff|int >= B_2kW|int %}
         {{ pv_min|int }}

  - sensor:
      - name: "Boiler 2kW ON"
        unique_id: Boiler_2kW_ON
        icon: mdi:water-boiler-auto
        state: >
         {% set HCx = 1.15 | float %}
         {% set B_2kW = 2000  | int %}
         {% set B_4kW = 4000  | int %}
         {% set HCx_pw = ( states('sensor.house_consumption') | float * HCx | float ) | float | int %}
         {% set Boiller_2kW = ( (PV_suff | int >= B_2kW | int)  and  (PV_suff | int < B_4kW | int) )  | int | default(0) %}
         {{ Boiller_2kW | bool }}
        
  - sensor:
      - name: "Boiler 4kW ON"
        unique_id: Boiler_4kW_ON
        icon: mdi:water-boiler-auto
        state: >
         {% set HCx = 1.15 | float %}
         {% set B_4kW = 4000  | int %}
         {% set B_6kW = 6000  | int %}
         {% set HCx_pw = ( states('sensor.house_consumption') | float * HCx | float ) | float | int %}
         {% set Boiller_4kW = ( (PV_suff | int >= B_4kW | int)  and  (PV_suff | int < B_6kW | int) )  | int | default(0) %}
         {{ Boiller_4kW | bool }}

  - sensor:
      - name: "Boiler 6kW ON"
        unique_id: Boiler_6kW_ON
        icon: mdi:water-boiler-auto
        state: >
         {% set HCx = 1.15 | float %}
         {% set B_6kW = 6000  | int %}
         {% set HCx_pw = ( states('sensor.house_consumption') | float * HCx | float ) | float | int %}
         {% set Boiller_6kW = (PV_suff | int >= B_6kW | int) | int | default(0) %}
         {{ Boiller_6kW | bool }}

  - sensor:
      - name: "House consumption without boiler"
        unit_of_measurement: "W"
        unique_id: hc_wo_boiler
        state: >
         {% set HC_wo_b = ( states('sensor.house_consumption') | float - Shelly_PW | int ) | float | int %}
         {{ HC_wo_b | int }}

You’ve got a few issues…

  1. A few of your templates rely on a variable PV_suff which isn’t defined for them.
  2. All the “Boiler…ON” sensors define a variable HCx_pw then never use it.
  3. You don’t need to apply float and int conversion filters to everything. Specifically, when you set a variable’s value to a number there is no need to apply int() and/or float() to the variable later on.
  4. All of the “Boiler…ON” entities would be better as binary sensors.
  5. Since it’s based on a mathematic comparison, the “PV power min” will only ever return 0 or 1… is that intentional?
template:
  - sensor:
      - name: "PV suff"
        unit_of_measurement: "W"
        unique_id: pv_sufficient
        state: >
          {( states('sensor.PV_power')|float(0) - states('sensor.house_consumption')|float(0) }}

      - name: "On grid now"
        unit_of_measurement: "W"
        unique_id: on_grid_now
        state: >
          {{ expand('sensor.on_grid_l1_power', 'sensor.on_grid_l2_power', 
          'sensor.on_grid_l3_power') | map(attribute='state') | map('float', 0) | sum }}

      - name: "Shelly power 3f"
        unit_of_measurement: "W"
        unique_id: shelly_pw_3f
        state: >
          {{ expand('sensor.shelly_boiler_channel_a_power', 'sensor.shelly_boiler_channel_b_power',
          'sensor.shelly_boiler_channel_c_power') | map(attribute='state') | map('float', 0) | sum }}
 
      - name: "House consumption without boiler"
        unit_of_measurement: "W"
        unique_id: hc_wo_boiler
        state: >
          {{ states('sensor.house_consumption') | float(0) - states('sensor.shelly_power_3f') | float(0) }}

Thank you for answer.
I’ll get back to you tomorrow, I’m busy right now.
At least briefly: The variable “PV_suff” is defined at the beginning of the sent code in the first declared sensor. Which makes me think that if this variable is an expression of the sensor “PV-suff”, then in the next code I should use the expression “sensor.pv_sufficient” and not “PV_suff” as the variable?
Thanks again.

In actual configuration (unlike the template editor) Jinja variables only have local scope, their values are not available in other sensor’s configurations. The definition of PV_suff exists only for the configuration of sensor.pv_suff, everywhere else the value of PV_suff is undefined.

You can just add a set statement to each sensor after the first using PV_suff …something like:

{% set PV_suff = states('sensor.pv_suff') | float(0) %}