How can I get YAML to evaluate a numerical expression - yaml

I have the following expression in YAML:
service: mqtt.publish
data:
topic: zigbee2mqtt/Bathroom_Dimmer/set
payload:{"brightness_step": 150-{{state_attr('light.Bathroom_Dimmer','brightness')|int}} }
It outputs:
Message 7 received on zigbee2mqtt/Bathroom_Dimmer/set at 10:07 PM:
{"brightness_step": 150-255}
How can I get it to output "-105" instead of "150-255"?
EDIT:
I think that automations in Home Assistant use the Jinja2 engine. The double brackets make it evaluate. So the problem was that the 150 was not in the brackets. Adding extra brackets did not work, so it seems they cannot be nested.

YAML is not code, so you can't expect a generalised behaviour of evaluating any kind of expression. It's up to the application that uses that data to decide what to do with it and you may find that some applications do support an expression syntax within YAML.
I'm guessing this is something to do with a ZigBee config file which I have never used, but I don't see any evidence that it supports expressions. Do you have any reason to believe that it does? If so, this behaviour comes from the application, not from YAML.

Related

Unexpected token ':' in Nifi Expression Language

I just got an error about an unexpected token while using the NiFi expression language.
'prod' validated against ${hostname:contains("prod")} is invalid because
Unexpected token ':' at line 1, column 10. Query: ${hostname:contains(prod)}
In this case the problem occurs in the RouteOnAttribute processor.
My question: What is/are the typical causes of this error?
Of course it is good to check the actual expression, however in this case I did not find any problems with the expression.
The problem comes from hostname
As documented here:
There also exist some functions that expect to have no subject. These functions are invoked simply by calling the function at the beginning of the Expression, such as ${hostname()}
After this it was quickly fixed by calling my attribute host_name instead.

Escape single quote in Xtend template expression

I have a very simple question, but could not figure it out by Google search, please help.
I want to produce this string '\u0000' (note the simple quote marks surrounding it!) using the following simple Xtend method containing a template expression:
def String makeDefaultChar()
{
''''\u0000''''
}
However, this is not accepted as proper syntax (probably because of the four ''''. Is there an escape character for this use case or what is the right syntax?
Thank you in advance!
P.S.
Of course I could use plain Java string like this "'\\u0000'" to achieve the same, but I want to use an Xtend template expression.
My Xtend version is: 2.9.1.v201512180746
There is no "escaping" in template expressions, so you have to use the workaround you mentioned:
'''«"'\\u0000'"»'''
or
'''«"'"»\u0000«"'"»'''
Related discussion: https://groups.google.com/forum/#!topic/xtend-lang/bVZ0nKmQGAI
Single quotes are allowed within Xtend templates as long as they do not occur at the beginning or the end of the template. So a simple workaround is to add an empty expression before/after the single quote:
'''«»'\u0000'«»'''

How to extract jmeter response value

I did read few responses but my regular expression extractor is not working.
Mine is a simple case where this is my response
token.id=AQIC5wM2LY4Sfcz4cOT2RrremxWJmM3llZmPl6k0bP_r5D4.AAJTSQACMDUAAlNLABQtNDI1OTg4NzgxODg5MDM1ODU2NQACUzEAAjI3
I am trying to grab the value using this expression
token.id="(.*?)"
which is not saving the value into the variable i assigned. My next request when trying to use the value fails since its not grabbing it.
Can someone let me know what exactly is missing. thanks.
There are few problems with your regular expression:
You need to escape dot between "token" and "id" with backslash as it is a special character. See Literal Characters article for more information.
You don't need the quotations marks as your response doesn't contain them (does it?)
So your regular expression needs to be amended as token\.id=(.*) (however I would rather go for something like token\.id=(\w.+)
You can use View Results Tree listener in "RegExp Tester" mode to test your regular expressions directly against response without having to re-run the request.
See Regular Expressions JMeter documentation chapter and How to debug your Apache JMeter script guide for extended information on the above approaches.

Freemarker string escaping doesn't work with default operator

We use Freemarker 2.3.20 and came across a strange behavior, when using the default operator together with string escaping like this:
${picture.#author[0]!""?js_string}
in this case, quotes in the authors value are not escaped if !"" is present.
We need to check first for the value and can't use the default op:
<#if picture.#author[0]??>${picture.#author[0]?js_string}</#if>
this is quite ugly and blown up code.
Is this a bug or a feature?
It's because of the operator precedences. ${picture.#author[0]!""?js_string} means ${picture.#author[0]!(""?js_string)}. What you want is ${(picture.#author[0]!"")?js_string}.

Problem With Regular Expression to Remove HTML Tags

In my Ruby app, I've used the following method and regular expression to remove all HTML tags from a string:
str.gsub(/<\/?[^>]*>/,"")
This regular expression did just about all I was expecting it to, except it caused all quotation marks to be transformed into “
and all single quotes to be changed to ”
.
What's the obvious thing I'm missing to convert the messy codes back into their proper characters?
Edit: The problem occurs with or without the Regular Expression, so it's clear my problem has nothing to do with it. My question now is how to deal with this formatting error and correct it. Thanks!
Use CGI::unescapeHTML after you perform your regular expression substitution:
CGI::unescapeHTML(str.gsub(/<\/?[^>]*>/,""))
See http://www.ruby-doc.org/core/classes/CGI.html#M000547
In the above code snippet, gsub removes all HTML tags. Then, unescapeHTML() reverts all HTML entities (such as <, &#8220) to their actual characters (<, quotes, etc.)
With respect to another post on this page, note that you will never ever be passed HTML such as
<tag attribute="<value>">2 + 3 < 6</tag>
(which is invalid HTML); what you may receive is, instead:
<tag attribute="<value>">2 + 3 < 6</tag>
The call to gsub will transform the above to:
2 + 3 < 6
And unescapeHTML will finish the job:
2 + 3 < 6
You're going to run into more trouble when you see something like:
<doohickey name="<foobar>">
You'll want to apply something like:
gsub(/<[^<>]*>/, "")
...for as long as the pattern matches.
This regular expression did just about
all I was expecting it to, except it
caused all quotation marks to be
transformed into “ and all
single quotes to be changed to ”
.
This doesn't sound as if the RegExp would be doing this. Are you sure it's different before?
See this question here for information about the problem, it has got an excellent answer:
Get non UTF-8 form fields as UTF-8 in php.
I've run into a similar problem with character changes, this happened when my code ran through another module that enforced UTF-8 encoding and then when it came back, I had a different file (slurped array of lines) on my hands.
You could use a multi-pass system to get the results you are looking for.
After running your regular expression, run an expression to convert &8220; to quotes and another to convert &8221; to single quotes.

Resources