I have an item that receives a json string from an mqtt broker. I then have a corresponding rule that parses the json string to extract the relevant data. The mqtt topic is a persistent topic so that when the openhab server starts up it will receive a string from the broker. The problem I’m seeing is that the item is updated before the rules are loaded. The effect is that my rule is not executed on the initial data that comes from the mqtt broker – it has to wait for a change to come in. Seems like the rule should be initialized first so that this works properly. Is there a way to work around this to make it work correctly?
I don’t understand how to simply update & display a string value using a rule. Seems like I should be able to figure this out but I have yet to see how this works.
The item: String mqttTemp
The value in the sitemap: Frame label=“Outdoor Temp” { Text item=mqttTemp “” }
The action in the rule: mqttTemp.postUpdate(temp)
One good reason could be that the data in the JSON is used to populate the state of multiple Items.
Don’t know if that is the case here but that is a design pattern I’ve seen several times, though typically with the lower level bindings like TCP and Serial.
Well, your Item is getting updated, it is just your rule that isn’t being executed. So you can make this work by creating a System started rule to process those Items which assumes that their current state needs to be processed. Or just resend the command to trigger the rule:
MyItem.sendCommand(MyItem.state)
to trigger the rule.
You can also make the rules load first by changing the polling time in openhab.cfg to a smaller number than the Items. However, be aware that you will get a whole mess of exceptions and errors if any of those rules execute before your Items have loaded (chicken and egg problem). So you cannot have any System started triggered rules and probably should avoid Time cron triggered rules if you go down this path.
I’m going to guess that the root problem you are trying to solve is get any messages that were sent when OH was down. The way that I solved this problem was to add in a way for OH to request the sensors to publish their current states to the MQTT topic. I basically have an update topic the sensors subscribe to and when that topic receives any message they publish their current state. Then in a System started rule I have OH publish a message to the update topic. Of course I implemented this having never even considered trying to use persistent topics.
I’m afraid I don’t follow this. I have several items that are populated from different parameters of the same MQTT message using a different transform.