Can't use template in history_graph

Hi!

HA version is 0.69.1.

I would like to change hours_to_show attribute dynamically via input_number.

collector:
  entities:
  - sensor.collector_air_temperature
  - sensor.collector_in_temperature
  - sensor.collector_out1_temperature
  hours_to_show: {{ states('input_number.history_period') | int }}
  refresh: 60

But there is a check configuration error:

Error loading /home/homeassistant/.homeassistant/configuration.yaml: invalid key: “OrderedDict([(“states(‘input_number.history_period’) | int”, None)])” in “/home/homeassistant/.homeassistant/history_graph.yaml”, line 8, column 0

If I add double quotes I receive an error handling request.

collector:
  entities:
  - sensor.collector_air_temperature
  - sensor.collector_in_temperature
  - sensor.collector_out1_temperature
  hours_to_show: "{{ states('input_number.history_period') | int }}"
  refresh: 60

Error handling request

Traceback (most recent call last):
File “/srv/homeassistant/lib/python3.5/site-packages/aiohttp/web_protocol.py”, line 381, in start
resp = await self._request_handler(request)
File “/srv/homeassistant/lib/python3.5/site-packages/aiohttp/web_app.py”, line 322, in _handle
resp = await handler(request)
File “/srv/homeassistant/lib/python3.5/site-packages/aiohttp/web_middlewares.py”, line 88, in impl
return await handler(request)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/http/static.py”, line 68, in staticresource_middleware
return await handler(request)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/http/real_ip.py”, line 27, in real_ip_middleware
return await handler(request)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/http/ban.py”, line 68, in ban_middleware
return await handler(request)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/http/auth.py”, line 54, in auth_middleware
return await handler(request)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/http/view.py”, line 104, in handle
result = await result
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/config/core.py”, line 24, in post
errors = yield from async_check_ha_config_file(request.app[‘hass’])
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/config.py”, line 690, in async_check_ha_config_file
check_ha_config_file, hass)
File “/usr/lib/python3.5/asyncio/futures.py”, line 380, in iter
yield self # This tells Task to wait for completion.
File “/usr/lib/python3.5/asyncio/tasks.py”, line 304, in _wakeup
future.result()
File “/usr/lib/python3.5/asyncio/futures.py”, line 293, in result
raise self._exception
File “/usr/lib/python3.5/concurrent/futures/thread.py”, line 55, in run
result = self.fn(*self.args, **self.kwargs)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/scripts/check_config.py”, line 347, in check_ha_config_file
config = component.CONFIG_SCHEMA(config)
File “/srv/homeassistant/lib/python3.5/site-packages/voluptuous/schema_builder.py”, line 267, in call
return self._compiled(, data)
File “/srv/homeassistant/lib/python3.5/site-packages/voluptuous/schema_builder.py”, line 587, in validate_dict
return base_validate(path, iteritems(data), out)
File “/srv/homeassistant/lib/python3.5/site-packages/voluptuous/schema_builder.py”, line 379, in validate_mapping
cval = cvalue(key_path, value)
File “/srv/homeassistant/lib/python3.5/site-packages/voluptuous/schema_builder.py”, line 769, in validate_callable
return schema(data)
File “/srv/homeassistant/lib/python3.5/site-packages/voluptuous/schema_builder.py”, line 267, in call
return self._compiled(, data)
File “/srv/homeassistant/lib/python3.5/site-packages/voluptuous/schema_builder.py”, line 587, in validate_dict
return base_validate(path, iteritems(data), out)
File “/srv/homeassistant/lib/python3.5/site-packages/voluptuous/schema_builder.py”, line 379, in validate_mapping
cval = cvalue(key_path, value)
File “/srv/homeassistant/lib/python3.5/site-packages/voluptuous/schema_builder.py”, line 769, in validate_callable
return schema(data)
File “/srv/homeassistant/lib/python3.5/site-packages/voluptuous/schema_builder.py”, line 267, in call
return self._compiled(, data)
File “/srv/homeassistant/lib/python3.5/site-packages/voluptuous/schema_builder.py”, line 587, in validate_dict
return base_validate(path, iteritems(data), out)
File “/srv/homeassistant/lib/python3.5/site-packages/voluptuous/schema_builder.py”, line 379, in validate_mapping
cval = cvalue(key_path, value)
File “/srv/homeassistant/lib/python3.5/site-packages/voluptuous/schema_builder.py”, line 769, in validate_callable
return schema(data)
File “/srv/homeassistant/lib/python3.5/site-packages/voluptuous/validators.py”, line 535, in call
if self.min is not None and not v >= self.min:
TypeError: unorderable types: str() >= int()

What’s wrong in my configuration? Or is it a bug?

You can’t put templates in areas that don’t allow templates. Most configs do not allow templates. History graph does not allow it.

1 Like

Thanks!
What could I do for change hours_to_show in runtime?

I’d wager that you’d need to adjust the components python code to account for an entity id in that field. Not sure how it would behave though.

I made a python scrypt. It’s my first python script :slight_smile:
For entity_id parameter could be used string for single entity_id or list for multiple.
For hours_to_show I use input_number slider in the view with history_graphs.

def change_hours( hass, entity_id, hours_to_show ):
        state = hass.states.get(entity_id)
        attr = state.attributes.copy()
        attr['hours_to_show'] = hours_to_show
        hass.states.set(entity_id, state.state, attr)
        return

hours_to_show = data.get('hours_to_show', '1')
entities = data.get('entity_id')
if isinstance(entities, str):
        entity_id = entities
        change_hours( hass, entity_id, hours_to_show )
else:
        for entity_id in entities:
                change_hours( hass, entity_id, hours_to_show )

groups.yaml

history_view:
  view: yes
  name: История
  icon: mdi:chart-line
  entities:
    - input_number.history_period
    - history_graph.collector
    - history_graph.podval

automation_old.yaml

- id: '1526236744263'
  alias: change_history_graph_period
  trigger:
  - entity_id: input_number.history_period
    platform: state
  condition: []
  action:
  - data_template:
      entity_id:
      - history_graph.collector
      - history_graph.podval
      hours_to_show: "{{ states('input_number.history_period') | int }}"
    service: python_script.history_graph_change_hours
1 Like

Nice work! Wasn’t too bad was it?

Thanks!
It wasn’t hard to do.

Sorry for the thread necro, but I’m literally doing the same thing and can’t find the information I need in the docs, forums or on the web.

I’ve got the automation and script running, but obviously not updating the charts as expected. I’m pretty sure it’s related to the entity names:

- history_graph.collector
- history_graph.podval

Long story short, how do I go about getting the correct entity_id's of the history_graph's for the automation?

Edit to add: I’ve tried using history_graph.dishwasher and history_graph.sensor.dishwasher_watts

His python script is creating those entities. In a normal system, history_graph is not a valid domain.