I'm writing an automation that needs to work around some limitations in Hue devices (scenes don't seem to save the color of lights that were off when the scene was saved).
So I instead save those off lights' colors to a JSONified list of dictionaries, then load it later in my automation.
Something like:
- variables:
saved_off: |
{% set data = namespace(rows=[]) %}
{% for ent in were_off %}
{%
set data.rows = data.rows + [{
'id': ent,
'cm': state_attr(ent, 'color_mode'),
'hs': state_attr(ent, 'hs_color'),
'ct': state_attr(ent, 'color_temp'),
'xy': state_attr(ent, 'xy_color'),
'bri': state_attr(ent, 'brightness')
}]
%}
{% endfor %}
{{ data.rows | to_json }}
Then later:
- repeat:
for_each: "{{ were_off }}"
sequence:
- variables:
ent: "{{ repeat.item }}"
s: "{{ (saved_off | from_json)[repeat.index - 1] }}"
But sometimes that fails with a cryptic error about how from_json can't parse a perfectly parse-able string (I know because I've copied it into a REPL and used json.loads to pasrse it). When I create a variable with value saved_off | typeof it shows Wrapper instead of str when it fails.
So instead I have to use "{{ (saved_off | string | from_json)[repeat.index - 1] }}" to force the Wrapper to be a string, which works.
Why is the result sometimes a Wrapper and sometimes a string? If it's relevant I'm triggering the automation from the UI.
I found one similar example here, but the answer there was to simplify the logic down to a one-liner and in my case I can't do that: https://community.home-assistant.io/t/automation-referencing-a-variable-inside-another-variable-error-typeerror-unhashable-type-wrapper/799966
I was trying to add a volume setting to my alexa notification service, and was trying to be cute and preserve the old volume of the echo i am calling and then set it back after the notification happens. I am having an issue with setting a variable that always seems to error out with: Error: TypeError: unhashable type: 'Wrapper' Here is the pa...