Wrong brightness from time in template if statement

Having an automation that contains this action:

  action:
  - service: light.turn_on
    data_template:
      entity_id: light.hallway
      color_temp: 250
      brightness: >-
        {% if now().hour < 6 %}
        {{ 8 }}
        {% elif now().hour < 9 %}
        {{ (((now().hour*60 + now().minute) * (1.0222)) - 360 ) | round(0) }}
        {% elif now().hour < 22 %}
        {{ 192 }}
        {% else %}
        {{ (((now().hour*60 + now().minute) * (-1.5333)) + 2216 ) | round(0) }}
        {% endif %}

To my understanding, in an if statement the first one with outcome true will be set.
A few times a week the brightness is set to 8 while the hour is not smaller than 6 but e.g. 20. Why is it set to 8 then?

Your template is correct. Your problem doesn’t lie in this. Do you have another automation that turns on the light?

  • An automation that triggers the one above when a door is opened, but with condition that the light is off.
  • An automation that turns on the light with 255 brightness when an entity comes home.
  • Automations that turn on the light when a Xiaomi button is used.
  • Automations that set the light to a specific colour from actionable notifications on iOS.

All of these did not happen.

If I remember correctly I saw this brightness 8 being set when the original (first post) automation was triggered by it’s motion sensors. It’s also the only automation I have that would set that light to brightness 8.

Well the template is correct. I set up a test quickly to verify your logic and look at the outputs. All hours and minute combinations of the day are set correctly. Starting at 6 it slowly increases to 192 until 9 am and stays steady until 11 pm. Then it slowly drops to 10 at 11:59 pm and it’s 8 again at midnight. So, if what you are saying is correct and it’s getting set wrong, then it’s possible that your timezone in HA or your timezone in your OS is wrong. Or the hardware is failing. You may also want to have your logs set to info and watch state changes to verify that 8 is being sent to it.

time brightness
00:00 8
00:01 8
00:02 8
00:03 8
00:04 8
00:05 8
00:06 8
00:07 8
00:08 8
00:09 8
00:10 8
00:11 8
00:12 8
00:13 8
00:14 8
00:15 8
00:16 8
00:17 8
00:18 8
00:19 8
00:20 8
00:21 8
00:22 8
00:23 8
00:24 8
00:25 8
00:26 8
00:27 8
00:28 8
00:29 8
00:30 8
00:31 8
00:32 8
00:33 8
00:34 8
00:35 8
00:36 8
00:37 8
00:38 8
00:39 8
00:40 8
00:41 8
00:42 8
00:43 8
00:44 8
00:45 8
00:46 8
00:47 8
00:48 8
00:49 8
00:50 8
00:51 8
00:52 8
00:53 8
00:54 8
00:55 8
00:56 8
00:57 8
00:58 8
00:59 8
01:00 8
01:01 8
01:02 8
01:03 8
01:04 8
01:05 8
01:06 8
01:07 8
01:08 8
01:09 8
01:10 8
01:11 8
01:12 8
01:13 8
01:14 8
01:15 8
01:16 8
01:17 8
01:18 8
01:19 8
01:20 8
01:21 8
01:22 8
01:23 8
01:24 8
01:25 8
01:26 8
01:27 8
01:28 8
01:29 8
01:30 8
01:31 8
01:32 8
01:33 8
01:34 8
01:35 8
01:36 8
01:37 8
01:38 8
01:39 8
01:40 8
01:41 8
01:42 8
01:43 8
01:44 8
01:45 8
01:46 8
01:47 8
01:48 8
01:49 8
01:50 8
01:51 8
01:52 8
01:53 8
01:54 8
01:55 8
01:56 8
01:57 8
01:58 8
01:59 8
02:00 8
02:01 8
02:02 8
02:03 8
02:04 8
02:05 8
02:06 8
02:07 8
02:08 8
02:09 8
02:10 8
02:11 8
02:12 8
02:13 8
02:14 8
02:15 8
02:16 8
02:17 8
02:18 8
02:19 8
02:20 8
02:21 8
02:22 8
02:23 8
02:24 8
02:25 8
02:26 8
02:27 8
02:28 8
02:29 8
02:30 8
02:31 8
02:32 8
02:33 8
02:34 8
02:35 8
02:36 8
02:37 8
02:38 8
02:39 8
02:40 8
02:41 8
02:42 8
02:43 8
02:44 8
02:45 8
02:46 8
02:47 8
02:48 8
02:49 8
02:50 8
02:51 8
02:52 8
02:53 8
02:54 8
02:55 8
02:56 8
02:57 8
02:58 8
02:59 8
03:00 8
03:01 8
03:02 8
03:03 8
03:04 8
03:05 8
03:06 8
03:07 8
03:08 8
03:09 8
03:10 8
03:11 8
03:12 8
03:13 8
03:14 8
03:15 8
03:16 8
03:17 8
03:18 8
03:19 8
03:20 8
03:21 8
03:22 8
03:23 8
03:24 8
03:25 8
03:26 8
03:27 8
03:28 8
03:29 8
03:30 8
03:31 8
03:32 8
03:33 8
03:34 8
03:35 8
03:36 8
03:37 8
03:38 8
03:39 8
03:40 8
03:41 8
03:42 8
03:43 8
03:44 8
03:45 8
03:46 8
03:47 8
03:48 8
03:49 8
03:50 8
03:51 8
03:52 8
03:53 8
03:54 8
03:55 8
03:56 8
03:57 8
03:58 8
03:59 8
04:00 8
04:01 8
04:02 8
04:03 8
04:04 8
04:05 8
04:06 8
04:07 8
04:08 8
04:09 8
04:10 8
04:11 8
04:12 8
04:13 8
04:14 8
04:15 8
04:16 8
04:17 8
04:18 8
04:19 8
04:20 8
04:21 8
04:22 8
04:23 8
04:24 8
04:25 8
04:26 8
04:27 8
04:28 8
04:29 8
04:30 8
04:31 8
04:32 8
04:33 8
04:34 8
04:35 8
04:36 8
04:37 8
04:38 8
04:39 8
04:40 8
04:41 8
04:42 8
04:43 8
04:44 8
04:45 8
04:46 8
04:47 8
04:48 8
04:49 8
04:50 8
04:51 8
04:52 8
04:53 8
04:54 8
04:55 8
04:56 8
04:57 8
04:58 8
04:59 8
05:00 8
05:01 8
05:02 8
05:03 8
05:04 8
05:05 8
05:06 8
05:07 8
05:08 8
05:09 8
05:10 8
05:11 8
05:12 8
05:13 8
05:14 8
05:15 8
05:16 8
05:17 8
05:18 8
05:19 8
05:20 8
05:21 8
05:22 8
05:23 8
05:24 8
05:25 8
05:26 8
05:27 8
05:28 8
05:29 8
05:30 8
05:31 8
05:32 8
05:33 8
05:34 8
05:35 8
05:36 8
05:37 8
05:38 8
05:39 8
05:40 8
05:41 8
05:42 8
05:43 8
05:44 8
05:45 8
05:46 8
05:47 8
05:48 8
05:49 8
05:50 8
05:51 8
05:52 8
05:53 8
05:54 8
05:55 8
05:56 8
05:57 8
05:58 8
05:59 8
06:00 8
06:01 9
06:02 10
06:03 11
06:04 12
06:05 13
06:06 14
06:07 15
06:08 16
06:09 17
06:10 18
06:11 19
06:12 20
06:13 21
06:14 22
06:15 23
06:16 24
06:17 25
06:18 26
06:19 27
06:20 28
06:21 29
06:22 30
06:23 32
06:24 33
06:25 34
06:26 35
06:27 36
06:28 37
06:29 38
06:30 39
06:31 40
06:32 41
06:33 42
06:34 43
06:35 44
06:36 45
06:37 46
06:38 47
06:39 48
06:40 49
06:41 50
06:42 51
06:43 52
06:44 53
06:45 54
06:46 55
06:47 56
06:48 57
06:49 58
06:50 59
06:51 60
06:52 61
06:53 62
06:54 63
06:55 64
06:56 65
06:57 66
06:58 67
06:59 68
07:00 69
07:01 70
07:02 71
07:03 72
07:04 73
07:05 74
07:06 75
07:07 76
07:08 78
07:09 79
07:10 80
07:11 81
07:12 82
07:13 83
07:14 84
07:15 85
07:16 86
07:17 87
07:18 88
07:19 89
07:20 90
07:21 91
07:22 92
07:23 93
07:24 94
07:25 95
07:26 96
07:27 97
07:28 98
07:29 99
07:30 100
07:31 101
07:32 102
07:33 103
07:34 104
07:35 105
07:36 106
07:37 107
07:38 108
07:39 109
07:40 110
07:41 111
07:42 112
07:43 113
07:44 114
07:45 115
07:46 116
07:47 117
07:48 118
07:49 119
07:50 120
07:51 121
07:52 122
07:53 124
07:54 125
07:55 126
07:56 127
07:57 128
07:58 129
07:59 130
08:00 131
08:01 132
08:02 133
08:03 134
08:04 135
08:05 136
08:06 137
08:07 138
08:08 139
08:09 140
08:10 141
08:11 142
08:12 143
08:13 144
08:14 145
08:15 146
08:16 147
08:17 148
08:18 149
08:19 150
08:20 151
08:21 152
08:22 153
08:23 154
08:24 155
08:25 156
08:26 157
08:27 158
08:28 159
08:29 160
08:30 161
08:31 162
08:32 163
08:33 164
08:34 165
08:35 166
08:36 167
08:37 168
08:38 169
08:39 171
08:40 172
08:41 173
08:42 174
08:43 175
08:44 176
08:45 177
08:46 178
08:47 179
08:48 180
08:49 181
08:50 182
08:51 183
08:52 184
08:53 185
08:54 186
08:55 187
08:56 188
08:57 189
08:58 190
08:59 191
09:00 192
09:01 192
09:02 192
09:03 192
09:04 192
09:05 192
09:06 192
09:07 192
09:08 192
09:09 192
09:10 192
09:11 192
09:12 192
09:13 192
09:14 192
09:15 192
09:16 192
09:17 192
09:18 192
09:19 192
09:20 192
09:21 192
09:22 192
09:23 192
09:24 192
09:25 192
09:26 192
09:27 192
09:28 192
09:29 192
09:30 192
09:31 192
09:32 192
09:33 192
09:34 192
09:35 192
09:36 192
09:37 192
09:38 192
09:39 192
09:40 192
09:41 192
09:42 192
09:43 192
09:44 192
09:45 192
09:46 192
09:47 192
09:48 192
09:49 192
09:50 192
09:51 192
09:52 192
09:53 192
09:54 192
09:55 192
09:56 192
09:57 192
09:58 192
09:59 192
10:00 192
10:01 192
10:02 192
10:03 192
10:04 192
10:05 192
10:06 192
10:07 192
10:08 192
10:09 192
10:10 192
10:11 192
10:12 192
10:13 192
10:14 192
10:15 192
10:16 192
10:17 192
10:18 192
10:19 192
10:20 192
10:21 192
10:22 192
10:23 192
10:24 192
10:25 192
10:26 192
10:27 192
10:28 192
10:29 192
10:30 192
10:31 192
10:32 192
10:33 192
10:34 192
10:35 192
10:36 192
10:37 192
10:38 192
10:39 192
10:40 192
10:41 192
10:42 192
10:43 192
10:44 192
10:45 192
10:46 192
10:47 192
10:48 192
10:49 192
10:50 192
10:51 192
10:52 192
10:53 192
10:54 192
10:55 192
10:56 192
10:57 192
10:58 192
10:59 192
11:00 192
11:01 192
11:02 192
11:03 192
11:04 192
11:05 192
11:06 192
11:07 192
11:08 192
11:09 192
11:10 192
11:11 192
11:12 192
11:13 192
11:14 192
11:15 192
11:16 192
11:17 192
11:18 192
11:19 192
11:20 192
11:21 192
11:22 192
11:23 192
11:24 192
11:25 192
11:26 192
11:27 192
11:28 192
11:29 192
11:30 192
11:31 192
11:32 192
11:33 192
11:34 192
11:35 192
11:36 192
11:37 192
11:38 192
11:39 192
11:40 192
11:41 192
11:42 192
11:43 192
11:44 192
11:45 192
11:46 192
11:47 192
11:48 192
11:49 192
11:50 192
11:51 192
11:52 192
11:53 192
11:54 192
11:55 192
11:56 192
11:57 192
11:58 192
11:59 192
12:00 192
12:01 192
12:02 192
12:03 192
12:04 192
12:05 192
12:06 192
12:07 192
12:08 192
12:09 192
12:10 192
12:11 192
12:12 192
12:13 192
12:14 192
12:15 192
12:16 192
12:17 192
12:18 192
12:19 192
12:20 192
12:21 192
12:22 192
12:23 192
12:24 192
12:25 192
12:26 192
12:27 192
12:28 192
12:29 192
12:30 192
12:31 192
12:32 192
12:33 192
12:34 192
12:35 192
12:36 192
12:37 192
12:38 192
12:39 192
12:40 192
12:41 192
12:42 192
12:43 192
12:44 192
12:45 192
12:46 192
12:47 192
12:48 192
12:49 192
12:50 192
12:51 192
12:52 192
12:53 192
12:54 192
12:55 192
12:56 192
12:57 192
12:58 192
12:59 192
13:00 192
13:01 192
13:02 192
13:03 192
13:04 192
13:05 192
13:06 192
13:07 192
13:08 192
13:09 192
13:10 192
13:11 192
13:12 192
13:13 192
13:14 192
13:15 192
13:16 192
13:17 192
13:18 192
13:19 192
13:20 192
13:21 192
13:22 192
13:23 192
13:24 192
13:25 192
13:26 192
13:27 192
13:28 192
13:29 192
13:30 192
13:31 192
13:32 192
13:33 192
13:34 192
13:35 192
13:36 192
13:37 192
13:38 192
13:39 192
13:40 192
13:41 192
13:42 192
13:43 192
13:44 192
13:45 192
13:46 192
13:47 192
13:48 192
13:49 192
13:50 192
13:51 192
13:52 192
13:53 192
13:54 192
13:55 192
13:56 192
13:57 192
13:58 192
13:59 192
14:00 192
14:01 192
14:02 192
14:03 192
14:04 192
14:05 192
14:06 192
14:07 192
14:08 192
14:09 192
14:10 192
14:11 192
14:12 192
14:13 192
14:14 192
14:15 192
14:16 192
14:17 192
14:18 192
14:19 192
14:20 192
14:21 192
14:22 192
14:23 192
14:24 192
14:25 192
14:26 192
14:27 192
14:28 192
14:29 192
14:30 192
14:31 192
14:32 192
14:33 192
14:34 192
14:35 192
14:36 192
14:37 192
14:38 192
14:39 192
14:40 192
14:41 192
14:42 192
14:43 192
14:44 192
14:45 192
14:46 192
14:47 192
14:48 192
14:49 192
14:50 192
14:51 192
14:52 192
14:53 192
14:54 192
14:55 192
14:56 192
14:57 192
14:58 192
14:59 192
15:00 192
15:01 192
15:02 192
15:03 192
15:04 192
15:05 192
15:06 192
15:07 192
15:08 192
15:09 192
15:10 192
15:11 192
15:12 192
15:13 192
15:14 192
15:15 192
15:16 192
15:17 192
15:18 192
15:19 192
15:20 192
15:21 192
15:22 192
15:23 192
15:24 192
15:25 192
15:26 192
15:27 192
15:28 192
15:29 192
15:30 192
15:31 192
15:32 192
15:33 192
15:34 192
15:35 192
15:36 192
15:37 192
15:38 192
15:39 192
15:40 192
15:41 192
15:42 192
15:43 192
15:44 192
15:45 192
15:46 192
15:47 192
15:48 192
15:49 192
15:50 192
15:51 192
15:52 192
15:53 192
15:54 192
15:55 192
15:56 192
15:57 192
15:58 192
15:59 192
16:00 192
16:01 192
16:02 192
16:03 192
16:04 192
16:05 192
16:06 192
16:07 192
16:08 192
16:09 192
16:10 192
16:11 192
16:12 192
16:13 192
16:14 192
16:15 192
16:16 192
16:17 192
16:18 192
16:19 192
16:20 192
16:21 192
16:22 192
16:23 192
16:24 192
16:25 192
16:26 192
16:27 192
16:28 192
16:29 192
16:30 192
16:31 192
16:32 192
16:33 192
16:34 192
16:35 192
16:36 192
16:37 192
16:38 192
16:39 192
16:40 192
16:41 192
16:42 192
16:43 192
16:44 192
16:45 192
16:46 192
16:47 192
16:48 192
16:49 192
16:50 192
16:51 192
16:52 192
16:53 192
16:54 192
16:55 192
16:56 192
16:57 192
16:58 192
16:59 192
17:00 192
17:01 192
17:02 192
17:03 192
17:04 192
17:05 192
17:06 192
17:07 192
17:08 192
17:09 192
17:10 192
17:11 192
17:12 192
17:13 192
17:14 192
17:15 192
17:16 192
17:17 192
17:18 192
17:19 192
17:20 192
17:21 192
17:22 192
17:23 192
17:24 192
17:25 192
17:26 192
17:27 192
17:28 192
17:29 192
17:30 192
17:31 192
17:32 192
17:33 192
17:34 192
17:35 192
17:36 192
17:37 192
17:38 192
17:39 192
17:40 192
17:41 192
17:42 192
17:43 192
17:44 192
17:45 192
17:46 192
17:47 192
17:48 192
17:49 192
17:50 192
17:51 192
17:52 192
17:53 192
17:54 192
17:55 192
17:56 192
17:57 192
17:58 192
17:59 192
18:00 192
18:01 192
18:02 192
18:03 192
18:04 192
18:05 192
18:06 192
18:07 192
18:08 192
18:09 192
18:10 192
18:11 192
18:12 192
18:13 192
18:14 192
18:15 192
18:16 192
18:17 192
18:18 192
18:19 192
18:20 192
18:21 192
18:22 192
18:23 192
18:24 192
18:25 192
18:26 192
18:27 192
18:28 192
18:29 192
18:30 192
18:31 192
18:32 192
18:33 192
18:34 192
18:35 192
18:36 192
18:37 192
18:38 192
18:39 192
18:40 192
18:41 192
18:42 192
18:43 192
18:44 192
18:45 192
18:46 192
18:47 192
18:48 192
18:49 192
18:50 192
18:51 192
18:52 192
18:53 192
18:54 192
18:55 192
18:56 192
18:57 192
18:58 192
18:59 192
19:00 192
19:01 192
19:02 192
19:03 192
19:04 192
19:05 192
19:06 192
19:07 192
19:08 192
19:09 192
19:10 192
19:11 192
19:12 192
19:13 192
19:14 192
19:15 192
19:16 192
19:17 192
19:18 192
19:19 192
19:20 192
19:21 192
19:22 192
19:23 192
19:24 192
19:25 192
19:26 192
19:27 192
19:28 192
19:29 192
19:30 192
19:31 192
19:32 192
19:33 192
19:34 192
19:35 192
19:36 192
19:37 192
19:38 192
19:39 192
19:40 192
19:41 192
19:42 192
19:43 192
19:44 192
19:45 192
19:46 192
19:47 192
19:48 192
19:49 192
19:50 192
19:51 192
19:52 192
19:53 192
19:54 192
19:55 192
19:56 192
19:57 192
19:58 192
19:59 192
20:00 192
20:01 192
20:02 192
20:03 192
20:04 192
20:05 192
20:06 192
20:07 192
20:08 192
20:09 192
20:10 192
20:11 192
20:12 192
20:13 192
20:14 192
20:15 192
20:16 192
20:17 192
20:18 192
20:19 192
20:20 192
20:21 192
20:22 192
20:23 192
20:24 192
20:25 192
20:26 192
20:27 192
20:28 192
20:29 192
20:30 192
20:31 192
20:32 192
20:33 192
20:34 192
20:35 192
20:36 192
20:37 192
20:38 192
20:39 192
20:40 192
20:41 192
20:42 192
20:43 192
20:44 192
20:45 192
20:46 192
20:47 192
20:48 192
20:49 192
20:50 192
20:51 192
20:52 192
20:53 192
20:54 192
20:55 192
20:56 192
20:57 192
20:58 192
20:59 192
21:00 192
21:01 192
21:02 192
21:03 192
21:04 192
21:05 192
21:06 192
21:07 192
21:08 192
21:09 192
21:10 192
21:11 192
21:12 192
21:13 192
21:14 192
21:15 192
21:16 192
21:17 192
21:18 192
21:19 192
21:20 192
21:21 192
21:22 192
21:23 192
21:24 192
21:25 192
21:26 192
21:27 192
21:28 192
21:29 192
21:30 192
21:31 192
21:32 192
21:33 192
21:34 192
21:35 192
21:36 192
21:37 192
21:38 192
21:39 192
21:40 192
21:41 192
21:42 192
21:43 192
21:44 192
21:45 192
21:46 192
21:47 192
21:48 192
21:49 192
21:50 192
21:51 192
21:52 192
21:53 192
21:54 192
21:55 192
21:56 192
21:57 192
21:58 192
21:59 192
22:00 192
22:01 191
22:02 189
22:03 187
22:04 186
22:05 184
22:06 183
22:07 181
22:08 180
22:09 178
22:10 177
22:11 175
22:12 174
22:13 172
22:14 171
22:15 169
22:16 168
22:17 166
22:18 164
22:19 163
22:20 161
22:21 160
22:22 158
22:23 157
22:24 155
22:25 154
22:26 152
22:27 151
22:28 149
22:29 148
22:30 146
22:31 145
22:32 143
22:33 141
22:34 140
22:35 138
22:36 137
22:37 135
22:38 134
22:39 132
22:40 131
22:41 129
22:42 128
22:43 126
22:44 125
22:45 123
22:46 122
22:47 120
22:48 118
22:49 117
22:50 115
22:51 114
22:52 112
22:53 111
22:54 109
22:55 108
22:56 106
22:57 105
22:58 103
22:59 102
23:00 100
23:01 99
23:02 97
23:03 95
23:04 94
23:05 92
23:06 91
23:07 89
23:08 88
23:09 86
23:10 85
23:11 83
23:12 82
23:13 80
23:14 79
23:15 77
23:16 76
23:17 74
23:18 72
23:19 71
23:20 69
23:21 68
23:22 66
23:23 65
23:24 63
23:25 62
23:26 60
23:27 59
23:28 57
23:29 56
23:30 54
23:31 53
23:32 51
23:33 49
23:34 48
23:35 46
23:36 45
23:37 43
23:38 42
23:39 40
23:40 39
23:41 37
23:42 36
23:43 34
23:44 33
23:45 31
23:46 30
23:47 28
23:48 26
23:49 25
23:50 23
23:51 22
23:52 20
23:53 19
23:54 17
23:55 16
23:56 14
23:57 13
23:58 11
23:59 10

Thanks for checking.
HA time and timezone is ok.
Device time and timezone is ok.
A few weeks ago I had to enable hourly time sync though because when syncing once a day it was some minutes (~7) off.

It wouldn’t be the case that reading the time during this hourly sync results in trouble, right?

Will be difficult to debug as it only happens a few times a week.

Honestly, I don’t know. I’ve never used sync.

I agree, but in order to track it down. Maybe send yourself a message. Make an automation or something that triggers between the non 8 hours and send you the message if the brightness is 8. Then immediately go check the info logs. Or at least you have a timestamp to then check the logs later at night.

Notification for brightness < 9 is set up and working.
Just got a hit and there is no log for it on the INFO level. At least, the following one is related, but this seems to be happening when the turn_off is activated (id: 001).
ERROR (MainThread) [homeassistant.helpers.condition] Error during template condition: UndefinedError: 'mappingproxy object' has no attribute 'brightness'

Can increase the log level but then we have to be more specific to prevent thousands of entries.

Here is the full automation including the one it enables:

- id: '002'
  alias: Hallway lamp dynamic brightness < Motion
  trigger:
  - entity_id: binary_sensor.motion_sensor_1, binary_sensor.motion_sensor_2
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: light.hallway_sphere_hanging
    state: 'off'
#  - condition: numeric_state
#    entity_id: 'sensor.illumination_1'
#    below: 40
#  - condition: numeric_state
#    entity_id: 'sensor.illumination_2'
#    below: 100
  action:
  - service: light.turn_on
    data_template:
      entity_id: light.hallway_sphere_hanging
      color_temp: 250
      brightness: >-
        {% if now().hour < 6 %}
        {{ 8 }}
        {% elif now().hour < 9 %}
        {{ (((now().hour*60 + now().minute) * (46/45)) - 360 ) | round(0) }}
        {% elif now().hour < 22 %}
        {{ 192 }}
        {% else %}
        {{ (((now().hour*60 + now().minute) * -(184/119)) + 2233.0084 ) | round(0) }}
        {% endif %}
  - service: automation.turn_on
    data:
      entity_id: automation.hallway_lamp_off_no_motion_2_min

- id: '001'
  alias: Hallway lamp off < No motion 2 min.
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_1, binary_sensor.motion_sensor_2
    to: 'off'
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: binary_sensor.motion_sensor_1
        state: 'off'
      - condition: state
        entity_id: binary_sensor.motion_sensor_2
        state: 'off'
  action:
  - service: light.turn_off
    data:
      entity_id: light.hallway_sphere_hanging
  - service: automation.turn_off
    data:
      entity_id: automation.hallway_lamp_off_no_motion_2_min
  • I updated the formula for better accuracy.
  • There are 2 binary sensors that can trigger it, but this was happening also when there was only one binary sensor.

is another automation stepping on it’s toes?

points to an automation with a condition that is checking brightness.

Seems like the debugging automation caused this.

- id: '456789'
  alias: Debug - Hallway lamp b<9 > Phone
  trigger:
     platform: template
     value_template: '{{ states.light.hallway_sphere_hanging.attributes.brightness | int < 9 }}'
  condition:
  - condition: time
    after: '06:01'
    before: '23:59'
  action:
    service: notify.ios_phone
    data:
      message: "Hallway lamp b<9"

Disabled: gone.
Enabled again: back.
Guess this is because the attribute brightness disappears when a light is off.

that it does. change template to:

     value_template: "{{ 0 < state_attr('light.living_room','brightness') | int < 9 }}"

Nice works indeed, no more error. Why is that?

Any idea how to proceed on the main problem of the topic now? Maybe enable more specific debug level.

because the method returns None, and when none gets converted into an int with the int filter, it makes it zero. So that template is looking for values between 0 and 9, which should be what you are looking for.

Honestly no, need to see what could be causing the problem. I’d say debug logging but don’t keep it on too long. You’ll have a huge log.

Thanks.

Seems like I was not running on info level but on warn level. :man_facepalming:
Now:


logger:
  default: info
#  default: warn
  logs:
    homeassistant.components.automation: debug
    homeassistant.helpers.script: debug
    pygatt.backends.gatttool.gatttool: warn
    homeassistant.components.ios.notify: warn
    homeassistant.components.device_tracker.nmap_tracker: warn
    homeassistant.components.http.view: warn

@petro

08:28:14 INFO (MainThread) [homeassistant.components.automation] Executing Hallway lamp dynamic brightness < Motion
08:28:14 INFO (MainThread) [homeassistant.helpers.script] Script Hallway lamp dynamic brightness < Motion: Running script
08:28:14 INFO (MainThread) [homeassistant.helpers.script] Script Hallway lamp dynamic brightness < Motion: Executing step call service
08:28:15 INFO (MainThread) [homeassistant.helpers.script] Script Hallway lamp dynamic brightness < Motion: Executing step call service
08:28:15 INFO (MainThread) [homeassistant.components.automation] Initialized trigger Hallway lamp off < No motion 2 min.
08:28:35 INFO (MainThread) [homeassistant.components.automation] Executing Debug - Hallway lamp b<9 > Phone
08:28:35 INFO (MainThread) [homeassistant.helpers.script] Script Debug - Hallway lamp b<9 > Phone: Running script
08:28:35 INFO (MainThread) [homeassistant.helpers.script] Script Debug - Hallway lamp b<9 > Phone: Executing step call service
08:30:27 INFO (MainThread) [homeassistant.components.automation] Executing Hallway lamp off < No motion 2 min.
08:30:27 INFO (MainThread) [homeassistant.helpers.script] Script Hallway lamp off < No motion 2 min.: Running script
08:30:27 INFO (MainThread) [homeassistant.helpers.script] Script Hallway lamp off < No motion 2 min.: Executing step call service
08:30:27 INFO (MainThread) [homeassistant.helpers.script] Script Hallway lamp off < No motion 2 min.: Executing step call service

Nothing weird right?
I guess we need more debug level.

Seems like you’re turning off the lamp at that point. You shouldn’t be getting the notification.

The first automation turns on the light. It also enables an automation to turn off the light when the motion sensors do not detect motion any more.
Apparently the brightness is below 9 and thus the notification is sent.
A little over 2 minutes later the trigger for no motion is met and the light is turned off. (Note: Xiaomi Aqara motion sensors only send the ‘no motion’ signal if there is already no motion detected by them for 2 minutes.)

Only odd thing I see is the ‘below 9 brightness’ trigger happening about 20 seconds after turning on the light. Thus theoretically it would be possible that ‘something’ turned down the brightness 20 seconds after the trigger. No clue what that could be though, not in HA, as well as outside HA.

Are you sure this isn’t a hardware issue?

This is the only odd thing happening to my setup.
No crashes, no instability, no high resource usage.
Time seems off max 5 seconds per hour, with synchronization each hour. Not nice for a modern computer (RPi 3b) but should still be fine.

That leaves the option open that it is the light bulb itself…then again, brightness 8 is a huge coincidence then. I am changing the template value to 6 now.

The next template runs on another light and I haven’t seen a wrong brightness setting.

      brightness: >-
        {% if now().hour < 6 %}
        {{ 128 }}
        {% else %}
        {{ 255 }}
        {% endif %}

As far as I am able to see, I would point to HA. Maybe there still is something in it’s way. Maybe the way I coded the if statement is wrong (maybe it needs single or double quotes?). Maybe HA trips over it.

None of that should matter. If it fails setting a brightness it would assign it zero if anything. The template looks good. I see nothing wrong with it and I even tested all the numbers. How about this. Make a template sensor with the same equation. Perform a update sensor service call in your automation before setting the lights. Then in your notification, add that sensor value to the message. Then you’ll see if the template is really messing up during runtime.