Cannot get getHue to work

  • Platform information:
    • Hardware: MiniPC
    • OS: Debian
    • Java Runtime Environment: The one suggested by Openhab website
    • openHAB version: 3
      I’m trying to change color of a lightbulb with a double press of a button.

This is the code i wrote:

case "DOUBLE_PRESSED": {

          var HSBType lettocoltotale = letto_lucecolore.state.toString
          logInfo("Heartbeat", lettocoltotale)

        	var DecimalType phue = lettocoltotale.getHue
          logInfo("Heartbeat", phue)

          var PercentType psat = lettocoltotale.getSaturation
          logInfo("Heartbeat", psat)

          var PercentType pbri = lettocoltotale.getBrightness
          logInfo("Heartbeat", pbri)

        	phue = new DecimalType (phue + 60)
        	if (phue => 360){
        		phue = 0
        	}
        	var HSBType letcolore = new HSBType(phue,psat,pbri)
        	letto_lucecolore.sendCommand(letcolore)
        	return
      }

And this is what comes in the logs:

2023-08-26 19:52:47.701 [INFO ] [openhab.event.ChannelTriggeredEvent ] - shelly:shellyplusi4:4381f6ef98:status1#button triggered DOUBLE_PRESSED

2023-08-26 19:52:47.704 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'Lettoi41' received command DOUBLE_PRESSED

2023-08-26 19:52:47.707 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'Lettoi41' predicted to become DOUBLE_PRESSED

==> /var/log/openhab/openhab.log <==

2023-08-26 19:52:47.708 [INFO ] [.openhab.core.model.script.Heartbeat] - 212,85,59

2023-08-26 19:52:47.711 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'shellyi4-1' failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.library.types.HSBType.getHue() on instance: 212,85,59 in shellyi4


I tried many variations. If i remove .toString from the first var, i get

==> /var/log/openhab/openhab.log <==

2023-08-26 19:41:09.058 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'shellyi4-1' failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.model.script.actions.Log.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null in shellyi4

I really have no clue what i’m doing wrong, tried searching in the community and on google for a solution with no luck.

Can anyone point me in the right direction?

The lightbulb is a philips Hue and it works perfectly with UI.

I was using the same script in openhab 2 and it used to work.

shouldn’t that be

var HSBType lettocoltotale = letto_lucecolore.state
logInfo("Heartbeat", lettocoltotale.toString)

Thanks for your answer.
Unfortunately it’s not.
If i don’t put the “.toString” on the var, i get a null value and it gives me an error.
The loginfo (without the toString) gives me the correct value if I put the “toString” on the var, but getHue still doesn’t work.

Yeh but you can’t call letto_lucecolore.state.toString which returns a String and magically treat it like an HSBType later: lettocoltotale.getHue. A String doesn’t have a getHue method so that’s not going to work.

You can’t take a String and magically convert it to an HSBType just by declaring the variable as an HSBType. Unfortunately Rules DSL will just see that the value you have is a String and just use that, ignoring your var HSBType entirely. That’s the source of @Wolfgang_S’s suggestion. It’s almost always better not to specify the type of variables unless absolutely necessary. It only causes problems and you can’t even trust it.

Once it’s a String, you can only do String stuff with it.

case "DOUBLE_PRESSED": {

          var lettocoltotale = letto_lucecolore.state as HSBType
          logInfo("Heartbeat", lettocoltotale.toString)

        	var phue = lettocoltotale.getHue
          logInfo("Heartbeat", phue.toString)

          var psat = lettocoltotale.getSaturation
          logInfo("Heartbeat", psat.toString)

          var pbri = lettocoltotale.getBrightness
          logInfo("Heartbeat", pbri.toString)

        	phue = new DecimalType (phue + 60)
        	if (phue => 360){
        		phue = 0
        	}
        	var letcolore = new HSBType(phue,psat,pbri)
        	letto_lucecolore.sendCommand(letcolore)
            return;
      }

Thanks for your answer.

I did try it without the .tostring in the state and with it in the loginfo.

It was just not working in any way.

Dunno why, but i rewrote the code from zero, looked identical at the previous one and worked

I’m puzzled.