developer tools: states, services and template

I waited way to long while configuring hass.io before I started to look into the Developer Tools menu that can be found in the left menu of the hass.io web interface.

There are especially three parts that I eventually learned are really useful; states, services and templating.

States
After you have added new features to hass.io like hardware or sensors I always start by looking at the States page. There I get an overview of all entities, their states and attributes so that I know what and how I should include them in my code.

For troubleshooting purposes it is always the place I start, I can by looking at the table realize that an entity has disappeared for some reason or that is was never added in the first place. If I add or troubleshoot hardware that should change state (on/off) such as motion or door sensors I can see that the expected change of state actually is picked up by hass.io.

With this information it is so much easier to solve problems and write correct code from the start.

Figure 1. The States view in hass.io.


Services
Services is another great tool that I use all the time. With services I can for testing purposes start automation, turn on lights, start scripts and so on. If you for some reason have problems with your code while developing it is really convenient to fall back to Services and check what is working and what is not.

Figure 2. The Services view in hass.io.

Template
The third tool under Development Tools that I use alot is Template where I can test parts of my code before I add it to scripts and automations. This is very useful both during development and troubleshooting since you get the results from the code in real time as seen in Figure 3.

Figure 3. The Template view in hass.io.

In the Template Editor statements are enclosed in {{ statement }} and clauses are enclosed in {% clause %} as in example below.
{{ (as_timestamp(states.sensor.date_time_iso.state) | float) |timestamp_custom('%H:%M') }}

{{ states('sensor.installed_version') }}
{{ states('sensor.latest_available_version') }}

{%- if states('sensor.installed_version') == states('sensor.latest_available_version') -%}
  Yes
{%- else -%}
  No
{%- endif -%}
Note: when you move the code from Template to your yaml files make sure that the statement is put within " " if on a single line. See below for real yaml code examples.
- platform: template
  sensors:
    omxpipercent:
      value_template: "{{ state_attr('sensor.omx_pi', 'changePercent') }} %"
- platform: template
  sensors:
    ha_update_available:
      friendly_name: 'HA Update Available'
      value_template: >-
        {%- if states('sensor.installed_version') == states('sensor.latest_available_version') -%}
          Yes
        {%- else -%}
          No
        {%- endif -%}
      icon_template: >-
        {%- if states('sensor.installed_version') == states('sensor.latest_available_version') -%}
          mdi:approval
        {% else %}
          mdi:alert-box
        {% endif %}
      friendly_name_template: >-
        {{ states('sensor.latest_available_version') }}
Note: Combining " " and ' ' on the same line can be tricky work, make sure you get it right.

You can read more about template at home-assistant.io.

Please let me know in the comments section if you have any questions or comments.

Comments