123
(Taras)
March 18, 2021, 3:04am
21
I agree.
action:
- choose:
- conditions: "{{ is_state(trigger.entity_id, 'Aus') }}"
sequence:
- service: light.turn_off
data:
entity_id: light.wohnzimmer
transition: -1
default:
- service: hue.hue_activate_scene
data:
group_name: Wohnzimmer
scene_name: '{{ states(trigger.entity_id) }}'
How about loops? You could use templates to generate a list for options.
petro
(Petro)
March 18, 2021, 11:13am
23
use repeat
for loops. Make a list and store it in a variable, then use repeat.index
to grab each item as you move through the repeat. This can also be combined with choose
. The current automation engine is excellent and can pretty much do anything in yaml without templates. Templates only compliment them. And if youāre good enough, you can take long yaml automations and tighten them up with variable templates.
123
(Taras)
March 18, 2021, 1:48pm
24
Show me an example of a template that does that. Remember, weāre talking about a template that generates options (like the one I posted above).
jazzyisj
(Jason)
January 13, 2022, 8:31pm
25
Iām so excited. I actually get to answer a question for petro! I stumbled across your comment here looking for something totally different (info on zwave.refresh_values).
The data:
parameter of a service call DOES accept a template! I didnāt realize it myself until I tried it out helping out a fella on this thread . Maybe this has been added since your comment (this is an old thread after all) but I really donāt want to go back through all the release notes to check.
That guy never did report back if it worked for him but I tried it out locally with a test script to call another script in my config and it worked a treat.
Test Script
test_alarm_options:
alias: 'Test Alarm Options'
fields:
zone:
description: 'Alarm zone.'
mode:
description: 'Alarm arm mode.'
code:
description: 'Alarm arm code.'
override:
description: 'Bypass open sensors.'
variables:
zone: "{{ zone|default('') }}"
mode: "{{ mode|default('') }}"
code: "{{ code|default('') }}"
override: "{{ override|default('') }}"
options: >
{% set option_dict = namespace(value='') %}
{% set options = ['zone','mode','code','override'] %}
{% set values = [zone,mode,code,override] %}
{% for item in options %}
{% if values[loop.index0] != '' %}
{% set option_dict.value = option_dict.value
~ '"' ~ item ~ '":"'
~ values[loop.index0] ~ '"' ~ ',' %}
{% endif %}
{% endfor %}
{{ '{' ~ option_dict.value[:-1] ~ '}' }}
sequence:
- service: script.arm_alarm
data: '{{ options }}'
Iām sure there has to be a more efficient way to do it, but this does work.
I do have a question. Is there an easier way to build the dictonary? I did something very similar here but it feels like there should be an easier way to work with the dictonary than building it as a string. Maybe not since that was the approach @123 used in this thread.
4 Likes
123
(Taras)
January 13, 2022, 9:10pm
26
It was added by koying and incorporated into the November 2021 release.
home-assistant:dev
ā koying:addservicedict
opened 08:58AM - 05 Oct 21 UTC
## Proposed change
<!--
Describe the big picture of your changes here to comā¦ municate to the
maintainers why we should accept this pull request. If it fixes a bug
or resolves a feature request, be sure to link to that issue in the
additional information section.
-->
Currently, service calls only accept a Dict in the data part, possibly of templates, and not a single template returning a Dict. That confuses a number of users, and forces to use "choose" rather than using pure templates for differentiated attributes of a service call.
As we now support typed templates, there doesn't seem to be a reason to not accept a single template returning a dict as well, e.g.
```yaml
- service: climate.set_temperature
data: >
{% if trigger.to_state.state == "on" %}
{"hvac_mode": "heat", "temperature": 12 }
{% else %}
{"hvac_mode": "auto" }
{% endif %}
```
If the PR principle is accepted, I'll add tests and documentation.
## Type of change
<!--
What type of change does your PR introduce to Home Assistant?
NOTE: Please, check only 1! box!
If your PR requires multiple boxes to be checked, you'll most likely need to
split it into multiple PRs. This makes things easier and faster to code review.
-->
- [ ] Dependency upgrade
- [ ] Bugfix (non-breaking change which fixes an issue)
- [ ] New integration (thank you!)
- [X] New feature (which adds functionality to an existing integration)
- [ ] Breaking change (fix/feature causing existing functionality to break)
- [ ] Code quality improvements to existing code or addition of tests
## Additional information
<!--
Details are important, and help maintainers processing your PR.
Please be sure to fill out additional details, if applicable.
-->
- This PR fixes or closes issue: fixes #
- This PR is related to issue:
- Link to documentation pull request: https://github.com/home-assistant/home-assistant.io/pull/19922
- Feature request: https://community.home-assistant.io/t/service-data-allow-templates-returning-a-dict-in-addition-to-a-dict-of-templates/343869
## Checklist
<!--
Put an `x` in the boxes that apply. You can also fill these out after
creating the PR. If you're unsure about any of them, don't hesitate to ask.
We're here to help! This is simply a reminder of what we are going to look
for before merging your code.
-->
- [X] The code change is tested and works locally.
- [X] Local tests pass. **Your PR cannot be merged unless tests pass**
- [X] There is no commented out code in this PR.
- [X] I have followed the [development checklist][dev-checklist]
- [X] The code has been formatted using Black (`black --fast homeassistant tests`)
- [X] Tests have been added to verify that the new code works.
If user exposed functionality or configuration variables are added/changed:
- [X] Documentation added/updated for [www.home-assistant.io][docs-repository]
If the code communicates with devices, web services, or third-party tools:
- [ ] The [manifest file][manifest-docs] has all fields filled out correctly.
Updated and included derived files by running: `python3 -m script.hassfest`.
- [ ] New or updated dependencies have been added to `requirements_all.txt`.
Updated by running `python3 -m script.gen_requirements_all`.
- [ ] For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
- [ ] Untested files have been added to `.coveragerc`.
The integration reached or maintains the following [Integration Quality Scale][quality-scale]:
<!--
The Integration Quality Scale scores an integration on the code quality
and user experience. Each level of the quality scale consists of a list
of requirements. We highly recommend getting your integration scored!
-->
- [ ] No score or internal
- [ ] š„ Silver
- [ ] š„ Gold
- [ ] š Platinum
<!--
This project is very active and we have a high turnover of pull requests.
Unfortunately, the number of incoming pull requests is higher than what our
reviewers can review and merge so there is a long backlog of pull requests
waiting for review. You can help here!
By reviewing another pull request, you will help raise the code quality of
that pull request and the final review will be faster. This way the general
pace of pull request reviews will go up and your wait time will go down.
When picking a pull request to review, try to choose one that hasn't yet
been reviewed.
Thanks for helping out!
-->
To help with the load of incoming pull requests:
- [ ] I have reviewed two other [open pull requests][prs] in this repository.
[prs]: https://github.com/home-assistant/core/pulls?q=is%3Aopen+is%3Apr+-author%3A%40me+-draft%3Atrue+-label%3Awaiting-for-upstream+sort%3Acreated-desc+review%3Anone
<!--
Thank you for contributing <3
Below, some useful links you could explore:
-->
[dev-checklist]: https://developers.home-assistant.io/docs/en/development_checklist.html
[manifest-docs]: https://developers.home-assistant.io/docs/en/creating_integration_manifest.html
[quality-scale]: https://developers.home-assistant.io/docs/en/next/integration_quality_scale_index.html
[docs-repository]: https://github.com/home-assistant/home-assistant.io
I havenāt used it yet but itās an interesting addition to oneās toolkit.
1 Like
petro
(Petro)
January 13, 2022, 9:37pm
27
Hah, yes. @koying added it a bit ago, been using it for some time. Thanks for the heads up though!
1 Like
petro
(Petro)
January 13, 2022, 9:48pm
28
I use it to get around validation on yaml, like when you need to put a string representation of an object in a notification message. Normally that would fail with āMessage needs to be a stringā, but if you template the whole data section, it doesnāt validate the data for message.