Strange behavior for MQTT templates using payload and attributes
-
I'm re-writing my shelly_scenecontroller mqtt template because I added a new pushbutton and I had strange results. Basically, in the last couple of days, it fired itself, because of the way the logic works. Everytime I'm pushing a button, an "S" is sent as the event, so the
button.state
never changes. I'm looking forbutton.since
to determine if the button is pressed again.
That's good, but everytime a poll is done, thebutton.since
attribute is updated, because it is tied totime()
.
So, I introduced the use of
event_cnt
from the payload, comparing the value to the stored one, in order to understand if it has been pushed again, or, in general, the number of times the button is pushed.
Updated template:events: "shellies/%topic%/input_event/%channel%": - attribute: "value_sensor.value" json_payload: true expr: "float(payload?.event_cnt)" - attribute: "button.since" json_payload: true expr: > entity.attributes?.button?.since == null || float(payload?.event_cnt) != entity.attributes?.value_sensor?.value ? time() : entity.attributes.button.since - attribute: "scene_activation.since" # just copy button.since expr: "entity?.attributes?.button?.since" - attribute: "button.state" json_payload: true expr: > payload?.event == "S" ? "single" : payload?.event == "SS" ? "double" : payload?.event == "SSS" ? "triple" : payload?.event == "L" ? "long" : "unknown" - attribute: "scene_activation.scene_id" json_payload: true expr: "payload?.event" - attribute: "x_mqtt_device.online" expr: true
This should work, but instead I have this value:
and that's beyond strange and I can't honestly figure it out. @toggledbits any hint is appreciated. Thanks.
-
Debug level 5 for MQTTController and log files are your friend.
That said, I'm suspicious of the operator precedence effect on your expression. Wrap a set of parens around the conditional of the ternary operator and try it again.
-
No joy from the logs. I added parens but no joy. The only thing that worked was a delete of the entity/restart of Reactor.
[latest-25272]2025-09-30T14:49:47.414Z <MQTTController:5:MQTTController.js:599> MQTTController#mqtt topic shellies/shelly-gatebutton/input_event/0 associated with Array(1)[ "shelly_gatebutton" ] [latest-25272]2025-09-30T14:49:47.414Z <MQTTController:5:MQTTController.js:601> MQTTController#mqtt dispatching shellies/shelly-gatebutton/input_event/0 to shelly_gatebutton [latest-25272]2025-09-30T14:49:47.414Z <MQTTController:5:MQTTController.js:1152> MQTTController#mqtt handling shellies/shelly-gatebutton/input_event/0 (event shellies/shelly-gatebutton/input_event/0) for shelly_gatebutton: {"event":"","event_cnt":2} [latest-25272]2025-09-30T14:49:47.415Z <MQTTController:5:MQTTController.js:1168> MQTTController#mqtt topic shellies/shelly-gatebutton/input_event/0 affects entity shelly_gatebutton attributes value_sensor.value, button.since, scene_activation.since, button.state, scene_activation.scene_id, x_mqtt_device.online [latest-25272]2025-09-30T14:49:47.415Z <MQTTController:5:MQTTController.js:1274> MQTTController#mqtt topic shellies/shelly-gatebutton/input_event/0 setting shelly_gatebutton value_sensor.value=2 [latest-25272]2025-09-30T14:49:47.415Z <MQTTController:5:MQTTController.js:1274> MQTTController#mqtt topic shellies/shelly-gatebutton/input_event/0 setting shelly_gatebutton button.since={"event":"","event_cnt":0} [latest-25272]2025-09-30T14:49:47.416Z <MQTTController:5:MQTTController.js:1274> MQTTController#mqtt topic shellies/shelly-gatebutton/input_event/0 setting shelly_gatebutton scene_activation.since={"event":"","event_cnt":0} [latest-25272]2025-09-30T14:49:47.416Z <MQTTController:5:MQTTController.js:1274> MQTTController#mqtt topic shellies/shelly-gatebutton/input_event/0 setting shelly_gatebutton button.state=unknown [latest-25272]2025-09-30T14:49:47.416Z <MQTTController:5:MQTTController.js:1274> MQTTController#mqtt topic shellies/shelly-gatebutton/input_event/0 setting shelly_gatebutton scene_activation.scene_id= [latest-25272]2025-09-30T14:49:47.417Z <MQTTController:5:MQTTController.js:1274> MQTTController#mqtt topic shellies/shelly-gatebutton/input_event/0 setting shelly_gatebutton x_mqtt_device.online=true [latest-25272]2025-09-30T14:49:47.417Z <MQTTController:5:MQTTController.js:1905> MQTTController#mqtt _handle_ext_event() shellies/shelly-gatebutton/input_event/0 matches 0 subscriptions
Maybe it's something that when it's corrupted with json, doesn't work as expected. I'm reporting as an edge case, but it's very hard to reproduce. Thanks anyway. New build for my MQTT-contrib incoming.
-
Looking more deeply at your logic, I don't think it's going to work. Here's where I think you are:
The current value of
button.since
is{"event":"", "event_cnt":0}
. I assume it got set this way from prior effort leading up to this point. Now, based on what you currently have configured...When the topic
shellies/shelly-gatebutton/input_event/0
arrives withevent_cnt:2
in the payload, the first thing MQTTController is doing is settingvalue_sensor.value
to 2. You can see that in the log snippet.The next thing is it attempts to set
button.since
, but since your expression is set up so thatbutton.since
is only changed if if the value ofvalue_sensor.value
is different frompayload.event_cnt
, which it won't be, because it just set them to the same value. So the expression simply returns the current value ofbutton.since
and thus changes nothing.See the problem now?
You have control over the order in which the attributes are processed for the topic/event.
-
Looking more deeply at your logic, I don't think it's going to work. Here's where I think you are:
The current value of
button.since
is{"event":"", "event_cnt":0}
. I assume it got set this way from prior effort leading up to this point. Now, based on what you currently have configured...When the topic
shellies/shelly-gatebutton/input_event/0
arrives withevent_cnt:2
in the payload, the first thing MQTTController is doing is settingvalue_sensor.value
to 2. You can see that in the log snippet.The next thing is it attempts to set
button.since
, but since your expression is set up so thatbutton.since
is only changed if if the value ofvalue_sensor.value
is different frompayload.event_cnt
, which it won't be, because it just set them to the same value. So the expression simply returns the current value ofbutton.since
and thus changes nothing.See the problem now?
You have control over the order in which the attributes are processed for the topic/event.
@toggledbits said in Strange behavior for MQTT templates using payload and attributes:
See the problem now?
You have control over the order in which the attributes are processed for the topic/event.
I see it! Thanks! (Never code in a hurry while doing 100 other things).
-
@toggledbits said in Strange behavior for MQTT templates using payload and attributes:
See the problem now?
You have control over the order in which the attributes are processed for the topic/event.
I see it! Thanks! (Never code in a hurry while doing 100 other things).
@therealdb said in Strange behavior for MQTT templates using payload and attributes:
Never code in a hurry while doing 100 other things
Ummm... is there any other way?