Hi all!
I’m relatively new here, but have a very good grasp on how to code in python 2.7, 3.3+ and so HomeAssistant jumped right off the shelf at me. Getting used to the platform was fairly easy, but quickly became limiting. I have many lights in might apartment that are controlled via a single panel, and I found that tapping/clicking on a light just to control its brightness was incredibly tedious, so I tried to make a simple automation to control it!
- alias: "Set Brightness Level from Slider"
hide_entity: False
trigger:
platform: state
entity_id: input_slider.lights_brightness_level
action: >-
service: light.turn_on
{%- for state in states.light if state.state == "on" -%}
entity_id: {{ state.entity_id }}
data_template:
brightness: {{ (trigger.to_state | int * 255 / 100) | round }}
{%- endfor -%}
However there was one problem with that … you can’t dynamically determine entity_ids
! Templating only allows you to go so far as to format or manipulate the data you want to send TO the entity which bummed me out. This is a nascent project though, so I quickly decided to port over my automations to AppDaemon apps! This thing is great, I love it. The above automation is achieved with the code I have below.
import appdaemon.appapi as appapi
class SliderBrightnessLevel(appapi.AppDaemon):
def initialize(self):
self.listen_state(self.adjust_bright_level, 'input_slider.lights_brightness_level')
#entity can be just "light" to listen to all lights
def adjust_bright_level(self, entity, attribute, old, new, kwargs):
self.log("{}.{} changed from {} to {}.".format(entity, attribute, old, new))
for entity_id, state in self.get_datapoint_from_all(datapoint='state', service='light'):
if state == 'on':
# brightness is in 0 - 255, constrain to 255 to get percentage-of
blevel = round(int(float(new)) * 255 / 100)
self.log("light.{} set to {}.".format(entity_id, blevel))
self.turn_on(entity_id, brightness=blevel)
# helper functions
def get_datapoint_from_all(self, datapoint, service):
all_things = self.get_state(service)
for entity_id in all_things:
for data in all_things[entity_id]:
if data == datapoint:
yield entity_id, all_things[entity_id][data]
Ninja edit: It should be noted that the logging done here is unnecessary, as HomeAssistant keeps a record of everything that is done already. I was using logs to debug what is/isn’t happening as I learn the framework that the plugin class requires.
Python is about being beautiful, and so I wrote a helper function that allows my code to stay descriptive and well, beautiful. It pulls out specific datapoints from a group of like-entities. In this case, you can see that on line 12, I am pulling all the lights
. You’ll see the adjust_brightness
does just that - it loops through all the lights, and if they are on
, then they will have their brightness level set to blevel
!
This is still a very, very basic automation … so it is just the beginning for me! Be on the lookout, as I will surely share more projects and scripts as I learn to grow with the platform.
Thanks for reading,
SupahNoob