Switching on/off, checking status before sending on or off command

  • Platform information:
    • Pi 4 4GB RAM: _
    • Openhabian, latest version
    • openHAB version: 2.5
  • Issue of the topic:
    Using openhabian in combination with a Siemens PLC.
    Now I have the openhabian app or web app and a 2nd app.
    Problem is that if I switch on the light in openhabian app and I switch it off in the other app that i loose sync.
    Is there a way to update the status of the switch based on the status of the output?

I’ll go more in detail. This is what happens if I switch off the lamp in openhabian:

  • Item ‘GF_chickencoop_LightONOFF’ received command OFF
  • GF_chickencoop_LightONOFF changed from ON to OFF
  • GF_chickencoop_LightStatusONOFF changed from OPEN to CLOSED
    I have “GF_chickencoop_LightStatusONOFF” available to check the status of the lamp (Item of type ‘Contact’).
    Suppose the lamp was already ‘OFF’ I’d be already satisfied if the ‘OFF command’ when switching off the switch is not send. Maybe that’s possible with a rule?

Thanks,
Tom.

Hi,

If I understand you correctly, the “2nd app” communicates with the PLC directly and not with OpenHAB? So you want to update GF_chickencoop_LightONOFF based on the state of GF_chickencoop_LightStatusONOFF?

I guess you could create a rule like this:

rule "Chickencoop status update"
when
    Item GF_chickencoop_LightStatusONOFF changed
then
    switch NewState
    {
              case OPEN: GF_chickencoop_LightONOFF.postUpdate(OFF)
              case CLOSED: GF_chickencoop_LightONOFF.postUpdate(ON)
    }
 end

postUpdate changes the state of an item without any further actions:
See: https://www.openhab.org/docs/configuration/rules-dsl.html#manipulating-item-states

I think the root of the issue is that the device is not sending status updates to openHAB. So, when you control it using the separate app, it’s not reporting that change to the server. If that doesn’t happen, then you have no way to know what it’s actual state is at any given time. It’s the same as using an IR remote control with your TV; the remote is just firing commands blindly at your TV without any feedback (other than your eyes and ears).

So the real question is, can your device send its status to OH? And if so, why isn’t it?

If the device can’t send status, then your options range from not using the 2nd app to using an energy-monitoring smart plug to sense when the lamp is on/off.

In my understanding that is what the contact is for? The contact is somehow connected to the output of the PLC?

So output (GF_chickencoop_LightONOFF) high / relais closed / light on == contact (GF_chickencoop_LightStatusONOFF) closed. (or open…)

I think you might be right, but there’s a lot open to interpretation in the original post. We need to know more about how openHAB is connecting to the actual devices.

Hi,

Thanks for the help.

Tried with the case statements, wasn’t working. Put them in comment and tried with if else. It’s not working neither. Added ‘eettafellicht1ONOFF’ to see if and where the rule is run.

rule "GF_Chickencoop_LightStatusChanged"
when
    Item GF_chickencoop_LightStatusONOFF changed
then
    if( GF_chickencoop_LightStatusONOFF == OPEN ) 
    { 
    GF_chickencoop_LightONOFF.postUpdate(ON)
    eettafellicht1ONOFF.sendCommand(ON)
    } 
    else
    {
    GF_chickencoop_LightONOFF.postUpdate(OFF) 
    eettafellicht1ONOFF.sendCommand(OFF)
    }
    //switch NewState
    //{
    //          case "UIT": GF_chickencoop_LightONOFF.postUpdate(OFF)
    //          case "AAN": GF_chickencoop_LightONOFF.postUpdate(ON)
    //}
end

This is what happens in the log:

  • GF_chickencoop_LightStatusONOFF changed from CLOSED to OPEN
  • Item ‘eettafellicht1ONOFF’ received command OFF
  • eettafellicht1ONOFF predicted to become OFF
  • GF_chickencoop_LightStatusONOFF changed from OPEN to CLOSED
  • Item ‘eettafellicht1ONOFF’ received command OFF
  • eettafellicht1ONOFF predicted to become OFF

So i get in the ‘else’ but not in the ‘if’. I tried one hour playing with ‘= instead of ==’; ’ OPEN instead of “OPEN” ’ and so on…

Any help would be appreciated.

Thanks,
Tom.

A bit more explanation about what i’m trying to do.
Because at work i get involved a lot with PLC I’m evaluating wether it would be a good thing to use it for my home automation. As I do have a PLC for my chickencoop I can use it as a test case.
PLC’s are great because they will last for 20 years.

However I’d like to use openHAB to be able to control everything in one app. For example also the heating. And i’d like to use myopenhab to be able to check things / control things from outside my LAN.
And I’d like to connect with Google Assistant.

Indead “GF_chickencoop_LightStatusONOFF” is the PLC output. If ON the lights are on, if OFF the lights are off.

I someone would like to know more, I’d be glad to explain…

Regards,
Tom.

If that is an Item, it’s the state you are interested in I expect. Items have many properties - type, label, icon etc. and you need to spell out which one you want.

GF_chickencoop_LightStatusONOFF.state

Thanks Rosko,

I indeed needed to add ‘.state’.

Any idea why this rule:

   rule "GF_Chickencoop_LightStatusChanged"
    when
        Item GF_chickencoop_LightStatusONOFF changed
    then
        if( GF_chickencoop_LightStatusONOFF.state == OPEN ) 
        { 
        GF_chickencoop_LightONOFF.postUpdate(ON)
        } 
        else
        { 
        GF_chickencoop_LightONOFF.postUpdate(OFF)
        } 
     end

when I change with the other app the “GF_chickencoop_LightStatusONOFF” from OPEN to CLOSED results in this (events.log):

  • GF_chickencoop_LightStatusONOFF changed from OPEN to CLOSED
  • GF_chickencoop_LightONOFF changed from ON to OFF
  • GF_chickencoop_LightONOFF changed from OFF to ON

Thanks,
Tom.

Log timestamps is helpful.

I don’t know anything about your Item GF_chickencoop_LightONOFF. Is it linked to a binding of some kind? That’s probably updating it again.

It’s linked to an output of the PLC, Q0.0:
Contact GF_chickencoop_LightStatusONOFF “status licht kippenhok: [MAP(light_dutch.map):%s]” {simatic=“plc:Q0.0”}

If i turn the lights ON, nothing happens with the ‘GF_chickencoop_LightStatusONOFF’. Problem is only when I turn them off with the other app.

2020-03-08 17:09:51.810 [vent.ItemStateChangedEvent] - GF_chickencoop_LightStatusONOFF changed from CLOSED to OPEN
2020-03-08 17:10:14.346 [vent.ItemStateChangedEvent] - GF_chickencoop_LightStatusONOFF changed from OPEN to CLOSED
2020-03-08 17:10:14.364 [vent.ItemStateChangedEvent] - GF_chickencoop_LightONOFF changed from ON to OFF
2020-03-08 17:10:16.598 [vent.ItemStateChangedEvent] - GF_chickencoop_LightONOFF changed from OFF to ON

for now I’m only using the other app to control the PLC and this happens in real:
I just went online on the PLC and actually GF_chickencoop_LightONOFF is always ON even though the log says it goes from ON to OFF and then 2s later from OFF to ON. And the change of status is also visualised in openHAB’s Basic UI.

I’m not sure what all that means. Your Item is linked to the Simatic binding, right? I think that polls your PLC. You can do what you like to your Item, but at every poll the binding will update it to what was reported from the PLC.
If your PLC is misreporting, it’s likely a configuration issue at one end or the other. You’re not going to fix that with rules.

your remark about the polling and updating with the PLC made me remember that you can add a polling direction, optional. Thus saying it’s output or input, or reading / writing. Seen from openHAB (PLC output is I, PLC input is O)
Tried doing that:
Switch GF_chickencoop_LightONOFF "Licht Kippenhok" <lightbulb> {simatic="plc:DB12.DBX0.0:O"}
Contact GF_chickencoop_LightStatusONOFF "status licht kippenhok: [MAP(light_dutch.map):%s]" {simatic="plc:Q0.0:I"}
And guess what? It works! :smiley: Hooray!

> 2020-03-08 18:48:08.221 [vent.ItemStateChangedEvent] - GF_chickencoop_LightStatusONOFF changed from CLOSED to OPEN
> 2020-03-08 18:48:08.241 [vent.ItemStateChangedEvent] - GF_chickencoop_LightONOFF changed from OFF to ON
> 2020-03-08 18:48:10.479 [vent.ItemStateChangedEvent] - GF_chickencoop_LightStatusONOFF changed from OPEN to CLOSED
> 2020-03-08 18:48:10.488 [vent.ItemStateChangedEvent] - GF_chickencoop_LightONOFF changed from ON to OFF

Thanks a lot Rossko and Tom Smit!

Hello Tom,
Little late but You can make it without rule. I assume You have programmed in PLC simple toggle function (Flip/Flop). With the next few lines in PLC you can achieve the same without a rule. Writing a rule for each switch will be painful :slight_smile: I have a PLC with HMI, tablets with habmin and physical switches in the wall and they are all synchronized.

hi Rafal,

very eager to see how you did it? You typed ‘with the next few lines in PLC…’ but i don’t see anything…

Thanks,
Tom.

Hi Tom,
I’m using Fatek PLC programed with Lader, I will make code with comments so you can adopt it to Siemens. The Idea is to combine monostable switch (physical) with bistable switch (OH).
Regards
Rafal

Ok,
I simplified my Ladder. TOGG element is bistabile toglle (flip-flop), arrow symbols ↑ means that the line is “transparent” for raising edge of the signal.


Regards
Rafal

Dear Rafal,

I think i understand. You can control Vlight1 or light1 both from openhab and the pysical switch. But how do you keep sync if you control Vlight1 also with a 2nd app (like in my case) or a HMI (like in your case)? I mean sync between openHAB and the HMI or 2nd app?

Or do I miss something?

Thanks,
Regards,
Tom.

So this part is not working ?

Hi Tom!
X0 is your phisical monostabile switch (contact), M20 is virtual bistabile switch, so when you use M20 in OH items as a switch or/and in other app, HMI etc. it wil be synchronized, for example: When the wall swith X0 is pressed then M20 go to ON, so in OH, app and HMI you wil see it as ON now can be turned OFF by switch or OH, HMI, app. I can send You a pdw file with lader, you can install winproloader and simulate it.