diff --git a/examples/Switches.md b/examples/Switches.md new file mode 100644 index 0000000..56f3aaf --- /dev/null +++ b/examples/Switches.md @@ -0,0 +1,59 @@ +# Switches + +In order to facilitate custom switches at this time, you must create a template switch in HomeAssistant. + +```yaml +switch: + - platform: template + switches: + : + friendly_name: + value_template: + turn_on: + service: + data: + entity_id: + : + turn_off: + service: + data: + entity_id: + : +``` + +Then you can use the following in your config: + +```json +{ + "entity": "switch.", + "name": "", + "type": "toggle" +} +``` + +## Example (covers) + +```yaml +switch: + - platform: template + switches: + cover: + friendly_name: Cover + value_template: "{{ is_state('cover.cover', 'open') }}" + turn_on: + service: cover.open_cover + data: + entity_id: cover.cover + turn_off: + service: cover.close_cover + data: + entity_id: cover.cover +``` + +```json +{ + "entity": "switch.cover", + "name": "Cover", + "type": "toggle" +} +``` diff --git a/examples/Templates.md b/examples/Templates.md new file mode 100644 index 0000000..f6aaea5 --- /dev/null +++ b/examples/Templates.md @@ -0,0 +1,66 @@ +# Templates + +In order to provide the most functionality possible the content of the card is a user-defined template (i.e. you generate your own text). This allows you to do some pretty cool things. It also makes the config a bit more complicated. This page will help you understand how to use templates. + +In this file anything between `<` and `>` is a placeholder. Replace it with the appropriate value. + +Anything between `{{` and `}}` is a template. Templates are used to dynamically insert values into the content. For more info see [the docs](https://www.home-assistant.io/docs/configuration/templating/). + +## States + +In this example we get the battery level of the device and add the percent sign. *Very simple* + +```json +{ + "entity": "sensor._battery_level", + "name": "Phone", + "type": "template", + "content": "{{ states('sensor._battery_level') }}%" +} +``` + +## Conditionals + +Anything between `{%` and `%}` is a directive (`if`, `else`, `elif`, `endif`, etc.). Conditionals are used to dynamically change the content based on the state of the entity. + +In this example we get the battery level of the device and add the percent sign. If the device is charging we add a plus sign. + +```json +{ + "entity": "sensor._battery_level", + "name": "Phone", + "type": "template", + "content": "{{ states('sensor._battery_level') }}%{% if is_state('binary_sensor._is_charging', 'on') %}+{% endif %}" +} +``` + +Here we also use the else clause as well to give proper text instead of just `on` or `off`. + +```json +{ + "entity": "binary_sensor.garage_doors", + "name": "Garage Doors", + "type": "template", + "content": "{% if is_state('binary_sensor.', 'on') %}Open{% else %}Closed{% endif %} {% if is_state('binary_sensor.', 'on') %}Open{% else %}Closed{% endif %}" +} +``` + +## Advanced + +Here we generate a bar graph of the battery level. We use the following steps to do this: + +- Convert the state to a number. +- Divide by 100 to get a fraction. +- Multiply by the width to get the number of `#`s. +- Multiply by the `#` char to make a string. +- Subtract the width from the number of `#`s to get the number of `_`s. +- Multiply by the `_` char to make a string. + +```json +{ + "entity": "sensor._battery_level", + "name": "Phone", + "type": "template", + "content": "{{ states('sensor._battery_level') }}%{% if is_state('binary_sensor._is_charging', 'on') %}+{% endif %} {{ '#' * (((states('sensor._battery_level') | int) / 100 * ) | int) }}{{ '_' * ( - (((states('sensor._battery_level') | int) / 100 * ) | int)) }}" +} +```