Rule for set a switch status from the light status

I have a light that can switch on and off from the wall and from my openhab.

So… if i switch off from the wall the status of my button doesnt change.

i use MQTT binding and an ESP board, so i can receive the status of the light in one GPIO pin of the board. and send the on off command in anotheer GPIO pin.

So… is possible create a rule that update the switch status when the light status change?

When the GPIO pin of the board detects the light has turned on, publish an MQTT message to a topic. Have openHAB subscribe to that topic to receive that MQTT message and update the appropriate Item’s state.

1 Like

i created a rule to set the light switch status from an external signal (that indicate the real lamp status but there is an error)

the item light switch has autoupdate=false tag.

rule “postupdate by external signal”

when Item Light_Status changed

then
postUpdate (Item Light_Switch)

end

1 Like

Have you seen the wiki about actions?

postUpdate needs different parameters. first an item name to be posted to, then a state for what to post

You don’t need to use rules for this. Just as Rich mentioned, the MQTT binding supports status updates.
Have your ESP publish the state changes to a topic, for example:

 /mylightswitch/state

Then update your item definition to accept the inbound message:

Switch Light {mqtt=">[localbroker:/mylightswitch/gpio/2:command:ON:1],>[localbroker:/mylightswitch/gpio/2:command:OFF:0],<[localbroker:/mylightswitch/state:state:default]"}

Remember that while your ESP may be using 1 and 0 for control, OH expects to receive ON and OFF so you will either have to send your state as such, or use a transform to get the ON/OFF from 1/0.

Very thanks for your help…

So… In the topic <[localbroker:/mylightswitch/state:state:default] I can just wire my status signal for the lamp…

and wath i have to do with autoupdate=false tag…?

Correct. In the example, when you press the button on your wall switch, in addition to the button triggering the relay to turn the light on or off, it would also publish “on” or “off” to the topic /mylightswitch/state.

OH will then update it’s status for the device. No need for autoupdate.

Obviously, you will have to edit the example to use whatever topic you are using for the device. The key is the < which tells OH that this is incoming (status) rather than outgoing (command).

thanks Cody

obviously i want update in OH the status of all my lamps at the startup of the system…
All the lamps are connected by mqtt in the same way of this situation.

need i a rule in this case?

I haven’t quite got that far with my setup so others may have better ideas, but I’ll give it a shot.

You could set up persistence so that states are saved and reloaded on OH startup. This isn’t %100 effective. If a light is manually switched while OH is offline, OH won’t know about it.

Since you mentioned an ESP, I’m assuming that these are DIY devices? In that case you could have OH rules to poll a specific topic on your device when started, send a “get” to /mylightswitch/status for example. Then code your ESP to send the state back to /mylightswitch/state as discussed above when it receives that “get” message.

If you happen to be running ESPEasy on your devices, I know that they have a REST command for returning the state of gpio pins. I haven’t wrapped my head around parsing JSON with OH yet so I haven’t implemented it in my setup yet.

Hello Damiano, I’m new to the world of Arduino and OH. I’m trying to do something similar to your project. Turn on and off a light from a physical switch and from OH. If it is not too much trouble I would appreciate some example code for arduino and for OH. To be able to do this task. VEngo following your comments, roughly understood the operation but I would lack some example to finish understanding. Thank you very much and greetings from Argentina.