c0ntax
(Martin)
September 8, 2020, 4:44pm
1
I’m trying to create a simple automation that turns off certain devices when my phone is placed on a wireless charger.
For some reason the below code doesn’t work. Also, when I place my phone on a wireless charger the state in Developer Tools still shows state as ‘Not Charging’.
Am I missing something? Or is it not possible when charging wirelessly?
- id: '1599580459864'
alias: Bedtime Secure
description: ''
trigger:
- platform: state
entity_id: sensor.iphone_pro_max_battery_state
from: not_charging
to: charging
condition: []
action:
- delay: '00:01:00'
- scene: scene.secure_all_off
mode:
I use this trigger :
{{is_state_attr('sensor.pixel_3_battery_state', 'charger_type', 'wireless')}}
c0ntax
(Martin)
September 8, 2020, 5:10pm
3
Where would I place that in my code?
Here’s my automation :
- id: '1595330718111'
alias: Turn off Bedroom Lights if phone charging
description: ''
trigger:
- platform: template
value_template: '{{is_state_attr(''sensor.pixel_3_battery_state'', ''charger_type'',
''wireless'')}}'
condition:
- condition: and
conditions:
- condition: device
device_id: 0da2f43299264982a4a2b886404db6e8
domain: switch
entity_id: switch.tasmota_upstairs_left_tasmota_upstairs_left
type: is_on
- condition: device
device_id: 8a3216f96367488e8602080b7d7f7013
domain: switch
entity_id: switch.tasmota_upstairs_right_tasmota_upstairs_right
type: is_on
- condition: template
value_template: '{{as_timestamp(now()) - as_timestamp(states.script.goodnight.attributes.last_triggered)|
int <= 3600}}'
action:
- data: {}
service: script.sleep_time
mode: single
1 Like
c0ntax
(Martin)
September 8, 2020, 5:14pm
5
Ah, ok, I’ve not worked with templates yet. I’ll have a look at incorporating it.
Thanks
c0ntax
(Martin)
September 8, 2020, 5:18pm
7
Thanks. Just looked at the Template section and it looks complicated. It may take me a while
Just replace your trigger with this in your YAML
trigger:
- platform: template
value_template: '{{is_state_attr(''sensor.iphone_pro_max_battery_state'', ''charger_type'',
''wireless'')}}'
Or select TEMPLATE as the trigger in the UI and paste this in
{{is_state_attr('sensor.iphone_pro_max_battery_state', 'charger_type', 'wireless')}}
c0ntax
(Martin)
September 8, 2020, 5:37pm
9
So I have the below now but it still doesn’t work. Sorry for being thick but I’m on a very steep learning curve
- id: '1599580459864'
alias: Bedtime Secure
description: ''
trigger:
- platform: template
value_template: '{{is_state_attr(''sensor.iphone_pro_max_battery_state'', ''charger_type'',
''wireless'')}}'
condition: []
action:
- delay: 00:01:00
- scene: scene.secure_all_off
mode: single
Hmm … I wonder if it’s the iPhone app … I am using android. Check in the developer tools what the charger_type changes to when you stick on to charge …
c0ntax
(Martin)
September 8, 2020, 8:23pm
11
Ok, so this is odd. I’ve placed the phone on both wireless and wired charger and the state in dev tools continues to show ‘Not Charging’.
I’ve restarted HA and rebooted but the state still says the same regardless of whether it’s physically charging or not.
c0ntax
(Martin)
September 9, 2020, 2:14pm
12
After some investigation it appears that to obtain the battery state I need to push the location to HA. This, apparently can be done by simply opening the companion app on my phone.
That defeats the object of wanting the automation to run simply by setting the device on the charger.
Is there a way I can solve this by making changes or additions to my automation code?
First post! If this helps anyone, this worked for me. (Android)
In automation UI use trigger type template and paste in the following:
{{ is_state(‘sensor.sm_g998b_charger_type’, ‘wireless’) }}
You will need to substitute ‘sensor.sm_g998b_charger_type’ for your entity I.d.