Newbie having problems parsing RGB hex string

Hi all,

I’m pretty new to home assistant and Jinja. I have some stuff running in OpenHAB for quiet a bit and now I’m trying to move to home assistant. I use some actors I built myself. They are accessible via MQTT. One of them I’m currently struggling with is a RGB LED strip controller. To set (and get its current state) I have an RGB topic. The topic receives and transmits the pure RGB hex value, e.g. cc00ff.
I’m not using JSON as I’m kind of an old-school emebedded guy trying to keep everything simple and with as less overhead as possible, so I didn’t use JSON here. But actually with my zero-knowledge of Jinja I have no idea how to construct a template parsing (and sending) the RGB data to/from my actor.
Do I even have a chance to do so? Or should I consider changing my data format, which I’m not too comfortable with right now.

I would really appreciate any suggestions,
Manuel

Have the same problem. As sending non hex data to mys node will have a lag where light first switches green than blue than red channels. And with sending HEX data everything is done on the node. So any solution for this?

I’m not sure I have this problem the right way around, but if it’s hex to rgb - the api for Mote lights might be useful:

well in openhab i have this

import java.awt.Color

rule "Set bed_lgt_RGB value"
when
	Item bed_lgt_RGB changed
then
	val HSBType hsb = bed_lgt_RGB.state as HSBType
	var Color color = Color::getHSBColor(hsb.hue.floatValue / 360, hsb.saturation.floatValue / 100, hsb.brightness.floatValue / 100)
	val String rgb = String::format("%1$02x%2$02x%3$02x", color.red, color.green, color.blue)
	publish("mqt", "mygateway1-in/36/0/1/0/40", rgb)
end

So any ideas on how to send the RGB color in hex format? I can guess this can be done in template for RGB value but lack the knowledge and experience?

Al i need is a color wheel and publishing the hex falue for color like ffffff to an mqtt topic

1 Like

Hi

Did you ever solve this ?
I am using SONOFF-Tasmota and it needs RRBBGG in hex and also returns same

2 Likes

Sorry, but no. I’m currently still running openHAB 2 and didn’t finish my experiments with HA.

Well just adding arduino as mysensors node with the following code
https://forum.mysensors.org/topic/6765/rgb-led-strip

Works flawlessly

Hi All,

I managed to get this working by editing the mqtt.py as follows:

cd ./usr/local/lib/python3.5/dist-packages/homeassistant/components/light
sudo pico mqtt.py

found this line and changed it to:

mqtt.async_publish(self.hass, self._topic[CONF_RGB_COMMAND_TOPIC],‘{:02X}{:02X}{:02X}’.format(*kwargs[ATTR_RGB_COLOR]), self._qos,self._retain)

Then in the Light.yaml

  - platform: mqtt
    name: "Tempature Show Strip Light"
    state_topic: "stat/cap_sw/POWER2"
    command_topic: "cmnd/cap_sw/POWER2"
    brightness_state_topic: "stat/cap_sw/Scheme"
    brightness_command_topic: "cmnd/cap_sw/Scheme"
    rgb_state_topic: "stat/cap_sw/Color"
    rgb_command_topic: "cmnd/cap_sw/Color"
#    state_value_template: "{{ value_json.POWER2 }}"
    brightness_value_template: "{{ value_json.Dimmer }}"
    rgb_value_template: "{{ value_json.Color | join(',') }}"
    rgb_command_mode: hex
    brightness_scale: 100
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    optimistic: false

Note

This is not my code I found it in the HA repo as suggestion thanks to you,

Would it not be easier if the command mode could change, so Tasmota users can use hex and other intergrations use RGB.

1 Like

Agree 100% but how do we submit this as a feature request? I can confirm as well that the above change works.

rgb_command_template: "{{ ‘%02x%02x%02x’ | format(blue, green, red)}}"

Add that into your light mqtt config.

Finally the color picker works! HASS doesn’t get the value back and I am not fighting to get that value for now as now I can pick solid colors

I’m having issues using ninja mqtt cape and trying to setup ninja lights.

This is my lights.yaml:

- alias: Ninja Status
  platform: mqtt
  name: "Ninja Status"
  state_topic: "ninjaCape/input/999"
  rgb_state_topic: "ninjaCape/input/999"
  command_topic: "ninjaCape/output/999"
  rgb_command_topic: "ninjaCape/output/999"
  rgb_command_template: "{{ '%02x%02x%02x' | format(red, green, blue) }}"
  qos: 0
  #optimistic: false
  #retain: true
  payload_off: "000000"
  payload_on: "111111"

And logs complains with:

2017-09-25 09:42:05 ERROR (MainThread) [homeassistant.core] Error doing job: Exception in callback async_subscribe.<locals>.async_mqtt_topic_subscriber('ninjaCape/input/1007', b'F513FF', 0) at /srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/mqtt/__init__.py:213
Traceback (most recent call last):
  File "/usr/lib/python3.4/asyncio/events.py", line 120, in _run
    self._callback(*self._args)
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/mqtt/__init__.py", line 232, in async_mqtt_topic_subscriber
    hass.async_run_job(msg_callback, dp_topic, payload, dp_qos)
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/core.py", line 247, in async_run_job
    target(*args)
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/light/mqtt.py", line 252, in rgb_received
    templates[CONF_RGB](payload).split(',')]
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/light/mqtt.py", line 251, in <listcomp>
    self._rgb = [int(val) for val in
ValueError: invalid literal for int() with base 10: 'F513FF'

It’s latest home assistant and dependencies.

Any clue?

Thanks!

I don’t know anything about the ninjacape but I don’t think the command_topic and rgb_command_topic can be the same.

Here is my complete config that works…

platform: mqtt
name: “Goose-LED”
retain: true
state_topic: “stat/IOT-Ext-Goose/POWER”
command_topic: “cmnd/IOT-Ext-Goose/POWER”
rgb_command_template: “{{ ‘%02x%02x%02x’ | format(blue, green, red)}}”
rgb_state_topic: “stat/IOT-Ext-Goose/Color”
rgb_command_topic: “cmnd/IOT-Ext-Goose/Color”
effect_state_topic: “stat/IOT-Ext-Goose/Scheme”
effect_command_topic: “cmnd/IOT-Ext-Goose/Scheme”
state_value_template: “{{ value_json.state }}”
brightness_state_topic: “stat/IOT-Ext-Goose/Dimmer”
brightness_command_topic: “cmnd/IOT-Ext-Goose/Dimmer”
brightness_value_template: “{{ value_json.Dimmer }}”
brightness_scale: 80
effect_value_template: “{{ value_json.scheme }}”
effect_list:
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10

As far as I see you’re playing with Sonoff Tasmota by Theo Arendst. I have made a small modifications to standard MQTT light component to work with led strips on Tasmota. Here it is: https://github.com/ardeus-ua/ha-tasmota