# Create a temporary scene in a function

**URL:** <https://discourse.nodered.org/t/create-a-temporary-scene-in-a-function/86692>\
**Category:** General\
**Created:** [23 March 2024 12:46 UTC](https://discourse.nodered.org/t/create-a-temporary-scene-in-a-function/86692 "2024-03-23T12:46:40Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![leacho73](https://avatars.discourse-cdn.com/v4/letter/l/2bfe46/32.png) [@leacho73](https://discourse.nodered.org/u/leacho73)\
**Post date:** [23 March 2024 12:46 UTC](https://discourse.nodered.org/t/create-a-temporary-scene-in-a-function/86692/1 "2024-03-23T12:46:40Z")

</div>

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.

```auto
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

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [23 March 2024 13:31 UTC](https://discourse.nodered.org/t/create-a-temporary-scene-in-a-function/86692/2 "2024-03-23T13:31:53Z")

</div>

Your syntax is a little bit off. Try this and see if it works for you...  
 ![sat_B](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/1/e1d96b5d048e2d79269f6a9089a27758f29b4160.png)  
 ![sat_A](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/a/4aadc17070edf56b03986f8d8e51e76a87844335.png)

My JavaScript code in the function node is...

```auto
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...
}

```

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

**Author:** ![leacho73](https://avatars.discourse-cdn.com/v4/letter/l/2bfe46/32.png) [@leacho73](https://discourse.nodered.org/u/leacho73)\
**Post date:** [23 March 2024 13:46 UTC](https://discourse.nodered.org/t/create-a-temporary-scene-in-a-function/86692/3 "2024-03-23T13:46:56Z")

</div>

Thanks @dynamicdave

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

 ![Screenshot 2024-03-23 at 13.44.39](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/6/d6866b0bd1b890660a09f3b5d763a0ccb5c30666.png)

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

Thanks  
Leacho

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [23 March 2024 14:09 UTC](https://discourse.nodered.org/t/create-a-temporary-scene-in-a-function/86692/4 "2024-03-23T14:09:21Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [23 March 2024 16:46 UTC](https://discourse.nodered.org/t/create-a-temporary-scene-in-a-function/86692/5 "2024-03-23T16:46:55Z")

</div>

> [@leacho73](#):
>
> ```auto
> msg.data = '{"scene_id":"lounge_climate_scene","snapshot_entities":"climate.lounge"}'
>   
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [23 March 2024 16:49 UTC](https://discourse.nodered.org/t/create-a-temporary-scene-in-a-function/86692/6 "2024-03-23T16:49:16Z")

</div>

Ha, ha - well spotted Steve and you get today's star prize' 🍾 💫 *️⃣

---

<div class="post-metadata">

**Author:** ![leacho73](https://avatars.discourse-cdn.com/v4/letter/l/2bfe46/32.png) [@leacho73](https://discourse.nodered.org/u/leacho73)\
**Post date:** [23 March 2024 17:12 UTC](https://discourse.nodered.org/t/create-a-temporary-scene-in-a-function/86692/7 "2024-03-23T17:12:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [23 March 2024 17:24 UTC](https://discourse.nodered.org/t/create-a-temporary-scene-in-a-function/86692/8 "2024-03-23T17:24:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![leacho73](https://avatars.discourse-cdn.com/v4/letter/l/2bfe46/32.png) [@leacho73](https://discourse.nodered.org/u/leacho73)\
**Post date:** [23 March 2024 17:43 UTC](https://discourse.nodered.org/t/create-a-temporary-scene-in-a-function/86692/9 "2024-03-23T17:43:42Z")

</div>

Spot on thank you!!

Thank you also @dynamicdave and @Sean-McG for your assistance! All working now!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [21 June 2024 17:44 UTC](https://discourse.nodered.org/t/create-a-temporary-scene-in-a-function/86692/10 "2024-06-21T17:44:08Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
