Unifi Protect Service in Automation won't accept |int from template

Trying to make my Unifi Doorbell’s WDR settings following light intensity and therefore made below automation with template.
The developer of the Unifi Protect integration added the service on my request and unfortunately isn’t working as expected. The developer can’t find anything in his code what the error is causing and so he’s thinking about a general fault in my template or bug in the HA templating code.

Here’s my desired automation and below in the Github link there are more thing we tried and not work;

- alias: set_doorbell_wdr
  trigger:
    - platform: state
      entity_id: input_number.zonnepanelen_cloud_coverage
    - platform: state
      entity_id: sensor.period_of_day
  action:
    - service: unifiprotect.set_wdr_value
      data:
        entity_id: camera.voordeur_bel
        value: >
          {% set wdr = 0|int %}
          {% if(states("sensor.period_of_day")|lower=='day') %}
            {% if((states("sensor.liveproduction")|int>100 and states("sensor.clearsky_current")|int>100)) %}
                {% if((states("input_number.pv_cloud_coverage")|int)<50) %}
                  {% set wdr = 2 %}
                {% else %}
                  {% set wdr = 3 %}
                {% endif %}         
            {% else %}
              {% set wdr = 1 %}
            {% endif %}
          {% elif(states("sensor.period_of_day")|lower=='night') %}
            {% set wdr = 0 %}
          {% elif(states("sensor.period_of_day")|lower=='morning' or states("sensor.period_of_day")|lower=='dusk') %}
            {% set wdr = 1 %}
          {% else %}
            {% set wdr = 1 %}
          {% endif %}
          {{wdr|int}}

The error from the logbook;

Logger: homeassistant.components.automation.set_doorbell_wdr
Source: components/automation/__init__.py:416
Integration: Automatisering ([documentation](https://www.home-assistant.io/integrations/automation), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+automation%22))
First occurred: 18:14:02 (4 occurrences)
Last logged: 18:27:06
Error while executing automation automation.set_doorbell_wdr: expected int for dictionary value @ data['value']

The Github issue where more examples are posted:

Hopefully somebody can point me in the right direction to solve this :slight_smile:

What version of HA are you using?

On a recent 2021.3.2 version.
The Unifi Protect Dev also tested some older versions as noted in one of the comments on Github.

System Health

version core-2021.3.2
installation_type Home Assistant Container
dev false
hassio false
docker true
virtualenv false
python_version 3.8.7
os_name Linux
os_version 5.4.0-66-generic
arch x86_64
timezone Europe/Amsterdam
Lovelace
dashboards 1
resources 37
views 28
mode yaml

Also when I test the template in the dev tools, it always says its a String result. Very strange.
Screenshot - 10_03_2021 , 21_20_44

It don’t matter what I try, its always a result string.

If you put it all into the template editor, of course it will be a string. Put just the template into the template editor. I did, and it comes out as a number:

Not sure exactly why you’re getting that error. But at the very least, the dev could try changing this:

to this:

SET_WDR_VALUE_SCHEMA = vol.Schema(
    {
        vol.Required(ATTR_ENTITY_ID): cv.entity_ids,
        vol.Required(CONF_VALUE): vol.Coerce(int),
    }
)

BTW, your template could be reduced to:

          {% if(states("sensor.period_of_day")|lower=='day') %}
            {% if((states("sensor.liveproduction")|int>100 and states("sensor.clearsky_current")|int>100)) %}
                {% if((states("input_number.pv_cloud_coverage")|int)<50) %}
                  2
                {% else %}
                  3
                {% endif %}         
            {% else %}
              1
            {% endif %}
          {% elif(states("sensor.period_of_day")|lower=='night') %}
            0
          {% else %}
            1
          {% endif %}

Thanks for the optimization!
Just tested this template in the editor and also your template results in a “string result”;


I reduced your template to 1 online, to prevent interfering chars/spaces/etc.

I know that I only have to put the template in the “template editor” and not the whole automation :slight_smile:

Locally I changed the suggested code and my automation/template is working now!!
Asked the dev of the integration to look at your suggestion.
Could you maybe give some explanation on how your code it better then current code and how its fixing the issue?

Thanks in advance for time and helping!!

How do you have legacy_templates set? If you set it to true then all templates will result in strings as they always used to do.

The code change allows the value parameter of the service call to automatically convert strings to a number. This way either "3" or 3 would work. This is the typical way to write a service schema.

1 Like

Oh yeah I have legacy_templates set! I totally forgot that! :slight_smile: :blush:

Then I have to check all my templates and set it to false. Thanks for pointing it out!

1 Like

Solutions;

Code change in the custom_component:

legacy_templates: