Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working anymore with 0.115.0 #44

Closed
enkama opened this issue Sep 17, 2020 · 13 comments
Closed

Not working anymore with 0.115.0 #44

enkama opened this issue Sep 17, 2020 · 13 comments

Comments

@enkama
Copy link

enkama commented Sep 17, 2020

The Variable Component is not working any longer, with the 0.115.0 Update. It's throwing out this Error:

2020-09-17 21:24:23 ERROR (MainThread) [homeassistant.components.automation.last_motion] Last Motion: Error executing script. Unexpected error for call_service at pos 1: Error rendering data template: UndefinedError: 'variable' is undefined
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 285, in async_render
    return compiled.render(kwargs).strip()
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/usr/local/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 2, in top-level template code
  File "/usr/local/lib/python3.8/site-packages/jinja2/sandbox.py", line 407, in getattr
    value = getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'variable' is undefined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 138, in async_prepare_call_from_config
    service_data.update(template.render_complex(config[conf], variables))
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 81, in render_complex
    return {
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 82, in <dictcomp>
    render_complex(key, variables): render_complex(item, variables)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 86, in render_complex
    return value.async_render(variables)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 287, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'variable' is undefined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 206, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 385, in _async_call_service_step
    domain, service, service_data = async_prepare_call_from_config(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 140, in async_prepare_call_from_config
    raise HomeAssistantError(f"Error rendering data template: {ex}") from ex
homeassistant.exceptions.HomeAssistantError: Error rendering data template: UndefinedError: 'variable' is undefined
2020-09-17 21:24:23 ERROR (MainThread) [homeassistant.components.automation.last_motion] While executing automation automation.last_motion
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 285, in async_render
    return compiled.render(kwargs).strip()
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/usr/local/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 2, in top-level template code
  File "/usr/local/lib/python3.8/site-packages/jinja2/sandbox.py", line 407, in getattr
    value = getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'variable' is undefined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 138, in async_prepare_call_from_config
    service_data.update(template.render_complex(config[conf], variables))
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 81, in render_complex
    return {
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 82, in <dictcomp>
    render_complex(key, variables): render_complex(item, variables)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 86, in render_complex
    return value.async_render(variables)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 287, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'variable' is undefined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/automation/__init__.py", line 426, in async_trigger
    await self.action_script.async_run(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 944, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 198, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 206, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 385, in _async_call_service_step
    domain, service, service_data = async_prepare_call_from_config(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 140, in async_prepare_call_from_config
    raise HomeAssistantError(f"Error rendering data template: {ex}") from ex
homeassistant.exceptions.HomeAssistantError: Error rendering data template: UndefinedError: 'variable' is undefined

And it doesn't have to do with the script/automation because switching back to 0.114.4 is fixing the error and its working just fine. Probably has to do with the new "Variables" component they brought out.

@Mariusthvdb
Copy link

I feared that, hence asked Balloob and the devs about it, see the answer home-assistant/core#39915 (comment)

supposedly another thing?

@phdelodder
Copy link

I have managed to get it working like this:

  - service: variable.set_variable
    data:
      variable: last_motion
      attributes_template: "{\n      \"history_1\": \"{{ trigger.to_state.attributes.friendly_name|replace('motion\
        \ sensor','') }}\",\n      \"history_2\": \"{{ states.variable.last_motion.attributes.history_1\
        \ }}\",\n      \"history_3\": \"{{ states.variable.last_motion.attributes.history_2\
        \ }}\",\n      \"history_4\": \"{{ states.variable.last_motion.attributes.history_3\
        \ }}\",\n      \"history_5\": \"{{ states.variable.last_motion.attributes.history_4\
        \ }}\",\n      \"history_6\": \"{{ states.variable.last_motion.attributes.history_5\
        \ }}\",\n      \"history_7\": \"{{ states.variable.last_motion.attributes.history_6\
        \ }}\",\n      \"history_8\": \"{{ states.variable.last_motion.attributes.history_7\
        \ }}\",\n      \"history_9\": \"{{ states.variable.last_motion.attributes.history_8\
        \ }}\",\n      \"history_10\": \"{{ states.variable.last_motion.attributes.history_9\
        \ }}\"\n}\n"
    data_template:
      value: '{{ trigger.to_state.attributes.friendly_name|replace(''motion sensor'','''')
        }}: {{as_timestamp(states.variable.last_motion.last_changed)| timestamp_custom(''%X'')
        }}

        '

@Mariusthvdb
Copy link

Mariusthvdb commented Sep 18, 2020

what exactly did you change, I hope you're not referring to all those newlines and slashes? Please explain some more what you had before and how you managed to find what was not working? thanks!

or was it merely the addition of 'states' to each line ?
compared to:

      - service: variable.set_variable
        data:
          variable: last_motion
          attributes_template: >
            {
             "history_1":"{{variable.state}}",
             "history_2":"{{variable.attributes.history_1}}",
             "history_3":"{{variable.attributes.history_2}}",
             "history_4":"{{variable.attributes.history_3}}",
             "history_5":"{{variable.attributes.history_4}}",
             "history_6":"{{variable.attributes.history_5}}",
             "history_7":"{{variable.attributes.history_6}}",
             "history_8":"{{variable.attributes.history_7}}",
             "history_9":"{{variable.attributes.history_8}}",
             "history_10":"{{variable.attributes.history_9}}"
            }
        data_template:
          value: >
            {{trigger.to_state.name|replace(' sensor motion','')}}:
            {{as_timestamp(trigger.from_state.last_changed)|timestamp_custom('%X')}}

@phdelodder
Copy link

It was the addition of states.variable.entity_id that solved it for me.

@enkama
Copy link
Author

enkama commented Sep 18, 2020

Yea that seems to work. Here is the code a bit more clean.

  - service: variable.set_variable
    data:
      variable: last_motion
      attributes_template: >
       {
        "history_1":"{{ trigger.to_state.attributes.friendly_name|replace('Motion Sensor','') }}",
        "history_2":"{{ states.variable.last_motion.attributes.history_1}}",
        "history_3":"{{ states.variable.last_motion.attributes.history_2}}",
        "history_4":"{{ states.variable.last_motion.attributes.history_3}}",
        "history_5":"{{ states.variable.last_motion.attributes.history_4}}",
        "history_6":"{{ states.variable.last_motion.attributes.history_5}}",
        "history_7":"{{ states.variable.last_motion.attributes.history_6}}"
       }
    data_template:
      value: '{{trigger.to_state.attributes.friendly_name|replace(''Motion Sensor'','''')}}:{{as_timestamp(states.variable.last_motion.last_changed)| timestamp_custom(''%X'')}}'

@Mariusthvdb
Copy link

Mariusthvdb commented Sep 19, 2020

confirming this to work, and seems even cleaner:

      - service: variable.set_variable
        data:
          variable: last_motion
          attributes_template: >
            {
             "history_1":"{{states('variable.last_motion')}}",
             "history_2":"{{state_attr('variable.last_motion','history_1')}}",
             "history_3":"{{state_attr('variable.last_motion','history_2')}}",
             "history_4":"{{state_attr('variable.last_motion','history_3')}}",
             "history_5":"{{state_attr('variable.last_motion','history_4')}}",
             "history_6":"{{state_attr('variable.last_motion','history_5')}}",
             "history_7":"{{state_attr('variable.last_motion','history_6')}}",
             "history_8":"{{state_attr('variable.last_motion','history_7')}}",
             "history_9":"{{state_attr('variable.last_motion','history_8')}",
             "history_10":"{{state_attr('variable.last_motion','history_9')}}"
            }
        data_template:
          value: >
            {{trigger.to_state.name|replace(' sensor motion','')}}:
            {{as_timestamp(trigger.from_state.last_changed)|timestamp_custom('%X')}}

note we now need th state the full template to the variables state, where before we only needed to reference variable.attributes or variable.state.
Wonder if we even have to set the variable under the data: field (since we need to reference it completely in the attributes_template?

Also, the main default config

variable:
  last_motion:
    value: Not set
    restore: true
    attributes:
      icon: mdi:map-marker
      friendly_name: Last Motion

doesnt set the starting value to Not set, instead it reads None now.
@rogro82 is this something you can look into?
thanks!

@Mariusthvdb
Copy link

Mariusthvdb commented Sep 25, 2020

btw we can now (HA 115) leave out the data_template

      - service: variable.set_variable
        data:
          variable: last_motion
          attributes_template: >
            {
             "history_1":"{{states('variable.last_motion')}}",
             "history_2":"{{state_attr('variable.last_motion','history_1')}}",
             "history_3":"{{state_attr('variable.last_motion','history_2')}}",
             "history_4":"{{state_attr('variable.last_motion','history_3')}}",
             "history_5":"{{state_attr('variable.last_motion','history_4')}}",
             "history_6":"{{state_attr('variable.last_motion','history_5')}}",
             "history_7":"{{state_attr('variable.last_motion','history_6')}}",
             "history_8":"{{state_attr('variable.last_motion','history_7')}}",
             "history_9":"{{state_attr('variable.last_motion','history_8')}}",
             "history_10":"{{state_attr('variable.last_motion','history_9')}}"
            }
#        data_template:
          value: >
            {{trigger.to_state.name|replace(' sensor motion','')}}:
            {{as_timestamp(trigger.from_state.last_changed)|timestamp_custom('%X')}}

@dorko87
Copy link

dorko87 commented Sep 28, 2020

Guys many thanks. Working again!

@skynet01
Copy link

This stopped working again after the latest 2020.12.0 update. I am getting the
template value should be a string for dictionary value @ data['attributes_template']
Any suggestions on how to fix?

@enkama
Copy link
Author

enkama commented Dec 14, 2020

Yea, its definetly still working for me on 2020.12.0. Show your config?

@skynet01
Copy link

skynet01 commented Dec 14, 2020

weird.. this is mine

- id: update_last_motion 
  alias: "Update Last Motion"
  trigger:
    - platform: state
      entity_id: >
        binary_sensor.guest_big_motion,
        binary_sensor.fireplace_motion,
        binary_sensor.frontdoor_motion,
        binary_sensor.living_room_motion,
        binary_sensor.master_bathroom_motion,
        binary_sensor.kitchen_motion,
        binary_sensor.office_motion,
        binary_sensor.guest_small_motion,
        binary_sensor.bedroom_motion
      to: 'on'
  mode: queued
  action:
    - service: variable.set_variable
      data:
        variable: last_motion
        attributes_template: >
         {
          "history_1":"{{ trigger.to_state.attributes.friendly_name|replace('Motion','') }}",
          "history_2":"{{ states.variable.last_motion.attributes.history_1}}",
          "history_3":"{{ states.variable.last_motion.attributes.history_2}}",
          "history_4":"{{ states.variable.last_motion.attributes.history_3}}"
         }
      data_template:
        value: '{{trigger.to_state.attributes.friendly_name|replace(''Motion'','''')}}'

@Mariusthvdb
Copy link

Mariusthvdb commented Dec 14, 2020

not weird, have a look at: #47

@skynet01
Copy link

That fixed it, thanks for pointing me in the right direction :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants