Skip to content

Change state type on MQTT payload from boolean to string  #7066

@snakuzzo

Description

@snakuzzo

Is your feature request related to a problem? Please describe

According to Home Assistant documentation about MQTT Binary Sensor entities (HERE), Z2M publish states as boolean and not as string

This is the message published on MQTT broker...

zigbee2mqtt/mywindow {"battery":97,"contact":true,"linkquality":72,"temperature":24,"voltage":2995}

...and this is my binary_sensor...

binary_sensor:

  - platform: "mqtt"
    name: "mywindow"
    state_topic: "zigbee2mqtt/mywindow"
    availability_topic: "zigbee2mqtt/bridge/state"
    value_template: "{{ value_json.contact }}"
    payload_on: false
    payload_off: true
    device_class: "window"

Take a look at keesschollaart81/vscode-home-assistant#1148

Describe the solution you'd like

Publish states true and false as string in MQTT payload

Describe alternatives you've considered

...apply template on HA ?
I think it is necessary to update the documentation related to manual configurations, applying the templates

  - platform: "mqtt"
    name: "mywindow"
    state_topic: "zigbee2mqtt/mywindow"
    availability_topic: "zigbee2mqtt/bridge/state"
    value_template: "{{ 'true' if value_json.contact == true else 'false' }}"
    payload_on: "false"
    payload_off: "true"
    device_class: "window"

Additional context

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions