[SOLVED] MQTT force item update from a static topic

Sorry if this has already been answered. I did try searching, but got tons of threads, read a few of them, but the actual issue wasnt part of them…

My problem is, I have a Sonoff POW R2 (tasmota) device set up using MQTT, which is working fine…
However, I cant seem to get an item to update on a topic, which doesnt change… Only those topics which changes will update…
How can I force an update to a mqtt topic… is it even possible.
I tried restart OH, but that didnt help. I havn´t tried disconnect the thing and reconnect it. I believe this is not the way to do it at all. (I have a few, for ones I preferere not to toggle the switch. Infact I wish I could get the same device without the switch opputunity at all).

This is the message from the Tasmota consol:

18:36:04 MQT: tele/sonoffPOW1/SENSOR = {"Time":"2019-12-27T18:36:04","ENERGY":{"TotalStartTime":"2019-09-07T18:16:01","Total":87.303,"Yesterday":0.476,"Today":0.851,"Period":0,"Power":1,"ApparentPower":8,"ReactivePower":8,"Factor":0.15,"Voltage":235,"Current":0.034}}

The topic “TotalStarTime” is the one which doesnt update in OH, because it´s quite static.

The same goes for Power ON (the actual switch operation). It only updates if I toggle the switch. But OH doesnt read its state, if it doesnt change…

18:39:04 MQT: tele/sonoffPOW1/STATE = {"Time":"2019-12-27T18:39:04","Uptime":"49T21:46:21","Heap":25,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"POWER":"ON","Wifi":{"AP":1,"SSId":"Kepler","BSSId":"FC:EC:DA:17:CF:64","Channel":11,"RSSI":86,"LinkCount":677,"Downtime":"0T00:46:50"}}

Is this only possible by storing its previous state in a mapdb and then read it from there?
I´m not sure, but isn´t there something about mapdb cant store string values? (like the first with the datetime). Storing previous state is a option I havnt played with yet.

This is my mqtt things:

    Thing topic sonoffpow1 "Sonoff Pow1 " @ "A Room" {
    Channels:
        Type string : reachable    "Reachable"                [ stateTopic="tele/sonoffPOW1/LWT" ]
        Type switch : power        "Power"                    [ stateTopic="stat/sonoffPOW1/POWER", commandTopic="cmnd/sonoffPOW1/POWER" ]
        Type number : powerload    "Power load"               [ stateTopic="tele/sonoffPOW1/SENSOR", transformationPattern="JSONPATH:$.ENERGY.Power"]
        Type number : voltage      "Line voltage"             [ stateTopic="tele/sonoffPOW1/SENSOR", transformationPattern="JSONPATH:$.ENERGY.Voltage"]
        Type number : current      "Line current"             [ stateTopic="tele/sonoffPOW1/SENSOR", transformationPattern="JSONPATH:$.ENERGY.Current"]
        Type number : total        "Total energy today"       [ stateTopic="tele/sonoffPOW1/SENSOR", transformationPattern="JSONPATH:$.ENERGY.Today"]
        Type number : totalyest    "Total energy yesterday"   [ stateTopic="tele/sonoffPOW1/SENSOR", transformationPattern="JSONPATH:$.ENERGY.Yesterday"]
        Type number : totalall     "Total energy alltime"     [ stateTopic="tele/sonoffPOW1/SENSOR", transformationPattern="JSONPATH:$.ENERGY.Total"]
        Type number : rssi         "WiFi Signal Strength"     [ stateTopic="tele/sonoffPOW1/STATE", transformationPattern="JSONPATH:$.Wifi.RSSI"]
        Type number : period       "Energy Period"            [ stateTopic="tele/sonoffPOW1/SENSOR", transformationPattern="JSONPATH:$.ENERGY.Period"]
        Type string : starttime    "Start time"               [ stateTopic="tele/sonoffPOW1/SENSOR", transformationPattern="JSONPATH:$.ENERGY.TotalStartTime"]
    } 

How do you know?
Updated states with the same value are not logged
But a rule triggers with received update should trigger

Can you try?

Because I get the -.-.- in a sitemap:


Notice the Quooker Since -.-.-

Hmm okay… I was looking at the logfile as well…

How to trigger from a single topic?

rule "updated item"
when
    MyItem received update
then
    logInfo("TEST", MyItem.name.toString + " updated to: " + MyItem.state.toString)
end

So I should use the item to trigger the rule… I see and understand your point. But if the item doesnt update, I would be getting nowhere…
Anyway, I´ll give it a try ofcouse.

EDIT - Now I have tried… It doesnt update…
I may have been a bit unclear, but the problem is, this specific topic “TotalStarTime” only update the first time the device is turned on. It wont update again, unless I reset the parameter.

I’m not sure if I understand.
Apparently the total start time item is not set to a string that can be validly displayed in Habpanel, but the data is published according to your first post.
Are you sure your transformation is working fine?

Next to that, if the sensor topic isn’t being published often enough, you could check if you can set up the device to publish retained messages. And then in some way resubscribe periodically (for example via a rule).

It may help if you try to explain what you actually need, rather than the means you want to use to achieve that.

I think what you have is an openHAB Item, that gets a state from some remote device/service when an MQTT message comes.
So, guessing the need is … when no message comes e.g. openHAB boot time, how do you set the Item state?

I think what you really need is the mysterious sender of the MQTT message to give it a “retain” property. Then the broker would remember the last message, and when OH connects to the topic, be force-fed the last version.

Alternatvely, perhaps you have some way to provoke the mystery service/device to send an update. A status query or somesuch.

If you don’t have any of that, you’ll have to use OH persistence.

Thanks Rolf…
The sensor data is beeing published every 30 seconds.

21:17:04 MQT: tele/sonoffPOW1/SENSOR = {"Time":"2019-12-27T21:17:04","ENERGY":{"TotalStartTime":"2019-09-07T18:16:01","Total":87.490,"Yesterday":0.476,"Today":1.038,"Period":0,"Power":1,"ApparentPower":9,"ReactivePower":9,"Factor":0.14,"Voltage":236,"Current":0.038}}
21:17:34 MQT: tele/sonoffPOW1/SENSOR = {"Time":"2019-12-27T21:17:34","ENERGY":{"TotalStartTime":"2019-09-07T18:16:01","Total":87.490,"Yesterday":0.476,"Today":1.038,"Period":0,"Power":1,"ApparentPower":9,"ReactivePower":9,"Factor":0.14,"Voltage":237,"Current":0.038}}

So I guess thats not the problem.
I really believe the reason is, that the TotalStartTime isnt updating… It´s a datetime value of when the device was first connected to power. (In the above, this Sonoff POW R2 was connected to power on the 7th of September 2019. Ie 2019-09-07 at time 18:16:01.).

Now this part of the topic will never update again, unless I reset it from the Tasmota consol. But I really would like to have this inserted into OH. Unfortunatly OH dont receive this part of the topic, unless it somehow changes, which it wont ofcouse.

Time to show your Item definition

I´m trying.
I need OH to receive the latest published state of all the defined topics, wether or not they changed.
It works fine for, except for this: “TotalStartTime” topic and the “Power ON” topics. OH wont update the items, unless these has changed. So if I restart OH, these items dont receive an update.

I know I may have to use persistense which is normally required. But since the TotalStartTime is a one time value set only, I think it may be quite alot of work to use persistens for this…So I just wondered if it may be possible with some MQTT tricks or something.

“Power ON” topic I can force the update simply from toggling the switch. (I would however prefere not to).

My items:

String FF_Sonoff_POW_Reachable "Quooker Reachable: [%s]"                        <cu_switch>     (FF_Sonoff_POW)              { channel="mqtt:topic:mosquitto:sonoffpow1:reachable" }
Switch FF_Sonoff_POW_Power     "Quooker Power [%s]"                             <switch>        (FF_Sonoff_POW)              { channel="mqtt:topic:mosquitto:sonoffpow1:power", autoupdate="false" }
Number FF_Sonoff_POW_Load      "Quooker Power load [%.1f W]"                    <Energy>        (FF_Sonoff_POW,gPower)       { channel="mqtt:topic:mosquitto:sonoffpow1:powerload" }
Number FF_Sonoff_POW_Voltage   "Quooker Line voltage [%.1f V]"                  <Energy>        (FF_Sonoff_POW)              { channel="mqtt:topic:mosquitto:sonoffpow1:voltage" }
Number FF_Sonoff_POW_Current   "Quooker Line current [%.3f A]"                  <current>       (FF_Sonoff_POW)              { channel="mqtt:topic:mosquitto:sonoffpow1:current" }
Number FF_Sonoff_POW_Today     "Quooker Total energy today [%.3f kWh]"          <total_energy>  (FF_Sonoff_POW)              { channel="mqtt:topic:mosquitto:sonoffpow1:total" }
Number FF_Sonoff_POW_Yesterday "Quooker Total energy yesterday [%.3f kWh]"      <total_energy>  (FF_Sonoff_POW)              { channel="mqtt:topic:mosquitto:sonoffpow1:totalyest" }
Number FF_Sonoff_POW_Total     "Quooker Total energy all time [%.3f kWh]"       <Energy>        (FF_Sonoff_POW,gTotalEnergy) { channel="mqtt:topic:mosquitto:sonoffpow1:totalall" }
Number FF_Sonoff_POW_RSSI      "Quooker WiFi Signal Strength [%d %%]"           <wifi>          (FF_Sonoff_POW)              { channel="mqtt:topic:mosquitto:sonoffpow1:rssi" }
Number FF_Sonoff_POW_Period    "Quooker Period [%d Day(s)]"                     <time>          (FF_Sonoff_POW)              { channel="mqtt:topic:mosquitto:sonoffpow1:period" }
DateTime FF_Sonoff_POW_Starttime "Quooker since [%1$td.%1$tm.%1$tY]"     <time>          (FF_Sonoff_POW)                     { channel="mqtt:topic:mosquitto:sonoffpow1:starttime" }

This I can’t follow.
MQTT messages are pushed (published), in your case every 30 seconds apparently.
OpenHAB will have to process these if it is subscribed to the topic, which is the case as you’ve shown.

That means the only reason why it now shows an undefined date, is because the message is not properly processed by OpenHAB, either in the transform or in the way it is displayed in the UI (Which I highly doubt).

dont take me for granted, but i would say that what you need is retained topic on mqtt side, which you can define on the sonoff part, especially if you’re using tasmota. i know that this can be done on sonoff but how, really cant tell for sure…

You may be right. But I simply cant see any errors anywhere :frowning:

My sonoff devices are flashed with the Tasmota firmware… I´m not sure how to set the retained flag.
As far as I understand, the retained flag only works when the client subscrip to the topic, which may be good enough in this case, as it will work when OH starts then.
But I wonder how come I cant get it to update every time (every 30 seconds)…

can it be that difference between string and datetime? in thing definition you have it defined as string and in items is datetime…

1 Like

But your channel is just a string

There is a datetime channel type. You may need to chain a transform to convert the incoming string to be compatible with OH datetime, but it looks fairly sensible to begin with.

If I change the channel to datetime, I receive an error:
Normally I would say String should work as well… But I may be wrong here…
Btw I´m using the Mosquitto broker.

2019-12-27 22:36:46.839 [ERROR] [core.thing.internal.ThingManagerImpl] - Exception occurred during notification about bridge status change on thing 'mqtt:topic:mosquitto:sonoffpow1': null
java.util.ConcurrentModificationException: null
	at java.util.HashMap$ValueSpliterator.forEachRemaining(HashMap.java:1633) ~[?:?]
	at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482) ~[?:?]
	at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472) ~[?:?]
	at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[?:?]
	at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[?:?]
	at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:566) ~[?:?]
	at org.openhab.binding.mqtt.generic.internal.handler.GenericMQTTThingHandler.start(GenericMQTTThingHandler.java:87) ~[?:?]
	at org.openhab.binding.mqtt.generic.AbstractMQTTThingHandler.bridgeStatusChanged(AbstractMQTTThingHandler.java:162) ~[?:?]
	at org.eclipse.smarthome.core.thing.internal.ThingManagerImpl$4.run(ThingManagerImpl.java:901) [184:org.openhab.core.thing:2.5.0.M2]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:?]
	at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:?]
	at java.lang.Thread.run(Thread.java:748) [?:?]

EDIT - Hang ON… Even though I received the above error… I think it´s working anyway… The item updated…

Then leave the channel as string and make your Item of type String, just to prove it all works as advertised.

Incoming message form 2018-01-01T12:14:00 should be fine with datetime channel, according to binding docs, which can then be used with DateTime Item.

Temhe error is probably because you are trying to put something into a DateTime type which OpenHAB cannot translate into a date time, so you will need to do that manually.

Solved!
The change of the channel type did the trick…Dont know why the first device gave the above error. Changing all my other devices to channel type datetime did not give any error. All devices items now receive the update just fine…

Thanks alot guys… This has driven me nuts for quite a few days… I truely believed using string type channel would do as well… But I guess in this case it didn´t which is quite obvious now :face_with_raised_eyebrow:

Nope, the translation is working just fine from the items label. This is a standard DateTime value. I only use the date in the item though.