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

**URL:** <https://community.openhab.org/t/assigning-item-color-state-to-variable-for-sendcommand-in-jython-script/94746>\
**Category:** Scripts & Rules\
**Tags:** jython, openhab2-jython\
**Created:** [March 17, 2020, 9:24pm UTC](https://community.openhab.org/t/assigning-item-color-state-to-variable-for-sendcommand-in-jython-script/94746 "2020-03-17T21:24:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zylle](https://community.openhab.org/letter_avatar_proxy/v4/letter/z/f475e1/32.png) [@Zylle](https://community.openhab.org/u/Zylle)\
**Post date:** [March 17, 2020, 9:24pm UTC](https://community.openhab.org/t/assigning-item-color-state-to-variable-for-sendcommand-in-jython-script/94746/1 "2020-03-17T21:24:14Z")

</div>

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:

```csv
Color Lys_TvStue_BronzeKugle_Color

```

In the rule i am trying the following:

```php
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:

> [@Experimental Next-Gen Rules Engine Documentation 4 of : Writing Scripts](https://community.openhab.org/t/experimental-next-gen-rules-engine-documentation-4-of-writing-scripts/55963/1):
>
> However, if all you need is the Item’s state, there is a Map of Item names and their corresponding States available in the `items` Object.
> 
> ```auto
> // Both with or without library
> var MyItemState = items["MyItem"];
> 
> ```

---

<div class="post-metadata">

**Author:** ![5iver](https://community.openhab.org/user_avatar/community.openhab.org/5iver/32/21201_2.png) [@5iver](https://community.openhab.org/u/5iver)\
**Post date:** [March 17, 2020, 10:03pm UTC](https://community.openhab.org/t/assigning-item-color-state-to-variable-for-sendcommand-in-jython-script/94746/2 "2020-03-17T22:03:45Z")

</div>

> [@Zylle](#):
>
> 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.

Have you thought of using [profiles](https://www.openhab.org/docs/configuration/items.html#profiles) for this instead of rules?

> [@Zylle](#):
>
> 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:

```auto
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](https://www.openhab.org/docs/configuration/jsr223.html#events-operations) of the combinations of accepted arguments for sendCommand and postUpdate.

---

<div class="post-metadata">

**Author:** ![Zylle](https://community.openhab.org/letter_avatar_proxy/v4/letter/z/f475e1/32.png) [@Zylle](https://community.openhab.org/u/Zylle)\
**Post date:** [March 18, 2020, 8:26am UTC](https://community.openhab.org/t/assigning-item-color-state-to-variable-for-sendcommand-in-jython-script/94746/3 "2020-03-18T08:26:50Z")

</div>

First off, thanks for your help - much appreciated! 😃

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:

```php
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():
            heatSysDelayAlertTimer.cancel()
            heatingSysErrorDetection.log.debug(
                "Heating System Error Alert Delay Timer cancelled")
        # Cancel timer if it is still running
        if heatSysDelayAtHomeTimer is not None and not heatSysDelayAtHomeTimer.hasTerminated():
            heatSysDelayAtHomeTimer.cancel()
            heatingSysErrorDetection.log.debug(
                "Heating System Error At-Home-Delay Timer cancelled")
            heatingSysErrorDetection.log.info(
                "Heating System Error Detection - Error is cleared!!")
            # var newBulbColorState = items.Lys_TvStue_BronzeKugle_Color
            events.sendCommand("Lys_TvStue_StorKugle_Color",
                               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:

```php
            var newBulbColorState = items.Lys_TvStue_BronzeKugle_Color
            events.sendCommand(ir.getItem(
                "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):

```php
var HSBType newBulbColorState = items.Lys_TvStue_BronzeKugle_Color

```

---

<div class="post-metadata">

**Author:** ![5iver](https://community.openhab.org/user_avatar/community.openhab.org/5iver/32/21201_2.png) [@5iver](https://community.openhab.org/u/5iver)\
**Post date:** [March 18, 2020, 12:12pm UTC](https://community.openhab.org/t/assigning-item-color-state-to-variable-for-sendcommand-in-jython-script/94746/4 "2020-03-18T12:12:54Z")

</div>

> [@Zylle](#):
>
> 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?

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.

> [@Zylle](#):
>
> But VScode gives me a syntax error for the var

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

```auto
newBulbColorState = items.Lys_TvStue_BronzeKugle_Color

```

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [March 18, 2020, 12:26pm UTC](https://community.openhab.org/t/assigning-item-color-state-to-variable-for-sendcommand-in-jython-script/94746/5 "2020-03-18T12:26:44Z")

</div>

> [@Zylle](#):
>
> should I include the entire rule code in the future for completeness, or is it just more clutter than benefit?

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 😉

---

<div class="post-metadata">

**Author:** ![Zylle](https://community.openhab.org/letter_avatar_proxy/v4/letter/z/f475e1/32.png) [@Zylle](https://community.openhab.org/u/Zylle)\
**Post date:** [March 18, 2020, 2:31pm UTC](https://community.openhab.org/t/assigning-item-color-state-to-variable-for-sendcommand-in-jython-script/94746/6 "2020-03-18T14:31:51Z")

</div>

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

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! 😁
