Mqtt dimmer работающий по json

I started using HA quite recently, I ask for help in connecting the dimmer. The problem is that the dimmer expects a command like this:

Topic / house / room1 / led1 / BN-SZ01 / leds / status Message {“status”: “1000”, “activated”: “2”}

status - current value of brightness from 0 to 1000
activated - from which source the 2-mqtt 1-web control was performed
Sketch, please, what I need to write in yaml, I myself cannot figure out json.
Thank you !!

Hello and welcome to the forum.

This forum uses the English language to enable effective moderation and to be inclusive of the majority of users.
Please edit and translate your post to English. On-line translators are acceptable if you do not speak English.

Здравствуйте и добро пожаловать на форум.

Этот форум использует английский язык, чтобы обеспечить эффективную модерацию и охватить большинство пользователей.
Пожалуйста, отредактируйте и переведите свой пост на английский язык. Онлайн-переводчики приемлемы, если вы не говорите по-английски.

Would you mind to use NODERED addon? i can help.PM me

I want to find a solution with configuration files, I’m sure this is easy to solve, but this requires the necessary lines in the configuration. But thanks for the offer!

1 Like

You don’t really give enough information to give a smart answer, but the basic way to send a JSON payload to a topic is

          - service: mqtt.publish
            data:
              topic: 'house/room1/led1/BN-SZ01/leds/status'
              payload: '{"status": "1000", "activated": "2"}'

But how to make the value of status into a variable so that it can be further changed from the card?

What card?
See my point about “You don’t really give enough information to give a smart answer”.

Please provide the full environment/requirements so you don’t have us guessing what you’re trying to achieve, and what was not clear in the documentation so that you couldn’t achieve it by yourself.

Posting the YAML configuration for your (mqtt?) light would be a good start too.

card that in the HA interface

I want to add the device described above to HA.

I suggest you use the MQTT Light integration. Your LED light will appear as a light entity in Home Assistant. It will be represented as a toggle button in the Lovelace UI, allowing you to turn it on and off as well as change its brightness.

MQTT Light - Home Assistant

We can help you do that but we first need to know more about the LED light you are using. You have explained the MQTT topic that is used to get the light’s status. What is the topic used to control the light? Or does it use only one topic for controlling the light and reporting its current state?

Thanks for noticing! I apologize for incorrectly indicated in the first message. The device expects a json string, another parameter that makes the LEDs fade. I would be very grateful if you look at the information about the device:git

You are not listening to the advice you are being given which makes it very difficult to help you. Maybe it is a language thing.

Please answer this question:

How have you integrated this light into home assistant?

Once you have answered that we can find a solution for your brightness problem.

1 Like

I haven’t integrated it. For this I need help, so I create a theme. By “connection” I meant integration

Ok, so if you have not yet integrated the light and it is controlled via mqtt you need to use this integration (as 123 said above):

I can help with some configuration options but nothing else from the information you have provided so far:

light:
  - platform: mqtt
    name: "Your Light Name Here"
    state_topic: <you need to supply this>
    command_topic: <you need to supply this>
    brightness_state_topic: <you need to supply this>
    brightness_command_topic: "house/room1/led1/BN-SZ01/leds/status"
    rgb_state_topic: <you only need to supply this if it is an RGB light>
    rgb_command_topic:  <you only need to supply this if it is an RGB light>
    state_value_template: <you need to supply this>
    brightness_value_template: "{{ value_json.status }}"
    brightness_scale: 1000
    rgb_value_template: <you only need to supply this if it is an RGB light>
    qos: 0
    payload_on: <you need to supply this>
    payload_off: <you need to supply this>
    optimistic: false

My browser refuses to translate the git page from Russian to English but believe I understood that the command topic is the same as the state topic except without the final word status.

    command_topic: house/room1/BN-SZ01/leds

Включить на 50% с плавно нарастающей яркостью

Topic /home/room1/BN-SZ01/leds Message {"status":"500","smooth_change":"1"}

Ответ

Topic /home/room1/led1/BN-SZ01/leds/status Message {"status":"500","activated":"2"}

Yes, right

There are three kinds of MQTT Light schema:

  1. Default
  2. JSON
  3. Template

I believe the light you have requires the additional flexibility provided by the Template schema.

light:
  - platform: mqtt
    schema: template
    name: "Test Light"
    state_topic: /house/room1/led1/BN-SZ01/leds/status
    state_template: "{{ 'on' if value_json.status | int > 0 else 'off' }}"
    brightness_template: "{{ ((value_json.status | int) * 0.256) | round(0) }}"
    command_off_template: '{ "status": "0" }'
    command_on_template: '{ "status": "{{ (brightness * 3.90625) | round(0) }}" }'
    command_topic: /house/room1/led1/BN-SZ01/leds

Can you confirm that the MQTT topic you are using begins with /house and not /home as shown in the git page?

NOTE
There are many variables in the template schema and all must configured correctly to ensure it works properly with your device. In other words, there’s a very good change that what I have presented will fail to work and will need further modifications.


EDIT

I am not sure if the command to control the light requires the smooth_change key or will work without it. If it is necessary then the command_on_template must be changed to:

    command_on_template: '{ "status": "{{ (brightness * 3.90625) | round(0) }}", "smooth_change": "0" }'

Set to either 0 (disable “smooth change” effect) or 1 (enable “smooth change” effect).

Rather than using a brightness template why not use the brightness_scale option?

Though I admit I’ve never used it.

I don’t see brightness_scale listed as an available option for the Template schema. I guess the thinking is that if you have a brightness_template option you can use it to convert from one scale (0-1000) to another (0-256). Not as simple as having brightness_scale but it appears to be a constraint of the Template schema.

NOTE: the other schemas user uppercase ON and OFF to indicate state whereas the example in the Template schema indicates lowercase on and off. I’ve used that in the configuration above but I can’t help but wonder if it’s actually true or not.

1 Like