Issues with editing YAML

Hi all. I am new to HA and trying to build a smart home system over rs485 bus. I’m running into a problem editing the YAML config file correctly. I am not a programmer. I’m trying to get information about the status of the relay and relay module inputs. From the description of the module, I know what commands should be sent to the module in order to receive a response with input states and relay states. If I send these requests through a third-party terminal, then everything works. But all my attempts to create a query in a YAML file fail. I get a message that the file has not been edited correctly. For several days I tried to find YAML examples that can be copied and that would work, but there is nothing on the Internet. Only some scraps of non-working configurations. I wrote to several messages on the forums, but did not receive a single response. I’m confused and don’t understand why this is happening. Complete lack of support. I am willing to pay money to someone who will help me set up my configuration. Please somebody help!

p.s. This is the module I am trying to use.

This is the part of my code that works. But how do you add the rest of the code to get input states and relay states?

# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

modbus: 
  - name: hubrelay
    type: serial
    method: rtu
    port: /dev/ttyUSB0z
    baudrate: 9600
    stopbits: 1 
    bytesize: 8
    parity: N
    timeout: 1
    lights:
      - name: "light1"
        slave: 3
        address: 0
        write_type: coil        
      - name: "light2"
        slave: 3
        address: 1
        write_type: coil
      - name: "light3"
        slave: 3
        address: 2
        write_type: coil
      - name: "light4"
        slave: 3
        address: 3
        write_type: coil
           

I see this all the time. Editing/Creating yaml is not programming. It’s a data format. Just learn the format and you’re good. If you can fill out a form, you can edit yaml. Don’t let the thought of “Programming” yaml hold you back.


As for your modbus configuration. It looks correct assuming you’re using a USB serial connection with default settings. Typically default settings for serial connections are 9600 baudrate, 1 stopbit, 8 bytesize, with a parity of None (N).

The only thing in question is your port. Ensure that is the correct port.

Post the message as well. Please don’t paraphrase the message, post it exactly.

Please show with an example what needs to be written in the YAML file so that HA shows the state of the inputs and the state of the relay? Thank you.

My dude, what you have written is an example. It should work. You’re using the modbus protocol. What’s the address that you need to read? Do you know how modbus works?

  1. Read the relay status

Send: FF 01 00 00 00 08 28 12

Returns: FF 01 01 01 A1 AO

Remarks: Bit0–Bit7 of the 4th byte of the return frame, 0x01, represent relay 1–relay 8, 0 means off, and 1 means on.

  1. Read optocoupler input status

Send: FF 02 00 00 00 08 6C 12

Returns: FF 02 01 01 51 AO

Remarks: IN1–IN8 of the 4th byte of the return frame 0x01 represent the input signal of optocoupler 1 to optocoupler 8, 0 represents low level, and 1 represents high level

How to send the request described in paragraph 8 and paragraph 9?

That does not look like a normal modbus protocol. Normally, you read a register by polling it to get information. They tell you what the register address is, then you poll it.

What you’re posting here looks like a separate protocal, unrelated to modbus.

The device lists modbus, but does it actually use modbus?

EDIT: Unless the bytes represent the address. 08 28 12

Turn on the No. 1 relay (manual mode)

Send: FF 05 00 00 FF 00 99 E4

Return as it is: FF 05 00 00 FF 00 99 E4

Remarks: (1) The 3-4 bytes of the sending frame represent the relay address. The addresses of relay 1-relay 8 are 0x0000, 0x0001,0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007

(2) The 5-6 bytes of the sending frame represent data, 0xFF00 means opening the relay, and 0x0000 means closing the relay.

  1. Turn off the No. 1 relay (manual mode)

Send: FF 05 00 00 00 00 D8 14

Return as it is: FF 05 00 00 00 00 D8 14

  1. Turn on all relays

Send: FF 0F 00 00 00 08 01 FF 30 1D

Return: FF 0F 00 00 00 08 41 D3

  1. Turn off all relays

Send: FF OF 00 00 00 08 01 00 70 5D

Returns: FF OF 00 00 00 08 41 D3

At least this part works like a modbus (the code I posted earlier)

I’m sorry, but I’ve written a modbus protocol from the ground up. That does not look like a typical modbus protocol. There’s information in the manual for that product that’s missing. How is it expecting you to send that information? Via write_register or write_coils?

I do not know how in your Modbus system you need to correctly send a request. That’s why I created a topic with the question “how to get the data”? I have not found clear instructions on how to do this.

I guess it’s possible that they are simply telling you the information users typically don’t use? Very odd way to present the information. I guess they are assuming you aren’t using a modbus library.

Ok, I guess we have to assume that FF is the address and 05 or 0F is the function.

05 is write single coil. 0F is Write Multiple Registers.

FF is 255

Is this a light or what is this?

Through the terminal window, I was also able to change the address of the slave device from 255 to 3. So all the functions of the Chinese module work as described.

Yes, it’s light. Before proceeding with the installation, I want the system to start working stably on the table. I don’t want to use radio communication in my house. All devices must be wired only. Control of lighting, heating, ventilation, etc.

Ok, so assuming this turns on relay 1…

 command
   |         _______ open relay
   /\       /   \
FF 05 00 00 FF 00 99 E4
\/    \___/       \___/___ ?????
 |      \__ relay number
address

command on

00 00 FF 00 99 E4 → 4278229476

and command off…

00 00 00 00 D8 14 → 55316

I guess we will treat the relay as a light…

modbus: 
  - name: hubrelay
    type: serial
    method: rtu
    port: /dev/ttyUSB0z
    baudrate: 9600
    stopbits: 1 
    bytesize: 8
    parity: N
    timeout: 1
    lights:
      - name: "light1"
        address: 255
        write_type: coil
        command_on: 4278229476
        command_off: 55316

As I said before, typically API’s do not give you this information. They tell you the commands to run with the values to create. Right now, we have to reverse engineer the RAW api.

Looks like you’re going to have to learn how to reverse engineer protocols then. What you’re after is the hardest possible route with what appears to be a poorly documented device.

EDIT: Also keep in mind that you’re using modbus which means you have to poll the status of the light in order to react to it. It does not provide live changes…

IMO you’re going the wrong route. All you’re gaining is the ability to turn lights on and off, you won’t be able to react to them in real time. You’ll always be limited to a polling frequency.

Unfortunately the code doesn’t work. The switches that used to work are now not working either(

modbus: 
  - name: hubrelay
    type: serial
    method: rtu
    port: /dev/ttyUSB0z
    baudrate: 9600
    stopbits: 1 
    bytesize: 8
    parity: N
    timeout: 1
    lights:
      - name: "light1"
        slave: 3
        address: 0
        write_type: coil        
      - name: "light2"
        slave: 3
        address: 1
        write_type: coil
      - name: "light3"
        slave: 3
        address: 2
        write_type: coil
      - name: "light4"
        slave: 3
        address: 3
        write_type: coil
    lights:
      - name: "light11"
        address: 255
        write_type: coil
        command_on: 4278229476
        command_off: 55316     

you can’t have more than 1 lights section.

Remove this

You’re saying

works?

Yes, it works. But after I added your piece of code, it doesn’t work anymore

And it turns on and off your light? Makes no sense based on what your documentation says. Anyways, if that works, then don’t bother trying what I wrote and throw out that manual.

That’s it works. This code only controls the relay. How to add a login poll? I don’t want to delite the code for relay control. I want to add a new fragment to the already existing code, with the help of which you can poll the state of the inputs or some other functions.

modbus: 
  - name: hubrelay
    type: serial
    method: rtu
    port: /dev/ttyUSB0z
    baudrate: 9600
    stopbits: 1 
    bytesize: 8
    parity: N
    timeout: 1
    lights:
      - name: "light1"
        slave: 3
        address: 0
        write_type: coil        
      - name: "light2"
        slave: 3
        address: 1
        write_type: coil
      - name: "light3"
        slave: 3
        address: 2
        write_type: coil
      - name: "light4"
        slave: 3
        address: 3
        write_type: coil
    

But you added to port: /dev/ttyUSB0(z) After that, everything stopped working. What is the “z” letter of the USB configuration for?