GithubHelp home page GithubHelp logo

Comments (9)

mikeage avatar mikeage commented on May 31, 2024 4

So first of all, I'm an idiot :-) The two changes I was seeing was because I'm toggling a light group... with two bulbs. Didn't dawn on me until I started looking at the CL logs that it'd toggle the lights individually; the first arrived when only one light had changed, so the group's change was effectively half (the average...) of what it desired. Anyway...

I did see the link, and I'm using it now (there's a typo there; service.cancel should be service.turn_off). I chose to cancel the first and use the second, figuring that if it needs a delay, best to use the delay after the last change. In any case, it seems like 5 seconds is far too much, but that's all right for now.

Ideally, though, I'd love if there was some way to have both scripts run to completion. The reason is that I want to template this and apply it to a bunch of lights and groups, and if I change multiple lights in close succession, canceling the script for light 1 just because light 2 changed isn't the right thing to do. But worst case, copy paste.

Here's what I'm using now (slightly improved over the original, where I use an input_select to have a tristate "On / Temporarily Off / Off" for CL rather than just On/Off. This will let me reset it as needed if there was a manual override (maybe on the next off, maybe after 60 minutes, maybe the next day, whatever):

configuration.yaml

light:
  - platform: group
    name: Parents' Room Lights
    entities:
      - light.yeelight_ct2_7c49eb1a8ee6
      - light.yeelight_ct2_7c49eb1a8f09
switch:
  - platform: circadian_lighting
    name: parents_room
    lights_ct:
      - light.yeelight_ct2_7c49eb1a8ee6
      - light.yeelight_ct2_7c49eb1a8f09

input_select:
  circadian_lighting_parents_room:
    name: Circadian Lighting Parents Room
    options:
      - 'On'
      - 'Overridden'
      - 'Off'
    icon: mdi:theme-light-dark

automation.yaml (still has some debug left in)

- id: cl_disable_parents
  alias: 'Disable Circadian Light on manual change'
  trigger:
    platform: state
    entity_id: light.parents_room_lights
  condition:
  - condition: state
    entity_id: switch.circadian_lighting_parents_room
    state: 'on'
  - condition: template
    value_template: "{{ trigger.from_state.state == 'on' }}"
  - condition: template
    value_template: "{{ trigger.to_state.state == 'on' }}"
  action:
  - service: script.turn_off
    entity_id: script.cl_disable_parents
  - service: script.cl_disable_parents
    data_template:
      trigger_to_state_object_id: "{{ trigger.to_state.object_id }}"

- id: cl_disable_parents_internal
  alias: 'Disable Circadian Light on manual change (internal)'
  trigger:
  - platform: state
    entity_id: input_select.circadian_lighting_parents_room
  action:
  - service_template: "switch.{% if is_state('input_select.circadian_lighting_parents_room', 'Off') or is_state('input_select.circadian_lighting_parents_room', 'Overridden') %}turn_off{% else %}turn_on{% endif %}"
    data:
      entity_id: switch.circadian_lighting_parents_room

script.yaml:

cl_disable_parents:
  alias: Disable Circadian Lighting Parents Room
  sequence:
  - service: system_log.write
    data_template:
      message: "CL Disable triggered. Comparisons will be: {{ ((1000000 / states['light'][trigger_to_state_object_id].attributes.color_temp) | int) }} -  {{ state_attr('sensor.circadian_values', 'colortemp') }} and {{ ((100 * states['light'][trigger_to_state_object_id].attributes.brightness / 255) | int) }} - {{ state_attr('switch.circadian_lighting_parents_room', 'brightness') }}. Now waiting"
      level: info
  - delay: 00:00:05
  - service: system_log.write
    data_template:
      message: "After the delay. Comparisons are: {{ ((1000000 / states['light'][trigger_to_state_object_id].attributes.color_temp) | int) }} -  {{ state_attr('sensor.circadian_values', 'colortemp') }} and {{ ((100 * states['light'][trigger_to_state_object_id].attributes.brightness / 255) | int) }} - {{ state_attr('switch.circadian_lighting_parents_room', 'brightness') }}. Now checking..."
      level: info
  - condition: or
    conditions:
    - condition: template
      value_template: "{{ (((1000000 / states['light'][trigger_to_state_object_id].attributes.color_temp) | int) - state_attr('sensor.circadian_values', 'colortemp')) | abs > 200 }}"
    - condition: template
      value_template: "{{ (((100 * states['light'][trigger_to_state_object_id].attributes.brightness / 255) | int) - state_attr('switch.circadian_lighting_parents_room', 'brightness')) | abs > 5 }}"
  - service: input_select.select_option
    data:
      entity_id: input_select.circadian_lighting_parents_room
      option: "Overridden"

from hass-circadian_lighting.

mxwlsavard avatar mxwlsavard commented on May 31, 2024 2

How about an automation like this:
trigger: state change of your lights
condition: color and brightness of lights doesn't equal color and brightness of CL component AND light is not off (so you don't turn off CL when you turn off a light)
action: turn off CL

You might have to add a delay before you check the color and brightness if your lights don't turn on to the right color immediately. You also might have to add a range to the color and brightness check (within 1 or 2%).

from hass-circadian_lighting.

mikeage avatar mikeage commented on May 31, 2024 1

Ok, I just about have this working. One problem I have is that when CL is enabled, it sometimes may make several distinct changes on the way to the target brightness and color temperature (haven't checked, but I suspect this is color and brightness together). One way to solve this might be with a delay; another is to reenable if they get too close. The delay isn't an option if you use a condition. I'm not sure if I can put an explicit test in an action; I can use a template to see which values I send, but not, I think, if I call a service at all. Need to read up on that...

edit: getting close won't work. If the switch is disabled, it doesn't have a brightness. Color_temp is on the sensor, so that can always be checked. But brightness is on the switch. Guess I'll look into the delay.

edit2: this seems to work!

- id: cl_disable_parents
  alias: 'Disable Circadian Light on manual change'
  trigger:
    platform: state
    entity_id: light.parents_room_lights
  condition:
  - condition: state
    entity_id: switch.circadian_lighting_parents_room
    state: 'on'
  - condition: template
    value_template: "{{ trigger.from_state.state == 'on' }}"
  - condition: template
    value_template: "{{ trigger.to_state.state == 'on' }}"
  action:
  - delay: 00:00:05
  - condition: or
    conditions:
    - condition: template
      value_template: "{{ (((1000000 / states[trigger.to_state.domain][trigger.to_state.object_id].attributes.color_temp) | int) - state_attr('sensor.circadian_values', 'colortemp')) | abs > 200 }}"
    - condition: template
      value_template: "{{ (((100 * states[trigger.to_state.domain][trigger.to_state.object_id].attributes.brightness / 255) | int) - state_attr('switch.circadian_lighting_parents_room', 'brightness')) | abs > 5 }}"
  - service: switch.turn_off
    entity_id: switch.circadian_lighting_parents_room

I test for color temp +/- 200K and brightness +/- 5%. This seems to be reasonable in my testing (at ~3 hours past sunset now, the brightness is often off by about 2% now).

I'd like to find a way to dynamically determine which switch to use for which lights, but that's already a bit more advanced ;-)

Thanks for your help!

edit: not completely working. When turning on via Alexa (rather than via the GUI), it seems like I'm getting multiple state changes, and at least one of them is on->on, but with a brightness or color temperature that looks like it was an attempt at a change. Back to investigating...

edit: another issue is this: https://community.home-assistant.io/t/delay-function-in-automations/92224/15 . When the second notification comes, it resumes the first one immediately, before the delay completes. I'm collecting some logs via the system_log.write service, and will report back when I understand this a bit better.

from hass-circadian_lighting.

mxwlsavard avatar mxwlsavard commented on May 31, 2024 1

Did you see the post about canceling the new or already running instance of an automation in that threat you linked?

from hass-circadian_lighting.

claytonjn avatar claytonjn commented on May 31, 2024

To my knowledge, there's no way of knowing if an adjustment was made by another integration, the frontend, or Circadian Lighting itself. If I'm mistaken, hopefully someone can share.

If I get time to implement #1 that may work for you, otherwise the best thing for your example may be to expose the CL switches to Alexa and say something like "Alexa, Turn off CL and set brightness to 50%".

from hass-circadian_lighting.

mikeage avatar mikeage commented on May 31, 2024

Yeah, I was afraid of that. I didn't see anything in the state change notification that would indicate the state. I was thinking about maybe trying to disable listening right before CL makes a change, then reenabling it after, and treating all of the incoming notifications as external, meaning override CL, but that sounds convoluted and I'm not sure if the message queue system guarantees ordering. :-/

I can expose the switch, but I'm really hoping for a family-friendly solution that's intuitive.

from hass-circadian_lighting.

mikeage avatar mikeage commented on May 31, 2024

Clever. It has to be quicker than the CL auto adjust, though. Might also want to try to avoid the case of off->on, and only catch on->on (I e., change), but this might just work! Thanks :)

from hass-circadian_lighting.

mikeage avatar mikeage commented on May 31, 2024

One more note; the script above is working great for my white bulbs (yeelight YLDP05YL) but not for my color bulbs (YLDP06YL). More precisely, when setting a color but not a color temperature, the color_temperature retains the original value and the RGB (or maybe HS; not sure which is triggered by which) are updated. To detect those, I added the following condition as well. What I saw is that the hue is between 25 and 30 for almost all values, except the warmest temperatures where it goes up to about 50 or 55. To catch this, I'm checking for any hues outside of this range.

    - condition: template
      value_template: "{{ is_state('light.yeelight_color2_04cf8c759aec', 'on') and
      state_attr('light.yeelight_color2_04cf8c759aec', 'hs_color') and
      not ( 25 <= state_attr('light.yeelight_color2_04cf8c759aec', 'hs_color')[0] <= 30 or
      0 <= state_attr('light.yeelight_color2_04cf8c759aec', 'hs_color')[0] <= 60 and
      0 <= state_attr('light.yeelight_color2_04cf8c759aec', 'hs_color')[1] <= 10) }}"

There might be a better way to handle this, long term, so I'd like to leave this issue open, but for my purposes, this is working and giving me what I need, so I'm leaving the notes here if it helps anyone else in the future.

Thanks to claytonjn for an excellent component, and to TravelinMax for the idea!

from hass-circadian_lighting.

maniscalcojohn avatar maniscalcojohn commented on May 31, 2024

One more note; the script above is working great for my white bulbs (yeelight YLDP05YL) but not for my color bulbs (YLDP06YL). More precisely, when setting a color but not a color temperature, the color_temperature retains the original value and the RGB (or maybe HS; not sure which is triggered by which) are updated. To detect those, I added the following condition as well. What I saw is that the hue is between 25 and 30 for almost all values, except the warmest temperatures where it goes up to about 50 or 55. To catch this, I'm checking for any hues outside of this range.

    - condition: template
      value_template: "{{ is_state('light.yeelight_color2_04cf8c759aec', 'on') and
      state_attr('light.yeelight_color2_04cf8c759aec', 'hs_color') and
      not ( 25 <= state_attr('light.yeelight_color2_04cf8c759aec', 'hs_color')[0] <= 30 or
      0 <= state_attr('light.yeelight_color2_04cf8c759aec', 'hs_color')[0] <= 60 and
      0 <= state_attr('light.yeelight_color2_04cf8c759aec', 'hs_color')[1] <= 10) }}"

There might be a better way to handle this, long term, so I'd like to leave this issue open, but for my purposes, this is working and giving me what I need, so I'm leaving the notes here if it helps anyone else in the future.

Thanks to claytonjn for an excellent component, and to TravelinMax for the idea!

@mikeage Thank you for sharing your script...it works great, although I've run into one problem. If I change the color, the light retains its color settings in the attributes, when the light is turned off. If CL is then turned back on and the light is switched on, when CL goes to change the color of the light , the automation compares the updated CL setting to the previous changed color and disables CL for the light after it makes the initial change. I think a slight delay would work, but not sure how to add that into a script...

from hass-circadian_lighting.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.