Widget update

Hello,

I tried to update a widget (buton) when I press it. However, when I press, the buton is not updated and still waiting for refresh.

The buton is link to a relay (ON/OFF) when I press it. (relay1_data_widget)

How to update a buton with Python ?

Thanks for your help

Here is the code when I receive a Mqtt message :

Cayenne widget information according to the Dashboard.

ID = ‘v1/’+ MQTT_USERNAME + ‘/things/’ + MQTT_CLIENT_ID
Buton1_cmd = ID + ‘/cmd/2’ # Bouton 1 → receive Actuator command
Buton1_data = ID + ‘/data/2’ # Bouton 1 → send Actuator Updated Value

The callback for when a message is received from Cayenne.

def on_message(message):

print("message received: " + str(message))  
   
m = message.topic.split('/')                
p = message.payload.decode().split(',')     
channel = m[5]
state = p[1]
print("")
print("message received from Cayenne mydevice")
print("channel =" + str(channel) + "  state   = " + str(state))
client.mqttPublish(client.rootTopic + '/cmd/2', '1')
if channel=='2':
    if state=='1':
        client.publish(Buton1_data, "1")
        print("Bouton 1 is ON")
        B1 = "on"
    else:
        client.publish(Buton1_data, "0")
        print("Relai 1 is OFF")
        R1 = "off"

client = mqtt.Client(MQTT_CLIENT_ID)
client.username_pw_set(MQTT_USERNAME,MQTT_PASSWORD)
client.connect(MQTT_SERVER,MQTT_PORT)
client.on_message = on_message
client.subscribe(Buton1_data)
client.loop_forever()

Based on DOC : Send Actuator Updated Value

In order to let the dashboard know of the current status of an actuator, the device must publish that value to the corresponding channel. This will ensure that the widget state (on/off) remains consistent with the state of the actuator.

Topic PUB SUB
v1/ username /things/ clientID /data/channel X

Payload must contain simply the true binary value of a digital actuator (1/0) or numerical value for analog actuators.

  • (string) 1
  • (string) 0.8

Use this:

# The callback for when a message is received from Cayenne.
def on_message(message):
    print("message received: " + str(message))
    if (message.channel == 4) and (message.value == "1"):
        //do whatever you want.

Doesn’t works. No on_message call. But code is much simple. Thanks.
Channel ==2. and not 4 on my exemple.
Do you have any exemple of Python code with a buton refresh ?

try this code Cayenne-MQTT-Python/Example-03-CayenneClient.py at master · myDevicesIoT/Cayenne-MQTT-Python · GitHub

With cayenne-mitt library it works ! but not with Paho-mqtt. Thanks !

The button ON/OFF works fine on Cayenne web site but it’s not synchronized with Cayenne IOS APP. How do you synch both ?
Should I publish the status of the widget button ? (client.virtualWrite(2, 1,“digital_sensor”, “d”))
I cannot find an exemple in Python with cayenne-mitt library. Could you help ?

is there something like : client.publish(Buton1_data, “1”)

can you restart the app and see if changes are shown.

Yes. If I qui the App and restart it the status is updated. Otherwise not.
Is there a way tu push the update on the App or is it a bug ?

no there is no way.