Automation condition - hue color bulb is NOT a particular color

Hi, fairly new to Home Assistant here. Working on migrating from a decade long Homeseer environment.

One of the common tasks I use on HS is the ability to check that a hue bulb is NOT some particular values - i.e. if a particular scene is active then don’t do this other action.

Looking around it seems like the only way to check if a state value is NOT some value is to use a template, which seems complicated to me (for now as a newbie to HA).

So in a condition, I’m trying to just check these values from a hue bulb:

condition: state
entity_id: light.kitchen_island_center
attribute: brightness
state != '15'
attribute: hs_color
state != [ 120.472, 49.804 ]

If those are both true (not sure how to AND those conditions), then call a scene. I know my yaml syntax is probably wrong but hopefully the idea gets across.

Any suggestions on how to do this?

Thanks!

condition: not
conditions:
  - condition: state
    entity_id: light.kitchen_island_center
    state: '15'
    attribute: brightness

Here’s the template that can do that:

{{ state_attr('light.kitchen_island_center', 'brightness') != 15 and state_attr('light.kitchen_island_center', 'hs_color') != [ 120.472, 49.804 ] }}

However, whether you use the template directly in a Template Condition, or to create a Template Binary Sensor like CO_4X4 suggested, the attributes you are interested in only exist when the light is on (go to Developer Tools > States and look at the light’s attributes to confirm they fail to exist when off).

That means the template, in its current form, will report a false positive when the light is off. It’s brightness and hs_color won’t exist and so their values are null which won’t match the values you specified and so the template will report a false positive (true).

Thanks for the quick replies here!

rossk, would you be able to provide the additional syntax for more than just the brightness state? Here’s my guess:

condition: not
conditions:
  - condition: state
    entity_id: light.kitchen_island_center
    state: '15'
    attribute: brightness
condition: and  <-- I know this line can't be right...
condition: not
conditions:
  - condition: state
    entity_id: light.kitchen_island_center
    state: [ 120.472, 49.804 ]
    attribute: hs_color

Conditions are ‘and’ by default so simply listing them under the ‘not’ should equate to what you are looking for, I believe:

OK, it’s not quite working. Here’s the basic workflow and the automation script:

  1. triggered by button on remote
  2. run scene family room normal
  3. trigger automation called tv daily lights
  4. Now the conditional stuff:
    a. check the kitchen island center hue is brightness 15 and color_hs are those values
    b. TRUE: do nothing
    c. FALSE: run the kitchen island normal scene
trigger:
  - platform: event
    event_type: '' (TBD here)
condition: []
action:
  - service: automation.trigger
    target:
      entity_id: automation.family_room_tv_lights_on_daily_color
  - scene: scene.family_room_normal_scene
  # run above every time, conditional stuff below
  - condition: not
    conditions: null  # I don't know why this gets added but it's automatic
  - condition: state
    entity_id: light.kitchen_island_center
    state: '15'
    attribute: brightness
  - condition: state
    entity_id: light.kitchen_island_center
    state:
      - 120.472
      - 49.804
    attribute: hs_color
  - scene: scene.kitchen_island_normal_scene
mode: single

Is the null thing my problem? I think I tried removing it but it won’t pass pre-checks.

Triggering automations with the automation.trigger service call is not a typical use-case (call a script containing the actions you require).

An inline condition with action is fine; if the condition isn’t met the execution sequence halts. However, an inline condition must be handled as a single condition. You appear to have multiple conditions (unlike what rossk suggested) so the execution sequence halts at the first condition to fail (one of the reasons I provided you with a compact Template Condition).

You didn’t mention how you want that inline condition to behave if light.kitchen_island_center is off (because when off it doesn’t have the attributes your condition is checking).

Re: not using automation.trigger – I got this directly from the docs at: Automation Services - Home Assistant

Is there any reason why it’s a bad idea to use it this way? The automation it’s triggering is a complicated file that turns on lights and colors them depending on what day it is, so I’d really like to keep it intact instead of copying it into a script and calling that (although I have no idea how that’s done).

Oh, and regarding the situation where it’s off – I want it to set the scene just like it would do if the hue values do not match the specified values. In other words, even if the bulbs are off, trigger the “island normal” scene to turn them on.

The main reason for me that this is not a good idea is the fact that manually triggering an automation like this will ignore any conditions set in the automation and will simply carry out the actions sections, therefore basically making it a script. There is no technical reason why you can’t do this if you use that automation elsewhere and it has other triggers and or conditions - however if not you should really just cut and paste it in to a script.

The purpose of an automation is to perform a set of actions under the right circumstances (defined by its trigger and conditions). The automation.trigger service call short-circuits the “under the right circumstances” part. The automation’s trigger and condition are skipped and only its action is executed.

A better design pattern is to move the automation’s actions to a script and then the automation simply calls the script; the script can also be called by other automations. In other words, the actions can be common to any number of automations.

OK, so how do you intend to do that if you use an inline condition that you have designed to work only when the light is on?

Consider using a Template Condition like this:

  - scene: scene.family_room_normal_scene
  - condition: template
    value_template: >
      {% set light = light.kitchen_island_center %}
      {{ is_state(light, 'off') or
          (state_attr(light, 'brightness') != 15 and
            state_attr(light, 'hs_color') != [120.472, 49.804]) }}
  - scene: scene.kitchen_island_normal_scene

Wow, I’m surprised that I can’t seem to find any info on how to start using templating. The only stuff I can find are example templates, but I need some template 101 basics on how to get started. I’m willing to try it out but can only find a 30 minute video on how to get started.

I’m going to keep looking. So far the only place I can find templating in the HA web interface is under development tools, but I think that’s just for testing templates, not creating them.

Guesses:

  1. Create a directory under config/ called templates
  2. Create a file called templates.xxx - I don’t think they’re yaml files
  3. Add this as part of an automation somewhere (?)

OK, here’s my attempt and it’s making more sense to me now :slight_smile:

action:
  - service: automation.trigger
    target:
      entity_id: automation.family_room_tv_lights_on_daily_color
    data:
      skip_condition: true
  - scene: scene.family_room_normal_scene
  - condition: template
    value_template: >
      {% set light = light.kitchen_island_center %}
      {{ is_state(light, 'off') or
          (state_attr(light, 'brightness') != 15 and
            state_attr(light, 'hs_color') != [120.472, 49.804]) }}
  - scene: scene.kitchen_island_normal_scene
mode: single

Unfortunately it still doesn’t work so I’m still digging into it :slight_smile:
I assume when I “Run Actions” from the automation editor it’ll still consider all the conditions rather than just ignore and run all actions…

Thanks for the suggestion on the script, I was able to do my first script by moving the action section (many lines of yaml) into a script, and then using the call service to run it.

Both work: 1) my other automation that calls the script based on remote button presses, and 2) calling it from this automation.

So thanks for that suggestion!

alias: 'Family room: Normal scene'
description: Button F on the mini remotes
trigger:
  - platform: event
    event_type: insteon.button_on
    event_data:
      address: 4fbd1c
      button: f
condition: []
action:
  - service: script.family_room_tv_lights_on_daily_color
  - scene: scene.family_room_normal_scene
  - condition: template
    value_template: |
      {% set light = light.kitchen_island_center %} {{ is_state(light, 'off') or
          (state_attr(light, 'brightness') != 15 and
            state_attr(light, 'hs_color') != [120.472, 49.804]) }}
  - scene: scene.kitchen_island_normal_scene
mode: single

By the way, I’m still debugging. Now I’m really confused as this simple test doesn’t work either:

action:
  - service: script.family_room_tv_lights_on_daily_color
  - scene: scene.family_room_normal_scene
  - condition: template
    value_template: |
      {% set light = light.kitchen_island_center %} {{ is_state(light, 'off') }}
  - scene: scene.kitchen_island_normal_scene

I’m just checking to see if the light is off and it still fires the kitchen normal scene, so there’s something really wrong here. Maybe it has something to do with the Philips Hue integration. Oh and yes, the kitchen island center bulb is definitely on.

Now it’s getting frustrating… this doesn’t work either!

action:
  - service: script.family_room_tv_lights_on_daily_color
  - scene: scene.family_room_normal_scene
  - condition: state
    entity_id: light.kitchen_island_center
    state: 'off'
  - scene: scene.kitchen_island_normal_scene
mode: single

My scene.family_room_normal_scene included the kitchen island lights, so just calling that caused them to change colors, effectively overriding anything else.

Removing them from the scene made everything work properly :slight_smile:

Thanks to everyone for helping me jump into both templates and scripting for this automation! It’s all working using both and now I’m expanding into new realms here.

I’m a long time Homeseer user and am progressing through HA as I migrate away from HS.

Dennis

This automation is turning out to be a bit of a headache. Well this doesn’t seem to work for the instance where the light is off and this is triggered. The expected behavior is that it would set the kitchen island normal scene, but it’s not triggering that.

action:
  - service: script.family_room_tv_lights_on_daily_color
  - scene: scene.family_room_normal_scene
  - condition: template
    value_template: |
      {% set light = light.kitchen_island_center %} {{ is_state(light, 'off') or
          (state_attr(light, 'brightness') != 15 and
            state_attr(light, 'hs_color') != [120.472, 49.804]) }}
  - scene: scene.kitchen_island_normal_scene
mode: single

Is the is_state(light, 'off') part correct? I’m trying to find some documentation on the syntax of this kind of test.

So there’s a bug here and I’ve been trying to fix it without any luck. If the kitchen island lights are OFF then it’s supposed to activate the light.kitchen_island_center scene but that’s not happening. Here’s the state of the kitchen island center (it’s off) so it should activate the scene, but it doesn’t.

Any ideas?

By the way, the script is the same as in the above most recent post.

Right so the other part of the Automation Editor is pretty important here, so that we can determine the right things are happening. You need to be looking at the Automation Trace, which will step you through everything the Automation did, and did not do, and the reasons why. And yes is_state(device,state) is the correct way to do things, because on startup the state of the device is not known, and can cause errors.

and

for docs.

Interesting … the trace shows the template is false so it stops there.

But to test, I duped the automation and changed it to something simple and this still fails:

condition: template
value_template: |
  {% set light = light.kitchen_island %} {{ is_state(light, 'off') }}

Note that I have a light group called kitchen_island which has all 3 lamps in it.