Welcome to the Athom Community Forum! This is a place to share ideas, ask for help and discuss about Homey in general.

Please read Welcome to the forum to get started.
Ask your Homey-related questions in this category. You can also go to athom.com/support to get support from Athom.

How to remove substring from string (/specific text from 'speech' input text)

Hello, does anyone know how to remove text from another section of text?

Say I've got a text variable TextA which has a value "Remove This. I only want this". 

How can I create a new variable TextB with value "I only want this" ?

I tried using the creating another variable TextC with value "Remove This. " and then another Logic card that sets TextB by subtracting TextC from TextA, but none worked, e.g.
{{TextA}} - {{TextC}}
{{{TextA}} - {{TextC}}}
{{$TextA}}} - {{$TextC}}
{{$TextA}}} - {{$TextC}}

(No idea what the sytax should be or even where to look it up or what language Homey uses, so I'm just guessing in the wind with the curly brackets and dollar signs ...)

Comments

  • k1sk1s Member
    Could somebody at least tell me where to look?  Where is the syntax guide or what language Homey uses?
  • JPeJPe Member
    Did you study Better Logic App and 
    https://forum.athom.com/discussion/840/better-logic-variable-management
    maybe there is something for you? 

  • k1sk1s Member
    JPe said:
    Did you study Better Logic App and 
    https://forum.athom.com/discussion/840/better-logic-variable-management
    maybe there is something for you? 

    I did - some very long threads but I didn't find the answer to my question or even a clue how to find out what syntax might work
  • k1sk1s Member

    JPe said:
    Thanks, I can't tell if you're pointing me to a specific example or to the thread in general.  I see there is an app called HomeyScripts and have looked at the github pages.  It is clearly aimed at developers and beyond simple understanding from a  non-programmer.  I see some examples of using tags and tokens (I don't know what a token is), but not how to strip text from a the text stored as a tag. 

    I'm guessing from the extension ".js" that perhaps Homey uses a version of javascript - is that right?  Googling how to remove text from a string in javascript  I found this example:

    var str = "data-123";
    str = str.replace("data-", "");

    ...which is supposed to change the variable str from "data-123" to just "123".

    So presumably my version could be something like:

    var TextA = "Remove This. I only want this";
    var TextC = "Remove This. ";
    var TextB = TextA.replace(TextC, "");

    But... 

    When I tried to put this into a flow, like this:


    It didn't work.  The push message just showed $TextB

    I tried again with BetterLogic instead of Logic, like this:


    ...to see if it would change the variable values, but in fact looking in Settings after the flow ran, I saw all of the 'Last Set' 'Values' were blank, so obviously the syntax is not correct.



  • JPeJPe Member
    Sorry I was so unclear, but it is not my intention to give a complete solution but to try to give you a direction where to find probably an idea for a solution, as I see you doing very well in your searches, but at the HomeyScript, you missed something, probably because you think it is only for developers. Don't be afraid, just try some examples and you see how it works and then you can make your own solution.
    Did you Visit https://homeyscript.athom.com to manage your scripts? That is where you have to enter the JavaScript lines.
    The way you made your Flows is not going to work, this has to be done in the HomeyScript environment.
  • k1sk1s Member
    JPe said:
    Sorry I was so unclear, but it is not my intention to give a complete solution but to try to give you a direction where to find probably an idea for a solution

    That's fair enough, but if you do actually know how to do it, it would be much kinder just to tell me.  I'm not a programmer, nor do I have the time or inclination to become one.  I'm just trying to recoup some of the significant investment in time I already made into this Homey as a simple home user.  I know nothing about javascript, nor HomeyScript.

    I installed the app and created the following script:


    // my script
    var TextA = "Remove This. I only want this";
    var TextC = "Remove This. ";
    var TextB = TextA.replace(TextC, "");

    I hit Save & Test, and got this: 
    ---------------- Script returned: undefined

    I have no idea what that means or what to do with it
  • JPeJPe Member
    edited May 15
    I'm not a programmer too, just tried your Lines en expanded a little to see the result:
    // my script
    var TextA = "Remove This. I only want this";
    var TextC = "Remove This. ";
    var TextB = TextA.replace(TextC, "");
    console.log(TextB)
    return(TextB)


    Edit-1:
    did some further investigation an came to this: (args is the input text from your Flow)

    var strg = String(args);
    var rest = strg.slice(13); // removes what you don't want to see (first 14 Char, rest is shown)
    //console.log("strg=", strg, " /// rest=", rest );  //option
    console.log(" >>", rest,"<< ");  //option
    // insert here the destination of your changed text
    return(rest);


    Edit-2:  //The result can be put in a "Better Logic" (String) variable:
    console.log('---------------new----------------------')
    var strg = String(args);
    var rest = strg.slice(13);
        //console.log("In=", strg, "   >>>  result=", rest );
    let bl = await Homey.apps.getApp({ id: 'net.i-dev.betterlogic' } );   // check for Better Logic
    if (!bl) {
    console.log('Better Logic not installed!');
    return false;
    }
    bl.apiPut('MyText/' + rest);           // Put the found result in the Better Logic variable "MyText"
Sign In or Register to comment.