Interesting idea. I never thought of using the segments like their own individual lights.
EDIT: Please forgive the quick jumps in thoughts. Was learning as I went…
You should make some template switches for each segment. This way, you can have the state check both the color and on/off position and correctly report on/off, as well as have the on command change the color of the segment.
If you wanted full control of each segment, you could make a template light.
This way you could control pretty much everything of each segment. In fact, I’ll get you started with the on/off command on a template light.
…
EDIT: Actually, we have a problem.
There is one main segment , Segment 0 by default. This segment has a few important differences to the rest of the segments.
- Color transitions only work on the main segment
- The main segment’s color is the one that will be reported to HTTP and MQTT APIs
There’s no way to get the status of these other segments. I just tried on mine by creating a few segments, but there were no state attributes or anything about those other segments.
But, the template light would work. Your template on/off command for each segment could just set the color/brightness for that segment via mqtt/rest/whatever api you’re using. No need to modify any code. As for keeping track of the state…you could just keep track manually. (If off command was sent, turn template light state off). You just can’t keep sync and it would be easy to get out of sync with the lights.
A bit more digging…
Wait, we’re back in action.
https://github.com/Aircoookie/WLED/wiki/HTTP-request-API#segments
https://github.com/Aircoookie/WLED/wiki/JSON-API
We can use a restful switch for the segments probably.
OK, lets just use some restful switches for each segment.
I turned on 2 segments and did a json/state request. Here is the output…
curl -X GET http://192.168.0.27/json/state
{
"on":true,
"bri":128,
"transition":7,
"ps":-1,
"pss":32895,
"pl":-1,
"ccnf":{
"min":1,
"max":5,
"time":12
},
"nl":{
"on":false,
"dur":60,
"fade":true,
"tbri":0
},
"udpn":{
"send":false,
"recv":true
},
"mainseg":0,
"seg":[
{
"id":0,
"start":0,
"stop":162,
"len":162,
"col":[
[
189,
116,
0,
252
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
]
],
"fx":0,
"sx":75,
"ix":128,
"pal":0,
"sel":true,
"rev":false
},
{
"id":1,
"start":150,
"stop":162,
"len":12,
"col":[
[
102,
153,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
0,
0
]
],
"fx":0,
"sx":75,
"ix":128,
"pal":0,
"sel":true,
"rev":false
}
]
}
There is only one on/off state and brightness for the main segment, BUT segment id 1 is reporting the color. So we can use that!
Frenck put in a feature request for having each segment report brightness. And it’s added to 0.9.2 milestone, so it could be there soon!
As for now, we’ll just change color to black/white. Once that feature is in, we’ll have way more control and dont have to just guess to which color to set it to for ‘on’.
color is an array of 3 or 4 bytes [R G B (W) ], for [primary, secondary, background]
switch:
- platform: rest
name: WLED Segment 1
resource: http://IP_ADDRESS/json/state
# Report this switch as on if primary color isn't [0,0,0]
# Add the white value here if you have RGBW.
# Also, we need to consider the main switch state as well.
# If that is off, we might not even get color attributes....
# Also, could use *this* sensor's name to auto pick the right segment
# as an enhancement rather than hard code this to segment 1 for example.
is_on_template: >
{% if is_state('light.wled', 'off') %}
False
{% else %}
{% set color = value_json.seg[1].col[0] %}
{{ not (color[0] == 0 and color[1] == 0 and color[2] == 0) }}
{% endif %}
headers:
Content-Type: application/json
# We can only set color, fx, sx...not much. So I guess we'll change them to black for off.
# As for 'on', not sure yet. Just hard coding 'white' here for now.
body_on: '{"seg[1]": [{"col": [[255,255,255]] } }'
body_off: '{"seg[1]": [{"col": [[0,0,0]] } }'
TL:DR: I would create some template switches to control each segment rather than modify any WLED code and use whatever means necessary to get the state info for that particular segment.