How to use correctly MQTT to have the real state of the device in HA?

Hi

I try to create manually a switch ON / OFF for my siren.

I think I don’t understand all parameters, I have the following code (inside the configuration.yaml):

switch:
  - platform: mqtt
    name: "Alarm Bathroom Switch"
    state_topic: "zigbee2mqtt/siren_alarm_bathroom/state"
    command_topic: "zigbee2mqtt/siren_alarm_bathroom/set"
    payload_on: "{\"alarm\": true,\"humidity_alarm\": false,\"melody\": 1,\"temperature_alarm\": false,\"volume\": \"low\",\"duration\": 2}"
    payload_off: "{\"alarm\": false,\"humidity_alarm\": false,\"melody\": 1,\"temperature_alarm\": false,\"volume\": \"low\",\"duration\": 2}"
    state_on: "ON"
    state_off: "OFF"
    optimistic: false
    qos: 0
    retain: true

What state should I write ? I put “ON” and “OFF” but not sure it’s like that we have to use it in the UI ?

I want at the end a toggle ON/OFF in Lovelace which toggle publishing a MQTT request on my siren to have alarm: true or alarm: false :slight_smile:

What should I do ?

Thank you in advance :slight_smile:

OK I just forgot to restart HA just after … The switch is finally available and start to work but I still have some issues.

Indeed, my alarm has a timer in seconds that we can notify in the mqtt request via duration: 4 means stays ON during 4 seconds then go Off again.

I would like to have the “real” state of my device in HA

In this moment, I set the duration to 40 for my alarm. So the alarm beeeeep during 40 seconds

The problem is about my toggle on the UI. When I toggled it into ON. The alarm is triggered for 40 seconds (behavior expected), however after 2 seconds, the toggle return into ‘OFF’ mode :(. My alarm continues during 38 seconds the beeeep as expected.
The STATE of the toggle is all the time ‘inactive’… It should be ON (active) during 40 seconds.

In this moment, I have the feeling that the state is not reported to HA.

Any idea what should I do?

In the following example, the duration was at 4 (so 4 seconds). I triggered it via my toggle in Lovelace.
I can see 4 seconds from “ON” until “OFF” state in logs

Zigbee2MQTT:info  2021-01-23 23:00:00: MQTT publish: topic 'zigbee2mqtt/siren_alarm_bathroom', payload '{"alarm":true,"humidity":37,"humidity_alarm":false,"linkquality":54,"melody":1,"temperature":"22.4","temperature_alarm":false,"volume":"low"}'
Zigbee2MQTT:info  2021-01-23 23:00:04: MQTT publish: topic 'zigbee2mqtt/siren_alarm_bathroom', payload '{"alarm":false,"humidity":37,"humidity_alarm":false,"linkquality":57,"melody":1,"temperature":"22.4","temperature_alarm":false,"volume":"low"}'

But in HA its always OFF…

image

Sorry to ping you, but maybe @123 or @petro you know how is it working to keep the real state more than 2 seconds. In fact for HA its like it doesn’t know that the siren is really ON. Tried a lot of things but northing works. The only thing I can do is to use optimistic value at true but it stays always on ON and it’s not true because alarm have a duration in X seconds before to change it’s state to OFF. :frowning:

Thank you in advance

Regards

Sorry, I can’t help you. Nothing in your explanation makes sense to me. Hopefully someone else can help. Good luck.

No problem, thank you :).
As I was not sure that my explanations was clear. I tried to explain it with a different way / words (editing the previous message) hopping that will be more easy this time to understand my issue.