Publish a binary sensor to mqtt

I have bee trying for a day now how to transfer a binary sensor state from one HA to another via MQTT but I only succeed to transfer it as a sensor. Can anybody help a retired who is not that good at understanding programming. I do not want to use the HACS client since it creates too much trouble for me.

You can manually set up an MQTT binary sensor:

I suspect you’re using MQTT discovery instead? Another alternative would be to create a template binary sensor reading from the MQTT sensor you already have:

Thank you Troon, I have previously read what you are pointing at and as I understood it is on the receiving side and my problem seems to be on the publishing side. What I publish (via mqtt) can only be read as a sensor not as a binary sensor although the source is a binary sensor. To be able to post it as a binary sensor will save a lot of work on the receiving side.

I’m open to correction but I don’t think you can. The receiving end has no way of knowing what might be sent in future, so can’t assume that the MQTT channel is only sending “binary” data.

Funny is that shelly units can send to MQTT that is interpred as binary by HA.

Try here. Looks like you may be able to do this with MQTT discovery if you name your topics appropriately.

Maybe, but I don’t understand how to send it. I have tried to send it with the built in mqtt “client” but no luck.

Post what you are currently using that isn’t working for you as expected.

I don’t have any code yet. I can publish a sensor on my MQTT broker, but not a binary sensor. As far as I understand I have to ceate the binary_sensor in my MQTT broker before using it and that is my challenge at the moment.

How are you publishing a sensor to your broker?

In node-red like this:
[{“id”:“dad76a3a.5ca568”,“type”:“mqtt out”,“z”:“6ee34006.eeb86”,“name”:“GaragePortSensor”,“topic”:“garage/inside/portsensor/status”,“qos”:“1”,“retain”:“true”,“broker”:“90d2f8f0.20f3d8”,“x”:410,“y”:600,“wires”:[]},{“id”:“a4a4c0e3.a89f7”,“type”:“server-state-changed”,“z”:“6ee34006.eeb86”,“name”:“GaragePortSensor”,“server”:“56d867e5.0abf88”,“version”:1,“exposeToHomeAssistant”:false,“haConfig”:[{“property”:“name”,“value”:""},{“property”:“icon”,“value”:""}],“entityidfilter”:“binary_sensor.garage_port_sensor”,“entityidfiltertype”:“exact”,“outputinitially”:true,“state_type”:“str”,“haltifstate”:"",“halt_if_type”:“str”,“halt_if_compare”:“is”,“outputs”:1,“output_only_on_state_change”:false,“for”:0,“forType”:“num”,“forUnits”:“minutes”,“ignorePrevStateNull”:false,“ignorePrevStateUnknown”:false,“ignorePrevStateUnavailable”:false,“ignoreCurrentStateUnknown”:false,“ignoreCurrentStateUnavailable”:false,“x”:130,“y”:600,“wires”:[[“dad76a3a.5ca568”]]},{“id”:“90d2f8f0.20f3d8”,“type”:“mqtt-broker”,“name”:“Husets”,“broker”:“192.168.xxx.xxx”,“port”:“1883”,“clientid”:“Garage”,“usetls”:false,“compatmode”:false,“keepalive”:“60”,“cleansession”:true,“birthTopic”:"",“birthQos”:“1”,“birthPayload”:"",“closeTopic”:"",“closeQos”:“0”,“closePayload”:"",“willTopic”:"",“willQos”:“1”,“willPayload”:""},{“id”:“56d867e5.0abf88”,“type”:“server”,“name”:“Home Assistant”,“addon”:true}]

There appears to be a language barrier. You said you don’t have any code yet you do have a Node-Red flow. You claim you can “publish a sensor” which can be interpreted as creating a sensor via MQTT Discovery. However, that’s not what your Node-Red flow is doing; it’s publishing a sensor value to the topic: garage/inside/portsensor/status

As for the Node-Red flow you posted, it’s unusable because the code is unformatted. It uses non-standard quotes that prevent the example from being imported and they must all be converted to standard quotes. It’s not difficult to do but represents yet another challenge to assist you.

Let’s start over and clarify what exactly do you want:

  1. Do you want to create an MQTT Binary Sensor via Home Assistant’s MQTT Discovery?
  2. Do you want to publish a value to an existing MQTT Binary Sensor?

Thank you for your patiens. Yes my mother tounge is Swedish so sometimes my way of expressing things may be a littele unclear When starting I didn’t need to crate the sensor i just showed up when publishing it via node-red.

  1. I really don’t care how I create a binary sensor as long as it is created.
  2. After creating it yes.

Ok, so follow the instructions in the first link in Troon’s post. It explains how to define an MQTT Binary Sensor. Whatever you choose for its state_topic is what you will use as the topic for publishing ON or OFF (to set the binary_sensor’s state).

So why did you bother to answer me, just waist of your time and my hopr of getting help? If that would have been clear enogh for me I would have been enough I would have aswered Troon thank you now it works!!

You did answer Troon, ten days later, and reported you didn’t “understand how to send it”. That’s why I tried to help you.

Glad to hear it works now.

What help did you give? Telling me to read what I already said I didn’t understand - and I still don’t. Why did you bother to answer at all? Do you enjoy adding to people’s frustration?

You won’t be getting any further help from me with an attitude like that. @123 was just trying to provide more help (I have been away), and understand what you had so far. You had not previously said this was a sensor being published via Node-Red, for example, and that is important information.

1 Like

MQTT Statestream could be a solution to OP’s question.