MiLight Status Feedback with NodeMCU!

Has anyone managed to get Night Mode working within Home Assistant?

Hi @garmck,

There is no support in the light component for night mode however it can be easily done with a script (just use your own light id topic). Alternatively, the night mode could be setup as effect and then use an automation to forward it to the light (but is going to flicker when state changes). Do you need help for the setup?

- service: mqtt.publish
  data_template:
    topic: 'milight_gw/:device_id/:device_type/:group_id'
    payload_template: '{ "command": "night_mode" }'

@Petrica thanks, i managed to get it working with the following:

  - platform: mqtt_template
    name: Night Light
    state_topic: "milight/update/0x1/rgbw/1"
    command_topic: "milight/command/0x1/rgbw/1"
    command_off_template: '{"state":"off"}'
    command_on_template: '{"command":"night_mode"}'
    state_template: '{value_json.state}'

This works great, but the state_template is not working as I believe its expecting true/false.
I’ve tried:

    state_template: >
      {% if ('value_json.bulb_mode', 'night') %}
      true
      {% else %}
      false
      {% endif %}

But this doesn’t work, I’ll keep trying to figure out how to get bulb mode - night to be true.

Hello @Petrica,
After reading your I post just noticed the effect’s option in MQTT json, can you show me an example how to set it up?

After enabling effect and effect_list with a listed name for the effects I use automations to forward those entries to publish the right effect topic?

Hi @garmck

Night mode is stateless thus it won’t publish it’s updates. As you are only trying to turn night mode on and off, the easiest way would be with a switch:

- platform: mqtt
  name: "Night light"
  state_topic: "milight/update/0x1/rgbw/1"
  command_topic: "milight/command/0x1/rgbw/1"
  payload_on: '{"command":"night_mode"}'
  payload_off: '{"state":"off"}'
  optimistic: true
  qos: 0
  retain: true

I have my lights setup as mqtt_json (except that there are FUT103 and FUT014 with rgb_cct profile) and it allows for all sort of magical things :slight_smile:. However, when in night mode, the light shows as off thus, with automation I can include conditions that either light or the night mode (switch) for a specific light is on.

- platform: mqtt_json
  name: "Light 1"
  state_topic: 'milight_gw/state/0x1/rgb_cct/1'
  command_topic: 'milight_gw/0x1/rgb_cct/1'
  brightness: true
  rgb: true
  color_temp: true
  effect: true
  effect_list: [ 0, 1, 2, 3, 4, 5, 6, 7, 8]
  optimistic: false
  qos: 0
  xy: false
  retain: true
3 Likes

Hi @Sthope,

MiLight supports 9 effects however they are called “mode”. With the setup for the light included in the message above, the automation would be:
automation

- alias: Effect2Mode
  initial_state: True
  trigger:
    - platform: mqtt
      topic: 'milight_gw/+/rgb_cct/+'
  condition: 
    condition: template
    value_template: '{{ "effect" in trigger.payload }}'  
  action:
    - service: mqtt.publish
      data_template:
        topic: "milight_gw/{{ trigger.topic.split('/')[-3] }}/rgb_cct/{{ trigger.topic.split('/')[3] }}"
        payload_template: '{ "mode": {{ trigger.payload_json.effect }} }'

The topics used in the webpage are as following:

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

In automation/script the action part that calls for effects would be:

- service: light.turn_on
  data_template:
    entity_id: light.light_id
    effect: any from 0 to 9

I use scripts (3 categories, for day, sunset and night) that are called from automations as the scenes do not allow for templating. This way, lights can be turned on with dimming and either white light (color temp) or color leds (providing separate r, g and b values).

Color names can be used (any color from CSS Color Module Level 3) with “color_name: xx”. I’ve made an input list with all of them (however some would resolve to white light) and use the option color_name: ‘{{ states.input_select.scenes_color_name.state}}’

Random colors can be used with rgb_color: [‘{{range(1,255)|random}}’, ‘{{range(1,255)|random}}’, ‘{{range(1,255)|random}}’]

MiLight platform doesn’t support transition but can be easily done with a script:

3 Likes

Hello @Petrica and thank you so much!
Gonna set them up, a great addition to my lights :slight_smile:

I’s working like a champ!

@Petrica This is great thanks so much.
Interestingly, for me when the mqtt switch turns on night mode, the main lights shows as “on” which is helpful.

Also i’ve set a mqtt sensor which displays current bulb mode, (white, color, night, scene)

- platform: mqtt  
  state_topic: "milight/update/0x1/rgbw/1"  
  name: "Night Light"  
  value_template: '{{ value_json.bulb_mode }}' 

I will try and work out if this can be useful in anyway for state.

I have finally got a night light bulb set up using the below.

  - platform: mqtt_template
    name: "Night Light"
    state_topic: "milight/update/0x1/rgbw/1"
    command_topic: "milight/command/0x1/rgbw/1"
    command_off_template: '{"state":"off"}'
    command_on_template: '{"command":"night_mode"}'
    state_template: >
      {% if value_json.bulb_mode == 'night' %}
      on
      {% elif value_json.bulb_mode %}
      off
      {% endif %}
    brightness_template: '{{ value_json.brightness }}'

This now works via HASS and state updates when the remote has set light to night mode.

This looks interesting!

Is there a simple way making this to only small script for transition one light over a set period of time?

Hi,

It actually depends on what you need. The platform I mentioned (MiLight) doesn’t support transition so I had to reside to several scripts (which probably work on any light platform). If using Hue or Lifx then transition is supported natively.

You can find below an over simplified version of the scripts referred in my post above (going from brightness 0 to 255 with increments of 1 at 3 seconds so it will take about 13 minutes to complete; I removed all the intermediary steps). For personal use I’ve done a few modifications since that initial version (such as adding an input boolean for ensuring proper exit of the script at demand).

Or you can have a look at the flux platform if you need to have the light follow the circadian rhythm (doesn’t involve setting a new script).

wakeup_room:
  alias: 'Wakeup'
  sequence:
    - service: light.turn_on
      data_template:
        entity_id: light.light1
        brightness: 0
        rgb_color: [1,1,1]
    - delay: '00:00:03'
    - service: script.turn_on
      entity_id: script.wakeup_room_turnon
wakeup_room_turnon:
  alias: 'Wakeup turnon'
  sequence:
    - service: script.turn_off
      entity_id: script.wakeup_room_iterate
    - service: light.turn_on
      data_template:
        entity_id: light.light1
        brightness: '{{ states.light.light1.attributes.brightness | int + 1 }}'
        rgb_color: [1,1,1]
    - delay: '00:00:03'
    - service: script.turn_on
      entity_id: script.wakeup_room_iterate
wakeup_room_iterate:
  alias: 'Wakeup iterate'
  sequence:
    - service: script.turn_off
      entity_id: script.wakeup_room_turnon
    - service_template: >
        {% if states.light.light1.attributes.brightness|int < 255 %}
          script.turn_on
        {% else %}
          script.turn_off
        {% endif %}
      data:
        entity_id: script.wakeup_room_turnon

Hello,

How can I get the status updatet in home assistant (groups 1-4) when I press the master ON/OFF button on my remote?

  - name: "All lights"
    state_topic: "milight/states/0x2DA6/rgb_cct/0"
    command_topic: "milight/commands/0x2DA6/rgb_cct/0"
    <<: *MILIGHT_PARAMS    

This does not seem to work.

0x2DA6 is my remotes ID

Thank you in advance!

Hi @jussbba

This is intended behavior. Group 0 state updates over MQTT doesn’t work anymore on firmware higher than 1.7.x (I think that it is 1.7 although I might be mistaken; 1.6.x should be safe, though). However, if you send commands to group 0 from the gateway, the payload is sent; it is only group 0 not being updated anymore in the state topic.

As an workaround you can pair ALL the bulbs from groups 1-4 of the remote 0x2DA6 to ANY of the groups 1 to 4 from an arbitrary remote (let’s say 0x0001). There are some other stuff you can look in the github:

https://github.com/sidoh/esp8266_milight_hub/issues/318

Thank you very much!

However, this automation does not seem to work. Do you have any idea why?

  - alias: 'All Off'
    initial_state: True
    trigger:
      - platform: mqtt
        topic: 'milight/states/0x2DA6/rgb_cct/0'
    action:
      - service: mqtt.publish
        data_template:
          topic: 'milight/states/0x2DA6/rgb_cct/2'
          payload_template: '{{ trigger.payload }}'  
      - service: mqtt.publish
        data_template:
          topic: 'milight/states/0x2DA6/rgb_cct/3'
          payload_template: '{{ trigger.payload }}'  

Thanks!

If you still use the gateway with 1.8.x firmware then the automation posted won’t work and you need to downgrade.

The workaround mentioned above was in regard of:

  1. Keep firmware on 1.8.x

  2. Pair all the bulbs with an arbitrary ID (0x0001) and group 1

  3. Set a new light in HA (“all lights new” or something like that):

       - name: "All lights new"
         state_topic: "milight/states/0x0001/rgb_cct/1"
         command_topic: "milight/commands/0x0001/rgb_cct/1"
    
  4. Modify the trigger part accordingly:

topic: 'milight/states/0x0001/rgb_cct/1'

With the above setup you can still use the physical remote (I assume 0x2DA6 is a physical device ID) and control any of the groups (including group 0, that is groups 1-4 and 0) and HA will keep track of groups 1-4 on 0x2DA6 and groups 0-4 on 0x0001

If you downgrade gateway to 1.6.5 then there is no need for any change and automation posted should work (if it doesn’t then the cause is in other place such as topics in HA not being the same as the one used on the gateway).

Another workaround (this is what I recommend in order to improve reliability and range) is to have two gateways: one updated with the latest firmware and one on 1.6.5. Only the 1.6.5 gateway should post states update while both of them are to be connected to the command topic (simply delete state topic on the 1.8.x gateway).

Hello,

Thank you for your help Petrica!

However, it is not working perfectly. If I turn all the lights off from 0x0001 and then try to turn on one specific group from home assistant, lights turn on but the state goes back to off after 1 second in home assistant. I used MQTT Lens and it shows that it is only sending data to command and update topics but not to state topic. Any ideas?

  • alias: ‘All lights on/off’
    initial_state: True
    trigger:
    • platform: mqtt
      topic: ‘milight/states/0x2222/rgb_cct/1’
      action:
    • service: mqtt.publish
      data_template:
      topic: ‘milight/states/0x2DA6/rgb_cct/3’
      payload_template: ‘{{ trigger.payload }}’
    • service: mqtt.publish
      data_template:
      topic: ‘milight/states/0x2DA6/rgb_cct/2’
      payload_template: ‘{{ trigger.payload }}’

One more thing. For me, it would be important to turn all the lights off from physical remotes master button.

You said that “With the above setup you can still use the physical remote (I assume 0x2DA6 is a physical device ID) and control any of the groups (including group 0, that is groups 1-4 and 0)”
–> Does this mean that it is also doable? To turn all the groups off from remotes master button and get the status updated in home assistant.

Please include details in regard of the gateway:

  • what firmware is currently running;
  • if you’re able to setup another physical gateway;

The options are:
a) single gateway with older firmware (1.6.5) -> use the light with remote 0x2DA6, group 0 and automation as is; you will be able to use the physical remote to turn any group from 0 to 4 (thus also master) and it will be updated into HA; you need the automation that you posted which will forward state from remote 0x2DA6, group 0 to groups 1 to 4 in order to have HA in sync with the physical state of the bulbs;
b) single gateway with newer firmware (1.8.3) -> you cannot have group 0 updated into HA (unless the developer agrees to bring back this specific functionality which is unlikely :frowning: ); in this situation you are not able to update group 0 from the physical remote into HA so I will not post any workaround;
c) one gateway with 1.8.3 (going forward update this gateway to the latest firmware) and one with 1.6.5 (and keep it on this firmware) -> you will be able to use it as described at point a) with the advantage of having a wider area covered. Also, as sometimes packets from the gateway are lost (either due to defective NRF24L01 modules, or due to light bulbs being on different frequency), this is able to improve reliability of the entire MiLight network (when I first started with MiLight in HA I was using a single gateway and turning on/off the switch triggered an automation that sent the payload to bulbs three times in a row with 100 milliseconds delay however I’ve switched to a multi-gateway solution). The setup of the two gateways will be similar except the fact that the gateway with 1.8.x will have a blank field for state topic. You can use as many gateways as needed to cover a very large area but at least one with 1.6.5 to have the physical remotes synced back to HA.

You can setup another physical remote (either FUT092/FUT089 for handheld remote with multiple groups/FUT088 for single group remote or B4/B8 for wall mounted with multiple groups/B0 for single group) by pairing it with the existing bulbs (a bulb can be paired with a maximum of 4 remotes, either physical or virtual) and forward states from that remote to the one that you already have. FUT088 and B0 can be used to control lights in a room (similar to using group 0 on a multi group remote) which should be easier than the multigroup remotes to use for non-residents :smile:

Also, you can use MiLight remotes to control (through HA) other brands of light bulbs (such as Philips Hue) - that is not only send on/off but also color/saturation/brightness commands.

Thank you for your effort!

I have a spare nodemcu and nrf24l01 module so I might try to make another gateway with firmware 1.6.5. My current firmware is 1.8.3

If I have 2 gateways, does the master on/off switch turn on in home assistant even if only one group (1-4) is turned on?

Let’me know if I understood correctly your question: you have only one group from the remote (let’s say 2) and want to turn it on from HA then have the group 0 light topic update it with the same settings as the ones for group 2?

If this is the case, then you can define group 0 as a separate light in HA. In the automation above you would change group 2 to be in the trigger part and group 0 in the action (so it will forward state from 2 to 0).

I meant that if I have for example turned groups 1, 2 and 3 on seperately, and then want to turn all them off simultaneously from HA.

I guess the state of group 0 is not changed so I can not turn them off since the group 0 switch state is already off?

edit: you probably answered for that already: “If this is the case, then you can define group 0 as a separate light in HA. In the automation above you would change group 2 to be in the trigger part and group 0 in the action (so it will forward state from 2 to 0).”

Thanks!