Create a temporary scene in a function

Hi All,

I'm trying to create a temporary scene using a function node, but I'm struggling with the data field for the service I am calling to create the scene. Normally the data field, using JSON would look like this:

{"scene_id":"lounge_climate_scene","snapshot_entities":"climate.lounge"}

Trying to add it to the function below errors - I'm guessing due to the quotes, and brackets.

case 'climate.lounge':
    if (msg.payload == 'auto') {
      msg.domain = 'scene'
      msg.action = 'create'
      msg.data = '{"scene_id":"lounge_climate_scene","snapshot_entities":"climate.lounge"}'
    } else {
      msg.domain = 'scene'
      msg.action = 'turn_on'
      msg.data = 'lounge_climate_scene'
    }
    break;

Can anyone advise where I am going wrong please?

Thanks
Leacho

Your syntax is a little bit off. Try this and see if it works for you...
sat_B
sat_A

My JavaScript code in the function node is...

switch (msg.topic) {
  case 'climate.lounge':
    if (msg.payload == 'auto') {
      msg.domain = 'scene';
      msg.action = 'create';
      msg.data = '{"scene_id":"lounge_climate_scene","snapshot_entities":"climate.lounge"}';
    } else {
      msg.domain = 'scene';
      msg.action = 'turn_on';
      msg.data = 'lounge_climate_scene';
    }
    return msg; // Moved inside the switch case block
    break; // Optional, as return statement exits the block
  // Other cases...
}

[{"id":"04d5b6390d20da79","type":"tab","label":"Flow 46","disabled":false,"info":"","env":[]},{"id":"a5c4894da83b581b","type":"function","z":"04d5b6390d20da79","name":"function","func":"switch (msg.topic) {\n  case 'climate.lounge':\n    if (msg.payload == 'auto') {\n      msg.domain = 'scene';\n      msg.action = 'create';\n      msg.data = '{\"scene_id\":\"lounge_climate_scene\",\"snapshot_entities\":\"climate.lounge\"}';\n    } else {\n      msg.domain = 'scene';\n      msg.action = 'turn_on';\n      msg.data = 'lounge_climate_scene';\n    }\n    return msg; // Moved inside the switch case block\n    break; // Optional, as return statement exits the block\n  // Other cases...\n}\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":140,"wires":[["52b76b8a2a9f502d"]]},{"id":"5c4b20273b5bf96e","type":"inject","z":"04d5b6390d20da79","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"climate.lounge","payload":"auto","payloadType":"str","x":150,"y":100,"wires":[["a5c4894da83b581b"]]},{"id":"52b76b8a2a9f502d","type":"debug","z":"04d5b6390d20da79","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":530,"y":140,"wires":[]},{"id":"348b22d034b4d946","type":"inject","z":"04d5b6390d20da79","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"climate.lounge","payload":"manual","payloadType":"str","x":160,"y":180,"wires":[["a5c4894da83b581b"]]}]

Thanks @dynamicdave

Nearly there I think - I don't seem to be able to pass it into my call-service node however:

I would imagine its to do with the formatting in the function?

Thanks
Leacho

You didn't mention a 'call-service' node in your original post.
If it's a Home Assistant node then I have no idea (as not many people use HA on this forum).

One thing I spotted...
the if statement returns an object
whereas the else returns a string

Maybe this is upsetting what your 'call-service' is expecting to receive?

I don't think you want those single quotes around this object ' just the {}

Ha, ha - well spotted Steve and you get today's star prize' :champagne: :dizzy: :asterisk:

Thanks both, even when removing the quotes I still get the same error.

Do not use :J in data field as there is no need to call the JSONata functions.
Use
msg. and just use data

2 Likes

Spot on thank you!!

Thank you also @dynamicdave and @smcgann99 for your assistance! All working now!