Faire une opération mathématique sur une “variable de scène”

Hello,

Thank you for sharing this API! And thanks for the adjustments currently underway to make it usable.

I tried to inject the calculation result into one of my devices but it doesn’t work.

The post URL: https://api.gladysgateway.com/v1/api/device/state/

I tried the injection without the variable and it works.

Am I doing something wrong or is the API result not in the correct format?

Thank you for your help

Are you sure about the content of the variable? Have you tested in a « send a message » action what it returns?

Hello @pierre-gilles, sending a message works fine.

Just in case, here is the excerpt of the MQTT device feature:

@PhilippeMA Everything seems fine to me. I went to check on the Gladys Plus side to see if I could spot an error, but nothing.

Could you maybe check your Gladys logs at the moment you send the request?

Good catch, there’s an error:

2022-10-04T10:04:30+0200 \u003cwarn\u003e scene.executeActions.js:37 (executeAction) Error: Parse error on line 2:
...": {{1.0.data.result}}}
-----------------------^
Expecting 'CLOSE', 'OPEN_SEXPR', 'ID', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'CLOSE_UNESCAPED'
    at Parser.parseError (/src/server/node_modules/handlebars/dist/cjs/handlebars/compiler/parser.js:267:19)
    at Parser.parse (/src/server/node_modules/handlebars/dist/cjs/handlebars/compiler/parser.js:336:30)
    at parseWithoutProcessing (/src/server/node_modules/handlebars/dist/cjs/handlebars/compiler/base.js:46:33)
    at HandlebarsEnvironment.parse (/src/server/node_modules/handlebars/dist/cjs/handlebars/compiler/base.js:52:13)
    at compileInput (/src/server/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:508:19)
    at ret (/src/server/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:517:18)
    at Object.http.request (/src/server/lib/scene/scene.actions.js:250:76)
    at executeAction (/src/server/lib/scene/scene.executeActions.js:32:35)
    at /src/server/lib/scene/scene.executeActions.js:66:13
    at tryCatcher (/src/server/node_modules/bluebird/js/release/util.js:16:23)
    at MappingPromiseArray._promiseFulfilled (/src/server/node_modules/bluebird/js/release/map.js:68:38)
    at MappingPromiseArray.PromiseArray._iterate (/src/server/node_modules/bluebird/js/release/promise_array.js:115:31)
    at MappingPromiseArray.init (/src/server/node_modules/bluebird/js/release/promise_array.js:79:10)
    at MappingPromiseArray._asyncInit (/src/server/node_modules/bluebird/js/release/map.js:37:10)
    at _drainQueueStep (/src/server/node_modules/bluebird/js/release/async.js:97:12)
    at _drainQueue (/src/server/node_modules/bluebird/js/release/async.js:86:9)
    at Async._drainQueues (/src/server/node_modules/bluebird/js/release/async.js:102:5)
    at Immediate.Async.drainQueues (/src/server/node_modules/bluebird/js/release/async.js:15:14)
    at processImmediate (internal/timers.js:464:21)

Can you confirm that you are actually running the scene by launching the scene? Not by clicking « Try »? (that won’t work as stated in the description)

Ah, actually I see what’s wrong, @PhilippeMA, are

Ah, OK, my apologies, I hadn’t realized that’s what was being fixed.

I confirm, it’s my production environment.

Thanks @pierre-gilles.

Fix published in Gladys Assistant v4.11 :slight_smile:

2 Likes

Hello,

I’m encountering a bug while using the following scene:

and the request sent is the following:

Did I make a mistake or is it another bug?

In your expression, if you have spaces, mathjs returns an error.
If you’re on mobile and you add a variable in a gladys block, I’ve noticed that it always adds a space after it.
Try removing the spaces in case that’s where it’s coming from.

However, the request sent does not contain the variable’s value, only an expression of the form {{0.0.last_value}}, which is why mathjs returns an error.

Maybe it’s because your variable doesn’t contain the right value? You retrieved it via an API, I suppose?
Can you show how you set it?

I am receiving the value correctly in the sent message.

The value comes from a device created via MQTT

device updated on request in Node-RED

In fact, this is the same as the bug encountered by lmilcent on 22/09, a bug that had been fixed. A regression?

Don’t forget that the scene must trigger itself for it to work. The « tester » button does not send a real value.

You have a very long device and feature name — maybe that’s the problem?

That’s fine, indeed I was testing with the ‹ Try › button. And I also confirm that you need to remove the spaces, otherwise there is no calculation result.
Thank you for the information and for your responsiveness.

1 Like

Hi, as mentioned here, I started looking into being able to perform calculations directly in scenes. I’m doing this on the « condition » and « control a device » blocks — here’s what it looks like so far

Now I need your help. Quick explanation: once the variables are replaced by values during the execution of the scene, we use the mathjs library to perform the calculation. The problem is that the evaluate() method could allow injecting unwanted JS code into Gladys, so we must restrict this method to the mathematical functions that we really want to use in Gladys. By default, we’ll have at least the standard operators +, -, *, /. What other mathematical functions would you like to use (or already use with the mathjs API)?

A list is available here math.js | an extensive math library for JavaScript and Node.js

3 Likes

Great, thanks for your help in moving the topic forward :blush:

I’d go with the basic operations you mentioned, personally I haven’t been able to use anything else yet.
Don’t forget the parentheses, obviously!

1 Like