Rules and Persistance Trigger

Hello, I have two items below; How can I save the Inlet Bar values to the database when the Water Pump is On. I want it to not record when the Water Pump is Off.

home.items
Switch Water_Pump					"Water Pump"					<selector>		(gG_Watering)	{channel="mqtt:topic:haybolat:gardenwatering:waterpump"}
Number Bar_G_Inlet					"Inlet Bar [%s bar]"			<manometer3>	(gG_Watering)	{channel="mqtt:topic:haybolat:gardenwatering:inletbar"}
home.sitemap
Switch item=Water_Pump
Text item=Bar_G_Inlet

One way I can think of is using a cron trigger with a condition that water_pump is on.
Then the action is a script: Bar_G_Inlet.persist (depending on your chosen scripting language)

Sorry, I’m a newbie, how do I do it?

What’s your preferred method of creating rules? Via web / mainui or file based?

file-based

and which language?

jdbc

I meant, do you use rulesdsl, jsscripting, something else?

rulesdsl

I haven’t tested this, but give it a try:

rule "Persist Bar_G_Inlet When Water_Pump is ON"
when 
    Time cron "0 */5 * * ? * ?"
then
    if Water_Pump.state == ON {
        Bar_G_Inlet.persist
    }
end

Do I copy this code into jdbc.persist?

No that’s not for jdbc.persist

Call it “bar_g_inlet_persistence.rules” (the name is not important, but the extension .rules is) and save that in <CONF>/rules/ folder

Thank you very much for your interest.

I created a file called bar.rules and pasted the code you gave there.
(rule “Persist Bar_G_Inlet When Water_Pump is ON”
when
Time cron “0 */5 * * ? * ?”
then
if Water_Pump.state == ON {
jdbc.persist
}
end)

Will I write anything to the jdbc.persist file?

No don’t add the same item to your persist file.

Unfortunately I couldn’t. :frowning:
Any chance to show an example?

What I’m trying to say here is that the Inlet Bar is working, but I want it to be taken regardless of whether the Water Pump is working in order to record its value. I hope I was able to express myself.

Edit your jdbc.persist file

Strategies {
        everyHour : "0 0 * * * ?"
        everyDay  : "0 0 0 * * ?"
}

Items {
  Water_Pump, Bar_G_Inlet : strategy = everyChange, everyHour, everyDay
}

It’s just an example you can change the Cron

“0 0 * * * ?”
To whatever you want

If “Water_Pump” is on, “Bar_G_Inlet” database will record it in 5 minutes

That is what you said you want it recording no matter what the pump is working.

Be more specific in your explanation take your time put it step by step what needs to happens in what order etc requirement in a understandable fashion.

Put his in the stratagies:
every5Minute : “0 */5 * ? * *”

  Water_Pump, Bar_G_Inlet : strategy = everyChange, every5Minute, everyHour, everyDay

it will graph it every 5 minutes whether it changes or not. I use this so I can graph my item.