build-blink1/home-assistant/blink1.yaml

105 lines
3.4 KiB
YAML

---
template:
- binary_sensor:
- name: "blink1_on"
device_class: light
state: >-
{{ state_attr('sensor.blink1', 'rgb') != "#000000" }}
sensor:
- platform: rest
resource: http://localhost:8011/blink1
name: blink1
json_attributes:
- rgb
- bright
value_template: "{{ value_json.rgb }}"
rest_command:
blink1_turn_on:
url: http://localhost:8011/blink1/on?bright=250
#url: http://localhost:8011/blink1/fadeToRGB?rgb=ff0ff
method: GET
content_type: "application/json"
blink1_turn_off:
url: http://localhost:8011/blink1/off
method: GET
content_type: "application/json"
blink1_turn_magenta:
url: http://localhost:8011/blink1/fadeToRGB?rgb=ff00ff
method: GET
content_type: "application/json"
blink1_set_color:
url: "http://localhost:8011/blink1/fadeToRGB?rgb={{ rgb }}"
method: GET
light:
- platform: template
lights:
blink1:
friendly_name: blink1
value_template: >-
{{ state_attr('sensor.blink1', 'rgb') != "#000000" }}
# color_template: >-
# # {{ state_attr('sensor.blink1', 'rgb') }}
#
turn_on:
- service: rest_command.blink1_turn_on
- delay:
milliseconds: 500
- service: homeassistant.update_entity
target:
entity_id: sensor.blink1
turn_off:
- service: rest_command.blink1_turn_off
- delay:
milliseconds: 500
- service: homeassistant.update_entity
target:
entity_id: sensor.blink1
set_color:
- service: rest_command.blink1_turn_off
- service: rest_command.blink1_set_color
data:
# https://github.com/velijv/home-assistant-color-helpers#rgb-to-hex
# https://community.home-assistant.io/t/advanced-light-template-help/175654
# https://community.home-assistant.io/t/using-hsv-hsb-to-set-colored-lights/15472
rgb: >-
{%- set h2 = h / 360 -%}
{%- set s2 = s / 100 -%}
{%- set v = 100 -%}
{%- set i = (h2 * 6 ) | round(2,'floor') | int-%}
{%- set f = h2 * 6 - i -%}
{%- set p = v * (1 - s2) -%}
{%- set q = v * (1 - f * s2) -%}
{%- set t = v * (1 - (1 - f) * s2) -%}
{%- if i % 6 == 0 -%}
{%- set r = v | int -%}
{%- set g = t | int -%}
{%- set b = p | int -%}
{%- elif i % 6 == 1 -%}
{%- set r = q | int -%}
{%- set g = v | int -%}
{%- set b = p | int -%}
{%- elif i % 6 == 2 -%}
{%- set r = p | int -%}n
{%- set g = v | int -%}
{%- set b = t | int -%}
{%- elif i % 6 == 3 -%}
{%- set r = p | int -%}
{%- set g = q | int -%}
{%- set b = v | int -%}
{%- elif i % 6 == 4 -%}
{%- set r = t | int -%}
{%- set g = p | int -%}
{%- set b = v | int -%}
{%- elif i % 6 == 5 -%}
{%- set r = v | int -%}
{%- set g = p | int -%}
{%- set b = q | int -%}
{%- endif -%}
{{ '%02x%02x%02x' | format(r, g, b) }}