This is the forum archive of Homey. For more information about Homey, visit the Official Homey website.

The Homey Community has been moved to https://community.athom.com.

This forum is now read-only for archive purposes.

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 2018
    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"
  • k1sk1s Member
    JPe Sorry I only just saw the reply.  Thanks for taking the time.

    In your last example, is 'args' the same as in the first example var TextA = "Remove This. I only want this";  ?? 

    When you say "The result can be put in a "Better Logic" (String) variable", what do you mean exactly? Do you mean I put this:
    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"
    return(null);
    ...into where it says "value" in the BetterLogic add new variable (type string?)

  • JPeJPe Member
    edited June 2018
    @k1s ; When you want to get informed when the topic is updated, then do the following:
       
    "star"-ing this topic on the top of this page, will send you a message when the topic is updated.  
    ===========================================================================
    .
    here starts the explaining of the solution to the problem you asked: "how to remove text from another section of text?"

    Tested with the text: "Remove This. I only want this"

    Translation of the Dutch texts in the above picture:
     ^^ A user said something             ^^ Execute a script with an Argument     ^^whatever you like, like send to TV

        "MyText"  is a Variable, type String, made in Better Logic.

    Axualy these are the lines you realy need in your HomeyScript:

    var strg = String(args);            // places the Argument (from the flow-card) as a string, in var strg
    var rest = strg.slice(13);         //  places the part, from strg position 13 to the end, in var rest
    let bl = await Homey.apps.getApp({ id: 'net.i-dev.betterlogic' } );
    bl.apiPut('MyText/' + rest);     // places the found rest in the Better Logic variable "MyText"

  • k1sk1s Member
    Something wierd happening with the forum.

    Your post comes out like this:


    I can't really work out what you said.


  • JPeJPe Member
    edited June 2018
    sorry, I give up, someone else can explain? 

    Edit: read again, I added some extra info, can't make it more clear, sorry.
Sign In or Register to comment.