[Solved] Int from switch-item (number) as now.plusMinutes

For my sprinklers I set up several “channels” in openHAB, that are grouped by a group:switch. Works. I’d like to stop watering automatically using a rule. My idea was to define a switch (that is not part of the switch-group) with (time-duration-)mappings as input for the rule:

sitemap:

    Frame label="Gartenbewässerung" {
        Switch item=gBewaesserung label="Gesamte Bewässerung" mappings=[OFF="AUS",ON="AN"]
        Switch item=Bewaesserungsdauer label="Bewässerungsdauer" icon="time" mappings=[30="30 min", 45="45 min", 60="1 h", 75="75 min"]
    	Switch item=Seitenstreifen
        Switch item=Hauptflaeche
        Switch item=Wassersteckdose
    	Switch item=Vorgarten
    	Switch item=Pumpe
    }

items

Group:Switch:OR(ON,OFF) gBewaesserung		"Bewässerung"	<water> 	(EG_Garten)
Number Bewaesserungsdauer					<clock>
Switch Seitenstreifen 		"Rasen Seitenstreifen"		<water> 	(gBewaesserung) {mqtt...}
[...]

rule

rule "Gartenbewaesserung 1"

when
    Item gBewaesserung changed to ON
then
    logInfo("rules", Bewaesserungsdauer.state)
     createTimer((now.plusMinutes(Bewaesserungsdauer.state as DecimalType).intValue), [|
	gBewaesserung.sendCommand(OFF)
    ])
end

However, rule doesn’t hit because of:
“Rule ‘Gartenbewaesserung 1’: An error occurred during the script execution: Could not invoke method: org.joda.time.DateTime.plusSeconds(int) on instance: 2018-12-16T23:40:12.182+01:0”
I also tried to set e.g. “30.0” instead of “30” - didn’t work.

Have you a duplicate named rule? The one you’ve shown us has plusMinutes, yet the error complains about plusSeconds

This should work
now.plusMinutes(Bewaesserungsdauer.state as Number).intValue
provided the Item always has a value i.e. not NULL

Thanks for your quick answer!
Well observed :slight_smile: No, it’s just because I changed it meanwhile (to seconds) in order to shorten waiting a bit :slight_smile: Later, in productive, it should be minutes. However, result was/is the same.
Unfortunately, your suggestion proposed, leads to the same error-msg. And I really made sure, this element was set.

Log:
2018-12-17 08:28:37.995 [ome.event.ItemCommandEvent] - Item ‘Bewaesserungsdauer’ received command 1
2018-12-17 08:28:41.405 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Gartenbewaesserung 1’: An error occurred during the script execution: Could not invoke method: org.joda.time.DateTime.plusMinutes(int) on instance: 2018-12-17T08:28:41.394+01:00

What is the output of this?

Almost the same - just put it in for debug-purposes:

2018-12-17 08:18:08.296 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Gartenbewaesserung 1’: An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.LogAction.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null

Anyway, all values are sent to mqtt and using the client, it shows the item is not empty. Tried it several times:

mosquitto_sub -v -t ‘openhab2/out/Bewaesserungsdauer/state’
openhab2/out/Bewaesserungsdauer/state 1
openhab2/out/Bewaesserungsdauer/state 30
openhab2/out/Bewaesserungsdauer/state 45
openhab2/out/Bewaesserungsdauer/state 60
openhab2/out/Bewaesserungsdauer/state 75

Weird…

Try that

rule "Gartenbewaesserung 1"
when
    Item gBewaesserung changed to ON
then
    logInfo("rules", Bewaesserungsdauer.state)
    val int timerTime = (Bewaesserungsdauer.state as Number).intValue
    createTimer((now.plusMinutes(timerTime), [|
	gBewaesserung.sendCommand(OFF)
    ])
end
1 Like

Had to remove one parenthesis - but anyway: I’m still getting the same
error-msg :frowning:

Ok,
But I still want to see the logInfo output, please

He posted the error of the logInfo above.It shows that the instance is null.

Yep,
Sorry.
Well, that is odd indeed.

What does the log shows when you change the value on the sitemap.
The log, not MQTT

openhab.log
2018-12-17 11:27:54.145 [ERROR] [ntime.internal.engine.RuleEngineImpl] -
Rule ‘Gartenbewaesserung 1’: An error occurred during the script
execution: Could not invoke method:
org.eclipse.smarthome.model.script.actions.LogAction.logInfo(java.lang.String,java.lang.String,java.lang.Object[])
on instance: null

=> It only produces one error-msg. When I remove logging, null-instance
error-msg is created for createTimer.

events.log
2018-12-17 11:27:53.079 [ome.event.ItemCommandEvent] - Item
‘Bewaesserungsdauer’ received command 1
2018-12-17 11:27:53.775 [vent.ItemStateChangedEvent] - Temperature_ZT
changed from 20.2 to 20.3
2018-12-17 11:27:54.088 [ome.event.ItemCommandEvent] - Item
‘gBewaesserung’ received command ON
2018-12-17 11:27:54.101 [ome.event.ItemCommandEvent] - Item
‘Seitenstreifen’ received command ON
2018-12-17 11:27:54.116 [ome.event.ItemCommandEvent] - Item
‘Hauptflaeche’ received command ON
2018-12-17 11:27:54.127 [ome.event.ItemCommandEvent] - Item
‘Wassersteckdose’ received command ON
2018-12-17 11:27:54.133 [ome.event.ItemCommandEvent] - Item ‘Pumpe’
received command ON
2018-12-17 11:27:54.140 [ome.event.ItemCommandEvent] - Item ‘Vorgarten’
received command ON
2018-12-17 11:27:54.163 [vent.ItemStateChangedEvent] - Seitenstreifen
changed from OFF to ON
2018-12-17 11:27:54.166 [vent.ItemStateChangedEvent] - Hauptflaeche
changed from OFF to ON
2018-12-17 11:27:54.170 [vent.ItemStateChangedEvent] - Wassersteckdose
changed from OFF to ON
2018-12-17 11:27:54.174 [vent.ItemStateChangedEvent] - Pumpe changed
from OFF to ON
2018-12-17 11:27:54.177 [vent.ItemStateChangedEvent] - Vorgarten changed
from OFF to ON

=> After setting duration (was already set, but I wanted to make sure,
it really is) I clicked on the group-button.

When having a closer look at the log, I recognize “Bewaesserungsdauer”
receives command, but doesn’t change state!? Or is this just the way
openHAB does logging?

Commands do not directly change Item states. As a generalism, a command would be sent out via a binding to hardware, and at some later time a response would come back in and update the Item.state.
OH includes an “autoupdate” feature that acts like a binding, and make the state update. It’s enabled by default and looks to be enabled for Bewaesserungsdauer.

With no event.log for state update and no evidence in your rule that state ever gets set - there is certainly something weird about that Item.

I’ll put forward my duplicate theory again - one from .items fle and one from paperUI.

Make yourself a fresh Item and see how that goes. Please give it both “label” and <icon>, or neither, for the sake of looking more sensible :slight_smile:

1 Like

In order to not run into the problem with having defined things twice
(cfg / paperUI), I usually don’t create any items via paperUI :slight_smile:
“Usually” means for Jeelink-things I did. However, I added a description
for the number-item and had a look at paperUI’s Items-list. After using
webbrowser’s search-function for “dauer”, it turned out that number-item
is there, but switch-item is not. But maybe this is, because I defined
the switch-item directly in the sitemap?!

Nobody complained to far, but is it correct, to define
“Bewaesserungsdauer” as switch in sitemap (not in items) while a
same-named item of type Number is defined in items?

Yes that’s fine.

There could be a hidden character somewhere in the files… It’s happened before.
Delete the item from the item file. Don’t comment it out, delete it.
Save the file. What do the logs say?
Recreate your item, by typing it in. Don’t copy and paste. Save. What do the logs say?

Then do the same for the rule.

All fine. The Item will be as you defined it ; whatever goes in the sitemap is purely about presentation.

@vzorglub:
Ok, I’ll do so later. Currently, I’m not at home and using my
smartphone’s terminal is a bit crappy for debugging-purposes :slight_smile:

@rossko57:
To be honest, I’m still having some problems with sitemap vs. items (not
technically, but understanding). In the past, I just had a pretty short
sitemap-file with one frame that pointed to a group. All the
group-configuration along with every item was done using an item-file.
However, as I learned recently, sitemap is more powerful and if I got it
right, item-file should only carry definitions while sitemap should be
the link for representation <=> items. That said, your sentence
“whatever goes in the sitemap is purely about presentation” made things
a lot clearer to me :slight_smile:

@All:
For now: many thanks for the support, which is really appreciated!

Hi, i think i can help.
Try this:

logInfo("rules", "test: " + Bewaesserungsdauer.state)

There must be a second Constant in the Log-Staetment

The Rule is like @vzorglub posted, including a further closing bracket after (timerTime))

Tested with

now.plusSeconds

rule "Gartenbewaesserung 1"

when
    Item gBewaesserung changed to ON
then
    logInfo("rules", "test" + Bewaesserungsdauer.state)
     val int timerTime = (Bewaesserungsdauer.state as Number).intValue
     createTimer((now.plusSeconds(timerTime)), [|
     gBewaesserung.sendCommand(OFF)
    ])
end
2 Likes

Indeed, fibu-freak, this works! Thank you!