How would I communicate with MLSC's API?

I am using MLSC to control some LED strips and it’s a lot of fun. It has a web UI, but I would like to communicate with the API to be able to use a custom light entity card on my HA dashboard. I have never done anything like this before so I’m wondering if someone could just help get me started and I could take it from there. I’m guessing this would involve a template light. Here is a quick example from the API documentation:

If anyone would be willing to chime in to help get me started I’d be very appreciative. …even if just to send me some links to other threads or websites that I could learn from/mimic.

This might be more useful. Here is a POST I found in the Discord channel.

{
    "device": "device_0",
    "effect": "effect_single",
    "settings": {
        "color": "red",
        "custom_color": [
            0,
            0,
            10
        ],
        "use_custom_color": true,
        "white": 0
    }
}

How could I send a POST command like this via HA …and use a color wheel in Lovelace?

Can you point to the documentation for the API for MLSC?

This is the only reference I can find in the docs, which makes it look as though I have to install it to look at the docs.

I really appreciate the reply, Nick. I’ll see what I can do. The API documentation is an interactive webpage hosted by the local MLSC server on my network. It isn’t a straightforward doc.

All I can think of is to stitch together screenshots. As you’ll probably notice, after I click on each URL, it creates an expanding window of content, in which there is the ability to enter the parameters for a POST and simply and “Execute” button for the GET. …and the response is below that.

I’m guessing any further attempt to explain what I see is unnecessary, as you probably know a lot about this sort of thing already.

I have exported the config.json file and could provide it if you’d like. I don’t know how to include it in a post on this forum though. And I don’t know if it would help.

I tried to install mlsc yesterday but ran into problems as the pi in question had too old os.

I’m sure it can be made to work.

What integration do you suggest I try to use? Template light?

Yes I was thinking rest coupled with putting it all together with template light.

I managed to update my pi and then successfully install MLSC. I see how the API is documented. I think that section is via Swagger, which I have heard of, but not used much in practice. Further work required :slight_smile:

I think the place to start is getting a few basic commands going with the REST integration. RESTful - Home Assistant

Thanks Nick! I really appreciate the replies.

I’ll sniff around the community a bit and see if I can get a rudimentary understanding of REST.

The swagger ui shows exactly what url to send. It’s kinda hard to explain, I’m sure someone will have done a youtube video of swagger in action.

I think RESTful Command is the integration I should try to use.

I know from the Discord server that the following payload should work.

{
  "device": "device_0",
  "effect": "effect_single",
  "settings": {
    "color": "blue",
    "custom_color": [
      0,
      0,
      255
    ],
    "use_custom_color": false,
    "white": 0
  }
}

 

So, I have added the following to my config as a preliminary test. It’s not working. Surely my YAML is not complete/correct. Any pointers?

rest_command:
  mlsc_post_effect:
    url: "http://192.168.0.105:8080/api/effect/active"
    method: POST
    payload: '{
  "device": "device_0",
  "effect": "effect_single"
  "settings": {
    "color": "blue",
    "custom_color": [
      0,
      0,
      255,
    ],
    "use_custom_color": false,
    "white": 0
  }
}'
    content_type: "application/json"

What do your logs say?

Either I’m looking in the wrong place or the Logbook doesn’t show any info pertaining to the command.

I think it’s working. I’m offsite (using VPN) but I’m sending POST commands through HA and checking the outcome with GET on the API documentation page and I think this works! Needed to fix the URL and add a comma before “settings”.

  mlsc_post_effect:
    url: 'http://192.168.0.105:8080/api/effect/active?'
    method: POST
    payload: '{
  "device": "device_0",
  "effect": "effect_single",
  "settings": {
    "color": "blue",
    "custom_color": [
      0,
      0,
      255
    ],
    "use_custom_color": false,
    "white": 0
  }
}'
    content_type: "application/json"

You’ll also see the changes reflected in the MLSC UI if it is successful.

Yes, they’re there as well. I’m not sure how to turn this into a template light but I’m one step closer. And here is a sample REST command for device settings (for device_0). All I want to be able to change is the brightness, but it seems all the parameters must be included in order for the API to accept the command.

  mlsc_post_brightness:
    url: 'http://192.168.0.105:8080/api/settings/device?'
    method: POST
    payload: '{
  "device": "device_0",
  "settings": {
    "device_name": "Bed",
    "fps": 100,
    "led_brightness": "70",
    "led_count": "380",
    "led_mid": "190",
    "led_strip": "ws2811_strip_rgb",
    "output_type": "output_raspi"
  }
}'
    content_type: "application/json"

Just in case others are interested, I’ve successfully created REST commands for controlling MLSC via HA. I haven’t figured out a way to incorporate a color wheel, but I can select among various predefined effects and colors for which I have created distinct REST commands in my HA config. Here are some examples. Note that changing the color(s) of a given effect requires a POST to a URL that is not the same as the URL for the activation of the effect. That is to say, you cannot send a command for “turn on effect_single” and “red” at the same time; there is a URL for editing effects and a separate URL for activating effects. Also, if you check your MLSC config file (downloadable from the API page) you will see what each LED strip device is called. They all follow a naming convention of “device_#” (device_0, device_1, etc.)

This command changes the color of the single effect to red for device_0:

rest_command:

  mlsc_device_0_edit_single_red:
    url: 'http://192.168.0.105:8080/api/settings/effect?'
    method: POST
    payload: '{
  "device": "device_0",
  "effect": "effect_single",
  "settings": {
    "color": "red",
    "custom_color": [
      255,
      0,
      0
    ],
    "use_custom_color": false,
    "white": 0
  }
}'
    content_type: "application/json"

Then, to activate the single effect on device_0:

  mlsc_device_0_post_single:
    url: 'http://192.168.0.105:8080/api/effect/active?'
    method: POST
    payload: '{
  "device": "device_0",
  "effect": "effect_single"
}'
    content_type: "application/json"

Here, to activate the advanced scroll effect on device_1

  mlsc_device_1_post_advanced_scroll:
    url: 'http://192.168.0.105:8080/api/effect/active?'
    method: POST
    payload: '{
  "device": "device_1",
  "effect": "effect_advanced_scroll"
}'
    content_type: "application/json"

…and so on. Based on these REST commands you should be able to create any further commands you would need, using the MLSC API documentation (available at the bottom of the MLSC web UI when you login on port 8080).