# Need help with temperature associated rule

**URL:** <https://community.openhab.org/t/need-help-with-temperature-associated-rule/76622>\
**Category:** Scripts & Rules\
**Created:** [June 18, 2019, 10:17pm UTC](https://community.openhab.org/t/need-help-with-temperature-associated-rule/76622 "2019-06-18T22:17:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![epicurean](https://community.openhab.org/user_avatar/community.openhab.org/epicurean/32/37104_2.png) [@epicurean](https://community.openhab.org/u/epicurean)\
**Post date:** [June 18, 2019, 10:17pm UTC](https://community.openhab.org/t/need-help-with-temperature-associated-rule/76622/1 "2019-06-18T22:17:17Z")

</div>

I currently use this rule to start the fans at my rack when it goes over a certain temperature.

```php
rule "Rackfan power to ON at 1200hr"
when
	Time cron "0 0 12 ? * Mon-Sun *" or
	Item PS3Powerstrip22URack_Temperature received update
then
	if ((PS3Powerstrip22URack_Temperature.state > 40) && now.getHourOfDay() >=13 && now.getHourOfDay() <=18) {
    Rackfan_Power.sendCommand(ON)
	sendTelegram("bot1", "Rackfan is switched ON as rack temperature is above 40°C" )
	logInfo("Info","Rackfan power ON Rule fired")	    
    }
	
    else if ((PS3Powerstrip22URack_Temperature.state < 40) && now.getHourOfDay() >=13 && now.getHourOfDay() <=18) {
    Rackfan_Power.sendCommand(OFF)
	logInfo("Info","Rackfan power OFF Rule fired")	
    }
        
end	

```

How do I change it that it only turns off at least 10 minutes after it detects the temperature falling below the threshold, and to fire the rule again only after at least 1 hour after it detects the temperature change (to stop spamming me with messages)

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [June 19, 2019, 2:11pm UTC](https://community.openhab.org/t/need-help-with-temperature-associated-rule/76622/2 "2019-06-19T14:11:13Z")

</div>

Create a timestamp when you turn it on. When the Rule fires check that timestamp to see if it has been enough time since the last command.

---

<div class="post-metadata">

**Author:** ![epicurean](https://community.openhab.org/user_avatar/community.openhab.org/epicurean/32/37104_2.png) [@epicurean](https://community.openhab.org/u/epicurean)\
**Post date:** [June 19, 2019, 10:38pm UTC](https://community.openhab.org/t/need-help-with-temperature-associated-rule/76622/3 "2019-06-19T22:38:20Z")

</div>

Hi Rick  
Are you able to share how exactly do you create a timestamp on the rule? I am rather new at this.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [June 20, 2019, 3:04pm UTC](https://community.openhab.org/t/need-help-with-temperature-associated-rule/76622/4 "2019-06-20T15:04:32Z")

</div>

Define a [global variable](https://www.openhab.org/docs/configuration/rules-dsl.html#the-syntax). Let’s call it timestamp.

In the rule

```
timestamp = now

```

---

<div class="post-metadata">

**Author:** ![Andrew\_Rowe](https://community.openhab.org/user_avatar/community.openhab.org/andrew_rowe/32/37075_2.png) [@Andrew\_Rowe](https://community.openhab.org/u/Andrew_Rowe)\
**Post date:** [June 23, 2019, 6:10am UTC](https://community.openhab.org/t/need-help-with-temperature-associated-rule/76622/5 "2019-06-23T06:10:28Z")

</div>

Here is a rule example which compares two times to make a simple decision similar to your use case. It might get you started. Ask more questions as your go and we’ll get this working!

> [@Rule example - compare two times](https://community.openhab.org/t/rule-example-compare-two-times/71328):
>
> This is a simple rule example for folks like me, who are new to openHAB, and need simple examples of rules to get started. In this example, two times are compared, enabling us to make a decision based on if one event occurred before another. In the example, a contact switch on an entry door triggers a time stamp. A motion detector also triggers a time stamp. The two times can be compared to see if the occupant (andy) is home (motion since door was opened) or left (no motion since door was opene…

---

<div class="post-metadata">

**Author:** ![epicurean](https://community.openhab.org/user_avatar/community.openhab.org/epicurean/32/37104_2.png) [@epicurean](https://community.openhab.org/u/epicurean)\
**Post date:** [June 23, 2019, 12:05pm UTC](https://community.openhab.org/t/need-help-with-temperature-associated-rule/76622/6 "2019-06-23T12:05:10Z")

</div>

Thanks so much Andrew. Will I will definately try to understand it
