Weird behavior node red switch

Guten Tag!
(Hello there!)

I´ve created a sensor to monitor the suns azimuth corrected by the rotation of my house. (only a few degrees but I did it anyways)
Then in Node Red I’ve set up a switch to turn on a bool if the azimuth is between 110 aund 250 degrees.

So far so good.
But at night for some reason the bool turns on and I don’t get why…
One time after the jump from 360 to 0 degrees and another time at arround 2:45 until 3:30.
(Would have put a picture of the history but am only allowed to post one picture)

Only thing I have noticed: In the first fals actication is between 1,1 and 2,5 and the second the azimuth is between 11 and 25.
Does the switch ignor the comma? Is there something I can do about this?

Lukas

You need to export it so that we see.

Sorry to be the rookie here but what do I need to export?

The sequence you posted an image of.
The image doesn’t say much, but if we get an export then we can see how it’s configured.

Ah ok. Than it should be this :slight_smile:

[
    {
        "id": "68836526b4ee5676",
        "type": "server-state-changed",
        "z": "074165d888588dac",
        "name": "",
        "server": "fa180590.ed60c8",
        "version": 4,
        "exposeToHomeAssistant": false,
        "haConfig": [
            {
                "property": "name",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            }
        ],
        "entityidfilter": "sensor.sun_azimuth",
        "entityidfiltertype": "exact",
        "outputinitially": false,
        "state_type": "str",
        "haltifstate": "",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "outputs": 1,
        "output_only_on_state_change": true,
        "for": "0",
        "forType": "num",
        "forUnits": "minutes",
        "ignorePrevStateNull": false,
        "ignorePrevStateUnknown": false,
        "ignorePrevStateUnavailable": false,
        "ignoreCurrentStateUnknown": false,
        "ignoreCurrentStateUnavailable": false,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "data",
                "propertyType": "msg",
                "value": "",
                "valueType": "eventData"
            },
            {
                "property": "topic",
                "propertyType": "msg",
                "value": "",
                "valueType": "triggerId"
            }
        ],
        "x": 280,
        "y": 120,
        "wires": [
            [
                "01ce31ae375be331"
            ]
        ]
    },
    {
        "id": "01ce31ae375be331",
        "type": "switch",
        "z": "074165d888588dac",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "gte",
                "v": "110",
                "vt": "str"
            },
            {
                "t": "else"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 530,
        "y": 120,
        "wires": [
            [
                "9616b72121d026d3"
            ],
            [
                "ce487844b79d589f"
            ]
        ]
    },
    {
        "id": "9616b72121d026d3",
        "type": "switch",
        "z": "074165d888588dac",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "lte",
                "v": "250",
                "vt": "str"
            },
            {
                "t": "else"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 670,
        "y": 100,
        "wires": [
            [
                "b192a069be3f2cf9"
            ],
            [
                "ce487844b79d589f"
            ]
        ]
    },
    {
        "id": "b192a069be3f2cf9",
        "type": "api-call-service",
        "z": "074165d888588dac",
        "name": "",
        "server": "fa180590.ed60c8",
        "version": 5,
        "debugenabled": false,
        "domain": "input_boolean",
        "service": "turn_on",
        "areaId": [],
        "deviceId": [],
        "entityId": [
            "input_boolean.rolladen_azimuth_trigger"
        ],
        "data": "",
        "dataType": "jsonata",
        "mergeContext": "",
        "mustacheAltTags": false,
        "outputProperties": [],
        "queue": "none",
        "x": 880,
        "y": 80,
        "wires": [
            []
        ]
    },
    {
        "id": "ce487844b79d589f",
        "type": "api-call-service",
        "z": "074165d888588dac",
        "name": "",
        "server": "fa180590.ed60c8",
        "version": 5,
        "debugenabled": false,
        "domain": "input_boolean",
        "service": "turn_off",
        "areaId": [],
        "deviceId": [],
        "entityId": [
            "input_boolean.rolladen_azimuth_trigger"
        ],
        "data": "",
        "dataType": "jsonata",
        "mergeContext": "",
        "mustacheAltTags": false,
        "outputProperties": [],
        "queue": "none",
        "x": 880,
        "y": 140,
        "wires": [
            []
        ]
    }
]

is sensor.sun_azimuth a template sensor from sun.sun?
The Node red part looks correct. Could it be that you have done restarts when it happens?

Is it going unavailable? Wouldn’t hurt to check these boxes.

image

I assume you are using sun.sun for the template sensor, you can just set you event state to output the azimuth.

[{"id":"6171b1038a96e13d","type":"server-state-changed","z":"f80b6c338afd5483","name":"","server":"","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sun.sun","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":true,"ignorePrevStateUnknown":true,"ignorePrevStateUnavailable":true,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"$entity().attributes.azimuth","valueType":"jsonata"}],"x":190,"y":500,"wires":[["29512de9cce52c7f"]]}]

The sensor is from an template that subtracts the “rotation of the house” from the azimuth given by sun.sun, so that a azimuth of 180 is the time when the sun is shining directly at the windows.

This doesn’t seem to be problematic, because watching at the history graph for this sensor it acts as expected.

At least neither the Log nor the history are saying it went unavailable. And every value that is shown in the history seems to be the right one.

I will check these boxes either way when I’m back home on Sunday.

How does the switch compare the values? Because I set the values from 110 to 250 and the gals activations are happening between 1,1 - 2,5 and 11 - 25

The only way to know for sure is if you post your template sensor also.

I can post my sensor on Sunday, but I copy pasted the sensor from this Thread and just changed the offset.

If you just remove 180 from that then you will get a negative value some times of the day.

You are also using string representations for the state output and for comparison.

I note that from the entity state node, your returned value is “104.48” which may well be a float number, and then you go on to compare this with “110” as a string, and being in Germany your system is probably working with 104,48 if it gets the chance.

The entity state node has the option for the entity state to be a string or a number - the output from HA history is I think always a string, but getting a live state from your template entity may mean you are actually turning 1,55 to “1.55” just to output a string when you could be using a number. Using number forces/converts/keeps the output as a number, which will be a float with 2 decimal places.

The comparison in switch node should ideally also be a number not a string. Comparing numbers as strings has potential for many issues.

Using whole integer numbers (albeit in strings) to compare with floats further compounds the opportunity for confusion. I always try to compare like with like - 15 to 110 or 15.5 to 110.0 (or 15,5 to 110,0 in your case) but not “1.55” to “110,0”.

May I suggest setting the entity state node entity state to ‘number’ and changing the switch comparison to number as 110 and 250 (use ‘between’ option too, saves a node)? Also consider converting your template output to integer and using integer values throughout?

My guess is it is down to HA working with “.” as decimal separator, and your system working with “,” as decimal separator, and shuffling between numbers, floats, and strings.

1 Like

As mentioned above NR brings some numbers in as strings. Look at the debug.

If it is a string i.e. the number is in quotes, you can use

$number($entity().state)

At the bottom of the node where it says output options to force it to a number.

image

Then for the switch node, I’d use is between instead of < and >.

image

Morning,
Finally I was able to play a little bit more.

The solution was to just set it to numbers, when comparing in the switch node. I honestly didn’t knew it existed, always thougt it would detect if I was trying to compare numbers or strings… Ups :smiley:
Althoug it compared the numbers but got verry confused with the comma/dot wich I still dont really get why.

The “is between” setting is propably something I should use.
And ´ $number ´ to force it into using numbers is good to know!

Thanks a lot :+1:
Lukas