nickshew1
(Nickshew)
April 9, 2024, 12:27am
1
Hello everyone,
I have had a look around, but can’t find the syntax for what I am trying to get working.
I have a sensor in Home Assistant which has 4 attributes:
- platform: template
sensors:
house_warning:
friendly_name: Current Active Warnings
value_template: >
{% if(states("input_boolean.house_alarm")) == "on" or (states("binary_sensor.ratgdov25i_11e256_obstruction")) == "on" (states("cover.ratgdov25i_11e256_door")) != "closed" or (state_attr("sensor.recycling_bin", "days")) < 2 (state_attr("sensor.green_bin", "days")) < 2 %}
true
{% else %}
false
{% endif %}
attribute_templates:
red: >
{% if(states("input_boolean.house_alarm")) == "on" or (states("binary_sensor.ratgdov25i_11e256_obstruction")) == "on" or (state_attr("sensor.recycling_bin", "days")) < 2 %}
100
{% elif(states("cover.ratgdov25i_11e256_door")) == "open" or (states("cover.ratgdov25i_11e256_door")) == "opening" %}
47
{% else %}
0
{% endif %}
green: >
{% if(states("cover.ratgdov25i_11e256_door")) == "closing" or (state_attr("sensor.recycling_bin", "days")) < 2 or (state_attr("sensor.green_bin", "days")) < 2 %}
100
{% else %}
0
{% endif %}
blue: >
{% if(states("cover.ratgdov25i_11e256_door")) == "open" or (states("cover.ratgdov25i_11e256_door")) == "opening" %}
78
{% else %}
0
{% endif %}
effect: >
{% if(states("input_boolean.house_alarm")) == "on" or (states("cover.ratgdov25i_11e256_door")) == "opening" or (states("cover.ratgdov25i_11e256_door")) == "closing" %}
"pulse"
{% else %}
"none"
{% endif %}
This sensor will be used to announce any warnings with the logic being inside Home Assistant.
I want ESPHome to listen and react to this sensor. If the value is true I wish to pass the red, green, blue and effect attributes as parameters to a script to control some leds.
binary_sensor:
- platform: homeassistant
id: Warning
name: "House Warning State"
entity_id: binary_sensor.house_warning
on_value:
then:
- if:
condition:
text_sensor.state:
id: Warning
state: 'off'
then:
- script.execute:
id: led_off_alert
- if:
condition:
not:
text_sensor.state:
id: Warning
state: 'off'
then:
- script.execute:
id: led_on_alert
red: attribute: red
green: attribute: green
blue: attribute: blue
effect: attribute: effect
script:
- id: led_on_alert
parameters:
red: int
green: int
blue: int
effect: string
mode: restart
then:
- lambda: >
ESP_LOGD("Alert", "On:");
- light.turn_on:
id: leds_alert
brightness: 1
red: !lambda return red;
green: !lambda return green;
blue: !lambda return blue;
effect: !lambda return effect;
How can I pass the attributes from the HA sensor to the script?
Thanks
Nick
OK, sussed it.
I created extra sensors for each of the attributes. I had to add a call to the LED on script from each as a garage door state change does not always give a boolean change to the binary sensors state. This in turn required a delay in the light on script so all colour changes could be captured before they were sent to the LEDs which would otherwise create some unwanted colours.
HA Sensor:
- platform: template
sensors:
house_notifications:
friendly_name: Current Active Notifications
unique_id: current_active_notifications
value_template: >
{% if(states("input_boolean.house_alarm")) == "on" or (states("binary_sensor.ratgdov25i_11e256_obstruction")) == "on" or (states("cover.ratgdov25i_11e256_door")) != "closed" or (state_attr("sensor.recycling_bin", "days")) < 2 or (state_attr("sensor.green_bin", "days")) < 2 %}
true
{% else %}
false
{% endif %}
attribute_templates:
red: >
{% if(states("input_boolean.house_alarm")) == "on" or (states("binary_sensor.ratgdov25i_11e256_obstruction")) == "on" or (state_attr("sensor.recycling_bin", "days")) < 2 %}
100
{% elif(states("cover.ratgdov25i_11e256_door")) == "open" or (states("cover.ratgdov25i_11e256_door")) == "opening" %}
47
{% else %}
0
{% endif %}
green: >
{% if(states("cover.ratgdov25i_11e256_door")) == "closing" or (state_attr("sensor.recycling_bin", "days")) < 2 or (state_attr("sensor.green_bin", "days")) < 2 %}
100
{% else %}
0
{% endif %}
blue: >
{% if(states("cover.ratgdov25i_11e256_door")) == "open" or (states("cover.ratgdov25i_11e256_door")) == "opening" %}
78
{% else %}
0
{% endif %}
effect: >
{% if(states("input_boolean.house_alarm")) == "on" or (states("cover.ratgdov25i_11e256_door")) == "opening" or (states("cover.ratgdov25i_11e256_door")) == "closing" %}
Pulse
{% else %}
none
{% endif %}
ESPHome Global Variables:
globals:
- id: notifications
type: bool
restore_value: no
initial_value: 'false'
- id: notifications_red
type: int
restore_value: no
initial_value: '0'
- id: notifications_green
type: int
restore_value: no
initial_value: '0'
- id: notifications_blue
type: int
initial_value: '0'
- id: notifications_effect
type: std::string
restore_value: no
initial_value: '"none"'
ESPHome Text Sensors:
text_sensor:
- platform: homeassistant
id: Notification_Red
name: "House Notification Red"
entity_id: binary_sensor.house_notifications
attribute: red
on_value:
then:
- globals.set:
id: notifications_red
value: !lambda 'return atoi(x.c_str());'
- platform: homeassistant
id: Notification_Green
name: "House Notification Green"
entity_id: binary_sensor.house_notifications
attribute: green
on_value:
then:
- globals.set:
id: notifications_green
value: !lambda 'return atoi(x.c_str());'
- if:
condition:
text_sensor.state:
id: Notification
state: 'on'
then:
- script.execute: led_on_notification
- platform: homeassistant
id: Notification_Blue
name: "House Notification Blue"
entity_id: binary_sensor.house_notifications
attribute: blue
on_value:
then:
- globals.set:
id: notifications_blue
value: !lambda 'return atoi(x.c_str());'
- if:
condition:
text_sensor.state:
id: Notification
state: 'on'
then:
- script.execute: led_on_notification
- platform: homeassistant
id: Notification_Effect
name: "House Notification Effect"
entity_id: binary_sensor.house_notifications
attribute: effect
on_value:
then:
- globals.set:
id: notifications_effect
value: !lambda 'return x;'
- if:
condition:
text_sensor.state:
id: Notification
state: 'on'
then:
- script.execute: led_on_notification
- platform: homeassistant
id: Notification
name: "House Notification"
entity_id: binary_sensor.house_notifications
on_value:
then:
- if:
condition:
text_sensor.state:
id: Notification
state: 'off'
then:
- script.execute: led_off_notification
- if:
condition:
text_sensor.state:
id: Notification
state: 'on'
then:
- script.execute: led_on_notification
ESPHome Scripts:
- id: led_on_notification
mode: restart
then:
- delay: 250ms
- light.turn_on:
id: leds_notification
brightness: ${button_brightness}
red: !lambda "return id(notifications_red)/100.0;"
green: !lambda "return id(notifications_green)/100.0;"
blue: !lambda "return id(notifications_blue)/100.0;"
effect: !lambda "return id(notifications_effect);"
- lambda: >
ESP_LOGI("Notification: ", "On");
- id: led_off_notification
mode: restart
then:
- light.turn_off:
id: leds_notification
- lambda: >
ESP_LOGI("Notification: ", "Off");
I didn’t include my light description as all strips would be different. FYI my LED strip was actually the LEDs surrounding a Sonoff TX Ultimate 120 switch.
If there is a more eloquent way please let me know.
Cheers
Nick