New MQTT format

No drama, just figured a good script would save a lot of time for everyone in the community.

I am facing a similar issue when I use the new format because the entity is gone after I use the new format.
The old format (is working):

binary_sensor RF_Sensor_Peter:
  - platform: mqtt
    name: "RF Sensor Peter"
    state_topic: "rflink/NewKaku-024726be"
    value_template: "{{ value_json.SWITCH1 }}"
    payload_on: "ON"
    payload_off: "OFF"
    qos: 1

switch RF_Switch_Peter:
  - platform: mqtt
    unique_id: house.rf.switch_peter
    name: RF SWitch Peter
    command_topic: "rflink/cmd"
    payload_on: "10;NewKaku;024726be;1;ON;"
    payload_off: "10;NewKaku;024726be;1;OFF;"
    state_on: "ON"
    state_off: "OFF"
    optimistic: false
    qos: 1
    retain: false

The new format (entities does not show up anymore, old one deleted):

mqtt:
  binary_sensor:
  - name: "RF Sensor Peter"
    state_topic: "rflink/NewKaku-024726be"
    value_template: "{{ value_json.SWITCH1 }}"
    payload_on: "ON"
    payload_off: "OFF"
    qos: 1

mqtt:
  switch:
  - unique_id: house.rf.switch_peter
    name: RF SWitch Peter
    command_topic: "rflink/cmd"
    payload_on: "10;NewKaku;024726be;1;ON;"
    payload_off: "10;NewKaku;024726be;1;OFF;"
    state_on: "ON"
    state_off: "OFF"
    optimistic: false
    qos: 1
    retain: false

What am I missing?
Thanks in advance.

But there is almost no time to save.

it would likely take you longer to download and then run the script as it would to make the minimal change needed.

People are making this way harder than it needs to be. Hence → drama.

Because you can use two “mqtt:” keys. you only use one and put both “switch:” and “binary_sensor:” under the same one.

It works the same as any other top level domain in HA. You can only have one “switch:”, “sensor:”, “script:”, etc. (unless you use packages but that’s completely different).

if you want more than one then you need to add stuff to the keys as you did in your original config.

switch RF_Switch_Peter:

is different than:

switch:

but it’s still a switch key.

That simple … only once mqtt: and everything listed under there … :hot_face:
Thanks!!!

1 Like

Hi
Same problem here. Nothing shows up. No errors in logs. No errors in configuration. And old format works correctly. I’m missing something or it’s still a work in progress.

5310

Your indentation is wrong.

mqtt:
  switch:
    - name: blah

You have

mqtt:
  switch:
  - name: blah

The entire config should look like:

mqtt:
  switch:
    - name: my switch 1

    - name: my switch 2

    - name: my switch 3

  binary_sensor:
    - name: my binary sensor 1

    - name: my binary sensor 2

    - name: my binary sensor 3

  sensor:
    - name: my sensor 1

    - name: my sensor 2

    - name: my sensor 3

etc

Don’t repeat the mqtt key.

mqtt:
  binary_sensor:
  - name: "RF Sensor Peter"
    state_topic: "rflink/NewKaku-024726be"
    value_template: "{{ value_json.SWITCH1 }}"
    payload_on: "ON"
    payload_off: "OFF"
    qos: 1
  switch:
  - unique_id: house.rf.switch_peter
    name: RF SWitch Peter
    command_topic: "rflink/cmd"
    payload_on: "10;NewKaku;024726be;1;ON;"
    payload_off: "10;NewKaku;024726be;1;OFF;"
    state_on: "ON"
    state_off: "OFF"
    optimistic: false
    qos: 1
    retain: false

That indentation is actually fine.

The only thing that really matters is that the indentation is consistent throughout the file.

You’ll need to give a bit more info than that for us to help you.

did you read the rest of the thread? Did you look at the docs for the new syntax?

you should post a large enough code snippet to show us what you tried.

Well, I put together a python script which converts to the new format, in case anyone is interested in using/improving it (could not figure out how to preserve comments).

Script:

import yaml

with open('configuration.yaml') as f:
    config = yaml.load(f, Loader=yaml.BaseLoader)

def mq_conv(type, config):
    config2 = {"mqtt":{type:[]}};
    for sensor in config[type]:
     if sensor['platform'] == "mqtt":
        del sensor['platform']
        config2["mqtt"][type].append(sensor)
    return config2


if __name__ == '__main__':
    print(yaml.dump(mq_conv('sensor',config)))
    print(yaml.dump(mq_conv('binary_sensor',config)))
    print(yaml.dump(mq_conv('switch',config)))
    print(yaml.dump(mq_conv('climate',config)))

Worked ok for my purposes, and applied to the OP’s yaml:

mqtt:
  sensor:
  - device_class: door
    name: Front Door
    payload_off: frontdoorclosed
    payload_on: frontdooropen
    qos: '0'
    state_topic: home/rfbridge/doorstate
  - device_class: door
    name: Kitchen Door Main
    payload_off: kitchendoorclosed
    payload_on: kitchendooropen
    qos: '0'
    state_topic: home/rfbridge/doorstate
  - device_class: door
    name: Kitchen Door L
    payload_off: kitchendoorlclosed
    payload_on: kitchendoorlopen
    qos: '0'
    state_topic: home/rfbridge/doorstate
  - device_class: window
    name: Kitchen Window L
    payload_off: kitchenwindowclosed
    payload_on: kitchenwindowopen
    qos: '0'
    state_topic: home/rfbridge/doorstate
  - device_class: window
    name: Kitchen Window R
    payload_off: kitchenwindowrclosed
    payload_on: kitchenwindowropen
    qos: '0'
    state_topic: home/rfbridge/doorstate
  - device_class: door
    name: Bedroom Door Left
    payload_off: bedroomdoorleftclosed
    payload_on: bedroomdoorleftopen
    qos: '0'
    state_topic: home/rfbridge/doorstate
  - device_class: door
    name: Bedroom Door Right
    payload_off: bedroomdoorrightclosed
    payload_on: bedroomdoorrightopen
    qos: '0'
    state_topic: home/rfbridge/doorstate
  - name: Fridge Door
    payload_off: fridgedoorclosed
    payload_on: fridgedooropen
    qos: '0'
    state_topic: home/rfbridge/doorstate

Cheers!

1 Like

Does the script handle !included files from configuration.yaml? Including sub-directories of the includes and packages?

Sorry, no. Its very simplistic :slight_smile:

then my opinion still stands that it’s easier to just take the literally 30 seconds it takes to make the change manually.

But good for you using it as a learning experience (if you did). :wink: