(Last Updated: May 15, 2020)

while loop with IF then elseif...

  • Not sure where to start, it's not clear in my mind.
    But I need to be able to have some code running all the time with something like

    IF multiple condition == ON
        do something
    elseif condition2 == ON
        do something different
    elseif condition3 == ON
       do something different2

    Right now I'm using RulesEngine to achieve 50% of what I need right now but RulesEngine is only able to do something like

    IF condition == ON
    then do something
    else do something

    I can't have multiple action in the same rules based on different condition...

  • RulesEngine... interesting... not Reactor?

    Should we have a thread for that?

  • @akbooer I never played with reactor and I would say that I would like to code more than using plugin..

  • This still isn't an openLuup topic per se:

    • Do you really need that to be in a polling loop?
    • Are the conditions related to device variable changes?
    • Could you use a scene with multiple triggers?

  • I put it in openLuup as I would like to use "openLuup" to achieve it, not a plugin 😉

    A scene with MULTIPLE triggers, is it a OR or AND ?

  • Ah! Got it!

    So these all can potentially be triggers? My view on triggers is that they are all EVENTS, therefore cannot occur concurrently, so all triggers are, by definition, OR. What you need to do is allow any of these changes to trigger a scene and then construct your logic in the Lua code, where you might choose to examine the current STATE of those variables, and pick your action appropriately.

    If you've got a slightly more concrete example to work on, that might help.

  • For example, right now I'm having a Rules Engine that check some motion sensors and other data to toggle WHERE to send the TTS in the house.

    By default it's in the living room but if I got some motion in the kitchen OR that music are playing in the kitchen, I will send the TTS in Kitchen.

    Until now, it's working fine but I would need to have more validation, for example, if music is playing outside (Patio), I need to send TTS there instead of inside.

    and if it's not kitchen or patio and for example TV in living room is OFF but TV in Master bedroom is ON, send TTS there.

    With Rules Engine I can't do that!

    So I was thinking to rewrite this logic without Rules Engine and made it in LUA directly somewhere in openLuup.

    Another LUA code I have it's to detect if I switch the system in "Summer mode" as you know, I have snow and real winter here, so based on that I'm changing some scene to "COOL" or "HEAT" the house. But with the COVID, I just find out that this code didn't work anymore 'cause of the weather that it's not the same as usual in May (related to pollution), so temperature is very cooler than usual. So I would need to change my code to run often with a scene that run each hour, but maybe if I can run it "all the time" I would add more code to check the temperature inside vs. outside...

  • Most people like to use Reactor for this sort of thing, but if you really want to write this in Lua, then fine.

    So where's the difficulty? A scene with AltUI Device Variable Watch triggers on any of the states you're interested in. Or a scene that's regularly scheduled, depending on how responsive you need it to be. Some Lua scene code execute the logic. What can't you do?

  • Yeah you're right. I can have a scene that run each 5 mins for exemple and do a bunch of testing/lua!

  • There are actually a lot of ways to do this with lua and scenes:
    For example you could watch all the motion sensors tripped variable and use them as triggers in a single scene.
    Within the scene you would then collect the tripped status of all the sensors/season/housemode and then run your lua logic according to their status.
    I would prefer this to a time based scene for being much more efficient.
    You could also create a scene trigger by each sensor and add lua filtering condition to then trigger another scene. (Technically a replacement of the trigger lua available on the vera). This is an example of how powerful lua scripting and openLuup can be.

  • For tts, I watch for last tripped and compose it accordingly. I also set alexa to DND programmatically to avoid the part of the house with interfering activities (calls, people sleeping, etc).

    I have a 15 min scene that see for avg temperature in the last hour to trigger things, plus one checking the max temp of the day (real, not forecast) in order to tell to open sprinkler to run in the afternoon (usually one in the early morning is enough).

  • @therealdb maybe you can share some code snippet in the forum section ? 😉

  • I access Datamine logs from a custom .NET Core C# app (running on linux) and to the math there, but I think it's doable in lua.
    maybe there's something already done in openluup, @akbooer? 🙂

  • @therealdb said in while loop with IF then elseif...:

    I think it's doable in lua.
    maybe there's something already done in openluup

    Not quite sure what you're asking for here, but you mentioned Datamine, so I thought I'd remind you all that openLuup can read Datamine databases without Datamine being installed. So if it's on a NAS, you have access...

    ...think I'm getting off topic, though.

Log in to reply