Global Function / Values

I’m very new to HomeAssistant and I’m having trouble getting my head around the scripting engine.

I’ve got my WALLC-s wall swtich working and have got it to switch on a Philips Hue light, but I’d like it to be able te determine the light color depending on the time of day.

What I have now is the following:

automation:
  alias: Switch test
  trigger:
    platform: event
    event_type: zwave.scene_activated
    event_data:
      entity_id: zwaveme_zme_wcd2_double_paddle_wall_controller_7
      scene_id: 11
  action:
    service: light.turn_on
    entity_id: light.livingroom

But I’d like to add the folliwng to action: Where Get_XY_Color is a function (template?)

        xy_color = {Get_XY_Color}

This is what I have in mind, but I have no idea how to do this in HA. Maybe I need to implement it in a totally different way, hopefully someone can help

# Sun below horizon set lights to soft warm
- alias: 'Set Relax theme for Hue lights'
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: 0
  action:
    - set:
      xy_color = 0.5134,0.4149

# Sun high, set lights to 'daylight color' (Concentrate theme)   
- alias: 'Set Concentrate theme for Hue lights'
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    above: 30
  action:
    - set:
      xy_color = 0.3693,0.3695

Scenes should help here - create 2 scenes, one for each color, then turn the scenes on as the action of your automation.

Hi aimc, thanks for your answer.

Sorry, I should have been clearer, above was just a quick example, in total I have six switches each controlling a separate Hue light, and there are three color themes I need.

With scenes I would need to create 18 different scenes if I understand correctly?

- Light One
  - Relax
  - Concentrate
  - Reading
- Light Two
  - Relax
  - Concentrate
  - Reading
...

And for the switches I would need 3 different automations per switch?

Switch One 
  - Night 
  - Scene: light_one_relax
Switch One
  - Day
  - Scene: light_one_concentrate
Switch One - Sun Below 20 degerees
  - Sun: Eleveation < 20
  - Scene: light_one_reading
...

I suppose that’s not too difficult to create, just a lot of copy/paste, it will be a pain to maintain though.

I see in this If…Then…Else thread that you mention an AppDaemon, I’ll also take a look at that.

I’m still playing around with this, trying to learn how HA works.

I’m considering using an input_select to store the current Hue theme name with the following automation:

  • An automation which changes on Sun Elevation change, depending on the elevation set the correct value in the input_select.
  • When pressing a button to switch on a light, set the correct theme depending on the value in input_select

Using an input_select will also allow me to use it as a trigger to change the theme for lights already on. (Either when changed manually or by the Sun Elevation)

Am I going about this in a roundabout way? HA is still very new to me as I’m used to Home Genie which uses programming languages for scripting (C#, javascript and Python)

Looks like no-one is interested in this, but I’m going to post my solution anyway in case someone else is looking for this kind of implementation.

First I created an input_select with the desired theme names:

input_select:
  current_light_profile:
    name: current_light_profile
    options:
      - Relax
      - Reading
      - Concentrate
      - Energise
    initial: Relax

I then added a light_profiles.csv file with the following:

id,x,y,brightness
Relax,0.5128,0.4147,200
Concentrate,0.3693,0.3695,200
Reading,0.4448,0.4066,200
Energise,0.3151,0.3252,200

Then Automation to change the current_light_profile selected depending on Sun.Elevation:

- alias: Set theme to Relax
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: -6
  action:
    service: input_select.select_option
    data:
      entity_id: input_select.current_light_profile
      option: Relax
      
- alias: Set theme to Reading
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    above: -6
    below: 6
  action:
    service: input_select.select_option
    data:
      entity_id: input_select.current_light_profile
      option: Reading
 
- alias: Set theme to Concentrate
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    above: 6
  action:
    service: input_select.select_option
    data:
      entity_id: input_select.current_light_profile
      option: Concentrate

And also an Automation when a button on the Wall switch is pressed. Now when the light is switched on via the wall switch it gets its theme from the input_select value:

- alias: WCD Switch 2_11
  trigger:
    platform: event
    event_type: zwave.scene_activated
    event_data:
      entity_id: zwaveme_zme_wcd2_double_paddle_wall_controller_7
      scene_id: 11
  action:
    service: light.turn_on
    entity_id: light.kitchen
    data_template:      
      profile: '{{ states.input_select.current_light_profile.state}}'

I’m also adding another Automation to slowly change the light theme (for lights that are already on) when current_light_profile changes: (untested)

- alias: kitchen_change_theme
  trigger:
    platform: state
    entity_id: input_select.current_light_profile
  condition:
    platform: state
    entity_id: light.kitchen
    state: 'on'
  action:
    service: light.turn_on
    entity_id: light.kitchen
    data_template:      
      profile: '{{ states.input_select.current_light_profile.state}}'
      transition: 60
15 Likes

This is awesome just what I wanted thanks for sharing :slight_smile:

Cool stuff, but I’m missing something here. Where are you reading your csv into the config? I see you setting current_light_profile to some name like “Relax” based on sun elevation, but when you actually turn on the light, how are the values from the csv read?

It’s read from the light_profiles.csv like this, in this case it’s getting the name from the input select:

    data_template:      
      profile: '{{ states.input_select.current_light_profile.state}}'

But you could also hard code the name of the required profile:

    data_template:      
      profile: 'Relax'

In case anyone else is reading this and unsure, the profile command is part of the Light Component and you place the light_profiles.csv file into your config folder.
See Light - Home Assistant for details under the light.turn_on service section.

Once you create a light_profiles.csv file, you can not only use the 4 that are specified on the built in profiles link, but you can create your own profiles using the x,y and brightness levels. You can also specify default setting for when a light is turned on and not triggering an automation to set a specific profile.

1 Like

Thanks for clarifying that.

Wow, this is really cool.
I’ve been doing this with script, but this feels much simpler and transparent on automations. Not to say you can create different input selects for different areas of your house and easily visualise on the interface.

Will definitely implement this.