#Setting up an MQTT cover with only position as single byte?

1 messages · Page 1 of 1 (latest)

boreal dirge
#

I have a self-made cover device that has only two MQTT topics, position and command. The command topic takes in a single byte from 0-255, and moves the motor to that postion. The position topic sends out a single byte from 0-255 after each move indicating its current position. Currently I have the following YAML:

mqtt:
- cover:
    device:
      identifiers:
        crabroll
      manufacturer: Gabe
      model: v1
      name: crabroll
    device_class: blind
    position_closed: 255
    position_open: 0
    position_template: '{{ unpack(value, "<B") }}'
    position_topic: crabroll/test/pos
    qos: 0
    set_position_template: '{{ pack(value, "<B") }}'
    set_position_topic: crabroll/test/command
    unique_id: 01KEHE0KF2K00XCSSD2NK8PAS7_c3bfba9a3af04e1a9bbbece23a366ee8

Sending the position works great, device recives it in the correct format and moves. However, Im having trouble figuring out the template to extract the position, it always shows as 'unkown'.

radiant gale
#

If it's self made, woudn't it just be simpler to just send the position as text? Aka, not as byte but as string. Just sprintf() and atoi() or something in C.

boreal dirge
#

I dont even have floats here.

radiant gale
#

What is the device running?
And floats are really a next thing, not needed here. But I just expect the device not to just send only a byte over

boreal dirge
#

But surely there is a way to decode a byte in the template engine, right? Would I be SOL if this was not a self-made device?

#

Ive tried the unpack in the template playground, and it works, returning an int. Ive tried converting that int into a string as well. What type does position_template expect the template to return?

radiant gale
#

No experience with rust on an ESP, but would say it should not be hard 😄
But uhm, like I said, if sending the position works then the pack() works.
Position should just work with int or string I think. In the background it's always converted to float/int.
And I can only imagine the value received is just a bit more then a single byte. I actually expect HA to always make value a string

#

Should have something in your log I assume.

boreal dirge
#

Where can I find the log for an entity? I can see the mqtt traffic in the entity, but not what its trying to decode it into.

radiant gale
#

if the template error on receiving it should just be in the HA core log

boreal dirge
#

Apparently it wants a bytes object.

radiant gale
#

yep, was afraid of that 🙂

#

Can you try to do value[0] | ord ?

boreal dirge
#

HOLY COW that worked!

#
Filter ord will return for a string of length one an integer representing the Unicode code point of the character when the argument is a Unicode object, or the value of the byte when the argument is an 8-bit string.

Having trouble parsing this figuring out if I can use this for sending data as well...

#

nor do I understand why the unpack didnt work... both ord and unpack("<B") return an int...

radiant gale
#

yeah, but unpack only works on native byte values 😅

boreal dirge
#

Ah, is there a way to convert a string into native byte values?

#

or is HA just not really setup to deal with things that expect binary?