#Sometimes `to_json` returns a `Wrapper`, which breaks later `from_json`?

1 messages · Page 1 of 1 (latest)

stark hornet
#

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

autumn bolt
#

scenes don't seem to save the color of lights that were off when the scene was saved

Random question: Why would scene need to save color if the light is off in the scene?

distant tapir
distant tapir
autumn bolt
distant tapir
#

not sure what you mean, but when a light is off it loses all attributes like brightness, hs_color, color_temp etc

#

so a scene will not be able to store those attributes, as they don't exist

autumn bolt
#

a ok, fair. thanks!