Dynamic Icons using icon_template

really hoping someone can help me out. I think I am on the right path to accomplishing dynamic icons based off a sensor input. At a high level I have a sensor for a garage door. It uses http GET command to retrieve a message “OPEN” or “CLOSED”. real simple. My sensors are working correctly and HA shows the correct status. I am following Bruh’s config on GIT hub where I have separate YAML files stored in ./sensors.

My error is invalid.config “sensor.template” Check config in the UI states…

Please help. Thanks

[sensor.template]: [Garage2] is an invalid option for [sensor.template]. Check: sensor.template->sensors->Garage2. (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.template/

so…
.homeassistant/configuration.yaml
.homeassistant/sensors/garage_1.yaml
.homeassistant/sensors/garage_2.yaml
.homeassistant/sensors/template_sensors/garage_1_template.yaml
.homeassistant/sensors/template_sensors/garage_2_template.yaml
so far so good I think
…Garage_1.yaml contains…
platform: rest
resource: http://192.168.1.180/device/Garage1status
method: GET
name: Garage Door 1 Status

Garage_1_template.yaml contains…
platform: template
sensors:
Garage_1_status:
value_template: ‘{{ states.sensor.garage_door_1_status }}’
icon_template: ‘{% if is_state(“sensor.garage_door_1_status”, “OPEN”) %}mdi:amazon{% else %}mdi:android{% endif %}’
friendly_name: “Garage 1 Status”

configuration.yaml contains…

homeassistant:

name: Home

Location required to calculate the time the sun rises and sets

latitude: 39.3795
longitude: -77.1656

Impacts weather/sunrise data (altitude above sea level in meters)

elevation: 197

metric for Metric, imperial for Imperial

unit_system: imperial

Pick yours from here: http://en.wikipedia.org/wiki/List_of_tz_database_time_zones

time_zone: America/New_York
customize: !include customize.yaml

logger:

default: info

Show links to resources in log and frontend

#introduction:

Enables the frontend

frontend:

Enables configuration UI

config:

http:

Uncomment this to add a password (recommended!)

api_password: PASSWORD

Uncomment this if you are using SSL or running in Docker etc

base_url: example.duckdns.org:8123

Checks for available updates

Note: This component will send some information about your system to

the developers to assist with development of Home Assistant.

For more information, please see:

https://home-assistant.io/blog/2016/10/25/explaining-the-updater/

updater:

mqtt:
broker: 192.168.1.102
port: 1883
client_id: TRW-HASS

Discover some devices automatically

discovery:

Allows you to issue voice commands from the frontend in enabled browsers

conversation:

Enables support for tracking state changes over time.

history:

View all events in a logbook

logbook:

Track the sun

sun:

Weather Prediction

Garage Door

switch:
platform: command_line
switches:
garage_door_1:
command_on: curl -X POST http://192.168.1.180/device/activateGarage1
command_off: curl -X POST http://192.168.1.180/device/activateGarage1
command_state: curl -X GET http://192.168.1.180/device/Garage1status
value_template: ‘{{ value_json.state == “on” }}’
friendly_name: Activate Garage Door 1
garage_door_2:
command_on: curl -X POST http://192.168.1.180/device/activateGarage2
command_off: curl -X POST http://192.168.1.180/device/activateGarage2
command_state: curl -X GET http://192.168.1.180/device/Garage2status
value_template: ‘{{ value_json.state == “on” }}’
friendly_name: Activate Garage Door 2

Outside Devices

light:

  • platform: mqtt
    name: “Outside Lights”
    command_topic: “/outdoors/lowvoltagelights/relay”
    state_topic: “/outdoors/lowvoltagelights/relay”
    payload_on: “1”
    payload_off: “0”

Text to Speech

tts:
platform: google

group: !include groups.yaml
automation: !include automations.yaml
###########INCLUDES#####
sensor: !include_dir_list sensors/


May need a .state at the end also it may be worth showing us the sensor template for garage2 since that is where HA thinks the error is :wink:

like this?
value_template: ‘{{ is_state.sensor.garage_door_2_status.state }}’

I get the same error for both Garage 1 and Garage 2

the formatting has me a bit confused though. Unfortunately the forum isn’t preserving the spacing eventhough I used the </> button. Anyhow for each garage_X_template.yaml file the first line has 2 spaces. then line 2 is sensors lined up with platform IE:
platform: template
sensors:
Garage_2_template:
value_template
icon_template
Here’s what I have in garage_2_template.yaml

  platform: template

sensors:
Garage_2_template:
value_template: ‘{{ is_state.sensor.garage_door_2_status.state }}’
icon_template: ‘{% if is_state(“sensor.garage_door_1_status”, “OPEN”) %}mdi:amazon{% else %}mdi:android{% endif %}’

Thanks

more logging…
2017-06-07 17:30:10 INFO (MainThread) [homeassistant.setup] Setting up automation
2017-06-07 17:30:10 ERROR (MainThread) [homeassistant.config] Invalid config for [sensor.template]: [Garage_2_template] is an invalid option for [sensor.template]. Check: sensor.template->sensors->Garage_2_template. (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.template/
2017-06-07 17:30:10 ERROR (MainThread) [homeassistant.config] Invalid config for [sensor.template]: [Garage_1_template] is an invalid option for [sensor.template]. Check: sensor.template->sensors->Garage_1_template. (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.template/
2017-06-07 17:30:10 INFO (MainThread) [homeassistant.setup] Setting up sensor
2017-06-07 17:30:10 INFO (MainThread) [homeassistant.setup] Setting up discovery
2017-06-07 17:30:10 INFO (MainThread) [homeassistant.setup] Setting up light
2017-06-07 17:30:10 INFO (MainThread) [homeassistant.setup] Setting up logbook
2017-06-07 17:30:10 INFO (MainThread) [homeassistant.setup] Setting up config
2017-06-07 17:30:10 INFO (MainThread) [homeassistant.setup] Setting up tts
Failed config
sensor.template:
platform: template
sensors: [source /home/jtroberts/.homeassistant/sensors/template_sensors/garage_1_template.yaml:3]
Garage_1_template: [source /home/jtroberts/.homeassistant/sensors/template_sensors/garage_1_template.yaml:4]
icon_template: {% if is_state(“sensor.garage_door_1_status”, “OPEN”) %}mdi:amazon{% else %}mdi:android{% endif %}
value_template: {{ is_state.sensor.garage_door_2_status.state }}

Successful config (partial)
sensor.template:

One other piece of info…

If I take my value_template and icon_template and put them individually into the developertools I get the correct results. That is OPEN or CLOSED and either ‘mdi:android’ or ‘mdi:amazon’

thanks for the help

I think this should be …

value_template: '{{ states.sensor.garage_door_2_status.state }}'

Note the single quotes and no is_state, see if that helps :slight_smile:

To get the forum to correctly format code put a clear line on either side of code then select the code and press the button < / >

OK, we’re getting somewhere. For others to see here’s what I have in my garage_2_template.yaml

# Garage Door 2
  platform: template
  sensors:
    garage_2_status:
      value_template: '{{ states.sensor.garage_door_2_status.state }}'
      icon_template: '{% if is_state("sensor.garage_door_2_status", "OPEN") %}mdi:amazon{% else %}mdi:android{% endif %}'
      friendly_name: "Garage 2 Status"

That is working but I do not have the dynamic icons.  any ideas?

here's what the developer tools show


That is exactly what I want to see. Well I assume so. Is sensor.garage_2_status “icon: mdi:android” a string or an actual icon. not sure. More importantly in the UI I do not get the amazon or android icons based on status. thoughts?

Am I on the right path? PS: I do have a customize.yaml. Here are it’s contents.

switch.garage_door_1:
   friendly_name: Garage Door 1
   icon: mdi:garage
 switch.garage_door_2:
   icon: mdi:garage
   friendly_name: Garage Door 2
 sensor.garage_1_status:
   friendly_name: Garage Door 1 Status
 sensor.garage_2_status:
   friendly_name: Garage Door 2 Status

Appreciate the help.  thx

I think this needs to be …

icon_template: {% if is_state(‘sensor.garage_door_2_status’, ‘OPEN’) %}mdi:amazon{% else %}mdi:android{% endif %}

Note that I’ve removed the single quotes round all and replaced double quotes for single, try that :slight_smile:

That throws this error…
2017-06-08 11:29:51 ERROR (MainThread) [homeassistant.bootstrap] Error loading /home/jtroberts/.homeassistant/configuration.yaml: while scanning for the next token
found character ‘%’ that cannot start any token
in “/home/jtroberts/.homeassistant/sensors/template_sensors/garage_2_template.yaml”, line 6, column 23

Here’s the garage_2_template.yaml contents…

# Garage Door 2
  platform: template
  sensors:
    garage_2_status:
      value_template: '{{ states.sensor.garage_door_2_status.state }}'
      icon_template: {% if is_state('sensor.garage_door_2_status', 'OPEN') %}mdi:amazon{% else %}mdi:android{% endif %}
      friendly_name: "Garage 2 Status"

OK, this … works

  sensors:
    dark_sky_template:
      value_template: "{{ states('sensor.dark_sky_summary') }}"
      icon_template: >
        {% if is_state('sensor.dark_sky_icon', 'clear-day') %}
          mdi:weather-sunny
        {% elif is_state('sensor.dark_sky_icon', 'clear-night') %}
          mdi:weather-night
        {% elif is_state('sensor.dark_sky_icon', 'rain') %}
          mdi:weather-rainy
        {% elif is_state('sensor.dark_sky_icon', 'snow') %}
          mdi:weather-snowy
        {% elif is_state('sensor.dark_sky_icon', 'sleet') %}
          mdi:weather-snowy-rainy
        {% elif is_state('sensor.dark_sky_icon', 'wind') %}
          mdi:weather-windy-variant
        {% elif is_state('sensor.dark_sky_icon', 'fog') %}
          mdi:weather-fog
        {% elif is_state('sensor.dark_sky_icon', 'cloudy') %}
          mdi:weather-cloudy
        {% elif is_state('sensor.dark_sky_icon', 'partly-cloudy-day') or is_state('sensor.dark_sky_icon', 'partly-cloudy-night') %}
          mdi:weather-partlycloudy
        {% elif is_state('sensor.dark_sky_icon', 'hail') %}
          mdi:weather-hail
        {% elif is_state('sensor.dark_sky_icon', 'thunderstorm') %}
          mdi:weather-lightning
        {% else %}
          mdi:help-circle
        {% endif %}

And the only difference apart for the fact mine are on different lines is the value_template is enclosed with double quotes and it uses the states() statement, tho’ I’m not sure why that would affect the icon :P.

1 Like

wow… good stuff. Unfortunately the devil is in the details. In my groups.yaml my entity wasn’t “garage_2_status”. So I never asked the UI to show the new icon. rookie mistake I guess. good new is both methods work. details below for others.

@keithh666 Thanks. I really appreciate it.

Summary to help others…

  • Configuration.yaml (see above) didn’t change but pay close attention to the last line where I load my sensor files.

  • customize.yaml

    switch.garage_door_1:
    friendly_name: Garage Door 1
    icon: mdi:garage
    switch.garage_door_2:
    icon: mdi:garage
    friendly_name: Garage Door 2
    sensor.garage_1_status:
    friendly_name: Garage Door 1 Status
    sensor.garage_2_status:
    friendly_name: Garage Door 2 Status

groups.yaml

outside_view:
  name: Outside
  view: yes
  entities:
    - group.outside
garage_view:
  name: Garage
  view: yes
  entities:
    - group.garage
####GROUPS
outside:
  name: Outside Devices
  entities:
    - light.outside_lights
garage:
  name: Garage Doors
  entities:
    - switch.garage_door_1
    - switch.garage_door_2
    - sensor.garage_1_status
    - sensor.garage_2_status
  • garage_1.yaml

    platform: rest
    resource: http://192.168.1.180/device/Garage1status
    method: GET
    verify_ssl: false
    name: Garage Door 1 Status

  • garage_2.yaml

    platform: rest
    resource: http://192.168.1.180/device/Garage2status
    method: GET
    verify_ssl: false
    name: Garage Door 2 Status

  • garage_1_template.yaml

    Garage Door 1

    platform: template
    sensors:
    garage_1_status:
    value_template: “{{ states(‘sensor.garage_door_1_status’) }}”
    icon_template: >
    {% if is_state(‘sensor.garage_door_1_status’, ‘OPEN’) %}
    mdi:amazon
    {% else %}
    mdi:android
    {% endif %}

  • garage_2_template.yaml

    Garage Door 2

    platform: template
    sensors:
    garage_2_status:
    value_template: ‘{{ states.sensor.garage_door_2_status.state }}’
    icon_template: ‘{% if is_state(“sensor.garage_door_2_status”, “OPEN”) %}mdi:amazon{% else %}mdi:android{% endif %}’

There’s more to do but I consider this a major hurdle. Thanks

@jtroberts Thank you for posting your final code, that was really helpful. The part that was throwing me for a loop, is that I didn’t realize that templates create a completely new and separate sensor. (Am I understanding that right?) With the config above, in your states tab, you should have both:
sensor.garage_door_1_status
sensor.garage_1_status

Looking at the front end created with groups.yaml, we will only see sensor.garage_1_status, while sensor.garage_door_1_status is not displayed at all, is that correct?