Trouble with UTC and lighting automation

#1

Hi all - I'm struggling with what should be simple, time-based lighting automation. Hoping there's a simple, elegant solution out there that I'm just not grasping yet...

The automation involves a motion sensor. When it's on, it checks the time. If the time is between 08:00 and 21:00, it sets the brightness to the maximum value. If it's outside of that range, it sets it to 5% brightness.

My issue is that everything in the flow shows up as UTC. As a result, the time range parameters are off, and the automation doesn't work the way I expect. I read in this thread that Node-RED defaults to UTC to avoid timezone and DST issues which makes sense, I just don't know how to work with it (I'm UTC -5). Inject and debugging shows the correct local time.

Here's an example of the flow:

Any suggestions?

#2

Not really an answer but if something is injecting the wrong timezone, you could use node-red-contrib-moment to translate to the correct timezone.

Also, did you make sure to set the correct timezone on your server?

1 Like
#3

I'm using Node-RED in a Docker container as a service and verified the container has the correct time zone. I'm not sure anything is injecting the wrong time per session, I just can't seem to figure out how to work with it for this specific automation, which relies on the UTC -5 times for it to make sense.

How would I use node-red-contrib-moment to do what you're describing?

Mobile

#4

contrib-moment allows you, amongst other things, to convert between timezones. So if a node is only processing utc but your input is local (or visa-versa), it will automatically convert it for you. Just set the input and output timezones.

#5

Hmmm, thanks for the response but I'm still not sure how to use the contrib-moment to achieve what you're talking about.

I read the documentation here, but it's just not clicking. I added node to the lighting flow in question and set the input/output TZ to "America/New_York" and then deployed the flow with no visible changes (i.e. time-stamps still show UTC time and the automation doesn't work as intended).

Is there an example you can share or any other suggestions?

#7

Can you give me a simplified example of your flow without specialist nodes? So that I can make sure I'm understanding the need. Then I will see what I can do for you.

#8

Sure and thank you for your continued support!

  1. A "state change" node checks a motion sensor in an office
  2. A switch evaluates the state change, if yes, it's passed to the already-on node, which checks to see if the office lighting is already on, if it is, the flow halts. If not, it's passed to a time-range-switch.
  3. The time-range-switch evaluates current time. This is where the whole UTC thing comes into play. I want lights to be turned on @ 100% brightness between 0800-2100. From 2101-0759, I want lights @ 5% brightness. This works, but because the time is in UTC, it's just not working as expected.
  4. A 20 minute timer runs in parallel to steps 2-3 whenever a light is on. If motion is not detected during that 20 minute range, office lights are turned off.

Hope that helps,

#9

Could you export your flow (just enough to show the problem) and post it here so we can see exactly what you have? Then we can run it and see what is going on. See this for how to post a flow if you don't know

#10
[
    {
        "id": "74bde181.c781",
        "type": "tab",
        "label": "Lighting",
        "disabled": false,
        "info": ""
    },
    {
        "id": "ba7fdad2.1f14b8",
        "type": "server-state-changed",
        "z": "74bde181.c781",
        "name": "Motion in Office",
        "server": "ad7854a1.e84738",
        "entityidfilter": "binary_sensor.motion_office",
        "entityidfiltertype": "substring",
        "outputinitially": false,
        "state_type": "str",
        "haltifstate": "",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "outputs": 1,
        "x": 160,
        "y": 360,
        "wires": [
            [
                "94f7c3f6.33217"
            ]
        ]
    },
    {
        "id": "8efff11b.00bcc",
        "type": "api-call-service",
        "z": "74bde181.c781",
        "name": "Turn on @ 100%",
        "server": "ad7854a1.e84738",
        "service_domain": "light",
        "service": "turn_on",
        "data": "{\"entity_id\":\"light.loft_light,light.desk_light\",\"brightness_pct\":\"100\",\"color_name\":\"white\"}",
        "render_data": false,
        "mergecontext": "",
        "output_location": "payload",
        "output_location_type": "msg",
        "x": 1030,
        "y": 260,
        "wires": [
            []
        ]
    },
    {
        "id": "94f7c3f6.33217",
        "type": "switch",
        "z": "74bde181.c781",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "on",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "off",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 500,
        "y": 480,
        "wires": [
            [
                "fdf0e749.f5d058",
                "6dab9f83.edeb"
            ],
            [
                "fdf0e749.f5d058"
            ]
        ]
    },
    {
        "id": "fdf0e749.f5d058",
        "type": "stoptimer",
        "z": "74bde181.c781",
        "duration": "20",
        "units": "Minute",
        "payloadtype": "num",
        "payloadval": "0",
        "name": "",
        "x": 830,
        "y": 470,
        "wires": [
            [
                "be83c083.e5411"
            ],
            []
        ]
    },
    {
        "id": "be83c083.e5411",
        "type": "api-call-service",
        "z": "74bde181.c781",
        "name": "Turn off",
        "server": "ad7854a1.e84738",
        "service_domain": "light",
        "service": "turn_off",
        "data": "{\"entity_id\":\"light.desk_light\"}",
        "render_data": false,
        "mergecontext": "",
        "output_location": "payload",
        "output_location_type": "msg",
        "x": 1000,
        "y": 360,
        "wires": [
            []
        ]
    },
    {
        "id": "6dab9f83.edeb",
        "type": "api-current-state",
        "z": "74bde181.c781",
        "name": "Already on?",
        "server": "ad7854a1.e84738",
        "halt_if": "on",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "override_topic": true,
        "override_payload": true,
        "override_data": true,
        "entity_id": "light.desk_light",
        "state_type": "str",
        "outputs": 2,
        "x": 410,
        "y": 80,
        "wires": [
            [
                "a197150.b9ea2e8"
            ],
            []
        ]
    },
    {
        "id": "a197150.b9ea2e8",
        "type": "time-range-switch",
        "z": "74bde181.c781",
        "name": "Determine brightness",
        "lat": "40.07562",
        "lon": "-82.86322",
        "startTime": "08:00",
        "endTime": "21:00",
        "startOffset": "",
        "endOffset": "",
        "x": 1020,
        "y": 100,
        "wires": [
            [
                "8efff11b.00bcc"
            ],
            [
                "4992fd03.1aaf14"
            ]
        ]
    },
    {
        "id": "4992fd03.1aaf14",
        "type": "api-call-service",
        "z": "74bde181.c781",
        "name": "Turn on @ 5%",
        "server": "ad7854a1.e84738",
        "service_domain": "light",
        "service": "turn_on",
        "data": "{\"entity_id\":\"light.desk_light\",\"brightness_pct\":\"5\"}",
        "render_data": false,
        "mergecontext": "",
        "output_location": "payload",
        "output_location_type": "msg",
        "x": 1020,
        "y": 310,
        "wires": [
            []
        ]
    },
    {
        "id": "58917183.08ff4",
        "type": "comment",
        "z": "74bde181.c781",
        "name": "Office Lighting",
        "info": "",
        "x": 150,
        "y": 300,
        "wires": []
    },
    {
        "id": "ad7854a1.e84738",
        "type": "server",
        "z": "",
        "name": "Home Assistant",
        "legacy": false,
        "hassio": false,
        "rejectUnauthorizedCerts": true,
        "ha_boolean": "y|yes|true|on|home|open"
    }
]
#11

Which node is it that is not behaving as you expect with the time. Is it node-red-contrib-time-range-switch or something else?

[Edit] Also, in the debug window the debug messages are shown with a timestamp at the top of each message. Are they correct?

#12

Yes, time-range-switch shows UTC timestamps. This impacts how I want lights to behave because it's 5 hours ahead of my actual time zone. The debug output is in local time (UTC -5).

The problem with this is if I want the brightness of lights to vary based on the

#13

What exactly do you mean by showing UTC timestamps? Do you mean that when you enter a time of 08:00 then it switches at 08:00 UTC instead of local time? If so that seems rather odd, though the node is not clear on that point as far as I can see. I can't easily test as we are on UTC here at the moment. I do notice though that there are start and end time offsets there so you could just put 5 hours offset there, though that would be a nuisance if your time zone uses DST as you would have to change it. If the node does require UTC to be entered and you need automatic DST adjustment then I can't see any solution with that node.
It might be worth asking in an issue associated with that node. In fact it would be worth checking whether there are any issues relating to that already.

#14

@subjectbit which version of node-red-contrib-time-range-switch are you using? I see there was a recent fix to operation with times configured.

#15

I'm using the latest 0.6.1 version. I read the documentation re: using the offsets, but as you noted, it's less than ideal as I would need to go through and update each year to account for DST. While certainly not the end of the world, I have quite a few flows I would need to update and have to imagine there's a more elegant solution.

In response to your question above, here's what I mean by "showing UTC timestamps" - see screenshot below circled in red for an example.

I am setting the range in the time-range-switch between 0800-2100. As you can see from the output of each node, it uses UTC to show when it last fired. This is problematic, especially after 1900 local time once the flow moves forward to the next day (e.g. 7pm UTC -5 on January 17 = 00:00 UTC on January 18).

#16

You have a number of different node types there. Let's just concentrate on the time-switch node first. If you make a new little flow with one of those with an inject on the input and debug nodes on the outputs, and enter appropriate times in the node does it switch at the correct times in local time? You will have to play with times around the current time to check.

#17

Hi,
I'm using the time-range-switch node in several flows without any issue, that means the time I set is the the local time (UTC+1).

#18

In this context, are you using offset or it "just works" as is (meaning the timestamps displayed in your node are shown as local time)?

#19

No, I don't use the offset. As you can see in the flow the times are as set.
timerange

#20

The timestamps displayed on the time switch node are correct are they not? It is the other nodes that show different timestamps. That is why I wanted you to confirm whether the time switch node is working correctly and it is just that the others are displaying the wrong time.