gadgetchnnel / lovelace-card-templater

Custom Lovelace card which allows Jinja2 templates to be applied to other cards
120 stars 6 forks source link

Issue with template sensor #12

Open zappasmart opened 4 years ago

zappasmart commented 4 years ago

Description of problem:

In my test I have a template sensor that changes value, name and icon based on an input boolean.

Displaying the template sensor in sensor or entities cards works well, state changes are correctly detected and value, name and icon properly updated when input boolean toggles.

However, if sensor or entities cards are surrounded by card-templater, they don't detect the input boolean state changes properly. I used the card-templater only to dinamically change the theme (a blue or orange theme), based on the input boolean state.

Input Boolean OFF

When input boolean is off, blue theme is correcly applied, card content is correctly displayed: image

Input Boolean turned ON

When input boolean is turned on, orange theme is correctly applied but card content is not updated: image

Input Boolean turned OFF

When input boolean is turned back off, blue theme is correctly applied but card content is wrongly updated: image

configuration.yaml:

input_boolean:
  test:

sensor:
  - platform: template
    sensors:
      test:
        entity_id:
          - input_boolean.test
        unit_of_measurement: '°C'
        device_class: temperature
        value_template: >-
          {% if is_state('input_boolean.test','on') %}
            20
          {% else %}
            -20
          {% endif %}
        icon_template: >-
          {% if is_state('input_boolean.test','on') %}
            mdi:fire
          {% else %}
            mdi:snowflake
          {% endif %}
        friendly_name_template: >-
          {% if is_state('input_boolean.test','on') %}
            Hot
          {% else %}
            Cold
          {% endif %}

lovelace:

    cards:
      - type: entities
        entities:
          - entity: input_boolean.test
            name: Heat
            icon: 'mdi:fire'
      - type: 'custom:card-templater'
        entities:
          - input_boolean.test
        card:
          type: sensor
          entity: sensor.test
          graph: line
          theme_template: >-
            {{ "OrangeTheme" if states.input_boolean.test.state == "on"
            else "BlueTheme" }}
      - type: 'custom:card-templater'
        entities:
          - input_boolean.test
        card:
          type: entities
          entities:
            - sensor.test
          theme_template: >-
            {{ "OrangeTheme" if states.input_boolean.test.state == "on"
            else "BlueTheme" }}
      - type: sensor
        entity: sensor.test
        graph: line
      - type: entities
        entities:
          - entity: sensor.test
gadgetchnnel commented 4 years ago

Try adding sensor.test under the entities of the card-templater card, like this:

- type: 'custom:card-templater'
  entities:
    - input_boolean.test
    - sensor.test
  card:
    type: sensor
    entity: sensor.test
    graph: line
    theme_template: >-
      {{ "OrangeTheme" if states.input_boolean.test.state == "on"
      else "BlueTheme" }}

Without that, it may not refresh when the state of that entity changes.

zappasmart commented 4 years ago

Seems like a workaround, but it works, thanks!

gadgetchnnel commented 4 years ago

@zappasmart I've just released a new beta version (0.0.8b3) which should fix an issue where, if entities which are not templated are updated, the card would not update. This should fix your issue and mean that you don't need to add sensor.test under the card-templater entities.

Can you please try this out and see if it helps? If you are using HACS you will need to enable Beta versions and, if not, you will need to download the latest pre-release version from Releases.

zappasmart commented 4 years ago

Just tried. Updated with 0.0.8b3 beta version, restarted home assistant, cleared browser cache. Unfortunately it's still not working.

zappasmart commented 4 years ago

image