Templated MQTT intities to publish payload

hi guys,
I have a dimmer defined in my configuration.yaml

  - platform: mqtt
    schema: template
    name: "Licht Eetkamer"
    command_topic: "cloudapp/QBUSMQTTGW/UL1/UL4/setState"
    state_topic: "cloudapp/QBUSMQTTGW/UL1/UL4/state"
    state_template: >-
          {% if value_json.properties.value > 0 %}
            {% else %}
            {% endif %}
    command_on_template: >
      {%- if brightness is defined -%}
      {"id":"UL4","type":"state","properties":{"value":{{brightness | float | multiply(0.39215686) | round(0)}}}}
      {%- else -%}
      {%- endif -%}
    brightness_template: "{{ value_json.properties.value | float | multiply(2.55) | round(0) }}"
    command_off_template: '{"id":"UL4","type":"state","properties":{"value":0}}'

this works super well, but I bump against the restrictions of only having an entity and no object / area etc.

With the help of 123 taras in an other topic, he guided me well and gave me a solution for my ‘normal lights’
these just send true and false and on and off, so no real logic is required.

The dimmers as you can see above have some more logic bound to them as they provide a changeable brightness.

alias: Test MQTT dimmer
  - service: mqtt.publish
      topic: homeassistant/light/licht_eetkamer/config
      retain: true
      payload: >-
        {  "name": "Licht eetkamer", "state_topic":
        "cloudapp/QBUSMQTTGW/UL1/UL4/state", "command_topic": 
        "cloudapp/QBUSMQTTGW/UL1/UL4/setState" }
mode: single
icon: mdi:server-plus

thus my new payload needs to contain this logic, but since I’m a noob at programming i don’t know how i can provide this ‘new payload’ with my dimmer logic.

can i just start an if else statement in that payload? etc

    command_on_template: >
      {%- set brightness = brightness is defined | iif(brightness * | multiply(0.39215686) | round(0) | int, 100) %}
      {{ {"id":"UL4","type":"state","properties":{"value":brightness} } }}

Does this become part of the payload?

I don’t follow you, I just fixed your code. It should work with that fix

thx for the effort :slight_smile:
you made the code alot more efficient :smiley:

but the first block of code I posted is completely functional (well it fulfills my need anyway :slight_smile: )

I don’t want to use the first block no more, i want to do it with the autodiscovery topic which i started to write in the second block.
so my question is how to I integrated that command_on part into that :slight_smile:

this is the way i was able to get a normal relay up and running, and i want to do the same for a dimmer:

  alias: Test MQTT
  - service: mqtt.publish
      topic: homeassistant/light/licht_keukenvuur/config
      retain: true
      payload: '{ "name": "Licht keuken vuur", "state_topic": "cloudapp/QBUSMQTTGW/UL1/UL14/state",
        "command_topic": "cloudapp/QBUSMQTTGW/UL1/UL14/setState", "payload_on": "{\"id\":
        \"UL14\",\"type\": \"state\",\"properties\": {\"value\": true}}", "payload_off":
        "{\"id\": \"UL14\",\"type\": \"state\",\"properties\": {\"value\": false}}",
        "state_on": "{\"id\":\"UL44\",\"properties\":{\"value\":true},\"type\":\"event\"}",
        "state_off": "{\"id\":\"UL44\",\"properties\":{\"value\":false},\"type\":\"event\"}",
        "unique_id": "licht_keukenvuur", "device": {    "identifiers": ["test_lights"],    "name":
        "Licht keuken vuur",    "model": "RelaisModule1",    "manufacturer": "Qbus"
  mode: single
  icon: mdi:server-plus

I hope this makes my intentions more clear, sorry if i worded it wrongly / unclear!

They are not clear, you shouldn’t need that Test MQTT script as it’s just creating the config you already have above in yaml.

the test mqtt script creates device + entity does it not?
while the ‘old’ thing only creates an entity without a ‘connected’ device or area setting options (want to use them with new area card)

here is the difference:

first one is locked and doesnt give me options,
second one isn’t

Yes, but so does your MQTT configuration in yaml.

That’s because you aren’t providing a unique_id in your ‘old’ configuration.

1 Like

thx for the quick replies, could it be that unique id isn’t supported bij my ‘old’ config

unique_id is supported

ok thx, the unique id seems to allow me to assign it to an area,
it does however not seem to create a Device :slight_smile: ,
but I will call this low impact great succes :slight_smile: since my goal for now was the area assignment