I use OH4, I have 2 channels: switch and contact. Contact is real wall contact, switch is output to control light via relay. I have 2 items and rule like when switch is on we need on light and visa verse. Is it possible to avoid rule and control light directly from contact?
Have a look at „follow“ profiles:
I didn’t find way how follow can help me. I have 2 MQTT channels:
- id: MQTT_ST_IO13
channelTypeUID: mqtt:contact
label: Label IO13
description: Label IO13
configuration:
stateTopic: iot/st-iot-1/event/13
off: "0"
on: "1"
- id: MQTT_ST_IO4
channelTypeUID: mqtt:switch
label: Label IO04
description: Label IO04
configuration:
commandTopic: iot/st-iot-1/command
off: '{ "pin": 4, "state": 0 }'
on: '{ "pin": 4, "state": 1 }'
One channel linked to wall switch, second to lightbulb. I don’t see how to connect these 2 items without rule. Open/Closed state should be send as On/Off to switch. Is it possible implement this behavior without rule?
First of all, create an item for each channel.
Go to Things - Channels - Click on the item and you will see at the bottom that you can select a follow profile. However, I do not know how to proceed further as I do not need follow profiles. But this profile is exactly what you need.
You could also try to link your light item to the switch channel. Not sure right now if this might lead to any problems.
I can’t link contact to switch, OH doesn’t allow do it in UI. I able to select profile during link item to channel, but as result I have 2 items linked to own channel. I don’t see how to connect 2 items without rule.
Contact is a read-only item type.
Did you try to link the switch item (light) to the contact channel (wall switch)?
@Oliver2 I have created this topic more than year ago but issue still exists.
Yes, I want to connect read only contact (wall switch) to switch item (light).
Today I want to to it with new switch and new lamp and want to avoid create 2 rules.
I have created transformation script:
if(newState == "NULL" || newState == "UNDEF") return newState;
if(newState == "OPEN") return "OFF"
if(newState == "CLOSED") return "ON"
return "UNDEF"
})(contactToSwitch)
I guess it will work but I can’t test it now because OH in UI doesn’t allow link contact channel to switch item.
Why I can’t link contact link to switch? Is it bug or I don’t understand how it should work.
Yes this was a bug. I believe it was fixed in 4.3.x (late December)
You could use a map transformation to do the same thing. It’s more lightweight than using a script.
As pointed out earlier there is a „follow“ profile available
Then this will work without any code at all.
However, ou might need to change conract type of your item to switch type. Have a try.
@jimtng did you see issue about this in guthub? I have updated to 4.3.2 today and don’t see changes in this behavior. Link channel UI doesn’t show option to link contact channel to switch item, only to switch.
Hmm maybe that didn’t get fixed I’m not sure. I’m sorry that I didn’t actually check this, buried in too many other things atm. This is the PR that I had in mind. It has been a while since I looked into this, I don’t remember the details, so chances are I’m wrong about it.
Thanks, it looks like it merged to 5.0 with comment:
Note this needs to be backported to 4.3.x
I tried to test this on 5.0 but it doesn’t work because I can’t install MQTT binding " Error resolving artifact com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.18.2"
that PR got backported into 4.3, so no need to test it on 5.0. And yes 5.0 snapshot is currently broken when you use mqtt binding.
@jimtng it looks like 4.3.2 release (a03ecf64236ba94a2ec3aa4db2b7a9a66dcd367d) has this fix. But it doesn’t work as I expect. With this change
diff --git a/bundles/org.openhab.ui/web/src/pages/settings/things/link/link-add.vue b/bundles/org.openhab.ui/web/src/pages/settings/things/link/link-add.vue
index 8f5b2b95..e323c36e 100644
--- a/bundles/org.openhab.ui/web/src/pages/settings/things/link/link-add.vue
+++ b/bundles/org.openhab.ui/web/src/pages/settings/things/link/link-add.vue
@@ -246,6 +246,7 @@ export default {
if (this.channel.itemType.startsWith('Number')) { compatibleItemTypes.push('Number', 'Switch') }
if (this.channel.itemType === 'Color') { compatibleItemTypes.push('Switch', 'Dimmer') }
if (this.channel.itemType === 'Dimmer') { compatibleItemTypes.push('Switch') }
+ compatibleItemTypes.push('Switch')
}
return compatibleItemTypes
},
I able link Contact to Switch but it doesn’t work. With this link switch become string and I don’t see any events in events.log
Link configuration:
Is it bug or I config it in wrong way?
Expression without quotes didn’t change anything.
It would set the state of the linked item, not send a command to it. But you want it to send a command to it? Then this particular config won’t do that.
the follow
profile, as @Oliver2 suggested is probably the way to go.
Personally I’d use a rule, simply because I could never understand how follow profile works unless I go read the source code, and then it would only stay in my memory for a short time.
Yes, I want to send command. I removed check for profile from UI and able to set follow profile but it doesn’t work. It also convert switch to string in UI and don’t work.
It looks that I should use rule but my idea is avoid rule for simple actions like this. With rules I must to create one rule for every switch. It looks like rule is overhead for this.
Haven’t read everything, but why are you creating a contact, just create another switch item, then use follow or whatever?
@apella12 why we have contact on OH? Switch can have commands. But wall switch it is read only contact, not switch.
@jimtng
I have changed mqtt:contact to mqtt:switch - no changes, light switch as readonly string.
I updated mqtt channel with postCommand: true. After that I see in events.log expected log but no real command in MQTT. I have added to MQTT channel commandTopic: FAKE_TOPIK and after that all works as expected.
It looks like a link switch item to readonly mqtt channel convert swith to read only switch and do not send commands even item has another link with command channel.
Is it bug or feature?