MiLight Status Feedback with NodeMCU!

Hi,

  1. Could you check the state topic for messages received from the gateway when sending/receiving commands? I think that the pub client used for the gatewy has a hard limit and the messages above a certain number of characters are split in two (the second message doesn’t include “state” thus it is flagged; “brightness” should be included in the first message, though)
    mosquitto_sub -h 192.168.x.y -d -t "milight/updates/#"

  2. In group state fields from the gateway you have status, hue, mode and bulb_mode fields that don’t have any correspondent for the light (refer to your settings above). Try to deselect these fields from the gateway’s MQTT fields.

  3. For the light settings in HA you have white_value and effect_list that have no (at least direct; although you can set it with an automation to forward messages from gateway’s state topic to appropriate topics in HA) correspondent.

@Petrica Thanks for the speedy reply!

So I have turned off the fields you suggested and this is the results from the mosquito_sub

docker exec -it MQTT mosquitto_sub -h localhost -v -d -t milight/updates/#
Client mosqsub/32-420207b90510 sending CONNECT
Client mosqsub/32-420207b90510 received CONNACK
Client mosqsub/32-420207b90510 sending SUBSCRIBE (Mid: 1, Topic: milight/updates/#, QoS: 0)
Client mosqsub/32-420207b90510 received SUBACK
Subscribed (mid: 1): 0
Client mosqsub/32-420207b90510 received PUBLISH (d0, q0, r0, m0, 'milight/updates/0x2DA5/rgbw/1', ... (11 bytes))
milight/updates/0x2DA5/rgbw/1 {"hue":141}
Client mosqsub/32-420207b90510 received PUBLISH (d0, q0, r0, m0, 'milight/updates/0x2DA5/rgbw/1', ... (11 bytes))
milight/updates/0x2DA5/rgbw/1 {"hue":141}
Client mosqsub/32-420207b90510 received PUBLISH (d0, q0, r0, m0, 'milight/updates/0x2DA5/rgbw/1', ... (15 bytes))
milight/updates/0x2DA5/rgbw/1 {"button_id":0}

Does that shed any light on it?

I do also have an automation to deal with white_mode but I have had it disabled

- id: Office_MiLight_Remote_sync 
  alias:  Office MiLight Remote sync
  initial_state: True
  trigger:
    - platform: mqtt
      topic: milight/commands/0x2DA5/rgbw/0
    - platform: mqtt
      topic: milight/updates/0x2DA5/rgbw/0
  condition:
    - condition: template
      value_template: >
        {{ trigger.topic.split('/')[4] == '0' }}
  action:
    - service: mqtt.publish
      data_template:
        topic: "milight/commands/0x2DA5/rgbw/1"
        payload_template: >
          {% if 'color_temp' in trigger.payload -%}
            {{ {"command":"set_white"} }}
          {%- else -%}
            {{ trigger.payload }}
          {%- endif %}        
    - service: mqtt.publish
      data_template:
        topic: "milight/commands/0x2DA5/rgbw/2"
        payload_template: >
          {% if 'color_temp' in trigger.payload -%}
            {{ {"command":"set_white"} }}
          {%- else -%}
            #{{ trigger.payload }}
          {%- endif %}        
    - service: mqtt.publish
      data_template:
        topic: "milight/commands/0x2DA5/rgbw/3"
        payload_template: >
          {% if 'color_temp' in trigger.payload -%}
            {{ {"command":"set_white"} }}
          {%- else -%}
            {{ trigger.payload }}
          {%- endif %}        
    - service: mqtt.publish
      data_template:
        topic: "milight/commands/0x2DA5/rgbw/4"
        payload_template: >
          {% if 'color_temp' in trigger.payload -%}
            {{ {"command":"set_white"} }}
          {%- else -%}
            {{ trigger.payload }}
          {%- endif %}        

I did try to use the following more concise example for the wiki but it results in an infinite loop which I really don’t understand

- alias: MiLight Forwarder
  initial_state: True
  trigger:
    platform: mqtt
    topic: milight/updates/0x1111/rgb_cct/+
  action:
    - service: mqtt.publish
      data_template:
        topic: "milight/commands/0x2222/rgb_cct/{{ trigger.topic.split('/')[4] }}"
        payload_template: >
          {{ trigger.payload }}

thanks

Actually you set the state_topic for the light to the delta_updates topic in the gateway’s settings. Delta only provides the change from previous state, not the full state updates (which is mqtt_state_topic_pattern).

See:

Ensure you have mqtt_state_topic_pattern set to milight/states/:hex_device_id/:device_type/:group_id on the gateway and then change state_topic: "milight/updates/0x2DA5/rgbw/0" to state_topic: "milight/states/0x2DA5/rgbw/0" in the HA light settings.

Try the above and will then check the automation part.

1 Like

You are amazing!! Thank you soooooooo much. I have been fighting with this for days. I don’t know how many time I must have misread that post you linked to. All working great now!

I can now go replicate this for the rest of my lights

You’re welcome :slight_smile:

Could I bother you with one more question?

So it now updates when you change colours and brightness but when I enter white mode it does not reflect that in HA. Would that be easy to add?

Thanks

I recommend to completely remove that automation that deals with white_mode because is going to give you only headaches:

  • first of all, the trigger for “milight/updates” topic which should be changed to “milight/states”;
  • even with the “milight/states” I think it’s going to run in a loop because it’s reacting first to command then to states changes and each time a command is send the states topic for the same light will react;
  • you already have color_temp property for both the light and the gateway so no need to do any transformation here (if color_temp is selected in the MQTT states group then it will be sent to HA light and it will work back and forth);
  • haven’t really looked into detail (so no authoritative answer here) but my guess is that the white_value for the HA light is the equivalent of saturation for the gateway. There is a total mess between the MQTT json light and the MiLight emulator settings as the gateway has sliders for brightness, color_temp and saturation (actually for FUT089/B8/B0 physical remote there is only one slider for both color_temp and saturation depending on white_mode/color_mode selection) however in HA there is only brightness, color_temp and white_value. The saturation is supported in HA as HS (Hue&Saturation) and it is modified by going from inward to outward and back on the colorwheel (thus not with a slider).

You can still use an input slider for saturation by mapping white_mode from HA light to saturation command topic. In order to have full duplex mode, another automation should forward saturation from gateway’s states topic to HA light white_mode.

thanks will have a play with it tonight and see what happens.

So if I disable the automation you are talking about then when I tell google to make the office lights white it does nothing.
With this automation it is all working, it just won’t update the gui when I make it white. But that is the case even if I use the gui to change it to white

- id: Office_MiLight_Remote_sync 
  alias:  Office MiLight Remote sync
  initial_state: True
  trigger:
    - platform: mqtt
      topic: milight/commands/0x2DA5/rgbw/0
    - platform: mqtt
      topic: milight/states/0x2DA5/rgbw/0
  condition:
    - condition: template
      value_template: >
        {{ trigger.topic.split('/')[4] == '0' }}
  action:
    - service: mqtt.publish
      data_template:
        topic: "milight/commands/0x2DA5/rgbw/1"
        payload_template: >
          {% if 'color_temp' in trigger.payload -%}
            {{ {"command":"set_white"} }}
          {%- else -%}
            {{ trigger.payload }}
          {%- endif %}        
    - service: mqtt.publish
      data_template:
        topic: "milight/commands/0x2DA5/rgbw/2"
        payload_template: >
          {% if 'color_temp' in trigger.payload -%}
            {{ {"command":"set_white"} }}
          {%- else -%}
            {{ trigger.payload }}
          {%- endif %}        
    - service: mqtt.publish
      data_template:
        topic: "milight/commands/0x2DA5/rgbw/3"
        payload_template: >
          {% if 'color_temp' in trigger.payload -%}
            {{ {"command":"set_white"} }}
          {%- else -%}
            {{ trigger.payload }}
          {%- endif %}        
    - service: mqtt.publish
      data_template:
        topic: "milight/commands/0x2DA5/rgbw/4"
        payload_template: >
          {% if 'color_temp' in trigger.payload -%}
            {{ {"command":"set_white"} }}
          {%- else -%}
            {{ trigger.payload }}
          {%- endif %}        

I think I will have to leave it for tonight as I am currently painting the kitchen and I need time to sit down and digest your reply.

Thanks sooo much for all your help its 95% working the way I want it

I think the problem resides with the fact that the bulbs you set are RGBW (not RGB+CCT) and GH cannot find a matching color. As far as I recall from my settings, for white light, HA sends color:{"r"=255,"g"=255,"b"=255} and no bulb_mode, but I need to double check that.

I don’t have remote access to my home and neither GH has (actually this might be a good thing :smile:) until Sunday as I screwed the router settings but will be able to help after resetting it.

with the milight rgbw you have 2 options for white light.

  1. color to 255,255,255 sets the rgb leds all on and that results in “white” light
  2. use effect white, which sets all rgb lights to off and set the white leds on

GH probably cant set effects, so to use the white leds an automation is needed to turn the white leds on.
thats why it works with the automation, and not without.

i noticed that when you set a light to an effect with an automation, the colorwheel doesnt show a state anymore, but the effect pull down in HA isnt updated. (and thats what @Unthred is missing)

Hello Petrica,

I believe you could have the answer for my question.

I set up 2 gateways to get the group 0 working (state updates) back in 2018 and you helped me out. (Discussion in this thread).

I haven’t touched the gateways after that.

What is the current situation of the gateway firmware? Do you still need 2 gateways with older firmwares to get the updates from group 0?

What is your solution? It would nice to have transitions which have been implemented now.

Thanks!

Hello @jussbba,

Chris has done a lot of improvements to the firmware in the last period (including async processing of MQTT payload, RF power control) and also changed the group 0 behavior so that the current firmware correctly reports the state of group 0. However, although you can update firmware to current version, I still recommend to keep both of them online (with only one having state reporting active) in order to have better coverage and reliability. Yes, it can work fine with only one gateway, but a two gateway system increases chances of packages reaching the lights.

That being said, I advise to get prepared also for re-flashing the firmware from the computer (over cable) as some of the changes in a previous version (I don’t recall which) broke the OTA update. It might have been an issue with my boards (WeMos D1 R2 for which the firmware is the one for WeMos D1 mini) but I had to manually re-flash the boards. Nothing was broken and I managed to get them easily back online. Also, you might want to keep a copy of your current settings for reference (even if there are more fields in the newer firmwares and you might not be able to flash the older settings).

Thank you for your reply!

Sounds good, I will sure keep the both gateways then!

Br. Jussbba

Hello,

I installed the newest firmware.

You still need the automation for group 0 right?

Like this:

  - alias: 'All Off  master'
    initial_state: True
    trigger:
      - platform: mqtt
        topic: 'milight/states/0x8224/cct/0'
    action:
      - service: mqtt.publish
        data_template:
          topic: 'milight/states/0x8224/cct/1'
          payload_template: '{{ trigger.payload }}'  
      - service: mqtt.publish
        data_template:
          topic: 'milight/states/0x8224/cct/2'
          payload_template: '{{ trigger.payload }}'
      - service: mqtt.publish
        data_template:
          topic: 'milight/states/0x8224/cct/3'
          payload_template: '{{ trigger.payload }}'  
      - service: mqtt.publish
        data_template:
          topic: 'milight/states/0x8224/cct/4'
          payload_template: '{{ trigger.payload }}'  
		  
		  
		  

What are your radio settings in milight hub? (Repeats, per loop, throttle etc.)?

Thank you!

Actually I didnt succeed in the installation.

I’m not sure how to erase the flash with nodemcy flasher. Do I need to flash the blank.bin file only for 0x00000?

The led above the reset button is flashing rapidly. What does that mean?

Thanks!

Hi,

AFAIK you still need it. In my opinion I think it is better to have it on the MQTT broker as it keeps the MCU free to receive commands (with rgb_cct profile there are 4 groups to push the same MQTT topic so there a bunch of payloads, however for FUT089 there are 8 groups which is a lot more to support on a such limited processor as ESP8266).

Here’s my settings file from the gateway. Needs to update mqtt section - server address, username, password, topics and LAN options and you can upload it to the gateway: Admin → Backup and restore modified settings; you can then change all in the interface.

`

settings.json

{“admin_username”:“”,“admin_password”:“”,“ce_pin”:16,“csn_pin”:15,“reset_pin”:0,“led_pin”:-2,“radio_interface_type”:“nRF24”,“packet_repeats”:50,“http_repeat_factor”:1,“auto_restart_period”:720,“mqtt_server”:“192.168.0.41”,“mqtt_username”:“”,“mqtt_password”:“”,“mqtt_topic_pattern”:“milight_gw/:device_id/:device_type/:group_id”,“mqtt_update_topic_pattern”:“milight_gw/update/:device_id/:device_type/:group_id”,“mqtt_state_topic_pattern”:“milight_gw/state/:device_id/:device_type/:group_id”,“mqtt_client_status_topic”:“”,“simple_mqtt_client_status”:true,“discovery_port”:48899,“listen_repeats”:3,“state_flush_interval”:3600,“mqtt_state_rate_limit”:500,“packet_repeat_throttle_sensitivity”:0,“packet_repeat_throttle_threshold”:200,“packet_repeat_minimum”:3,“enable_automatic_mode_switching”:false,“led_mode_wifi_config”:“Off”,“led_mode_wifi_failed”:“Flicker”,“led_mode_operating”:“Off”,“led_mode_packet”:“Off”,“led_mode_packet_count”:3,“hostname”:“milighthub2”,“rf24_power_level”:“MAX”,“rf24_listen_channel”:“MID”,“wifi_static_ip”:“192.168.0.112”,“wifi_static_ip_gateway”:“192.168.0.1”,“wifi_static_ip_netmask”:“255.255.255.0”,“packet_repeats_per_loop”:10,“home_assistant_discovery_prefix”:“”,“wifi_mode”:“n”,“default_transition_period”:500,“rf24_channels”:[“LOW”],“device_ids”:[2,3,4],“gateway_configs”:[[43690,5987,6],[5290,59999,6]],“group_state_fields”:[“state”,“brightness”,“color”,“mode”,“color_temp”,“bulb_mode”],“group_id_aliases”:{}}

`

I think that the board doesn’t have wifi connection.

Edit: I got it working now, I erased the flash with esptool.py and everything seems to be working now, I will test more tomorrow.

Btw, I am using 3 physical remote controller to control same bulbs. Do you know how I can send the payload to the main remotes topic (through automation for example) so that the states stays in sync?

I read something about that they go out of sync when using multiple remotes. (https://github.com/sidoh/esp8266_milight_hub/issues/198)

Thank you very much!

You could forward state topic from the secondary remote to the main; however, due to the specific of Milight packets (they only include a single command at a time - either state as on/off or color/color temperature/brightness/saturation change which can be either an advantage or a nightmare to work with, depending on scope), if using multiple remotes with one bulb, the state for each bulb is not updated.

Say you have remotes 1 (main, setup in HA) and 3 (secondary, doesn’t need to be set in HA) paired to the same bulb. When sending a command such as change color from remote 1 the state is properly updated in HA (something like {{“state”:“on”,“color”:{“r”:123,“g”:123,etc). However, if you previously used remote 3 to turn the light off, then the state for the remote 3 would be {{“state”:“off”}} which, if forwarded as such to state topic of remote 1, then the light would show as off in HA (although it is actually on).

The solution is to setup the update (delta) topic for the gateway and then forward payloads from the secondary remote update topic to the main remote command topic (might seem a little counter intuitive :slight_smile: ) - see below automation. The result of the packet being forwarded to command topic is that the light will receive another command (the one from the secondary remote) however it won’t result in a flicker because the physical state of lights is already set by the secondary remote. A similar approach can be used also to sync group 0 to groups 1 to 4 in the automation you posted.

The advantage of the fact that a single command at a time is sent (either state on/off or light color/temperature/brightness/saturation, but not both) mentioned above is that a command to change light properties is only applied to the lights that are on. Specific case is for controlling multiple groups of lights at the same time without any delay or if going from one room to another and want to change light properties without using a remote in each room. Although you can set an automation in HA to apply changes only to lights that are on (regardless of lights type, not only for Milight), this involves HA sending multiple commands (with more groups, more commands are sent and larger delay is incurred).

In my example remote 1 can be either physical or virtual but for remote 3 I would recommend a physical one (either for single groups - B0/FUT088 or multiple groups B4/FUT092 or B8/FUT089) in order to be able to turn the lights on/off even if HA or Milight emulator are not available (either as a result of wifi failures or due to power outages).

In fact, I think that configuring Milight with the emulator it can achieve a unique feature of being able to control the lights regardless of the hub/HA being online or not. This is because lights are directly paired with the remote and the emulator can either sniff on the commands sent by remote (and update the state in HA) or send separate commands as if coming from that remote (with Philips Hue you can also pair the bulbs directly to the remote however neither bulb or remote will be available to the hub anymore).

- alias: Bedroom lights forward
  initial_state: True
  trigger:
    - platform: mqtt
      topic: 'milight_gw/update/0x3/rgb_cct/1' #change (delta) topic of the secondary remote - NOT the state topic; can also be used for forwarding from group 0 to groups 1 to 4 of the same remote 
  action:
    - service: mqtt.publish
      data_template:
        topic: 'milight_gw/0x1/rgb_cct/2' #command topic of the main remote; or command topics of the groups 1 to 4 if forwarding from group 0
        payload_template: >
          {{ trigger.payload }}