[SOLVED] Can't update a State

Basically I want to update a Number (Setpoint item) variable to a default Value, if it’s null or rather not configured on the GUI.
According to the docs:
https://www.eclipse.org/smarthome/documentation/javadoc/org/eclipse/smarthome/core/types/State.html
It should work like this:
So I have the following code:

rule "Timer gLights"
when 
Member of gLights received command ON
then
//Int Variable der GUI
if(gLightsOffset.state.toString == "null")
{
    var State myState = new State(90)
    gLightsOffset.setState(myState)
}
timeOffset = (gLightsOffset.state as Number).intValue
    if(timer ===  null | timer.hasTerminated){
    timer = createTimer(now.plusMinutes(timeOffset), [|
        gLights.sendCommand(OFF);
        //Systembenachrichtigungen
        sendBroadcastNotification("Timer abgelaufen: Lichter wurden nach "+ timeOffset.toString + " Minuten automatisch ausgeschalten.")
        logInfo("GroupLights.rules","Das Ende des Skripts wurde erreicht. Lichter wurden ausgeschalten")
    ])
    }
    else
    {
        timer.reschedule(now.plusMinutes(timeOffset))
    } 
end

But I’m getting an Error at

    var State myState = new State(90)
    gLightsOffset.setState(myState) //postUpdate didn't work

After importing:

import org.eclipse.smarthome.core.types;

I get this Error:

2018-11-02 19:15:40.822 [ERROR] [e.internal.WriterInterceptorExecutor] - MessageBodyWriter not found for media type=text/event-stream, type=class org.glassfish.jersey.media.sse.OutboundEvent, genericType=class org.glassfish.jersey.media.sse.OutboundEvent.

What does that Exception mean?

NULL not null. NULL is an openHAB state. null is a java variable value/condition
setState ??? Where does that come from??
To update an item state use the postUpdate method

rule "Timer gLights"
when 
    Member of gLights received command ON
then
    //Int Variable der GUI
    if(gLightsOffset.state.toString == "NULL") {
        gLightsOffset.postUpdate(90)
    }

Please see:

Why transforming it to string, of course it work’s but shouldn’t the state return the datatype, and if not initialized return null?

postUpdate didn’t work sadly

From the name of the item (gLightsOffset) it looks like this is a group, or? I know you should be able to use .sendCommand() on a group item, but I am not sure you can use .postUpdate(). Anyone?

I didn’t you did. I took your rule and corrected null to NULL

You could always use:

 if (gLightsOffset.state == NULL) {

That would also work

Correct, if the item is a group then you can’t postUpdate because the item is updated by its members.
But using sendCommand will cascade the command to all the items in the group provided the group and the items are of the same type.

Sorry! Is it “can” or should it be “cannot”?

Cannot. You cannot update a Group state.

You can send a command via a Group, that gets passed to all members. That will not update the Group’s state either. The command may eventually result in members states changing, that may or may not cange the Group’s state, depending on if/how the Group’s aggregate functions are defined.

Short story : Group state is only calculated from member states, and then only if you define how in an aggregate function.

Cannot, typo corrected, sorry

gLightsOffset is a Number variable

Is it a Number variable or a Number item? Because the two are very different things

it’s an item, variable would be in a rules file righ? sorry didn’t thought about that

Good, can you post that item definition, please?

Number gLightsOffset "Maximale Licht andauer"

Ok, so that is the rule as it is now?

rule "Timer gLights"
when 
    Member of gLights received command ON
then
    //Int Variable der GUI
    if(gLightsOffset.state == NULL) {
        gLightsOffset.postUpdate(90)
    }
    timeOffset = (gLightsOffset.state as Number).intValue
    if (timer ===  null) {
        timer = createTimer(now.plusMinutes(timeOffset), [|
            gLights.sendCommand(OFF);
            //Systembenachrichtigungen
            sendBroadcastNotification("Timer abgelaufen: Lichter wurden nach "+ timeOffset.toString + " Minuten automatisch ausgeschalten.")
            logInfo("GroupLights.rules","Das Ende des Skripts wurde erreicht. Lichter wurden ausgeschalten")
        ])
    }
    else {
        timer.reschedule(now.plusMinutes(timeOffset))
    } 
end

And you are saying that the postUpdate(90) doesn’t work. What does the log show when you trigger that rule and gLightsOffset is NULL?

1 Like

I was wrong, PostUpdate actually didn’t worked, but I interpreted the logs wrong.

You’ve been right when you’ve said that it’s not null.
After changing it worked!

Thank you alot!