Change color mode on smart bulbs using call service function with node red

Hello,

Since a couple of months I am trying to learn home assistant, node red, java script only to make any kind of automatization, unfortunately I haven’t understood many things yet and I would like to understand some based things that are continue to block every kind of automatization I am trying to achieve.

At the time being, at home I have all smart wall switches with tasmota firmware which act as switches for all the smart bulbs in my house. I am using mqtt commands to comunicate between the smart switches and the smart bulbs.

Mainly I am using node red to build flow automatizations. Here I need to understand how to better use the call service node and in deep the field with the json data.

Usually to understand an entity which service could be activated or which attributes could be changed,

I am using the developer tool (state and service) and I look into the generated yaml code. The other thing is to use an inject+current state node+debug node in nodered.

Taking information from both, I am trying to write functions and calling call service nodes. In the data field of the call service node I am trying to put the json string built on the previous taken information.

Here I have the big problems. The entity in question is the light entity.
Here below two generated objects from the light state.

{
    "entity_id": "light.luci_camera",
    "state": true,
    "attributes": {
        "min_color_temp_kelvin": 2000,
        "max_color_temp_kelvin": 6535,
        "min_mireds": 153,
        "max_mireds": 500,
        "supported_color_modes": [
            "brightness",
            "color_temp",
            "hs"
        ],
        "color_mode": "hs",
        "brightness": 255,
        "hs_color": [
            48.134,
            2.008
        ],
        "rgb_color": [
            255,
            253,
            249
        ],
        "xy_color": [
            0.328,
            0.333
        ],
        "entity_id": [
            "light.luce_1cm",
            "light.luce_2cm",
            "light.luce_3cm"
        ],
{
    "entity_id": "light.luci_camera",
    "state": true,
    "attributes": {
        "min_color_temp_kelvin": 2000,
        "max_color_temp_kelvin": 6535,
        "min_mireds": 153,
        "max_mireds": 500,
        "supported_color_modes": [
            "brightness",
            "color_temp",
            "hs"
        ],
        "color_mode": "color_temp",
        "brightness": 255,
        "color_temp_kelvin": 6535,
        "color_temp": 153,
        "hs_color": [
            54.768,
            1.6
        ],
        "rgb_color": [
            255,
            254,
            250
        ],
        "xy_color": [
            0.326,
            0.333
        ],
        "entity_id": [
            "light.luce_1cm",
            "light.luce_2cm",
            "light.luce_3cm"
        ],

Here the variable that I need to change using the call service function is color_mode from “hs” to “color_temp” and viceversa.

Before going ahead, I achieved to change the colours of the smart bulbs with the following json string put in data field :

{
    "entity_id": "light.luci_camera",
    "rgb_color": [255,254,250]
}

And I succeeded to do this only after looking at the developer mode :

service: light.turn_on
data:
  rgb_color:
    - 255
    - 254
    - 250
target:
  entity_id: light.luci_camera

At the beginning I wrote as it shown in the debug window :

{
    "entity_id": "light.luci_camera",
    "attributes": {
        "rgb_color": [255,254,250]
     }
}

But it returned the error : “Call-service error. extra keys not allowed @ data[‘attributes’]”
So I deleted the “attributes” object from the json string.
Here I have a lot of questions, but mainly why? What is the right approach to understand for an entity which values can be modified and how to put them into the data field as json so it will work?
I lost every time a lot of time in order to find the right structure, is there exist a method which will help me to understand better, instead of doing many tries and lost hours until I achieve something?

Returning to my main question : how to change the color_mode as json string properly in the data field of the call service node so it will work? Or in other words how to switch from colour mode to while mode and viceversa?

Home Assistant reports properties other than “state” as being under “attributes” - you can see this in the States tab of Developer Tools. But this is not how you set them. To convert from Developer Tools to Node Red, generally you take the part under “data:” and stick it into “Data” in NR after manually converting from YAML to JSON. So with your YAML:

service: light.turn_on
data:
  rgb_color:
    - 255
    - 254
    - 250
target:
  entity_id: light.luci_camera

Converting the “data:” content to JSON:

{ "rgb_color": [255,254,250] }

The rest of the YAML goes in other properties of the node - “service:” is split into “Domain” and “Service”, and “target:” goes into “Area” or “Device” or “Entity” depending on your needs.

2 Likes

Thank you for the answer.
I have another question. I have a function node which is part of a more complex flow. This is the code :

var check = msg.data.attributes.brightness;
//node.warn("brightness: " + check);
if (check !== 255)

    return msg =
    {
        "entity_id": msg.entity_id,
        "execute": msg.execute,
        "payload": {
            "data": {
                "brightness_step": 64
                //"rgb_color": [255,200,255]
            }
        }
    }
else return msg =
{
    "entity_id": msg.entity_id,
    "execute": msg.execute,
    "payload": {
        "data": {
            "brightness": 64
            //"rgb_color": [255,200,255]
        }
    }
}

It outputs to a call service node : Imgur: The magic of the Internet
The execute variable takes the action from a previous node and it is used in this case to turn on the call service. This function simulates a dimmer switch, pressing multiple time a button of a switch, it increase the light until reach the maximum brightness and then it restarts from the minimum again.
If I remove from the call service the entity {{entity_id}}, the call service node returns : Imgur: The magic of the Internet
Do you have any idea, how to modify the json string inside the function node, so it could work without specifying the field entity in the call service node?
I know for sure that it could be done, I have an example which uses arrays and it works, but I didn’t understand it.