Magic to turn a light off

How do you set the magic such that a light will be turned off after a specified period of time after it was turned on (e.g. 1 hour after it was turned on).

There’s 2 options:

Option 1: You can create a countdown “thing”. Setting that to, e.g. 1 hour and then set up a rule to start the countdown when the light turns on and another rule to turn the light off when the countdown expires

Option 2: is using the ScriptEngine: here’s an example script that would do this:

import QtQuick 2.0
import nymea 1.0

Item {
    
    DeviceState {
        id: lightState
        deviceId: "{2423d0d3-95a0-4c25-a5b4-ab22486a6a18}" // Living room
        stateName: "power"
        onValueChanged: {
            if (value == true) {
                offTimer.start();
            }
        }
    }
    
    Timer {
        id: offTimer
        interval: 60 * 60 * 1000
        onTriggered: {
            lightState.value = false
        }
    }
}

So I did get option 2 working but am a little confused on scripting. (I did not figure out option 1 for this item).

But I am trying to figure out where the commands in this script come from. I found on my Ubuntu system (where my client is at) /usr/share/doc/nymea/html/index.html (its not in my start command list where I start nymea from). In there I could not find the following command used in this script: lightState

Is the “id: lightState” a combination of the interface “light” with the “API Type” “State”? if no where is this in the documentation so I can find other options?

also, while I can guess at their meaning, there are the commands onValueChanged and onTriggered, where are they defined so I can find other options?

Appreciate any help here.

Hello,

did you see this?

https://nymea.io/en/wiki/nymea/master/configuration/scripting

It should also open if you click the help button in the script editor on the top right, the question mark.

I’ve just improved that doc a bit. Please do not hesitate to ask more questions so I can improve it further.

Yes, I had found that webpage on scripting in the wiki.

Topic #1 The question remains “Where is the lists of the id: to be found?”

I have gone thru the new script site version as well but did not answer my question. What you did do is suggest there is yet another id: daylightState and id: lightPowerState. I do not find the lightState, daylightState or lightPowerState in the QML documentation. So where is the lists of the id: to be found? I have also looked in Tasmota documentation as well but they are not directly tasmota commands (i.e. all of my switches are tasmota).

Topic #2 Auto-fill
I have been playing around with the script editor. In the default script editor if I start typing Dev, auto fill then presents my with 3 options of which one is DeviceState. After selecting DeviceState and hitting enter between {} lets me start a new line. if I start typing dev I get deviceId: as the only option which after I add the " mark and get the list of devices to choose from (great). After choosing the light from the auto-fill list gives me, I add a new line. if I start typing sta on the new line I get two options but if I go back to the first " mark (on the previous line) delete it an reenter it I get the auto-fill menu again and if I use the cursor to select character 1 on the next line followed by selecting the last character on that blank line (there must be more than one character on that line) I get the auto-fill giving the options I might place after the deviceId line from which I can select stateName: adding the " mark gets me auto-fill and I can select power and then do an enter again. Again triggering auto-fill by deleting and reentering the " mark and selecting the first character and then the last character on the new created line I get the options for the next line in this case onValueChanged. etc.

->>So this is very clumsy for me to trigger the auto-fill to determine the options to put on the next line. Is there a better way to trigger the auto-fill function that tells me the options are for the line.

Also note that if I use the above trick to trigger auto-fill and place it on the id line, I do not get lightState option for the auto-fill (related to topic 1 issue). If it did, it would answer my question to topic 1 except for details about lightState if there are any.

Topic #3 website linkages
The Script editor “?” in the upper righthand corner does NOT link (trigger my web-browser) to anything on Ubuntu 18.04 (my operating system).
Unrelated to scripting - in the App tab “About nymea:app”, the “Visit the nymea website”, “Visit GitHub page” and “View privacy policy” do not trigger the web-browser to go to these pages. The website pages are shown below (in grey small font) for the apparent web-browser links. This may be something in my installation but not sure how to trouble shoot.

on my client computer nymead shows installed with apt (but is not running on my client computer). nymea-app (only on my client computer) does not show installed with apt. I am guessing that nymea-app was installed by snap method as it is in the snap directory (I just dont remember). this info is for the web-browser links issue just in-case it makes a difference.

sorry for the long response but to me it is all triggered by the initial question and your responses.

#1: So, the id properties are free form names to be able to reference the item (DeviceState, DeviceEvent etc) in the script… They are part of the QML language and are not required unless you need to reference to it in another place of the script. I had added that to the wiki page yesterday, but perhaps it’s still not clear enough… I’ll try to improve it further.

Here’s an example:

DeviceState {
    ...
    function doSomething() {
        console.log("doSomething called");
    }
    Component.onCompleted: doSomething();
}

In this example, the function doSomething() can be called just like that, because everyting is inside the same item. (Component.onCompleted:… will be executed directly when the script is loaded)

However, in the following example the item needs to have an id in order to allow the function to be called from somewhere else by using “id.doSomething()”

DeviceState {
    id: myFirstItem
    ...
    function doSomething() {
        console.log("doSomething called");
    }
}
DeviceState {
    ...
    Component.onCompleted: myFirstItem.doSomething()
}

I assume your confusion is because you thought this id references an actual device/thing in the nymea system, but no, you use deviceId for that.

#2: You should be able to invoke the auto-completion at any point by hitting Ctrl+Space (or cmd+space on macOS). Does this help?

I’ll add that information to the wiki page too, thanks.

ids of other elements should be included in the auto-complete list, but you can use them only as r-value or inside script blocks… I.e. you can do something like this:

DeviceState {
    id: lamp1
    deviceId: "1.."
    stateName: "power"
}
DeviceState {
    id: lamp2
    deviceId: "2..."
    stateName: "power"
    value: lamp1.value
}

This would bind lamp2 to follow lamp1. As the value of lamp2 is bound to lamp1.value. So whenever lamp1 value changes, it would also update lamp2s value to the same.

#3: Sounds like you don’t have a default browser set… Perhaps this might help? https://www.howtogeek.com/117709/how-to-change-your-default-applications-on-ubuntu-4-ways/
If it doesn’t please run nymea-app from the command line and send check if it prints an error message when trying to open links.

Yeah, sounds like you have the snap installed. You can check that with snap info nymea-app. Indeed this issue might be related to the snap, I’ll need to check and debug this. If you want, you can install it from apt by adding the nymea repository (if you haven’t done so - if you have nymead installed, the repository is probably there already)

sudo apt-add-repository "deb http://repository.nymea.io bionic main"
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-key A1A19ED6
sudo apt-get update
sudo apt-get install nymea-app

Lastly, no need to be sorry for the long response. In fact, this helps us a lot to improve nymea and we appreciate this kind of feedback. Please do let me know if things are clearer now or not.

Topic 1 - id:
my mistake was apparently that I cannot type. In the script you indicated the id: lightState I changed to id: mylightState
and also changed in the Timer section. I guess I did not type correctly as it threw an error when I ran the script and I assumed lightState was defined someplace else (i.e. where I got the idea that there needed to be a list of the options). Have since tested again and worked perfectly. Your explanations are very helpful for my understanding and clarity for future efforts.

Topic 2 - The ctrl + space was exactly what I was looking for. your wiki did a good explanation of auto-fill but left me with the impression that it only work with those few things you identified. Yes put ctrl + space in the wiki - probably in big bold letters.

Topic 3, ubuntu settings says I have firefox set as default and I have triggered firefox from other applications before. As far as the apt installation, I will wait on your feedback so I can test the snap version before I switch to the apt version. this lack is not a major issue for me.

Thanks again you are very helpful and timely in your feedback.

1 Like

I tried nymea-cli --host linux-server. It asked me for my username and password. It failed to log me in. I know the username and password as when I reboot to window I also have nymea-app there and it was also logged in using the username and password. so I don’t know what that means other than something is not right. and yes it is a snap version.

Please start a new thread in the forum for new topics next time, it helps other users finding answers to their questions :wink:

Anyhow, I just tried nymea-cli, seems to connect fine for me. Maybe an upper/lowercase issue? If you created the username/passwrod with your phone, maybe it automatically changed the first letter to uppercase? You can check in nymea:app in System Settings -> User Settings for your username and change the password there.

all of this is solved except for the browser issue. I am starting a new thread for the browser issue.