I’m trying to get my aqara cube registered via ZHA on rasp pi in home to rotate left or right to dim or brighten lights in 10% increments. The cube automation are done via events (I.e rotate left). Can anyone possibly help me create a automation for this. I am still slowly learning home assistant and am struggling to figure out how to input the data values for lights.
This is what the full data looks like:
brightness: >-
{%if trigger.event.data.action_value | float > 0 %} {{
states.light.philips_edison_1.attributes.brightness | int + 50 }} {% else %}
{{ states.light.philips_edison_1.attributes.brightness | int - 50 }} {% endif
%}
As I mentioned, I’m still learning and this is not working. Nothing happens.
Try this in the service data:
brightness_step_pct: 10 #percentage to increase
Or
brightness_step: 25
To decrease, use a negative value
More info here: https://www.home-assistant.io/blog/2020/02/26/release-106/#stepping-up-and-down-the-brightness-of-lights
I can not give you a specific tip, but what helps me in those situtations is using the template test tool. (“Development Tools” → “Template”[I think, mine is in German ]). You can put code sniplets in there and it comutes the result. You can directly see whether a certain expression work as intended.
To confuse you totally:
Yesterday my cude arrived. I loosely followed the tutoria from Dr. Zzs:
By that I managed to switch radio stations by flipping the cube. Rotation left/right turn the volumeup/down. “Dropping” switches on/off.
I was struggeling especially with the volume up/down part, since the media_player service “volume up/down” just does minimal increments on my sonos. So I grabbed the actual volume and added a value to it. I then send it to the “volume_set” part of the service.
So this is a similar approach to yours.
My node red flow look like the code below. Don’t worry, you do not need to type it. If you would like to see the flow in Node-Red, just click “import flow” and paste the code into it. It creates all bells and whistles then.
[
{
"id": "7e990ef4.69f198",
"type": "tab",
"label": "Flow 1",
"disabled": false,
"info": ""
},
{
"id": "2d4d3435.2b550c",
"type": "server-events",
"z": "7e990ef4.69f198",
"name": "",
"server": "687f8e2c.ee7ec",
"event_type": "zha_event",
"exposeToHomeAssistant": false,
"haConfig": [
{
"property": "name",
"value": ""
},
{
"property": "icon",
"value": ""
}
],
"waitForRunning": true,
"x": 230,
"y": 140,
"wires": [
[
"5613004a.b5c73"
]
]
},
{
"id": "5613004a.b5c73",
"type": "switch",
"z": "7e990ef4.69f198",
"name": "Cube1",
"property": "payload.event.device_ieee",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "00:15:8d:00:05:21:9c:e4",
"vt": "str"
}
],
"checkall": "true",
"repair": false,
"outputs": 1,
"x": 190,
"y": 240,
"wires": [
[
"435bb747.363b2",
"91285d33.9dcae8",
"a33c67dd.2b1c3",
"c1f4739c.256eb",
"a11fd732.d615e",
"db31a524.83a1d8"
]
]
},
{
"id": "435bb747.363b2",
"type": "switch",
"z": "7e990ef4.69f198",
"name": "Drop",
"property": "payload.event.command",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "drop",
"vt": "str"
}
],
"checkall": "true",
"repair": false,
"outputs": 1,
"x": 350,
"y": 200,
"wires": [
[
"7c3a06a3.d8289"
]
]
},
{
"id": "7c3a06a3.d8289",
"type": "api-call-service",
"z": "7e990ef4.69f198",
"name": "",
"server": "687f8e2c.ee7ec",
"version": 1,
"debugenabled": false,
"service_domain": "media_player",
"service": "media_play_pause",
"entityId": "media_player.kuche",
"data": "",
"dataType": "json",
"mergecontext": "",
"output_location": "",
"output_location_type": "none",
"mustacheAltTags": false,
"x": 590,
"y": 140,
"wires": [
[]
]
},
{
"id": "91285d33.9dcae8",
"type": "switch",
"z": "7e990ef4.69f198",
"name": "Rotate Right",
"property": "payload.event.command",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "rotate_right",
"vt": "str"
}
],
"checkall": "true",
"repair": false,
"outputs": 1,
"x": 370,
"y": 260,
"wires": [
[
"97dd92a5.e3c728"
]
]
},
{
"id": "a33c67dd.2b1c3",
"type": "switch",
"z": "7e990ef4.69f198",
"name": "Rotate Left",
"property": "payload.event.command",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "rotate_left",
"vt": "str"
}
],
"checkall": "true",
"repair": false,
"outputs": 1,
"x": 370,
"y": 320,
"wires": [
[
"9c8fa9b.3f81458"
]
]
},
{
"id": "7efe155c.323f4c",
"type": "api-call-service",
"z": "7e990ef4.69f198",
"name": "",
"server": "687f8e2c.ee7ec",
"version": 1,
"debugenabled": false,
"service_domain": "media_player",
"service": "volume_set",
"entityId": "media_player.kuche",
"data": "{\"volume_level\":{{payload}}}",
"dataType": "json",
"mergecontext": "",
"output_location": "",
"output_location_type": "none",
"mustacheAltTags": false,
"x": 1110,
"y": 240,
"wires": [
[]
]
},
{
"id": "c1f4739c.256eb",
"type": "switch",
"z": "7e990ef4.69f198",
"name": "Slide",
"property": "payload.event.command",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "slide",
"vt": "str"
}
],
"checkall": "true",
"repair": false,
"outputs": 1,
"x": 350,
"y": 380,
"wires": [
[]
]
},
{
"id": "a11fd732.d615e",
"type": "switch",
"z": "7e990ef4.69f198",
"name": "Drop",
"property": "payload.event.command",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "drop",
"vt": "str"
}
],
"checkall": "true",
"repair": false,
"outputs": 1,
"x": 350,
"y": 440,
"wires": [
[]
]
},
{
"id": "db31a524.83a1d8",
"type": "switch",
"z": "7e990ef4.69f198",
"name": "Flip",
"property": "payload.event.command",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "flip",
"vt": "str"
}
],
"checkall": "true",
"repair": false,
"outputs": 1,
"x": 210,
"y": 600,
"wires": [
[
"97c628b.a5bc758"
]
]
},
{
"id": "97c628b.a5bc758",
"type": "switch",
"z": "7e990ef4.69f198",
"name": "Side",
"property": "payload.event.args.activated_face",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "1",
"vt": "str"
},
{
"t": "eq",
"v": "2",
"vt": "str"
},
{
"t": "eq",
"v": "3",
"vt": "str"
},
{
"t": "eq",
"v": "4",
"vt": "str"
},
{
"t": "eq",
"v": "5",
"vt": "str"
},
{
"t": "eq",
"v": "6",
"vt": "str"
}
],
"checkall": "true",
"repair": false,
"outputs": 6,
"x": 350,
"y": 600,
"wires": [
[
"f6f5ecfc.e5dc"
],
[
"8781f7aa.ed361"
],
[
"4020f126.5e28c"
],
[
"17ac8233.ecc816"
],
[
"8422763e.703f"
],
[
"46d27d2d.fba3a4"
]
]
},
{
"id": "97dd92a5.e3c728",
"type": "api-current-state",
"z": "7e990ef4.69f198",
"name": "",
"server": "687f8e2c.ee7ec",
"version": 1,
"outputs": 1,
"halt_if": "",
"halt_if_type": "str",
"halt_if_compare": "is",
"override_topic": false,
"entity_id": "media_player.kuche",
"state_type": "num",
"state_location": "payload",
"override_payload": "msg",
"entity_location": "data",
"override_data": "msg",
"blockInputOverrides": false,
"x": 620,
"y": 240,
"wires": [
[
"35903133.a84d1e"
]
]
},
{
"id": "35903133.a84d1e",
"type": "function",
"z": "7e990ef4.69f198",
"name": "",
"func": "var payload=msg.data.attributes.volume_level;\nmsg.payload = payload + 0.1;\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"x": 860,
"y": 240,
"wires": [
[
"7efe155c.323f4c"
]
]
},
{
"id": "9c8fa9b.3f81458",
"type": "api-current-state",
"z": "7e990ef4.69f198",
"name": "",
"server": "687f8e2c.ee7ec",
"version": 1,
"outputs": 1,
"halt_if": "",
"halt_if_type": "str",
"halt_if_compare": "is",
"override_topic": false,
"entity_id": "media_player.kuche",
"state_type": "num",
"state_location": "payload",
"override_payload": "msg",
"entity_location": "data",
"override_data": "msg",
"blockInputOverrides": false,
"x": 620,
"y": 320,
"wires": [
[
"d1b2b9fc.71218"
]
]
},
{
"id": "d1b2b9fc.71218",
"type": "function",
"z": "7e990ef4.69f198",
"name": "",
"func": "var payload=msg.data.attributes.volume_level;\nmsg.payload = payload - 0.1;\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"x": 860,
"y": 320,
"wires": [
[
"7efe155c.323f4c"
]
]
},
{
"id": "f6f5ecfc.e5dc",
"type": "api-call-service",
"z": "7e990ef4.69f198",
"name": "",
"server": "687f8e2c.ee7ec",
"version": 1,
"debugenabled": false,
"service_domain": "media_player",
"service": "select_source",
"entityId": "media_player.kuche",
"data": "{\"source\":\"WDR2 Rhein und Ruhr\"}",
"dataType": "json",
"mergecontext": "",
"output_location": "",
"output_location_type": "none",
"mustacheAltTags": false,
"x": 740,
"y": 440,
"wires": [
[]
]
},
{
"id": "8781f7aa.ed361",
"type": "api-call-service",
"z": "7e990ef4.69f198",
"name": "",
"server": "687f8e2c.ee7ec",
"version": 1,
"debugenabled": false,
"service_domain": "media_player",
"service": "select_source",
"entityId": "media_player.kuche",
"data": "{\"source\":\"Absolut relax\"}",
"dataType": "json",
"mergecontext": "",
"output_location": "",
"output_location_type": "none",
"mustacheAltTags": false,
"x": 740,
"y": 500,
"wires": [
[]
]
},
{
"id": "4020f126.5e28c",
"type": "api-call-service",
"z": "7e990ef4.69f198",
"name": "",
"server": "687f8e2c.ee7ec",
"version": 1,
"debugenabled": false,
"service_domain": "media_player",
"service": "select_source",
"entityId": "media_player.kuche",
"data": "{\"source\":\"BR-Klassik\"}",
"dataType": "json",
"mergecontext": "",
"output_location": "",
"output_location_type": "none",
"mustacheAltTags": false,
"x": 740,
"y": 560,
"wires": [
[]
]
},
{
"id": "17ac8233.ecc816",
"type": "api-call-service",
"z": "7e990ef4.69f198",
"name": "",
"server": "687f8e2c.ee7ec",
"version": 1,
"debugenabled": false,
"service_domain": "media_player",
"service": "select_source",
"entityId": "media_player.kuche",
"data": "{\"source\":\"95.5 Charivari Party Hitmix\"}",
"dataType": "json",
"mergecontext": "",
"output_location": "",
"output_location_type": "none",
"mustacheAltTags": false,
"x": 740,
"y": 620,
"wires": [
[]
]
},
{
"id": "8422763e.703f",
"type": "api-call-service",
"z": "7e990ef4.69f198",
"name": "",
"server": "687f8e2c.ee7ec",
"version": 1,
"debugenabled": false,
"service_domain": "media_player",
"service": "select_source",
"entityId": "media_player.kuche",
"data": "{\"source\":\"TechnoBase.FM\"}",
"dataType": "json",
"mergecontext": "",
"output_location": "",
"output_location_type": "none",
"mustacheAltTags": false,
"x": 740,
"y": 680,
"wires": [
[]
]
},
{
"id": "46d27d2d.fba3a4",
"type": "api-call-service",
"z": "7e990ef4.69f198",
"name": "",
"server": "687f8e2c.ee7ec",
"version": 1,
"debugenabled": false,
"service_domain": "media_player",
"service": "select_source",
"entityId": "media_player.kuche",
"data": "{\"source\":\"Dance Classics\"}",
"dataType": "json",
"mergecontext": "",
"output_location": "",
"output_location_type": "none",
"mustacheAltTags": false,
"x": 740,
"y": 740,
"wires": [
[]
]
},
{
"id": "687f8e2c.ee7ec",
"type": "server",
"z": "",
"name": "Home Assistant",
"addon": true
}
]
Thank you! This worked!!
CeeCee thank you for your response. I have harmony hub and I want to use learn more about how to use cube for volume etc…it seems even more confusing. Hopefully I can use your flow to learn more.
Well, the basic idea of my flow should also work with templates.
use a binary command (rotate left/right) to decrease/increase a value.
If command received, get actual value, add/substract desired value from it, send new value
The rotation angle seems more cool to do this. You could directly set the new volume based on the relative angle change. But from my experience the value received heavily relies on the rotation spped applied by me. quick rotation gives good values, slow rotation is all over the place…
I wanted to control the percentage of brightness adjustment so that, for example if I turn the cube too much the lights will dim at greater values etc. I’m using zigbee2mqtt to pair my Aqara Cube, and in the entity attributes it tells you the angle of rotation:
So I created an automation which reads the changes in the angle and translates it in brightness step percentage:
id: '1601415207224'
alias: dim light from cube
description: ''
trigger:
- platform: state
entity_id: sensor.lumi_sensor_cube_aqgl01_action
to: rotate_left
attribute: action
- platform: state
entity_id: sensor.lumi_sensor_cube_aqgl01_action
attribute: action
to: rotate_right
condition:
- condition: state
entity_id: sensor.lumi_sensor_cube_aqgl01_action
state: '0'
attribute: side
action:
- service: light.turn_on
data:
brightness_step_pct: '{{ state_attr(''sensor.lumi_sensor_cube_aqgl01_action'',
''angle'') / 5 |int }}'
entity_id: light.yeelight_color_bulb
mode: single
I divided the result by 5 to have smaller steps of light dimming when turning the cube. Note that if you rotate the cube to the left the angle is negative so you won’t need seperate automations for up/down.
The state condition is added in order to control light only from side ‘0’ which is the Aqara logo on top. With this condition each side can be used to control brightness/volume etc of various entities
This amount of attributes is only available if you pair the cube with a generic cc2531 stick and use zigbe2mqtt. Perhaps it’s also possible using a conbee stick.
Hope this helps.
Did not found any desired suggested and created my own Aqara Cube light control automation with modern HA interface (Home Assistant ver 2023.7.1 + MQTT + Zigbee2mqtt integrations):
alias: cube light full control
description: testing
trigger:
- platform: mqtt
topic: zigbee2mqtt/cube1 # cube1 = your cube device name
condition: []
action:
- choose:
- conditions:
- condition: and
conditions: # check light is on, otherwise rotate action will turn lamp on
- condition: state
entity_id: light.lamp1 # lamp1 = your lamp/bulb device name
state: "on"
- condition: template # check a cube action type (rotate left/right)
value_template: >-
{{ trigger.payload_json.action in ['rotate_right',
'rotate_left'] }}
sequence:
- if:
- condition: template # check if difference between rotate angle and current brigtness is not below 0 - i dont want turn light off
value_template: >-
{{ (states.light.lamp1.attributes.brightness +
trigger.payload_json.action_angle | int) > 0}}
then: # let change brightness by angle value
- service: light.turn_on
data:
brightness_step: "{{trigger.payload_json.action_angle | int}}"
target:
entity_id: light.lamp1
else: # let set brightness to 1, forcing to not get a zero (turn off)
- service: light.turn_on
data:
brightness_pct: 1
target:
entity_id: light.lamp1
- conditions: # check if a cube action type is "shake" or "slide"
- condition: template
value_template: "{{ trigger.payload_json.action in ['shake', 'slide'] }}"
sequence: # toggle lamp1 power state on/off, you need set it manual with you lamp id directly in HA modern interface
- type: toggle
device_id: cf81c574478ed35464ef79c413ba74a0
entity_id: a93fbaf1597b2139de4173449b3979b2
domain: light
mode: single