Help with MQTT integration and automation

I am using homeassitant venv and would have been trying to get an automation going. I need to send a message via MQTT on a topic with a “brightness” value between 0-100, on the homeassistant side I need to run an automation where it gets the brightness value from the incoming message and sets it on an actual dimmer switch. I’m using the MQTT integration that showed up under integration options and was able to set that up and was also able to set up a trigger and action. Only thing I haven’t been able to do is get the brightness value from the incoming message and use that within the Action. I am currently sourcing the message from Node Red with Payload set to {“brightness”:75}. On the homeautomation-Automation-Action if I set brightness_pct to a static value of (say) 90 (see attachment) then it works to set the brightness of the lamp whenever I send a MQTT message but I need to be able to use the variable value coming on the message.
hass_action

I’d try the MQTT light integration instead of writing automations for this.

1 Like

Thanks for the response but I am not trying to create a new device, I need to wire up an automation between a trigger condition and an existing dimmer where the dimmer value is published as a value through MQTT. Is this not possible?

Sorry, I don’t get it.
Can you please explain your use case a bit more. Why do you need to set the brightness of a light through an MQTT message?

Why do you need to set the brightness of a light through an MQTT message?

Why not :slight_smile:
Rationalization aside, I’m more interested in the technical side of this use case. Is this kind of automation possible or not?

Create a mqtt sensor that catches the value of your nodered message. In your automation, publish with a payload_template derived from the state of that sensor.

If you publish this payload:

{"brightness":75}

to this topic:

light/dragon

then this automation will set the brightness_pct of light.dragon_level to 75.

- alias: 'Set Dragon Brightness'
  trigger:
    platform: mqtt
    topic: light/dragon
  action:
    service: light.turn_on
    data_template:
      entity_id: light.dragon_level
      brightness_pct: "{{ trigger.payload_json.brightness }}"

This automation demonstrates the use of an MQTT Trigger to receive a payload published to a topic and the MQTT Trigger State Object to use the received payload.

2 Likes

Bingo! Works like a charm. I searched for hours and couldn’t find anything, thanks for the additional documentation links too.
Thanks.

1 Like

Check the two links in my post, the first for the MQTT Trigger and the second for how to use the payload received via an MQTT Trigger.

There’s a Cookbook of examples but it may not contain one for receiving and publishing MQTT payloads. Beyond that, you would have to search this community forum for examples. Of course, the challenge with that is you must know what search words to use and that’s a bit of a circular problem.

I have been messing around with homeassistant for about 24 hours so my knowledge is lacking somewhat and I’ve found these community forums to be very helpful in quick rampup when it gets hard to locate relevant documentation.
Anyway, while I have your attention I’m wondering if you can help me extend that example a bit with conditions (again, I’m using the simple example as a way to understand the supported patterns and syntax for a bigger project). For instance, I would like to set the brightness only if the incoming value is greater than 90 and this did not work for the action:

action:
  condition: template
    value_template: '{{ trigger.payload_json.brightness > 90 }}'
  service: light.turn_on
  data_template:
    entity_id: light.dragon_level
    brightness_pct: "{{ trigger.payload_json.brightness }}"

If there’s a way to get that to work I would also like to know how to add another condition under action so under action it should (1) set brightness to incoming setting if that value > 90 (2) set brightness to 0 otherwise. Note: I do want the condition to be part of the action and not use it as a filter prior to hitting the action part of the Automation.

I looked at some documentation on Conditions, MQTT Condition and related thread here but none of those helped.

Thanks.

You were very close to the correct answer. The action you created effectively contains two “steps”, first it checks the condition and if it evaluates to true it moves on to the next step which is to call the light.turn_on service. You need to indicate these “steps” like this (note the use of the hyphen and the indentation):

action:
  - condition: template
    value_template: '{{ trigger.payload_json.brightness > 90 }}'
  - service: light.turn_on
    data_template:
      entity_id: light.dragon_level
      brightness_pct: "{{ trigger.payload_json.brightness }}"

This is handled differently than in the first example. You use the template to determine which payload to publish. Be advised that it’s a bit unusual to turn off a light by calling light.turn_on with “brightness_pct: 0” (although I tried it and it did work).

Here is how to do what you requested using an “inline if”:

action:
  service: light.turn_on
  data_template:
    entity_id: light.dragon_level
    brightness_pct: "{{ trigger.payload_json.brightness if trigger.payload_json.brightness else 0 }}"

That template will publish the received brightness payload if the received brightness payload is greater than 90 else it will publish 0.

Another way to write the same thing is like this:

action:
  service: light.turn_on
  data_template:
    entity_id: light.dragon_level
    brightness_pct: >
      {{ trigger.payload_json.brightness if trigger.payload_json.brightness else 0 }}

The > represents a “line continuation character” which indicates the template begins on the next line (and can span multiples line). In addition, the outer quotation marks are not required when you use this style and the template must be indented.