How to change states for Daikin AC integration from node-red function node?

Hi. I have Daikin AC integration with the some states.


I want to add logic to automatically turn off this device. I can get states value from function node, but I can’t change state.

I think I use wrong syntax for change climate.daikinap81365 state. Maybe someone can correct me.

OK, so your Home Assistant has the Daikin AC in an integration, which provides the A/C unit as an entity under the climate domain, as well as a bunch of sensors.

The HA entity climate.daikinap81365 has a state, and has a bunch of attributes.

In Home Assistant, you will find that you can use a service call, using the ‘Climate’ domain, and a service ‘Turn off’. This service call requires a target entity, which will be your climate.daikinap81365.

If you test this out in HA, using Developer Tools > Services, you should be able to turn off the A/C unit.

If you cannot run this service call in HA, there is no way you can get it to work in Node-RED, so please test it in HA first.

Now, the global variable that holds homeassistant states is read only and there is no way to change these values. If you attempt to change these values, then nothing will happen as they are copied from HA to NR, but not the other way around.

To change things in HA, from Node-RED, we use service calls.

If you use the Call Service node, and set this up with domain ‘climate’ and service ‘turn_off’ and entity ‘climate.dainkinap81236’, then this should call the ‘turn off’ service and turn off your A/C unit.

Since ‘turn off’ does nothing complicated, there is no Data field required.

And just to make it clear.
You can not set globals in a function node and then get a change on the device.
If the globals are set, then that is all that will happen and a few seconds later the actual device state will typically be updated and reset the globals back to that value again.
Service calls are the way to make a change on a device.

I try to call service from HA and this action is failed.

Are there ways to still control the air conditioner from NR?

Every service call requires

  • a domain (in this case ‘climate’)
  • a service (in this case ‘set HVAC mode’)
  • a target entity (in this case the Daikinap81365)
  • optionally additional settings in the ‘DATA’ object

Some service calls - like ‘turn_on’ don’t require anything else.
Some service calls - like ‘set HVAC mode’ require the HVAC mode setting

You will see that, to the right of HVAC mode is an entry box into which you can select one of the many mode (you will see what modes your A/C unit supports via the integration by looking at the entity, and the hvac_modes attributes)

The error message ‘… required key not provided @ data[‘hvac_mode’]. Got None’ is telling you that the service call, for this particular call (set HVAC mode) is expecting a Data object as {“hvac_mode”: “heat”} and it has not found the key “hvac_mode”.

And, when you try to do this in Node-RED, you will need to set up the Data object yourself

{“hvac_mode”: “cool”}

or, if you want to use the input message, eg msg.mode, then using J: Expresson (JSONata)

{“hvac_mode”: mode}

will do the trick.

There is lots of documentation on the Call Service node …

https://zachowj.github.io/node-red-contrib-home-assistant-websocket/guide/call-service.html

https://zachowj.github.io/node-red-contrib-home-assistant-websocket/cookbook/jsonata/call-service.html

I already figured out how to call this service and it works for me like this:

Now I interesting can I do it from Function node, with syntax used for it?
There are no examples in this documentation and it is difficult to understand how to set a value for a service.
Function node

Calling a Service from Node-RED is done using the Call Service node. This node has lots of documentation on how to use it, and how to set up the ‘data’ object to make it all work.

You do not call a service from the Function node. That is why the Function node does not have documentation about calling a service. Function nodes are for doing stuff (as a last resort) that you cannot do in any other regular node.

If you want to know how to set the message input for a Call Service node, then it is all documented in

https://zachowj.github.io/node-red-contrib-home-assistant-websocket/cookbook/jsonata/call-service.html

Since you can do the necessary coding in the Call Service node itself, or in a Change node, there is really no benefit from using a function node. However, if you really really want to use a function node, then the Call Service node requires an input msg.payload as an object, something like:

{
    "domain": "climate",
    "service": "set_hvac_mode",
    "target": {
        "entity_id": [ "climate.daikinap81365" ]
    }
    "data": {
        "hvac_mode": "off"
    }
}

The input values are optional and will override or merge with any UI configuration in the node itself. Again, you need to read the documentation for the Call Service node.