Get status of outlets at startup

I’m still a little flaky on rules, and especially transforms. I have several outlets running ESPeasy. I would like to get the state of the outlet when OH is started to pick up any changes that may have happened while it was offline.

I can query the information through both http or mqtt and it is returned like this

{
"log": "",
"plugin": 1,
"pin": 15,
"mode": "output",
"state": 0
}

This is what I have come up with so far, but I have no idea if I’m headed in the right direction.

when
System started

then

String outletStatus3 {http="http://ol3.home/control?cmd=status,gpio,15"}
var String s3raw = outletStatus1.state.toString
var String s3 = transform("JSONPATH", "$.state", s3raw)

if (s3 == "1"){
        postUpdate(outlet1, ON)
}
end

I recommend using Designer which will help identify syntax errors.

I have no clue what you are trying to do with this line. It makes no sense syntactically and you never actually use it. What are you trying to do here?

It looks like a binding config in which case it has no business in a Rule. You need to create an Item with that binding config.

Here is how I do it:

  1. Switch Item bound to an MQTT topic which publishes “ON” when the switch receives a command
  2. The devices receive this message and publish their current state to their own topic
  3. There are Items bound to MQTT which subscribe on these state update topics which use JSONPATH($.state) in the transform field of the Item binding.
  4. The System started rule simply sendCommand(ON) to the Item in 1.

That was the problem… I didn’t either. :open_mouth: I was trying to piece together a few other examples but it must have been too late.

Are your switches publishing to a “dummy” switch on your devices, or how does that not turn all of your devices on?

Regardless, I think I know what I need to do… it’s just not working. I’ve double checked things about a dozen times, so at this point I’m probably just tunnel vision.

Right now I’m simply trying to get a String or Number item to receive the state value when manually publishing to the status topic on the device. Nothing so far.

String outletStatus3 "OL3 Status" {mqtt="<[broker:/ol3/status:state:JSONPATH($.state)]"}

BTW I must be dense because the only thing I could get Designer to point out was when I removed an “end” from a rule.

Restarted my VM completely instead of just OH and now I’m seeing my Item (whether String or Number) update when I publish to the status topic. It’s not being displayed on the sitemap, however. Not a big deal since it’s not meant to be displayed anyway… just driving me nuts wondering if I’ve really been staring at it so long that I can’t get something so simple to work.

I use three different topics for each device:

  1. OH → Device : command channel
  2. Device → OH : update channel
  3. OH → Device: update request channel

To turn on/off the device OH sends a command to 1. Any change on the device gets published to 2. when OH restarts or it detects that the device went offline and came back then is sends a command to 3 which causes the devices to publish their current states on 2.

If you use state instead of command in the MQTT binding config on the Item then it will not trigger the outgoing MQTT part to publish the message when the update is received.

Sounds like maybe your Broker was down or OH lost its connection to the broker.

That is odd as it should have complained about that first line because you need a var or val before the String and before the http to make the code valid. Of course even though the syntax is valid it still doesn’t do anything.

Just wanted to report back as a reference for anyone looking to do something similar. I did get this working.
I’m using ESPeasy, so I don’t quite have the flexibility of creating multiple topics as Rich has done.

I set my Items up to receive the status from the manual poll like this:

Number ol1status {mqtt="<[broker:/ol1/status:state:JSONPATH($.state)]"}
Number ol2status {mqtt="<[broker:/ol2/status:state:JSONPATH($.state)]"}
Number ol3status {mqtt="<[broker:/ol3/status:state:JSONPATH($.state)]"}

And the Rules. The publish commands returns the JSON that is parsed into the Items above to get the status (1 or 0). When testing with only one outlet I needed to add a delay for my if statement to work correctly. With multiple queries there may be enough delay built in to work by itself, but at this point I just left well enough alone.

rule GetStartupStates
when
        System started
then
        publish("broker","/ol1/cmd","status,gpio,15")
        publish("broker","/ol2/cmd","status,gpio,15")
        publish("broker","/ol3/cmd","status,gpio,15")
        Thread::sleep(1000)
        if ((ol1status.state as DecimalType) == 1) { postUpdate(outlet1, ON) }
        if ((ol2status.state as DecimalType) == 1) { postUpdate(outlet2, ON) }
        if ((ol3status.state as DecimalType) == 1) { postUpdate(outlet3, ON) }
end