Error in automations.yaml:can not read an implicit mapping pair

Hi all,

what I would like to do is set the shutter in my office to a specific position depending on the weather and sund conditions. The trigger is either the sun rising or a change of the wheather state.

What I also would like to do is not running the automations (I have several for sevral triggers) if the cover is above 95% closed (or as HASS reads it, less than 5% open).

I try to use the following template in order to read out the cover position state, and in the template editor it works fine:

'{{ state_attr('cover.shelly_shsw_25_8caab505a689', 'current_position') < 5}}'

However, when I try to set up the condition in the automations.yaml, I geht the following error message:

can not read an implicit mapping pair; a colon is missed at line 22, column 19:
      '{{ state_attr('cover.shelly_shsw_25_8caab505a68 ... 
                      ^

Following is an example of one of the automations.yaml:

- id: '1586975582792'
  alias: Rolladen Büro runter
  description: Fährt Rolladen im Büro vormittags runter bei Sonnenschein
  trigger:
  - entity_id: weather.home
    for: 00:10:00
    platform: state
    to: sunny
  - entity_id: weather.home
    for: 00:10:00
    platform: state
    to: partlycloudy
  - entity_id: weather.home
    for: 00:10:00
    platform: state
    to: exceptional
  condition:
  - condition: time
    after: 06:00:00
    before: '10:59:00'
  - condition: template
  '{{ state_attr('cover.shelly_shsw_25_8caab505a689', 'current_position') < 5}}'
  action:
  - data:
      entity_id: cover.shelly_shsw_25_8caab505a689
      position: 55
    service: cover.set_cover_position

Any ideas what is going wrong?

  - condition: template
    value_template: "{{ state_attr('cover.shelly_shsw_25_8caab505a689', 'current_position') | int < 5}}"

EDIT

Additional correction as per joconnor’s comment below: also convert string value to integer before performing a less-than comparison with an integer.

1 Like

Convert to int first :slight_smile:

Also to the OP:

When you use quotes like that, make sure the outer quotes are different from the inside quotes. I usually use double quotes for the template, and single inside, but it usually doesn’t matter.

i.e. "{{ 'using single quotes inside' }}"

That’s in addition to missing the ‘value_template’ part as noted above.

… and if you find nested quotes to be confusing, use the line-continuation character (>) then put the template on a separate line. Now it doesn’t need outer quotes.

  - condition: template
    value_template: >
      {{ state_attr('cover.shelly_shsw_25_8caab505a689', 'current_position') | int < 5}}

I am new to HASS and programming in general, could you point me to documentation which explains what I need to do or provide me some sample code? Thanks!

Edit: also, how do I know when I need to convert what to what?

Thank you for the help, I will adjust my code!

In the documentation for Automation Conditions, the first example, containing a Template Condition, shows the use of outer double-quotes and inner single-quotes.

Every entity’s state is always a string value. This string value cannot exceed 255 characters in length.

An entity may also have attributes and these can be strings, integers, floating-point numbers, datetime objects, etc. An attribute can be longer than 255 characters.

With the documentation, I meant something which explains how I know when I need to convert something into something and how to do it :slight_smile:

Pretty much everything is a string. If you want to do math on it, you need to convert it to a number. And even if it is a number, there is no harm in converting a number to a number. So I just always do it.

The best thing is to use the built in template editor under developer tools and just play around and practice.

So I need to insert “int” somewhere along the line in order to convert the cover position, correct?

If by “somewhere along the line” you mean after the state_attr() function, then yes.

{{ state_attr('cover.shelly_shsw_25_8caab505a689', 'current_position') | int < 5 }}

Thanks both for your help. And had honestly no idea where to insert the int, i would have had to try every position…

The template you created uses a templating language called Jinja2. Home Assistant uses a subset of Jinja2’s features and adds a few of its own.

Home Assistant’s documentation does not explain the basic syntax of Jinja2 but directs the reader to Jinja2’s official documentation. That’s not ideal because Jinja2 documentation, although very detailed and complete, is not written with home automation users in mind. For the casual user, it’s dry, technical, and the examples are challenging to understand how they apply to Home Assistant.

To give you a taste of Jinja2’s power in Home Assistant, go to Developer Tools > Template, examine and experiment with the sample template it displays in the Template Editor, then paste this into it:

{{ states.light | selectattr('state', 'eq', 'on') | list | count }}

It will show you the number of lights that are currently on. Notice how the result of states.light is filtered by selectattr('state', 'eq', 'on') then converted to a list using the list filter and finally the lsits’s items are tallied using count.

Here’s one more. It displays a comma-delimited string containing the names of all lights that are on.

{{ states.light | selectattr('state', 'eq', 'on') | map(attribute = 'name') | list | join(',') }}
1 Like

That’s very helpful, thank you for your explanation. I scrolling through the Jinja Docs and it is difficult to undertand if you are completely new to programming or code in general.

1 Like