Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make variable entity_id available to value_template for MQTT binary sensor #19195

Conversation

emontnemery
Copy link
Contributor

@emontnemery emontnemery commented Dec 11, 2018

Description:

#18501, which proposed adding "toggle" functionality to binary sensor, was closed because the same thing should already be possible to achieve with a value template.

Doing it with a value template however requires hard coding the entity_id, which is tedious and error prone in case of manual edit of configuration.yaml, and impossible if the entity is automatically discovered.

This PR makes variable entity_id available to the value_template, in the same manner as it is already available to automation templates.

Pull request in home-assistant.io with documentation (if applicable): home-assistant/home-assistant.io#8021

Example entry for configuration.yaml (if applicable):

# Toggle the binary sensor each time a message is received on state_topic
binary_sensor:
  - platform: mqtt
    state_topic:lab_button/cmnd/POWER"
    value_template:"{%if is_state(entity_id,\"on\")-%}OFF{%-else-%}ON{%-endif%}"}

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.

If user exposed functionality or configuration variables are added/changed:

If the code does not interact with devices:

  • Tests have been added to verify that the new code works.

@fabaff fabaff changed the title MQTT binary_sensor: Make variable entity_id available to value_template Make variable entity_id available to value_template for MQTT binary sensor Dec 12, 2018
Copy link
Member

@balloob balloob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok to merge when test added.

@emontnemery emontnemery force-pushed the mqtt_binary_sensor_value_template_entity branch from 84bef02 to a18f98a Compare December 12, 2018 15:49
@balloob balloob merged commit b97f0c0 into home-assistant:dev Dec 14, 2018
@ghost ghost removed the in progress label Dec 14, 2018
@emontnemery emontnemery deleted the mqtt_binary_sensor_value_template_entity branch December 19, 2018 18:59
dshokouhi pushed a commit to dshokouhi/home-assistant that referenced this pull request Dec 25, 2018
… sensor (home-assistant#19195)

* MQTT binary_sensor: Make variable `entity_id` available to value_template

* Review comments

* Add testcase
@balloob balloob mentioned this pull request Jan 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants