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 . 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 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 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 and whether there is in the bedroom (motion and door sensors). The 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 app
let betterlogic = await Homey.apps.getApp({id: "net.i-dev.betterlogic"})
let getVar = async (name) => {
let x = await betterlogic.apiGet(name)
if (!x) throw new Error("Could not find variable [" + name + "]")
return x.value
}
// Fetch the aggregated state variables that is needed to do the logic.
let timeperiod = await getVar('timeperiod')
let presence = await getVar('presence-bedroom')
let bright = await getVar('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:
// timeperiod values: morning, day, evening, late, night
let scene = "off"
if (presence) {
if (bright && timperiod !== 'morning') {
scene = "normal"
} else if (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)
return true
Complete example
The complete homeyscript as described above:
sa-bedroom.js
// Helper functions to be able to fetch variables from the better logic app
let betterlogic = await Homey.apps.getApp({id: "net.i-dev.betterlogic"})
let getVar = async (name) => {
let x = await betterlogic.apiGet(name)
if (!x) throw new Error("Could not find variable [" + name + "]")
return x.value
}
// Fetch the aggregated state variables that is needed to do the logic.
let timeperiod = await getVar('timeperiod')
let presence = await getVar('presence-bedroom')
let bright = await getVar('bright')
// timeperiod values: morning, day, evening, late, night
let scene = "off"
if (presence) {
if (bright && timperiod !== 'morning') {
scene = "normal"
} else if (timeperiod !== "night") {
scene = "dimmed"
} else {
scene = "off"
}
}
console.log("Setting scene to [" + scene + "]")
betterlogic.apiPut("scene-bedroom/" + scene)
return true
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
] = await Promise.all([getVar('home'), getVar('timeperiod'),
getVar('presence-1etg'), getVar('presence-0etg'),
getVar('dark')]
)
More examples
Living room lights
Here is the homescript for kjellerstua:
sa-0etg.js
let sceneid = "off"
if (presence) {
if (tv) {
sceneid = "tv"
} else if (timeperiod === 'day') {
sceneid = "normal"
} else {
sceneid = "dim"
}
}
Outdoor lights
Example for outdoor lighting.
sa-outdoor.js
let sceneid = "off"
if (dark) {
if (timeperiod === 'morning' && presence) {
sceneid = 'normal'
} else if (timeperiod === 'day' && presence) {
sceneid = 'boost'
} else if (timeperiod === 'day' && home) {
sceneid = 'normal'
} else if (timeperiod === 'evening' && presence) {
sceneid = 'boost'
} else if (timeperiod === 'evening') {
sceneid = 'normal'
} else if (timeperiod === 'late' && presence) {
sceneid = 'dim'
}
}
Here is another older example for the second floor:
sa-floor2-old.js
let sceneid = "off"
if (timeperiod === 'night' && presence) {
sceneid = "nightp"
} else if (timeperiod === 'night') {
sceneid = "night"
} else if (!home) {
sceneid = "away"
} else if (!presence) {
sceneid = "night"
} else if (bright) {
sceneid = "bright"
} else if (timeperiod === "morning") {
sceneid = "soft"
} else if (timeperiod === "evening") {
sceneid = "soft"
} else if (timeperiod === "late") {
sceneid = "dim"
} else {
sceneid = "normal"
}
Children's room lights
Example from a childrens room. inputScene is a variable controlled by a person using a set of buttons for choosing modus.
let sceneid = null
if (inputScene === 'flood') {
sceneid = "flood"
} else if (!presence) {
sceneid = "off"
} else if (scene === 'night') {
sceneid = 'night'
} else if (scene === 'off') {
sceneid = 'off'
} else {
if (timeperiod === 'day') {
sceneid = "normal"
} else if (timeperiod === 'morning') {
// sceneid = "morning"
} else if (timeperiod === 'evening') {
if (leggetid) {
sceneid = "off"
} else {
sceneid = "leselys"
}
} else if (timeperiod === 'late') {
sceneid = "night"
} else if (timeperiod === 'night') {
sceneid = "night"
}
}
if (inputScene !== null) {
console.log("Set new scene " + sceneid)
betterlogic.apiPut("scene-1linus/" + sceneid)
} else {
console.log("No change. Keep current scene")
}
Music 2nd floor lights
Here is an example controlling a music scene, including a variable for music volume:
let d = new Date()
let m = d.getMonth()+1
let day = d.getDate()
let christmas = (m === 12) && day > 10
// console.log("Is christmas", christmas)
// Timeperiods: morning, day, evening, late, night
let music = "off"
let volume = 12
if (presenceAndreas && !presenceVigdis) {
volume = 16
}
if (!presence) {
music = "off"
} else if (!home) {
music = "off"
} else if (timeperiod === 'night') {
music = "off"
} else if (timeperiod === 'day' && !presenceAndreas && !presenceVigdis) {
music = "nrksuper"
} else if (timeperiod === 'day' && presenceAndreas && !presenceVigdis) {
music = "p4"
} else {
}
await setTagValue("music-2etg", {type: 'string', title:'Music 2etg'}, music)
await setTagValue("music-2etg-volume", {type: 'number', title:'Music 2etg volume'}, volume/100)
Music bathroom
Here is another logic for music, controlling music on the bathroom:
let d = new Date()
let m = d.getMonth()+1
let day = d.getDate()
let christmas = (m === 12) && day > 10
// console.log("Is christmas", christmas)
// Timeperiods: morning, day, evening, late, night
let music = "off"
let volume = 0.08
if (presenceAndreasBad) {
volume = 0.10
}
console.log("Christmas " + christmas)
if (!presence) {
music = "off"
} else if (timeperiod === 'night') {
music = "off"
} else {
music = "chill"
if (inputMusic !== 'auto') {
music = inputMusic
if(inputMusic === 'rock' && timeperiod !== 'late' && timeperiod !== 'evening') {
volume += 0.05
}
} else if ( timeperiod === 'morning') {
music = "spa"
} else if (christmas) {
music = "jul"
} else if (timeperiod === 'day') {
music = "pop"
}
}
if (timeperiod !== 'late') {
volume -= 0.03
}
if (timeperiod !== 'evening') {
volume -= 0.01
}
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 for that room.