Assigning item Color state to variable for .sendCommand in jython script

I have a rule where, if triggered, the color state of one hue bulb should be sent to another, resulting in the other bulb lighting up in the same color.
The bulbs, are assigneded to similar items as shown:

Color       Lys_TvStue_BronzeKugle_Color

In the rule i am trying the following:

var newBulbColorState = items.Lys_TvStue_BronzeKugle_Color
events.sendCommand("Lys_TvStue_StorKugle_Color", newBulbColorState)

I feel like this should be fairly easy, but can’t find anything on the forums regarding HSBType conversions in python examples, and I have tried the following without success:

Have you thought of using profiles for this instead of rules?

events.sendCommand("Lys_TvStue_StorKugle_Color", items.Lys_TvStue_BronzeKugle_Color.toString())

Since you have used a string for the Item name, you will need to use a string for the state. Here is a table of the combinations of accepted arguments for sendCommand and postUpdate.

First off, thanks for your help - much appreciated! :smiley:

I have thought of using profiles after having looked at them yesterday. I decided not to use them for 2 reasons:

  1. As I understand linking this profile to the other bulb would mean the 2 bulbs would toggle or follow each other always - I have conditions in my rule, which means that only under certain circumstances should the state be transferred. Here is the part of my rule that concerns this:
from core.rules import rule
from core.triggers import when
from time import sleep
from core.actions import ScriptExecution
from org.joda.time import DateTime

heatSysDelayAlertTimer = None
heatSysDelayAtHomeTimer = None

@rule("Advarsel ved varmesytem fejl", description="Blink pære i stuen rødt når en fejl på varmesystemet detekteres")
@when("Item Teknikrum_WemosD1_FejlKoder changed")
def heatingSysErrorDetection(event):
    global heatSysDelayAlertTimer
    global heatSysDelayAtHomeTimer
    heatingSysErrorDetection.log.debug("Heating System Error change detected")
    # If error is cleared
    if items.Teknikrum_WemosD1_FejlKoder == StringType("99"):
        # Cancel timer if it is still running
        if heatSysDelayAlertTimer is not None and not heatSysDelayAlertTimer.hasTerminated():
                "Heating System Error Alert Delay Timer cancelled")
        # Cancel timer if it is still running
        if heatSysDelayAtHomeTimer is not None and not heatSysDelayAtHomeTimer.hasTerminated():
                "Heating System Error At-Home-Delay Timer cancelled")
                "Heating System Error Detection - Error is cleared!!")
            # var newBulbColorState = items.Lys_TvStue_BronzeKugle_Color

(Just a follow up question - should I include the entire rule code in the future for completeness, or is it just more clutter than benefit?)

  1. I’m unsure whether it is the case, but given the above, in order to make the bulb state transfer circumstancial, it would mean writing alot more code than just the 1 line assigning the state of the other bulb to a var, and sending it.

Since I was planning on having other rules with the same approach (not using profiles, in these cases to save on lines of code) I was hoping to get some insight as how to do this for other datatypes as well. In this matter you have broadened my horizon, which is awesome - so if I understand this correctly, if I pass the item as the 1st arguement, my initial approach using the simple var assigned to the state should work and look like this:

            var newBulbColorState = items.Lys_TvStue_BronzeKugle_Color
                "Lys_TvStue_StorKugle_Color"), newBulbColorState)

But VScode gives me a syntax error for the var - I have also tried with the following but the result is the same (syntax error for HSBType):

var HSBType newBulbColorState = items.Lys_TvStue_BronzeKugle_Color

IMO, too much info is never a bad thing. In this case, it would have clarified things and your code may help someone else in the future.

Variables are not defined like this in Python. I don’t see a need for this variable, but you can simply use (no var)…

newBulbColorState = items.Lys_TvStue_BronzeKugle_Color

If worried about distracting from your actual problem, do a bit of work at your end and consider simplifying the rule under suspicion to some simpler demonstration. Sometimes you even find the actual issue that way :wink:

Blockquote[quote=“rossko57, post:5, topic:94746”]
If worried about distracting from your actual problem,

Its not so much about distraction, as it is wasting other peoples time making them look for the issues they are being asked to help with… but point taken gents!
And thanks for you help, I’m only used to programming in C/C++ so all this with not declaring types etc still stings my eyes a little - it just looks wrong! :grin: