Be aware: In order to use scene activations, basic knowledge of javascript is required.
This page was updated July 2021 reflecting the change from using Logic to Better Logic. Not all pages reflect this update, and there might be examples using logic variables.
I have one scene activation logic for each scene variable. A scene variable is a text value representing the different available scenes for a room or area.
The scene activation is responsible for translating a set of input variables (aggregated state) into one of the available scenes for a room.
A very basic example: bedroom
Bedroom has a scene variable scene-bedroom, a text string that always contains one of the defined scenes for that room. In a simple setup a room can have off, normal, dimmed.
The logic to decide the scene, may be bright, presence-bedroom and timeperiod. Both bright and presence-bedroom is boolean, indicating whether there is bright daylight and whether there is someone present in the bedroom (motion and door sensors). The timeperiod variable is a string that can contain one of a few values indicating which part of the day.
A scene activation consists of a homeyscript and a flow that triggers the homeyscript. I use homeyscript because it allows more advanced logic than what is available in the flow editor (which is far too limited).
The flow Scene activation bedroom, triggers when one of aggregated state variables that are involved changes.
The scene activation flow
Use the better logic "On of these variables changed" card, as seen in the illustration above.
The card takes a comma-separated list of better logic variables – the aggregated state variables that is needed for this flow.
The scene activation homeyscript
The scene activation homeyscript will run when one of the aggregated state variables changes. The script can be split into three sections:
Fetching the aggregated state variables
Logic calculating the output scene depending on the aggregated state.
Setting the output scene variable
Fetching the aggregated state variables
The first section with helper functions is the same for all scene activation homeyscript, and can be copied as part of a boiler plate template.
// Helper functions to be able to fetch variables from the better logic applet betterlogic =awaitHomey.apps.getApp({id:"net.i-dev.betterlogic"})letgetVar=async (name) => {let x =awaitbetterlogic.apiGet(name)if (!x) thrownewError("Could not find variable ["+ name +"]")returnx.value}// Fetch the aggregated state variables that is needed to do the logic.let timeperiod =awaitgetVar('timeperiod')let presence =awaitgetVar('presence-bedroom')let bright =awaitgetVar('bright')
In the example above we fetch three better logic variables into local javascript variables available to perform the logic.
Notice that there is a slight delay in fetching each of the logic variables of a few milliseconds.
Output the variables to the console for debugging is useful:
We use the aggregated state variables fetched above to decide which scene to activate for this room. It must always end up with one of the defined scenes for that room.
// timeperiod values: morning, day, evening, late, nightlet scene ="off"if (presence) {if (bright && timperiod !=='morning') { scene ="normal" } elseif (timeperiod !=="night") { scene ="dimmed" } else { scene ="off" }}
Setting the scene variable
Notice the trailing slash needed to be added to the name of the better logic scene variable.
console.log("Setting scene to ["+ scene +"]")betterlogic.apiPut("scene-bedroom/"+ scene)returntrue
Complete example
The complete homeyscript as described above:
sa-bedroom.js
// Helper functions to be able to fetch variables from the better logic applet betterlogic =awaitHomey.apps.getApp({id:"net.i-dev.betterlogic"})letgetVar=async (name) => {let x =awaitbetterlogic.apiGet(name)if (!x) thrownewError("Could not find variable ["+ name +"]")returnx.value}// Fetch the aggregated state variables that is needed to do the logic.let timeperiod =awaitgetVar('timeperiod')let presence =awaitgetVar('presence-bedroom')let bright =awaitgetVar('bright')// timeperiod values: morning, day, evening, late, nightlet scene ="off"if (presence) {if (bright && timperiod !=='morning') { scene ="normal" } elseif (timeperiod !=="night") { scene ="dimmed" } else { scene ="off" }}console.log("Setting scene to ["+ scene +"]")betterlogic.apiPut("scene-bedroom/"+ scene)returntrue
Testing the scene activation
Use the better logic app settings and manually edit each of the aggregate states, and verify that the scene variable are updated correctly, and also verify how the lighting, music or temperature changes accordingly to the scene.
One common mistake is the have typos in the "On of these variables changed"-card.
Alternative way of fetching variables
If you run into issues with delays adding up when fetching a large number of variables, you may fetch data in parallell like this:
let [ home, timeperiod, presence1, presence0, dark] =awaitPromise.all([getVar('home'),getVar('timeperiod'),getVar('presence-1etg'),getVar('presence-0etg'),getVar('dark')])