@@ -111,8 +111,8 @@ State variables can be accessed in any Python code simply by name. State variabl
111
111
``entity_id ``) are of the form ``DOMAIN.name ``, where ``DOMAIN `` is typically the name of the
112
112
component that sets that variable. You can set a state variable by assigning to it.
113
113
114
- State variables only have string values, so you will need to convert them to ``int `` or ``float `` if
115
- you need their numeric value.
114
+ State variables only have string values, so you will need to convert them to ``int `` or ``float ``
115
+ if you need their numeric value.
116
116
117
117
State variables have attributes that can be accessed by adding the name of the attribute, as in
118
118
``DOMAIN.name.attr ``. The attribute names and their meaning depend on the component that sets them,
@@ -133,8 +133,20 @@ component has a state variable name that collides with one of its services, you
133
133
134
134
Accessing state variables that don't exist will throw a ``NameError `` exception, and accessing
135
135
an attribute that doesn't exist will throw a ``AttributeError `` exception. One exception (!)
136
- to this that in a ``@state_trigger `` expression, undefined state variables will evaluate to
137
- ``None `` instead of throwing an exception.
136
+ to this that in a ``@state_trigger `` expression, undefined state variables and attributes will
137
+ evaluate to ``None `` instead of throwing an exception.
138
+
139
+ You can assign a state variable (or the return value of ``state.get() ``) to a normal Python variable,
140
+ and that variable will capture the value and attributes of the state variable at the time of the
141
+ assignment. So, for example, after this assignment:
142
+
143
+ .. code :: python
144
+
145
+ test1_state = binary_sensor.test1
146
+
147
+ the variable ``test1_state `` captures both the value and attributes of ``binary_sensor.test1 ``.
148
+ Later, if ``binary_sensor.test1 `` changes, ``test1_state `` continues to represent the previous
149
+ value and attributes at the time of the assignment.
138
150
139
151
State variables also support virtual methods that are service calls with that ``entity_id ``.
140
152
For any state variable ``DOMAIN.ENTITY ``, any services registered by ``DOMAIN ``, eg:
@@ -249,10 +261,12 @@ function.
249
261
250
262
``@state_trigger `` takes one or more string arguments that contain any expression based on one or
251
263
more state variables, and evaluates to ``True `` or ``False `` (or non-zero or zero). Whenever the
252
- state variables mentioned in the expression change, the expression is evaluated and the trigger
253
- occurs if it evaluates to ``True `` (or non-zero). For each state variable, eg: ``domain.name ``,
254
- the prior value is also available to the expression as ``domain.name.old `` in case you want to
255
- condition the trigger on the prior value too.
264
+ state variables or attributes values mentioned in the expression change, the expression is evaluated
265
+ and the trigger occurs if it evaluates to ``True `` (or non-zero). For each state variable,
266
+ eg: ``domain.name ``, the prior value is also available to the expression as ``domain.name.old ``
267
+ in case you want to condition the trigger on the prior value too. Attribute values can be used
268
+ in the expression too, using the forms ``domain.name.attr `` and ``domain.name.old.attr `` for
269
+ the new and old attribute values respectively.
256
270
257
271
Multiple ``str_expr `` arguments are logically "or"ed together, so the trigger occurs if any of the
258
272
expressions evaluate to ``True ``. Any argument can alternatively be a list or set of strings, and
@@ -297,24 +311,36 @@ You can also use state variable attributes in the trigger expression, with an id
297
311
form ``DOMAIN.name.attr ``. Attributes maintain their original type, so there is no need to cast
298
312
then to another type.
299
313
300
- You can specify a state trigger on any change with a string that is just the state variable name:
314
+ You can specify a state trigger on any change with a string that can take three forms:
315
+ ``"domain.entity" ``
316
+ triggers on any change to the state variable value
317
+ ``"domain.entity.attr" ``
318
+ triggers on any change to the state variable attribute ``attr `` value
319
+ ``"domain.entity.*" ``
320
+ triggers on any change to any state variable attribute (but not its value)
321
+
322
+ For example:
301
323
302
324
.. code :: python
303
325
304
326
@state_trigger (" domain.light_level" )
305
327
306
- If you use this form, there's no point in also specifying ``state_hold `` since the expression
307
- is always ``True `` whenever the state variable changes - there is no way for it to evaluate
308
- to ``False `` and to re-start the trigger process. If you do specify ``state_hold `` in this
309
- case it will simply delay the trigger by the specified time.
328
+ will trigger any time the value of ``domain.light_level `` changes (not its attributes), which
329
+ includes the cases when that variable is first created (ie, the ``old_value `` is ``None ``)
330
+ and when it is deleted (ie, the ``value `` is ``None ``).
331
+
332
+ If you use the "any change" form, there's no point in also specifying ``state_hold `` since the
333
+ expression is always ``True `` whenever the state variable changes - there is no way for it to
334
+ evaluate to ``False `` and to re-start the trigger process. If you do specify ``state_hold `` in
335
+ this case it will simply delay the trigger by the specified time.
310
336
311
337
The trigger can include arguments with any mixture of string expressions (that are evaluated
312
- when any of the underlying state variables change) and string state variable names (that trigger
313
- whenever that variable changes).
338
+ when any of the underlying state variables change) and string state variable or attribute
339
+ names (that trigger whenever that variable or attribute changes).
314
340
315
- Note that if a state variable is set to the same value, HASS doesn’t generate a state change event,
316
- so the ``@state_trigger `` condition will not be checked. It is only evaluated each time a state
317
- variable changes to a new value.
341
+ Note that if a state variable and attributes are set to the same value, HASS doesn’t generate a
342
+ state change event, so the ``@state_trigger `` condition will not be checked. It is only evaluated
343
+ each time a state variable or any of its attributes change to a new value.
318
344
319
345
When the trigger occurs and the function is executed (meaning any active checks passed too), keyword
320
346
arguments are passed to the function so it can tell which state variable caused it to succeed and
@@ -329,6 +355,11 @@ run, in cases where the trigger condition involves multiple variables. These are
329
355
" old_value" : old_value
330
356
}
331
357
358
+ The ``value `` and ``old_value `` represent the current and old values of the state variable
359
+ ``var_name `` whose change caused the trigger. Those variables include the state attributes too.
360
+ If the trigger occurs when the state variable is newly created, ``old_value `` will be ``None ``,
361
+ and if the trigger occurs when a state variable is deleted, ``value `` will be ``None ``.
362
+
332
363
If your function needs to know any of these values, you can list the keyword arguments you need,
333
364
with defaults:
334
365
@@ -1304,6 +1335,9 @@ You can use ``hass`` to compute sunrise and sunset times using the same method H
1304
1335
sunset = location.sunset(datetime.datetime.today()).replace(tzinfo = None )
1305
1336
print (f " today sunrise = { sunrise} , sunset = { sunset} " )
1306
1337
1338
+ (Note that the ``sun.sun `` attributes already provide times for the next sunrise and sunset, so
1339
+ this example is a bit contrived.)
1340
+
1307
1341
Here's another method that uses the installed version of ``astral `` directly, rather than the HASS
1308
1342
helper function. It's a bit more cryptic since it's a very old version of ``astral ``, but you can
1309
1343
see how the HASS configuration values are used:
0 commit comments