Detecting if light is on

Hello,

Right now my homeassistant is configured to change light temperature in the morning and in the evening with an automation that check if it’s almost sunset or sunrise and turn light on with the correct temperature settings, and than off. I turn the light on/off with a tradfri switch (the round one that change brightness levels) or via hue app.

I was thinking to make it a bit more “complicated” by detecting when light is turned on and than check what time is it and change light temperature according to that, is this possible or an event like this could only be started if I turn on the light via homeassistant (or some switch managed by homeassistant)?

thanks a lot.

That depends on your hardware. I could see this working but it wouldn’t be ‘seemless’. When you’d turn the device on, it would resume at the last setting momentarily, then switch to the desired light temperature.

EDIT: This is assuming that your switch turns on your light without home assistant. I.E. a normal switch.

I could see this working but it wouldn’t be ‘seemless’

Yes, that’s exactly what I was thinking, i turn on the light and after 10 seconds home assistant check what time is it and adjust the temperature, but how would I implement this? Do I need something in backgroud that check light status every n-seconds?

Na, if your switch is connected to home assistant, just build an automation that detects the switch ‘turn_on’ change. Make sure you only fire the automation when the switch is off.

- alias: adjust temperature of light
  trigger:
    - platform: state
      entity_id: xxx.xxx
      from: 'off'
      to: 'on'

if your switch is connected to home assistant

That’s exactly my problem, I can’t connect my switch to home assistant. :slight_smile:

What about your light? One of the two are connected to home assistant, otherwise how are you adjusting it?

1 Like

Yes! I had the solution literally under my eyes, and i was thinking something very complicated.Of course I have light state in homeassistant because lights are all connected to HA, thanks for pointing me in the right direction.

I’m gonna create a trigger that monitor the light entity from off to on and than a template that pass the ‘kelvin’ parameter to the ‘light.turn_on’ service, based from the sun position.

Thanks!

1 Like

this is exactly what I do with my externally switched parking light:

  - alias: 'Set Parking light'
    id: 'Set Parking light'
    # initial_state: 'on'
    trigger:
      platform: state
      entity_id: light.parking_light
      to: 'on'
      for:
        seconds: 30
    condition: []
    action:
      - service: light.turn_on
        data_template:
          entity_id: light.parking_light
          brightness: >
            {{states('input_number.outdoors_brightness')|int }}
          transition: 5
      - condition: template
        value_template: >
          {{ is_state('input_boolean.notify_system', 'on')}}
      - service: notify.notify
        data_template:
          message: >
            {{as_timestamp(now()) | timestamp_custom("%X") }}: Parking is set and safely lit 
             while Solar angle is {{states('sensor.solar_angle')}} and Offset is {{states('sensor.sunset_offset')}}.

depending on your platform of lights, you could also make a binary_sensor, and use that in the setup:

  - alias: 'Notify outside daylight'
id: 'Notify outside daylight'
#initial_state: on
trigger:
  platform: state
  entity_id: binary_sensor.outside_daylight_sensor
 # to: 'off'
condition: 
  condition: template
  value_template: >
    {{is_state('input_boolean.notify_system', 'on')}}
action:
  service: notify.notify
  data_template:
    message: >
      {{as_timestamp(now()) | timestamp_custom("%X") }}: 
      Daylight {{states('binary_sensor.outside_daylight_sensor') }}: Outside lights are powered 
      {{'on, ' if is_state('binary_sensor.outside_daylight_sensor', 'off') else 'off, '}}
      Solar angle is {{states('sensor.solar_angle')}} and Offset is {{states('sensor.sunset_offset')}}.

binary_sensor:
- platform: template
  sensors:
    outside_daylight_sensor:
      friendly_name: 'Outside daylight sensor'
      device_class: light
      value_template: >
        {{is_state('sensor.driveway_reachable', 'False')}} #based on Hue api reachable
1 Like

Ok I think I’m almost there, I’ve managed to doing it with sunset elevation:

- alias: "kitchen light temperature"
  trigger:
    - platform: state
      entity_id: light.kitchen_panel
      from: 'off'
      to: 'on'
  action:
    - service: light.turn_on
      data_template:
        entity_id: light.kitchen_panel
        kelvin: >
          {%- if states.sun.sun.attributes.elevation > 5 %}
            3000
          {%- elif states.sun.sun.attributes.elevation < 5 %}
            4000
          {%- endif %}
     - service: light.turn_on
      data_template:
        entity_id: light.kitchen_panel
        brightness: >
         {%- if states.sun.sun.attributes.elevation > 5 %}
            220
          {%- elif states.sun.sun.attributes.elevation < 5 %}
            180
          {%- endif %}

But what if I would like to use an offset before sunset / after sunrise? I don’t get how i should change the ‘states’… :expressionless: I need something like this (that I’m currently using to change light temps)

- alias: warm light in kitchen
  trigger:
    platform: sun
    event: sunset
    offset: "-00:59:00"

This may be of use (see link).

I also use Tradfri, at the moment I just have a “night mode” and a “day mode” automation where if the light is changed to ON it sets the colour and brightness correctly, but I plan on using the below instead:

1 Like

yes, and if you don’t want an extra custom component see here:
Circadian light with Philips Hue, independent from world clocks ;-)

Ive implemented that on 1 light and it works really nicely:

  - alias: 'Circadian Lights'
    id: 'Circadian Lights'
    initial_state: 'off'
    trigger:
      platform: time
      minutes: '/15'
      seconds: 00
    condition:
      condition: template
      value_template: >
        {{ is_state('light.hue_go_1', 'on')}}
    action:
      service: light.turn_on
      entity_id: light.hue_go_1
      data_template:
        color_temp: >
          {{ states('sensor.circadian_light') }}

sensor:
  platform: template

      length_of_day_factor:
        friendly_name: 'Length of day' # factor for circadian light calculation'
        value_template: >
          {% set daylength = ( (as_timestamp(state_attr('sun.sun','next_setting')) - 
                                as_timestamp(state_attr('sun.sun','next_rising')) ) / 
                                 3600) + 24 %}
              {{ ((daylength*-0.0063616)+0.11131) | round(5) }}

      circadian_light:
        friendly_name: 'Circadian light'
        unit_of_measurement: 'mired'
        value_template: >
          {% if is_state('sun.sun' , 'above_horizon') %}
            {{ ((states('sensor.length_of_day_factor') | float) * 
               ((state_attr('sun.sun','azimuth') )-180)**2 + 175) | round }}
          {% elif (as_timestamp(state_attr('sun.sun','next_dusk'))) - 
                  (as_timestamp(state_attr('sun.sun','next_setting'))) < 0 or 
                  (as_timestamp(state_attr('sun.sun','next_rising'))) - 
                  (as_timestamp(state_attr('sun.sun','next_dawn'))) < 0 %}
            350
          {% else %}
            390
          {% endif %}

Does that work with Tradfri as well as hue?

I wouldn’t know why not, as long as your bulbs support color_temp it should work. Thats easy enough to find out.

This is great, wow!

But… is there any way to prevent light going above 330 mired of color_temp?

Thank you so much.

I can confirm that works on Tradfri, I’m using it for a tradfri panel.

you could have an extra encompassing if then else to max to 330 I guess, but I am not sure that would be the correct way for implementing circadian algorithm.

In fact the “length of day” factor detemines the width and depth of the graph. If you change values here, you can influence how high the values will climb right before sunset and after sunrise. Simply put the formula in to MS Excel or similar and built your own graph. This will allow you to modify the variables used in the “length of day” factor so that you managed to achieve a max of 330 mireds.

for length of day, please see: Hours of Daylight, that might be an easier and locally adjusted way of calculating ?

{{ as_timestamp(state_attr('sun.sun','sunset')) -
   as_timestamp(state_attr('sun.sun','sunrise')) }}

And, of course, if you prefer it displayed as HH:MM:

{{ (as_timestamp(state_attr('sun.sun','sunset')) -
    as_timestamp(state_attr('sun.sun','sunrise')))
   |timestamp_custom('%H:%M',false) }}

using @pnbruckner custom component sun.py