# Calculating results of two fields

**URL:** <https://forum.inductiveautomation.com/t/calculating-results-of-two-fields/97435>\
**Category:** Ignition\
**Created:** [October 18, 2024, 2:12am UTC](https://forum.inductiveautomation.com/t/calculating-results-of-two-fields/97435 "2024-10-18T02:12:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aisha\_Shiddat](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@Aisha\_Shiddat](https://forum.inductiveautomation.com/u/Aisha_Shiddat)\
**Post date:** [October 18, 2024, 2:12am UTC](https://forum.inductiveautomation.com/t/calculating-results-of-two-fields/97435/1 "2024-10-18T02:12:01Z")

</div>

I am trying to calculate the results of 2 fields. I need to subtract a gross weight value from a scale value. The results should display in a field called net weight. Can this be done with an expression or either a focus event? Below I tried creating a property script on the net weight field but i'm not getting the desired results. What would be the best way to do this?

```python
def valueChanged(self, previousValue, currentValue, origin, missedEvents):
	
	gross_weight = self.parent.parent.parent.parent.getChild("Scrap_Receiving_Flex_Container").custom.gross_weight
	scale_weight = self.parent.parent.parent.parent.getChild("Scrap_Receiving_Flex_Container").custom.scale_weight
	result = gross_weight - scale_weight
	
	if(gross_weight is not None and scale_weight is not None):
		return result
		

```

 ![ScrapShipment Receiving](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/9/294763fb57d39ab11c71036ded5931a190671099.png)

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [October 18, 2024, 3:21am UTC](https://forum.inductiveautomation.com/t/calculating-results-of-two-fields/97435/2 "2024-10-18T03:21:05Z")

</div>

You can do this with a an expression along the lines of

```python
if(
    coalesce({path.to.gross.weight.value}, 0) != 0 && coalesce({path.to.scale.weight}, 0) != 0,
    {path.to.gross.weight.value} - {path.to.scale.weight.value}, ""
)

```

> [@Aisha\_Shiddat](#):
>
> `return result`

This isn't doing anything, you need to set the property value directly, like `path.to.some.property = result`

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [October 18, 2024, 5:55am UTC](https://forum.inductiveautomation.com/t/calculating-results-of-two-fields/97435/3 "2024-10-18T05:55:38Z")

</div>

> [@Aisha\_Shiddat](#):
>
> ```python
> gross_weight = 
> self.parent.parent.parent.parent.getChild(
> "Scrap_Receiving_Flex_Container"
> ).custom.gross_weight # code wrapped for legibility
> 
> ```

This sort of relative addressing becomes a big headache when moving components in and out of containers. Instead, move `gross_weight` and `scale_weight` out to custom properties on the view. Now your code (if you were still using a script) would become,

```python
def valueChanged(self, previousValue, currentValue, origin, missedEvents):
	if(view.custom.gross_weight is not None and view.custom.scale_weight is not None):
	    view.custom.net_weight = view.custom.gross_weight - view.custom.scale_weight

```

and Ryan's expression binding would become,

```python
if(
    coalesce({view.custom.weight.value}, 0) != 0 
    && 
    coalesce({view.custom.scale.weight}, 0) != 0,
    {view.custom.weight.value} - {view.custom.weight.value}, 
    ""
)

```

Now you can move components in and out of containers, nest them, or whatever and the bindings won't break.

---

<div class="post-metadata">

**Author:** ![Aisha\_Shiddat](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@Aisha\_Shiddat](https://forum.inductiveautomation.com/u/Aisha_Shiddat)\
**Post date:** [October 18, 2024, 12:41pm UTC](https://forum.inductiveautomation.com/t/calculating-results-of-two-fields/97435/4 "2024-10-18T12:41:33Z")

</div>

Call the expression on the net weight field correct?

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [October 18, 2024, 12:42pm UTC](https://forum.inductiveautomation.com/t/calculating-results-of-two-fields/97435/5 "2024-10-18T12:42:53Z")

</div>

> [@ryan.white](#):
>
> `coalesce({path.to.gross.weight.value}, 0) != 0`

You can use `!isNull({path.to.gross.weight.value})` instead.

> [@Aisha\_Shiddat](#):
>
> ```python
> result = gross_weight - scale_weight
> 	
> if(gross_weight is not None and scale_weight is not None):
> return result
> 
> ```

You should do the null check BEFORE using the values. If one of them really is null, the operation will raise an exception and the code will never reach the null check.  
something like this:

```python
gross_weight = self.parent.parent.parent.parent.getChild("Scrap_Receiving_Flex_Container").custom.gross_weight
scale_weight = self.parent.parent.parent.parent.getChild("Scrap_Receiving_Flex_Container").custom.scale_weight

if gross_weight is not None and scale_weight is not None:
	return gross_weight - scale_weight
else:
	return default_value
# OR
try:
	return gross_weight - scale_weight
except TypeError:
	return default_value

```

> [@Aisha\_Shiddat](#):
>
> Call the expression on the net weight field correct?

Yes, put an expression binding on the net weight field's value with that expression:

```python
if (!isNull({view.custom.weight.value}) && !isNull({view.custom.scale.weight}),
    {view.custom.weight.value} - {view.custom.weight.value}, 
    default_value
)

```

Make sure to replace `{default_value}` by whatever you want it to be. `""` or `null` probably
