Setting brightness by MQTT

Hello,

I would like to control a light with an incoming value (1-255) from an ESP8266.
I checked several examples, but I am still struggling with the automations.yaml.

The examples I found are quite different, for e.g. sometimes this is inlcuded
and sometimes not:

target:
 entity_id: xyz

As a beginner this is quite confusing. Therefore I am not sure, if my issue is “only”
for processing the value for brightness or it is something else…
Can someone help me, please?

So , the payload I receive from ESP is a value between 1-255, e.g. 87.

The publisehd topic is below.
This is working, I checked it with the Mosquitto of HA.

topic: /Bathroom/Light/BRT

Here is the automations.yaml. I tried several variations of it but this seems the most suitable.

- id: '1000003'
  alias: S Lampe BRT
  description: ''
  trigger:
  - platform: mqtt
    topic: /Bathroom/Light/BRT
  condition: []
  action:
  - service: light.turn_on
    data:
      brightness: "{{ value_json.brightness | from_json }}"
    device_id: 69c6babc1512d237ae2f374041751a0b
    entity_id: light.s
    domain: light
  mode: single

How do I set brightness correctly?

Many thanks in advance!

If the payload received from the ESP is in JSON format, for example, something like this:

{ "brightness": 87 }

then use the following template to get the brightness value:

{{ trigger.payload_json.brightness }}

Example

- id: '1000003'
  alias: S Lampe BRT
  description: ''
  trigger:
  - platform: mqtt
    topic: /Bathroom/Light/BRT
  condition: []
  action:
  - service: light.turn_on
    data:
      brightness: "{{ trigger.payload_json.brightness }}"
    target:
      entity_id: light.s
  mode: single

If the payload received from the ESP is simply an integer value, then use this template:

{{ trigger.payload }}

Reference: Automation Trigger Variables

1 Like

Hello Taras,

many thanks for your help, it is working!
Thank you also for the link to the Automation Trigger Values!

For the record, the payload is just a value between 1 and 255, e.g. 187.
No json format.

Do you think it is better to use json for the payload instead onyl the number?

In addition: the Philips Hue bulb is reacting very slowly, sometime they didn’t
react at all. Do you have here any suggestions as well?

Cheers,
Phil

1 Like

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.