Office abajour for working late at night

This is my working version for automating desk lamp.

I begin to do my home-office work early, at about 10hs and usually I do finish it around 18hs. Sometimes it can be even later, like 19hs or 20hs. While I’m working I may be deep involved into some task or I may be in a long video meeting so I don’t want to open some panel to switch on my desk light. I want it to just happen.

So, the rules:

  • I want it to turn on the light anytime between 1:15 hours before sunset (I do have some mountains here, it gets dark before sunset).
  • I want to have a panel switch to disable automation, easy for any user to find it.
  • How do I know if I’m on the desk and not somewhere else? The most precise way I found it is by the number of monitors. On my desk my second monitor is on, else, not. It also helps when I just close the Macbook lid, it means, no more work.
  • I want to be able to anytime, manually switch on or switch off the light. So I don’t want periodic trigger.
  • I want it to turn off the light in case I’m deep working on very early morning, I shouldn’t but, better to hate it as a rule! So, I want switch off 1:15 hours after sunrise.

So here is my automation rule. It took me quite a bit of time, it may have a lot of opportunities for improvement (and all of those would be welcomed!).
And if you find it useful for yourself, just grab it!

- id: '1651070652453'                                                                                                                                                                                                        
  alias: Abajour automático                        
  description: ''            
  trigger:                                                                                                         
  - platform: state                    
    entity_id: sensor.macbook_pro_de_aline_displays
  - platform: sun                                                                                                  
    event: sunset                                   
    offset: -01:15:00           
  condition:                         
  - condition: state                                                                                               
    entity_id: switch.abajour_automation_switch 
    state: 'on'                                  
  action:                                                                                                          
  - choose:                          
    - conditions:                                        
      - condition: or                                                                                              
        conditions:                                                                                                
        - condition: sun                                                                                           
          after: sunset                                                                                            
          after_offset: -01:15:00                                                                                  
        - condition: sun                                 
          before: sunrise                  
          before_offset: 01:15:00                        
      - condition: or      
        conditions:                                                                                                
        - condition: template
          value_template: '{{ is_state("sensor.macbook_pro_de_aline_displays", "2")                                
            }}'                                  
        - condition: template          
          value_template: '{{ trigger.to_state.state == "2" }}'                                                    
      sequence:                                                                                                    
      - service: switch.turn_on                                                                                    
        target:                           
          entity_id: switch.abajour            
    - conditions:                      
      - condition: or
        conditions:
        - condition: sun                    
          after: sunrise
          after_offset: 01:15:00
        - condition: sun
          before: sunset
          before_offset: -01:15:00
      - condition: or
        conditions:
        - condition: template
          value_template: '{{ not is_state("sensor.macbook_pro_de_aline_displays",
            "2") }}'
        - condition: template
          value_template: '{{ trigger.from_state.state == "2" }}'
      sequence:
      - service: switch.turn_off
        target:
          entity_id: switch.abajour
1 Like

Abajour - had to google that. French for lightshade?

Thanks for contributing.