# Set upper and lower limits for a variable

**URL:** <https://community.homey.app/t/set-upper-and-lower-limits-for-a-variable/119981>\
**Category:** Questions & Help\
**Created:** [November 7, 2024, 1:48pm UTC](https://community.homey.app/t/set-upper-and-lower-limits-for-a-variable/119981 "2024-11-07T13:48:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xander\_Hoose](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/xander_hoose/32/131829_2.png) [@Xander\_Hoose](https://community.homey.app/u/Xander_Hoose)\
**Post date:** [November 7, 2024, 1:48pm UTC](https://community.homey.app/t/set-upper-and-lower-limits-for-a-variable/119981/1 "2024-11-07T13:48:47Z")

</div>

I want to calculate the charge rate for my solar battery with the logic card below. However, the result can be negative in some cases, and I also want to set an upper limit. What is the easiest way to do the following with the output:

- If #charge\_rate is lower than 100, set charge\_rate to 100
- If #charge\_rate is higher than 2000, set charge\_rate to 2000

 ![Screenshot 2024-11-07 at 14.44.29](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/f/e/fe34f4d3d355e8b2b77e4b6cb1f43fb65553770d.png)

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [November 7, 2024, 5:01pm UTC](https://community.homey.app/t/set-upper-and-lower-limits-for-a-variable/119981/2 "2024-11-07T17:01:38Z")

</div>

This works  
`{{ min(max([value],100),2000) }}`

 ![image18](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/2/d/2d26390beb9e4a7472cc966b33ff3717b7263493.png)

So when I replace the example value `15000` with your calculation, it should be like this:

 ![Screenshot_20241107_175007_Firefox](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/1/6/1674cb2a967dc3ab3025dce421033bdd757cb18e.jpeg)

---

<div class="post-metadata">

**Author:** ![Xander\_Hoose](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/xander_hoose/32/131829_2.png) [@Xander\_Hoose](https://community.homey.app/u/Xander_Hoose)\
**Post date:** [November 8, 2024, 9:13am UTC](https://community.homey.app/t/set-upper-and-lower-limits-for-a-variable/119981/3 "2024-11-08T09:13:58Z")

</div>

Thanks, that’s amazing! I didn’t realize that the result could produce floats, but combined with the round(X,0) function the calculation works like a charm!

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [November 9, 2024, 2:04am UTC](https://community.homey.app/t/set-upper-and-lower-limits-for-a-variable/119981/4 "2024-11-09T02:04:48Z")

</div>

YW! Yeah adding round() is the finishing touch 😉
