MQTT to usable light entity (not autodiscoverred)

Hi lads,
I have a whole bunch of lights (even rollershutters etc) that are sending and receiving over mqtt

my state topic:

cloudapp/QBUSMQTTGW/UL1/UL4/state

with it’s value

{“id”:“UL4”,“properties”:{“value”:100},“type”:“event”}

the command topic:

cloudapp/QBUSMQTTGW/UL1/UL4/setState

with it’s value 100 equalling both brightness and 0 equaling off

{“id”:“UL4”,“type”:“state”,“properties”:{“value”:100}}

Can anyone help me with the right info for my yaml file?
i seem to be bumping against the json content

  - platform: mqtt
    schema: template
    name: "Bulb-white"
    command_topic: "cloudapp/QBUSMQTTGW/UL1/UL2/setState"
    state_topic: "cloudapp/QBUSMQTTGW/UL1/UL2/state"
    state_template: >-
          {% if is_state('{{value_json.properties.value}}', 'true') %}
            on
            {% else %}
            off
            {% endif %}
    command_on_template: '{"id": "UL2","type": "state","properties": {"value": true}}'
    command_off_template: '{"id": "UL2","type": "state","properties": {"value": false}}'

ok this works to toggle the light on and off,
but the state always goes to the or side of the logic,
is there a way to see what a variable / json has become so i know what value_json.properties.value is returning?

Based on the JSON example in your first post, try this version:

    state_template: "{{ 'on' if value_json.properties.value | int(0) > 0 else 'off' }}"

1 Like

i will do so directly,
my first example was one of a dimmer,
my second one i went more basic to try and get a normal relais light working give me a sec to test :slight_smile:

Made code that works for me and is readable:
first on is just on / of second one is a dimmer

  - platform: mqtt
    schema: template
    name: "Licht Kelder"
    command_topic: "cloudapp/QBUSMQTTGW/UL1/UL2/setState"
    state_topic: "cloudapp/QBUSMQTTGW/UL1/UL2/state"
    state_template: >-
          {% if value_json.properties.value == true %}
            on
            {% else %}
            off
            {% endif %}
    command_on_template: '{"id": "UL2","type": "state","properties": {"value": true}}'
    command_off_template: '{"id": "UL2","type": "state","properties": {"value": false}}'
   
    
  - 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 %}
            on
            {% else %}
            off
            {% endif %}
    
    command_on_template: >
      {%- if brightness is defined -%}
      {"id":"UL4","type":"state","properties":{"value":{{brightness | float | multiply(0.39215686) | round(0)}}}}
      {%- else -%}
      {"id":"UL4","type":"state","properties":{"value":100}}
      {%- endif -%}
      }
    brightness_template: "{{ value_json.properties.value | float | multiply(2.55) | round(0) }}"
    command_off_template: '{"id":"UL4","type":"state","properties":{"value":0}}'

I think you misunderstood the purpose of the Solution tag. It identifies the suggestion that resolves the problem, not the post that duplicates/re-packages/summarizes someone else’s suggestion.

It’s the custom of this community to assign it to the post that resolves the original problem. Your first post clearly shows a value key in the JSON payload with a numeric value. That’s what I addressed in my first post with a template that correctly evaluates the payload’s numeric value.

Your second post introduced a different problem which was ultimately solved for you by petro in a separate topic you created. Copying that solution into this one and then tagging your own post as the Solution is, to be diplomatic, not the suggested practice in this forum.

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

hi just to be clear, I made this topic since I had no idea how to make mqtt lights for my solution in a yaml file, the answer i checked as solution here is the actual solution to my problem.

the second topic made was on how to extract values from a json string, which was indeed solved by the other lad.

so if people now search for mqtt to light entity they’ll get a nice solution, and in the other topic the same goes :slight_smile:

That exactly how the Solution tag is not meant to be used.

It’s meant to identify the first post introducing the concept that fixes the issue. The post containing this information is most useful to other users because not everyone has the exact same situation.

If every user did what you’ve done here, all topics would conclude the same way where the topic’s author re-packages suggestions into their own post and crowns it the Solution. Effectively, users asking questions would appear to ultimately solve it themselves.