I have a Python script I wrote that changes the color temperature of a light depending on the time of day it is called. This allows me to have cooler lights in the morning and day, and warmer lights in the evening. The script works, but I have to pass it the current time when I call the script. How can I create a variable in my python script that gets the hour without passing it?
Python Script
#-- Variables --#
entity_id = data.get('entity_id')
hour = int(data.get('hour'))
temp = 0
# Sets temperature variable depending on the time
if hour <= 5:
temp = 500
elif hour <= 6:
temp = 250
elif hour <= 8:
temp = 200
elif hour <= 10:
temp = 150
elif hour <= 15:
temp = 125
elif hour <= 16:
temp = 175
elif hour <= 18:
temp = 200
elif hour <= 19:
temp = 250
elif hour <= 20:
temp = 300
elif hour <= 21:
temp = 375
elif hour <= 22:
temp = 400
elif hour <= 24:
temp = 500
else: # catch all
temp = 250
# Turns on light
hass.services.call('light', 'turn_on', { "entity_id" : entity_id, 'color_temp': temp, 'brightness': '255' })
Automation
automation:
- alias: 'Bedroom - Motion Lights ON'
trigger:
- platform: state
entity_id: sensor.bedroom_occupancy_status
to: 'Occupied'
action:
service: python_script.light
data_template:
entity_id: "light.b1, light.b2"
hour: "{{ now().hour }}"