It basically starts with how I label my entities. I have to use a consistent format. For example:
these specific lights start with ‘lt’, this tells me it’s for the litetouch lighting system. This light template reviews the status of an input_boolean. To turn the light on and off I call a script and pass data to the script. I’m telling the script which light keypad I’m using and which button.
lt_19_3:
friendly_name: Study
value_template: "{{ is_state('input_boolean.lt_19_3_boolean', 'on') }}"
turn_on:
service: script.light_onoff
data:
keypad: 19
button: 3
entity_id: "light.lt_19_3"
turn_off:
service: script.light_onoff
data:
keypad: 19
button: 3
entity_id: "light.lt_19_3"
Input boolean
lt_19_3_boolean:
name: Study
initial: off
Script - the script creates a custom event that I listen for in node red. It tells node red which keypad and button were pushed.
light_onoff:
sequence:
- event: litetouch_onoff
event_data_template:
keypad: "{{ keypad }}"
button: "{{ button }}"
entity_id: "{{ entity_id }}"
In node red I listen for the light touch event that gets broadcast when a button is pushed.
I then have a function that parses the payload from the node red event to determine the button and keypad used. Then formats the string that gets passed to the lighting controller. This code also sets the entity boolean name to be used later.
hex = function(num) {
return pad(num.toString(16).toUpperCase());
};
pad = function(num) {
num = num.toString();
while (num.length < 3) {
num = "0" + num;
}
return num;
};
msg.keypad = msg.payload.event.keypad;
msg.button = msg.payload.event.button;
entity_id = msg.payload.event.entity_id;
msg.button = parseInt(msg.button,10);
msg.payload = hex(msg.keypad) + (msg.button -1);
msg.ledkeypad = pad(msg.keypad);
msg.ledstates = "R,CGLES," + msg.ledkeypad +"\r";
entityid = msg.payload.entity_id;
msg.entityboolean = entity_id.replace('light','input_boolean') + "_boolean";
msg.payload = "R,CTGSW,"+ msg.payload +"\r";
return msg;
The controller returns the result, I then parse this data to determine if the light was turned on or not. As I still have the entity boolean id that I generated above (see msg.entityboolean = …). I then have a function that puts the entityboolean into a data object which can be passed back to HA.
msg.payload = {
"data": {
"entity_id": msg.entityboolean
}
}
return msg;
then I call the service intput boolean to set it to true by turning it on. Now the status of my light is on.