# How to save several states of a climate the best?

**URL:** <https://discourse.nodered.org/t/how-to-save-several-states-of-a-climate-the-best/69525>\
**Category:** General\
**Tags:** home-assistant\
**Created:** [25 October 2022 05:47 UTC](https://discourse.nodered.org/t/how-to-save-several-states-of-a-climate-the-best/69525 "2022-10-25T05:47:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![u20p17](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/u20p17/32/41780_2.png) [@u20p17](https://discourse.nodered.org/u/u20p17)\
**Post date:** [25 October 2022 05:47 UTC](https://discourse.nodered.org/t/how-to-save-several-states-of-a-climate-the-best/69525/1 "2022-10-25T05:47:31Z")

</div>

Hi all,  
I have mounted a window open/closed sensor at my window (which sends the data via mqtt) and I am planning to do the following with NodeRed (and HomeAssistant):  
If I open the window, NodeRed should save the climate settings and turn the climate off. When the window gets closed again it should turn on the climate with the same setting than before.

The following settings are possible with the climate:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/9/19b54036142affccba1ea68a3eda07c71a2fb3e6.png)

At the moment I save the climate state in a flow variable like this:

```auto
[
    {
        "id": "97421084ccfd057c",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "5e5c8a874d64033a",
        "type": "api-call-service",
        "z": "97421084ccfd057c",
        "name": "",
        "server": "42c5021e.042e7c",
        "version": 5,
        "debugenabled": false,
        "domain": "climate",
        "service": "turn_off",
        "areaId": [],
        "deviceId": [],
        "entityId": [
            "climate.schlafzimmer_2"
        ],
        "data": "",
        "dataType": "jsonata",
        "mergeContext": "",
        "mustacheAltTags": false,
        "outputProperties": [],
        "queue": "none",
        "x": 1640,
        "y": 435,
        "wires": [
            []
        ]
    },
    {
        "id": "0e1960f6671ea943",
        "type": "api-call-service",
        "z": "97421084ccfd057c",
        "name": "",
        "server": "42c5021e.042e7c",
        "version": 5,
        "debugenabled": false,
        "domain": "climate",
        "service": "set_hvac_mode",
        "areaId": [],
        "deviceId": [],
        "entityId": [
            "climate.schlafzimmer_2"
        ],
        "data": "",
        "dataType": "jsonata",
        "mergeContext": "",
        "mustacheAltTags": false,
        "outputProperties": [],
        "queue": "none",
        "x": 1670,
        "y": 360,
        "wires": [
            []
        ]
    },
    {
        "id": "470a7259b1705568",
        "type": "change",
        "z": "97421084ccfd057c",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "climate_state_before",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1135,
        "y": 435,
        "wires": [
            [
                "5e5c8a874d64033a"
            ]
        ]
    },
    {
        "id": "bdcb58795f5e7b70",
        "type": "api-current-state",
        "z": "97421084ccfd057c",
        "name": "",
        "server": "42c5021e.042e7c",
        "version": 3,
        "outputs": 1,
        "halt_if": "",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "entity_id": "climate.schlafzimmer_2",
        "state_type": "str",
        "blockInputOverrides": false,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "data",
                "propertyType": "msg",
                "value": "",
                "valueType": "entity"
            }
        ],
        "for": "0",
        "forType": "num",
        "forUnits": "minutes",
        "override_topic": false,
        "state_location": "payload",
        "override_payload": "msg",
        "entity_location": "data",
        "override_data": "msg",
        "x": 840,
        "y": 435,
        "wires": [
            [
                "470a7259b1705568"
            ]
        ]
    },
    {
        "id": "ddf3599b80f08a1e",
        "type": "switch",
        "z": "97421084ccfd057c",
        "name": "climate state before",
        "property": "climate_state_before",
        "propertyType": "flow",
        "rules": [
            {
                "t": "eq",
                "v": "dry",
                "vt": "jsonata"
            },
            {
                "t": "eq",
                "v": "cool",
                "vt": "jsonata"
            },
            {
                "t": "eq",
                "v": "heat",
                "vt": "jsonata"
            },
            {
                "t": "eq",
                "v": "heat_cool",
                "vt": "jsonata"
            },
            {
                "t": "eq",
                "v": "fan_only",
                "vt": "jsonata"
            },
            {
                "t": "eq",
                "v": "off",
                "vt": "jsonata"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 6,
        "x": 1380,
        "y": 375,
        "wires": [
            [
                "0e1960f6671ea943"
            ],
            [
                "0e1960f6671ea943"
            ],
            [
                "0e1960f6671ea943"
            ],
            [
                "0e1960f6671ea943"
            ],
            [
                "0e1960f6671ea943"
            ],
            [
                "0e1960f6671ea943"
            ]
        ]
    },
    {
        "id": "13a90ccbaa9aeb7c",
        "type": "change",
        "z": "97421084ccfd057c",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "climate_state_before",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1130,
        "y": 375,
        "wires": [
            [
                "ddf3599b80f08a1e"
            ]
        ]
    },
    {
        "id": "9f0f62575467ed4d",
        "type": "api-current-state",
        "z": "97421084ccfd057c",
        "name": "",
        "server": "42c5021e.042e7c",
        "version": 3,
        "outputs": 1,
        "halt_if": "",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "entity_id": "climate.schlafzimmer_2",
        "state_type": "str",
        "blockInputOverrides": false,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "data",
                "propertyType": "msg",
                "value": "",
                "valueType": "entity"
            }
        ],
        "for": "0",
        "forType": "num",
        "forUnits": "minutes",
        "override_topic": false,
        "state_location": "payload",
        "override_payload": "msg",
        "entity_location": "data",
        "override_data": "msg",
        "x": 835,
        "y": 375,
        "wires": [
            [
                "13a90ccbaa9aeb7c"
            ]
        ]
    },
    {
        "id": "5569d4be19f0d116",
        "type": "switch",
        "z": "97421084ccfd057c",
        "name": "closed/open",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "true",
                "vt": "jsonata"
            },
            {
                "t": "eq",
                "v": "false",
                "vt": "jsonata"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 535,
        "y": 405,
        "wires": [
            [
                "9f0f62575467ed4d"
            ],
            [
                "bdcb58795f5e7b70"
            ]
        ]
    },
    {
        "id": "30a66f6407b77831",
        "type": "mqtt in",
        "z": "97421084ccfd057c",
        "name": "Schlafzimmersensor",
        "topic": "zigbee2mqtt/Schlafzimmerfenster rechts",
        "qos": "2",
        "datatype": "json",
        "broker": "b496b411.fe5358",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 155,
        "y": 405,
        "wires": [
            [
                "c51b1d3eeaa4c37f"
            ]
        ]
    },
    {
        "id": "c51b1d3eeaa4c37f",
        "type": "change",
        "z": "97421084ccfd057c",
        "name": "closed/opened",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.contact",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 355,
        "y": 405,
        "wires": [
            [
                "5569d4be19f0d116"
            ]
        ]
    },
    {
        "id": "42c5021e.042e7c",
        "type": "server",
        "name": "Home Assistant",
        "version": 5,
        "addon": false,
        "rejectUnauthorizedCerts": true,
        "ha_boolean": "y|yes|true|on|home|open",
        "connectionDelay": true,
        "cacheJson": true,
        "heartbeat": false,
        "heartbeatInterval": "30",
        "areaSelector": "friendlyName",
        "deviceSelector": "friendlyName",
        "entitySelector": "friendlyName",
        "statusSeparator": "at: ",
        "statusYear": "hidden",
        "statusMonth": "short",
        "statusDay": "numeric",
        "statusHourCycle": "h23",
        "statusTimeFormat": "h:m",
        "enableGlobalContextStore": true
    },
    {
        "id": "b496b411.fe5358",
        "type": "mqtt-broker",
        "name": "MQTT",
        "broker": "192.168.0.2",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "sessionExpiry": ""
    }
]

```

(I didn't test the flow up to now so there could be an error in it...)  
The problem with this is, that I would need several flow variables (hvac\_modes, fan\_modes, preset\_modes...) and I though that there might be a simpler solution... Do you have any advise for me?

Thanks!

BR

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [25 October 2022 06:34 UTC](https://discourse.nodered.org/t/how-to-save-several-states-of-a-climate-the-best/69525/2 "2022-10-25T06:34:06Z")

</div>

> (I didn't test the flow up to now so there could be an error in it...)

We cannot test your flow as we don't have your devices (and/or are not using home assistant)

I thought you would be able to handle this directly in home assistant ?

---

<div class="post-metadata">

**Author:** ![u20p17](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/u20p17/32/41780_2.png) [@u20p17](https://discourse.nodered.org/u/u20p17)\
**Post date:** [25 October 2022 06:40 UTC](https://discourse.nodered.org/t/how-to-save-several-states-of-a-climate-the-best/69525/3 "2022-10-25T06:40:11Z")

</div>

Thanks for the answer. I understand that you can not test it due due missing nodes... What I am asking for is the approach how to save several states. Maybe there is another way (instead of several flow variables) which suits better for several states... 🙂

BR

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [25 October 2022 07:30 UTC](https://discourse.nodered.org/t/how-to-save-several-states-of-a-climate-the-best/69525/4 "2022-10-25T07:30:14Z")

</div>

> which suits better for several states...

This was my point of home assistant: it is a state machine, it knows current and previous values, you should be able to restore the previous state with [state triggers](https://www.home-assistant.io/docs/automation/trigger/#state-trigger) ?

Managing a state in node-red with home-assistant in between only makes things more complex.  
I would choose one or the other.

If you update a state via mqtt, does this not automatically update home assistant ?  
If yes, just save all previous states in flow variables and let it get triggered/handled through node-red directly.

Then again, I don't know how home-assistant works with the node-red nodes.

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [25 October 2022 14:15 UTC](https://discourse.nodered.org/t/how-to-save-several-states-of-a-climate-the-best/69525/5 "2022-10-25T14:15:20Z")

</div>

While bakman2's reply seems to be the way to go why not store all states in one object? (can't use your flow at the moment so you may already do this)

---

<div class="post-metadata">

**Author:** ![u20p17](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/u20p17/32/41780_2.png) [@u20p17](https://discourse.nodered.org/u/u20p17)\
**Post date:** [25 October 2022 14:39 UTC](https://discourse.nodered.org/t/how-to-save-several-states-of-a-climate-the-best/69525/6 "2022-10-25T14:39:06Z")

</div>

Hi,  
I will try it the way bakman2 suggested. Nevertheless, can you give me a small demo flow how to store more state in one object?

BR

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [25 October 2022 17:09 UTC](https://discourse.nodered.org/t/how-to-save-several-states-of-a-climate-the-best/69525/7 "2022-10-25T17:09:34Z")

</div>

As I said I cannot load your flow at the moment so I have no idea how your data is presented (show what you get as an input in a debug node) but using the Statusattribute that you showed as an example

if the payload contains the complete climate data

```auto
const climate = msg.payload

flow.set('climate', climate)

```

then get the various attributes

```auto
const climate = flow.get('climate')

let hvacMode = climate.hvac_mode
...
let friendlyName = climate.friendly_name

```

or altogether

```auto
// Use deconstruction to get object values
let [hvac_mode, min_temp, max_temp, fan_mode, preset_mode, swing_mode, current_temperature, temperature, fan_mode, friendly_name] = flow_get('climate')

```

If you do not get the data in one payload you just create the object and save the individual elements to it as they arrive

---

<div class="post-metadata">

**Author:** ![u20p17](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/u20p17/32/41780_2.png) [@u20p17](https://discourse.nodered.org/u/u20p17)\
**Post date:** [25 October 2022 18:11 UTC](https://discourse.nodered.org/t/how-to-save-several-states-of-a-climate-the-best/69525/8 "2022-10-25T18:11:30Z")

</div>

`the msg.data looks like this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/d/3d56144bc579a966a1582f996117d9e8ddbdccee.png)  
`  
if I understand you right I take a function node where I put in

> const climate = msg.data
> 
> flow.set('climate', climate)

and in another function node I can use the flow.get to get the whole object and with e.g.

> let swing\_modes= climate.swing\_modes

I would get the selected swing mode. Am I correct? This is very nice to know because I can use this for other things too 😃

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [25 October 2022 19:53 UTC](https://discourse.nodered.org/t/how-to-save-several-states-of-a-climate-the-best/69525/9 "2022-10-25T19:53:37Z")

</div>

Yes, although apart from selection options you can probably ignore the ` *_modes`

arrays as they will not change

---

<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:** [24 December 2022 19:54 UTC](https://discourse.nodered.org/t/how-to-save-several-states-of-a-climate-the-best/69525/10 "2022-12-24T19:54:34Z")

</div>

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