Set the color of a Hue (or RGB bulb) using Color names? Possible?

I’ve been reading over the posts off an on for a while, and I think the answer is no, but is it possible to set the color of a hue bulb(s) using natural language? I was able to do this with some other Home automation systems, and Amazon (not sure about google), echo, allows natural language color settings. Like Alexa set living room lamp to green, etc.

Again, I think the answer is no. I think you have to add the colors, hue, etc directly in your automation, script, or scene, which is fine.

I suspect the Alexa app, and proably the other Home Automation systems, probably just have a long list of RGB colors tied to a “template” to match the word with the values. Thought maybe someone has done something like that?

Really just asking, Seems like it might be nice, but maybe its just overkill and not necessary.

The light component does accept color_name as a parameter to its turn_on service, which it will convert to hs_color before calling the platform’s turn on method. So, yes, this should be possible. See light.turn_on service.

Awesome, totally missed that. Thanks!!

Gonna play with this tonight. I’m sure its just a bit off, but probably pretty close.

- alias: Notify based on traffic density
  trigger:
  - platform: state
    entity_id: sensor.me_traffic_density_to_work
  condition:
  - condition: state
    entity_id: input_select.house_mode
    state: Home
  - condition: or
    conditions:
      - condition: time
	after: '07:35:00'
      - condition: time
        before: '08:35:00'
  action:
  - service: light.turn_on
    data_template:
      entity_id: light.alcove_light
      brightness_pct: 100
	  color_name: >
	    {% if is_state("sensor.me_traffic_density_to_work", "Normal") %} {"color_name": "green"}}
	    {%-elif is_state("sensor.me_traffic_density_to_work", "Moderate") %} {"color_name": "orange"}}
	    {%-elif is_state("sensor.me_traffic_density_to_work", "Heavy") %} {"color_name": "red"}}
	    {%-elif is_state("sensor.me_traffic_density_to_work", "unknown") %} {"color_name": {"white}}
	  {% endif %}


      me_traffic_density_to_work:
        friendly_name: Traffic to work
        value_template: >
          {% if states("sensor.time_to_work") | int >= 30 %}
            Heavy
          {% elif states("sensor.time_to_work") | int < 30 and states("sensor.time_to_work") | int >= 25 %}
            Moderate
          {% elif states("sensor.time_to_work") | int < 25 and states("sensor.time_to_work") | int >= 10 %}
            Normal
          {% else %}
            unknown
          {% endif %}

Sensor get’s its value from either waze or google traffic calculator,

Some quick feedback:

  - condition: or
    conditions:
      - condition: time
        after: '07:35:00'
      - condition: time
        before: '08:35:00'

I assume you want this to allow the actions to run only between 7:35 and 8:35. If so, you can do that like this:

  - condition: time
    after: '07:35:00'
    before: '08:35:00'

(FWIW, the way you had it it would be true after 7:35 or before 8:35, which would be true all day. :slight_smile: )

And this:

  - service: light.turn_on
    data_template:
      entity_id: light.alcove_light
      brightness_pct: 100
      color_name: >
        {% if is_state("sensor.me_traffic_density_to_work", "Normal") %} {"color_name": "green"}}
        {%-elif is_state("sensor.me_traffic_density_to_work", "Moderate") %} {"color_name": "orange"}}
        {%-elif is_state("sensor.me_traffic_density_to_work", "Heavy") %} {"color_name": "red"}}
        {%-elif is_state("sensor.me_traffic_density_to_work", "unknown") %} {"color_name": {"white}}
        {% endif %}

You make the same mistake a lot of people seem to make. When you’re using a template for a parameter, never use an if without an else. In this (and a lot of cases I see) the last elif is useless - just make it an else. Or, add an else with an appropriate value/statement.

But this can probably be “simplified” anyway like this:

  - service: light.turn_on
    data_template:
      entity_id: light.alcove_light
      brightness_pct: 100
      color_name: >
        {{ {'Normal': 'green', 'Moderate': 'orange',
            'Heavy': 'red', 'unknown': 'white'
           }[states('sensor.me_traffic_density_to_work')] }}

Or if you want to be more robust:

  - service: light.turn_on
    data_template:
      entity_id: light.alcove_light
      brightness_pct: 100
      color_name: >
        {% set map = {'Normal': 'green', 'Moderate': 'orange', 'Heavy': 'red'} %}
        {% set state = states('sensor.me_traffic_density_to_work') %}
        {{ map[state] if state in map else 'white' }}

Ok, this might not be that much simpler, but it’s certainly easier to extend by just changing the map/dictionary.

Good call. I was copying from another automation that I run later in the evening (crossing midnight) until the morning.

  - condition: or
    conditions:
      - condition: time
        before: '05:00:00'
      - condition: time
         after: '22:30:00'

And this is great. Thanks for the tips on the else/if. I always seem to struggle a but with those, but your option is much smoother.

Thanks!!

Edit: I did just realize that this wont trigger unless there is a state change. 4 out of 5 times, the value would be normal before and after 7:35. I think I might need to set up a boolean as a trigger instead and have that turn on at 7:35. I think I did that a while back with a different sensor. Let me see what I can come up wth, thanks again for the amazing help with these.

Edit: 2 I think instead I’ll just add a light.turn_on for this bulb to my morning routine. Then this can change the colors as the state changes for traffic density

That can be done this way, too:

  - condition: time
    after: '22:30:00'
    before: '05:00:00'

The time condition code is specifically written to handle this scenario.

1 Like

It’s a common pattern to take all the conditions and then create triggers for each. So you might do this:

- alias: Notify based on traffic density
  trigger:
  - platform: state
    entity_id: sensor.me_traffic_density_to_work
  - platform: state
    entity_id: input_select.house_mode
    to: Home
  - platform: time
    at: '07:35:00'
  condition:
  - condition: state
    entity_id: input_select.house_mode
    state: Home
  - condition: time
    after: '07:35:00'
    before: '08:35:00'
  action:
    ...

So the condition part allows the actions to run when all the conditions are true. The trigger part tries to run the actions when one of the conditions becomes true.

1 Like

Perfect, much simplier.

here is the “final”, only change is I changed one of the triggers to be from: "night’, that’s when my good morning routine runs. But home could trigger in at other times when people come and go. i realize it still would not run because of the condtions, but the night trigger only happens once a day, so it would run just a few times less per day unnecessarily. At least that seems to make sense to me.

- alias: Notify based on traffic density
  trigger:
  - platform: state
    entity_id: input_select.house_mode
    from: 'Night'
  - platform: state
    entity_id: sensor.me_traffic_density_to_work
  - platform: time
    at: '07:35:00'
  condition:
  - condition: state
    entity_id: input_select.house_mode
    state: Home
  - condition: time
    after: '07:35:00'
    before: '08:35:00'
  action:
  - service: light.turn_on
    data_template:
      entity_id: light.alcove_light
      brightness_pct: 100
      color_name: >
        {% set map = {'Normal': 'green', 'Moderate': 'orange', 'Heavy': 'red'} %}
        {% set state = states('sensor.me_traffic_density_to_work') %}
        {{ map[state] if state in map else 'white' }}

Again, thank you very much for the help!

Something slightly off, I cannot find, ugg

2018-08-16 17:41:51 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: invalid template (TemplateSyntaxError: unexpected char “’” at 174) for dictionary value @ data[‘action’][0][‘data_template’][‘message’]. Got None. (See /home/homeassistant/.homeassistant/configuration.yaml, line 287). Please check the docs at https://home-assistant.io/components/automation/

I removed the whole map color section and still got the error, so I know its somethig else. Anyone see what I’m missing?

Doesn’t look like the error is from this automation. Notice it says the problem is in action: data_template: message:. Looks like you have a curly quote instead of a regular single quote '.

Youre fast. It was another “simplier automation” I found that must have a typo. :slight_smile:
Thanks

Dont see the wrong quotes. hmm

  - service: notify.pushbullet_notifications
    data_template:
      message: >
          {% set unlocked = states | selectattr('entity_id', 'in', state_attr('lock.all_locks','entity_id')) | selectattr('state','eq','unlocked') | map(attribute='name') | join(', ') '%}
          The following locks are unlocked: {{ unlocked }}

Well, you have an extra quote at the end that probably shouldn’t be there: | join(', ') '%}.

1 Like

do you catch the situation no locks are unlocked in the condition? otherwise this message would be empty?

Actually I stole this from @petro eariler today. The trigger is a binary sensor. I modified it for me to run after everyone has left and alert only of the sensor is on.

It’s something I had been meaning to figure out myself. Sorry I just realized I hijacvked my own thread. haah

BTW, follow up, the RGB based on traffic density worked perfect this morning, turned on at 7:35, and even changed colors from greeen to orange and back a couple of times while I was getting ready for work! Thanks @pnbruckner

2 Likes