# \[SOLVED\]Set default item state to OFF instead of NULL

**URL:** <https://community.openhab.org/t/solved-set-default-item-state-to-off-instead-of-null/20612>\
**Category:** Beginners\
**Tags:** rules, trigger, itemstate\
**Created:** [January 18, 2017, 11:51pm UTC](https://community.openhab.org/t/solved-set-default-item-state-to-off-instead-of-null/20612 "2017-01-18T23:51:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tidle](https://community.openhab.org/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@Tidle](https://community.openhab.org/u/Tidle)\
**Post date:** [January 18, 2017, 11:51pm UTC](https://community.openhab.org/t/solved-set-default-item-state-to-off-instead-of-null/20612/1 "2017-01-18T23:51:04Z")

</div>

Hello,

i have some rules with the condition “changed from OFF to ON”. unfourtanately everytime i restart my openhab the default item state is NULL so i need to write a second condition with NULL to ON.

is it possible to set the default state to OFF?

OH2 from yesterday on rpi2 (openhabian)

thank you for help.  
regards

---

<div class="post-metadata">

**Author:** ![watou](https://community.openhab.org/user_avatar/community.openhab.org/watou/32/70595_2.png) [@watou](https://community.openhab.org/u/watou)\
**Post date:** [January 18, 2017, 11:52pm UTC](https://community.openhab.org/t/solved-set-default-item-state-to-off-instead-of-null/20612/2 "2017-01-18T23:52:38Z")

</div>

> [@Tidle](#):
>
> i have some rules with the condition “changed from OFF to ON”

Could you change your condition simply to `changed to ON`? Then you’re not concerned with what the prior state was.

---

<div class="post-metadata">

**Author:** ![ThomDietrich](https://community.openhab.org/user_avatar/community.openhab.org/thomdietrich/32/71239_2.png) [@ThomDietrich](https://community.openhab.org/u/ThomDietrich)\
**Post date:** [January 19, 2017, 1:23am UTC](https://community.openhab.org/t/solved-set-default-item-state-to-off-instead-of-null/20612/3 "2017-01-19T01:23:30Z")

</div>

Exactly what @watou said.

Additionally in other constellations you may need to set items after startup to a specific state. Add such a rule (examples given):

```java
rule "Init"
when
	System started
then
	// let openHAB settle and give other rules the chance to fire
        createTimer(now.plusSeconds(180)) [|
		if (TimerActive.state == NULL) TimerActive.postUpdate(OFF)
                DefaultHeatingTemp.postUpdate(21.0)
                if (KodiSummary.state == NULL || KodiSummary.state == "") KodiSummary.postUpdate("(unknown)")
	]
end

```

---

<div class="post-metadata">

**Author:** ![Tidle](https://community.openhab.org/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@Tidle](https://community.openhab.org/u/Tidle)\
**Post date:** [January 19, 2017, 9:56am UTC](https://community.openhab.org/t/solved-set-default-item-state-to-off-instead-of-null/20612/4 "2017-01-19T09:56:52Z")

</div>

Hey guys,

thank your for your replies. that was exactly what i was looking for.

when i read in the documentation “Item changed [from] [to]” i thought i need to tell him both states. i did not think of simply give him just one state. Thanks!

to use the system started trigger would solve my problem too. Perfect! =)

Regards

---

<div class="post-metadata">

**Author:** ![AndrewZ](https://community.openhab.org/user_avatar/community.openhab.org/andrewz/32/7515_2.png) [@AndrewZ](https://community.openhab.org/u/AndrewZ)\
**Post date:** [January 19, 2017, 10:23am UTC](https://community.openhab.org/t/solved-set-default-item-state-to-off-instead-of-null/20612/5 "2017-01-19T10:23:02Z")

</div>

From my own experience - some times its better to check if the value is “not ON” (!ON) ) instead of OFF. Just my 2c 😉

---

<div class="post-metadata">

**Author:** ![Tidle](https://community.openhab.org/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@Tidle](https://community.openhab.org/u/Tidle)\
**Post date:** [January 19, 2017, 10:29am UTC](https://community.openhab.org/t/solved-set-default-item-state-to-off-instead-of-null/20612/6 "2017-01-19T10:29:32Z")

</div>

good point, thanks!

i want to show you my rule for a harmony hub and maybe you have ideas how to optimize that.

I want to say “Alexa, start SKY TV” and it turns everything on as defined with my harmony hub. I need to change the state of everything else to OFF then, because i may want to say “start kodi” but if kodi was started before, it will not start again, because it is already in the state of started.  
if there is a better solution for that, then i am grateful for any ideas how to optimize the construct 😉  
to switch everything off i made an extra rule called “alle Media Geräte aus” which is alway on if a acitvity was started. so i am able to say “alex, switch all devices off”.

the last small problem is, that if i am now saying “alexa, turn on sky tv” alexa asks me which device (because i have “sky tv” and “sky beamer” and i need to repeat what i was saying. very annoying. siri dont ask me that, she simply does her job 😉

items:

```csv
//Harmony
String Harmony_Activity "Current Activity [%s]" (Wohnzimmer) { channel="harmonyhub:hub:HarmonyHub:currentActivity" }
Switch Harmony_skytv "Sky Fernseher" (Wohnzimmer) ["Switchable"]
Switch Harmony_skybeamer "Sky Beamer" (Wohnzimmer) ["Switchable"]
Switch Harmony_koditv "Kodi Fernseher" (Wohnzimmer) ["Switchable"]
Switch Harmony_kodibeamer "Kodi Beamer" (Wohnzimmer) ["Switchable"]
Switch Harmony_alles "alle Media Geräte" (Wohnzimmer) ["Switchable"]

```

rules

```php
rule "Sky TV schauen"
    when
        Item Harmony_skytv changed to ON
    then
            Harmony_Activity.sendCommand("24360745")
            postUpdate(Harmony_koditv, OFF)
            postUpdate(Harmony_skybeamer, OFF)
            postUpdate(Harmony_kodibeamer, OFF)
            postUpdate(Harmony_alles, ON)
end

rule "Sky Beamer schauen"
    when
        Item Harmony_skybeamer changed to ON
    then

            Harmony_Activity.sendCommand("24360747")
            postUpdate(Harmony_koditv, OFF)
            postUpdate(Harmony_kodibeamer, OFF)
            postUpdate(Harmony_skytv, OFF)
            postUpdate(Harmony_alles, ON)
end

rule "Kodi TV schauen"
    when
        Item Harmony_koditv changed to ON
    then

            Harmony_Activity.sendCommand("24361209")
            postUpdate(Harmony_skybeamer, OFF)
            postUpdate(Harmony_kodibeamer, OFF)
            postUpdate(Harmony_skytv, OFF)
            postUpdate(Harmony_alles, ON)

end

rule "Kodi Beamer schauen"
    when
        Item Harmony_kodibeamer changed to ON
    then

            Harmony_Activity.sendCommand("24360748")
            postUpdate(Harmony_koditv, OFF)
            postUpdate(Harmony_skybeamer, OFF)
            postUpdate(Harmony_skytv, OFF)
            postUpdate(Harmony_alles, ON)

end

rule "Alle Media Geräte aus" //Switch everything off and update the activities
    when
        Item Harmony_alles changed from ON to OFF
    then

            Harmony_Activity.sendCommand("PowerOff")
 			postUpdate(Harmony_koditv, OFF)
            postUpdate(Harmony_skybeamer, OFF)
            postUpdate(Harmony_kodibeamer, OFF)
            postUpdate(Harmony_skytv, OFF)
            postUpdate(Harmony_alles, ON)

end

```

---

<div class="post-metadata">

**Author:** ![watou](https://community.openhab.org/user_avatar/community.openhab.org/watou/32/70595_2.png) [@watou](https://community.openhab.org/u/watou)\
**Post date:** [January 19, 2017, 10:44am UTC](https://community.openhab.org/t/solved-set-default-item-state-to-off-instead-of-null/20612/7 "2017-01-19T10:44:59Z")

</div>

Note the difference in meaning between

```auto
Item X changed to ON

```

and

```auto
Item X received command ON

```

both are valid and both have their purpose, but the first reacts to the state of the item having changed to ON, while the second reacts to some source having commanded the item to turn ON. `changed to ON` might actually work in practice in both situations, but only as a side effect of the item automatically adopting the command as its new state when the state of the item was not previously ON. `received command ON` will trigger every time the ON command is sent to the item, regardless of the of the item’s state.

---

<div class="post-metadata">

**Author:** ![Tidle](https://community.openhab.org/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@Tidle](https://community.openhab.org/u/Tidle)\
**Post date:** [January 19, 2017, 11:33am UTC](https://community.openhab.org/t/solved-set-default-item-state-to-off-instead-of-null/20612/8 "2017-01-19T11:33:28Z")

</div>

ah okay, so i could save all the postupdates by simply changing the trigger from changed to received command. because then it does not matter if an activity is in the state “on” although it is not really switched on and therefore it would not turn on if i use the trigger changed to on, but it should if i use the trigger received command ON, right?

thanks, very good point!

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [January 19, 2017, 6:29pm UTC](https://community.openhab.org/t/solved-set-default-item-state-to-off-instead-of-null/20612/9 "2017-01-19T18:29:03Z")

</div>

Or use persistence with restoreOnStartup. I don’t think (could be wrong) that the changed to ON rule will trigger when restoreOnStartup initialized the Item with whatever it was when the .items are initialized.

> [@Tidle](#):
>
> i did not think of simply give him just one state.

The docs use the standard convention of putting optional parameters in `[]`. However that convention may not be widely understood so perhaps a note saying as such is needed. Were you looking at the wiki or the OH 2 docs?

> [@Tidle](#):
>
> i want to show you my rule for a harmony hub and maybe you have ideas how to optimize that.

I always recommend using the sendCommand/postUpdate Item methods rather than the actions:

```
Harmony_koditv.postUpdate(OFF)

```

Note: postUpdate changes OH’s internal state for an Item. It does not forward that new state to the binding and on to the device. I don’t know how all this Alexa/Harmony stuff interacts but suspect sendCommand is the more correct choice. Could be wrong. I didn’t spend too much time trying to figure out your exact use case.

---

<div class="post-metadata">

**Author:** ![watou](https://community.openhab.org/user_avatar/community.openhab.org/watou/32/70595_2.png) [@watou](https://community.openhab.org/u/watou)\
**Post date:** [January 19, 2017, 6:40pm UTC](https://community.openhab.org/t/solved-set-default-item-state-to-off-instead-of-null/20612/10 "2017-01-19T18:40:25Z")

</div>

> [@rlkoshak](#):
>
> It does not forward that new state to the binding and on to the device.

This is almost—but not quite—universally true. When you want your script to cause a change to a system bound to openHAB, sending a command to an item is the way to go.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [January 19, 2017, 6:42pm UTC](https://community.openhab.org/t/solved-set-default-item-state-to-off-instead-of-null/20612/11 "2017-01-19T18:42:22Z")

</div>

> [@watou](#):
>
> This is almost—but not quite—universally true.

I hate how often this statement applies in OH right now.

---

<div class="post-metadata">

**Author:** ![watou](https://community.openhab.org/user_avatar/community.openhab.org/watou/32/70595_2.png) [@watou](https://community.openhab.org/u/watou)\
**Post date:** [January 19, 2017, 7:12pm UTC](https://community.openhab.org/t/solved-set-default-item-state-to-off-instead-of-null/20612/12 "2017-01-19T19:12:49Z")

</div>

> [@rlkoshak](#):
>
> I hate how often this statement applies in OH right now.

I know what you mean, but it’s for a good reason! (one of the most ominous phrases in the history of technology 😄).

It’s really elegant and concise to have a construct like:

```auto
Number RoomTemp "[%.1f]" { 
  ecobee="<[your_therm_id#runtime.actualTemperature]", 
  mqtt=">[mybroker:/myhouse/room/temp:state:*:default]" 
} 

```

With a single item, you have a gateway between a proprietary technology and an MQTT broker. The `<` and `>` could be reversed (and the binding config strings changed) to have a gateway operate in the other direction as well. This is all possible because updating item states can in fact cause the binding to react. Some bindings react to state updates, others don’t. Making the list of bindings that do something when their bound items’ states are updated is a big job that I won’t bring upon myself.

I wish more bindings allowed this, so this capability could be more universally used without having to need rules to trigger on `changed` and then call `sendCommand` in the rule body.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [January 19, 2017, 7:42pm UTC](https://community.openhab.org/t/solved-set-default-item-state-to-off-instead-of-null/20612/13 "2017-01-19T19:42:37Z")

</div>

Shouldn’t the above have a comma between the two binding configs? I’ve never had a need to bridge in this way. Of course, I don’t think OH 2 supports this sort of thing does it? I’ve not seen channels that do this. In particular I’ve seen several threads with the Exec binding where people used to do things like this and no longer can in the 2.0 version.

> [@watou](#):
>
> Making the list of bindings that do something when their bound items’ states are updated is a big job that I won’t bring upon myself.

Shouldn’t this be the responsibility of binding authors to document this sort of thing?

> [@watou](#):
>
> I wish more bindings allowed this, so this capability could be more universally used without having to need rules to trigger on changed and then call sendCommand in the rule body.

Agreed. Do you know if there is thought on letting this sort of thing work with channels?

---

<div class="post-metadata">

**Author:** ![watou](https://community.openhab.org/user_avatar/community.openhab.org/watou/32/70595_2.png) [@watou](https://community.openhab.org/u/watou)\
**Post date:** [January 19, 2017, 7:45pm UTC](https://community.openhab.org/t/solved-set-default-item-state-to-off-instead-of-null/20612/14 "2017-01-19T19:45:39Z")

</div>

> [@rlkoshak](#):
>
> Shouldn’t the above have a comma between the two binding configs?

Good catch; fixed!

> [@rlkoshak](#):
>
> Shouldn’t this be the responsibility of binding authors to document this sort of thing?

I think binding authors ought to document all features and functionality, yes.

> [@rlkoshak](#):
>
> Do you know if there is thought on letting this sort of thing work with channels?

It would be a pity if this were no longer possible with new bindings. I don’t know the answer.

---

<div class="post-metadata">

**Author:** ![Tidle](https://community.openhab.org/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@Tidle](https://community.openhab.org/u/Tidle)\
**Post date:** [January 20, 2017, 10:17am UTC](https://community.openhab.org/t/solved-set-default-item-state-to-off-instead-of-null/20612/15 "2017-01-20T10:17:57Z")

</div>

Hey guys, i didnt really get what you were talkin about. ^^ is the constellation i used for my rules ok or should i change something?

```php
rule "Sky Beamer schauen"
    when
        Item Harmony_skybeamer changed to ON
    then

            Harmony_Activity.sendCommand("24360747")
            postUpdate(Harmony_koditv, OFF)
            postUpdate(Harmony_kodibeamer, OFF)
            postUpdate(Harmony_skytv, OFF)
            postUpdate(Harmony_alles, ON)
end

```

---

<div class="post-metadata">

**Author:** ![ThomDietrich](https://community.openhab.org/user_avatar/community.openhab.org/thomdietrich/32/71239_2.png) [@ThomDietrich](https://community.openhab.org/u/ThomDietrich)\
**Post date:** [January 20, 2017, 1:54pm UTC](https://community.openhab.org/t/solved-set-default-item-state-to-off-instead-of-null/20612/16 "2017-01-20T13:54:53Z")

</div>

@watou @rlkoshak we should try to arrange all these relations in docs!

I have a related question actually. Imagine the following scenario:

- `Ceiling_Light` can be either turned on/off by openHAB command or by pressing the integrated wall switch.
- A **first rule** triggered by `Door_Sensor` turns `Ceiling_Light` on for 3 minutes (Timer), then off
- A press on the wall switch should switch to that state but a **second rule** should also delete the timer, leaving the light in one state indefintely
- (Maybe I’m confused right now) How do I **hinder the first rule from triggering the second rule?** Is there a trigger to act on updates received from binding only?

Minimal example (reduced/incomplete but you’ll get the idea):

```java
rule "rule 1"
when
    Item Door_Sensor changed from CLOSED to OPEN
then
    //... logic...
    Ceiling_Light.sendCommand(ON)
    //...
    timer = createTimer(deadline) [|
        //... the normal timer logic
        Ceiling_Light.sendCommand(OFF)
    ]
end

rule "rule 2"
when
    Item Ceiling_Light received update
then
    if (timer != null) {
        timer.cancel
    }
end

```

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [January 20, 2017, 4:22pm UTC](https://community.openhab.org/t/solved-set-default-item-state-to-off-instead-of-null/20612/17 "2017-01-20T16:22:55Z")

</div>

> [@Tidle](#):
>
> should i change something?

As a general rule, if you are asking a device to do something you should use sendCommand.

If you are updating the state of something based on something (e.g. a sensor is telling you a light is on so update the light switch’s state to ON) use postUpdate.

> [@ThomDietrich](#):
>
> (Maybe I’m confused right now) How do I hinder the first rule from triggering the second rule? Is there a trigger to act on updates received from binding only?

No, there isn’t. This is a complicated one that I ended up solving by creating the [Dead Man’s Switch Design Pattern](https://community.openhab.org/t/automation-orchestration-design-patterns/1967/22).

The tl;dr is create a state that defaults to “MANUAL”. Whenever the device is controlled by a Rule this state gets set to “RULE” (you can use multiple states if you need to distinguish between startup, timers, etc) while the rule is running and then goes back to MANUAL. Have a rule that triggers any time the device receives an update. If the state is “MANUAL” you know this is a command that was triggered from the device or from the sitemap. If not you know it came from a Rule. With this information you can check in the second rule where the command came from and behave accordingly.

I haven’t moved this design pattern into its own thread yet because it is complicate, a little brittle (because the timing needs to be managed) and this use case doesn’t come up very often.

I use it to set an override flag on my lights. During the day if the weather says its cloudy the lights turn on. If one of us manually turns the light on or off we want it to stay that way, overriding the weather behavior.

It turns out to be less useful than it could be in my case because the physical switches don’t report their current state and the refresh is too long.

To expand your minimal example:

```auto
var String whoCalled = "MANUAL"

rule "rule 1"
when
    Item Door_Sensor changed from CLOSED to OPEN
then
    whoCalled = "RULE"
    //... logic...
    Ceiling_Light.sendCommand(ON)
    //...
    timer = createTimer(deadline) [|
        //... the normal timer logic
        Ceiling_Light.sendCommand(OFF)
    ]
    Thread::sleep(100) // give the commands a chance to hit the bus and the rule below to trigger
    whoCalled = "MANUAL"
end

rule "rule 2"
when
    Item Ceiling_Light received update
then
    if(whoCalled != "RULE") {
        if (timer != null) {
            timer.cancel
        }
    }
end

```

---

<div class="post-metadata">

**Author:** ![alexschliebner](https://community.openhab.org/user_avatar/community.openhab.org/alexschliebner/32/43638_2.png) [@alexschliebner](https://community.openhab.org/u/alexschliebner)\
**Post date:** [January 25, 2020, 9:15am UTC](https://community.openhab.org/t/solved-set-default-item-state-to-off-instead-of-null/20612/18 "2020-01-25T09:15:18Z")

</div>

I know this is a rather old discussion. But there is something about it I really often face, when I’m trying to solve issues in my OH setup.

There are really good functions in OH, but they are so poorly documented! The official site describes things so superficially, I always miss the details like the explanation of the syntax, the complete description of features, special cases and so on. It’s really a pain!

For me as a developer this would not be acceptable for my own work.

Without the threads in the forums, many issues would not be solvable. Thanks!

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [January 25, 2020, 4:13pm UTC](https://community.openhab.org/t/solved-set-default-item-state-to-off-instead-of-null/20612/19 "2020-01-25T16:13:01Z")

</div>

We welcome any issues or PRs you might submit to improve anything in the docs you find to be superficial or poorly documented. It’s a volunteer effort. If the current volunteers are not producing docs to your standards there’s only one way to fix it and become a volunteer yourself.

Though it is important to realize that the above nearly three year old discussion and explanation isn’t applicable any more so that’s probably why you might not find it in the docs.

Also, as John said, it used to depend on the individual bindings supporting it so you would have to go to the individual binding docs to read about it.

Today, there _is_ a generic way to do it and it is in the docs. It’s the follow Profile.

---

<div class="post-metadata">

**Author:** ![alexschliebner](https://community.openhab.org/user_avatar/community.openhab.org/alexschliebner/32/43638_2.png) [@alexschliebner](https://community.openhab.org/u/alexschliebner)\
**Post date:** [January 27, 2020, 10:27pm UTC](https://community.openhab.org/t/solved-set-default-item-state-to-off-instead-of-null/20612/20 "2020-01-27T22:27:05Z")

</div>

Hi rlkoshak,

sorry I didn’t want to offend anybody. I’m open for helping to improve things. But currently I don’t feel in the position to do that for the documentation, as I have more questions than answers. This will change hopefully and then I will be happy to contribute.  
Maybe I just didn’t look at the right places.

I want to give you two examples of what I mean.

The `Dimmer` item accepts the commands `INCREASE` and `DECREASE`. I like features like that because they help to minimize the code inside rules. I don’t have to query the current state value to increase it by one and set it. Also I don’t have to check for the boundaries of 0 and 100.

Now I have two lamps I want to drive synchronously by the same remote control. So I created a virtual `Dimmer` item, which is not bound to a channel, to increase or decrease and populate the new value to both lamps by a rule. Unfortunately the virtual `Dimmer` item does not react in any way, when I do an `INCREASE` or `DECREASE` on it. This is unexpected to me and it’s not documented anywhere.

Then we have the `min` and `max` attributes I can define for a `Number` channel:

```
...
Type number : WhiteTemperature
[
    stateTopic = "stat/LAMP2/RESULT", transformationPattern = "JSONPATH:$.CT", 
    commandTopic = "cmnd/LAMP2/CT", 
    min = 153, max = 500
]
...

```

I can define them and they are respected by PaperUI. But how can I do anything with these attributes in a rule? So far I didn’t find anything in the documentation.

[Next page](https://community.openhab.org/t/solved-set-default-item-state-to-off-instead-of-null/20612.md?page=2)
