插座只有一个led灯,希望可以显示连接状态,也可以显示继电器状态。
代码如下:
output:
- platform: gpio
id: led1
pin:
number: GPIO2
inverted: True
light:
- platform: status_led
name: "Switch state"
id: status_led1
output: led1
internal: True
switch:
- platform: gpio
name: "电源开关"
pin: GPIO13
inverted: true
id: relay
restore_mode: RESTORE_DEFAULT_ON
on_turn_on:
- output.turn_on: led1
- switch.turn_off: ina
- switch.turn_on: inb
- delay: 100ms
- switch.turn_off: ina
- switch.turn_off: inb
- delay: 100ms
on_turn_off:
- output.turn_off: led1
- switch.turn_on: ina
- switch.turn_off: inb
- delay: 100ms
- switch.turn_off: ina
- switch.turn_off: inb
- delay: 100ms
- platform: gpio
pin: GPIO4
id: ina
- platform: gpio
pin: GPIO5
id: inb
但是发现,有时候断电再上电,LED状态和继电器状态并不对应。
于是改了下代码,每隔5秒查询一次继电器状态,再更改LED状态,代码如下:
output:
- platform: gpio
id: led1
pin:
number: GPIO2
inverted: True
light:
- platform: status_led
name: "Switch state"
id: status_led1
output: led1
internal: True
switch:
- platform: gpio
name: "电源开关"
pin: GPIO13
inverted: true
id: relay
restore_mode: RESTORE_DEFAULT_ON
on_turn_on:
- output.turn_on: led1
- switch.turn_off: ina
- switch.turn_on: inb
- delay: 100ms
- switch.turn_off: ina
- switch.turn_off: inb
- delay: 100ms
on_turn_off:
- output.turn_off: led1
- switch.turn_on: ina
- switch.turn_off: inb
- delay: 100ms
- switch.turn_off: ina
- switch.turn_off: inb
- delay: 100ms
- platform: gpio
pin: GPIO4
id: ina
- platform: gpio
pin: GPIO5
id: inb
interval:
- interval: 5s
then:
if:
condition:
api.connected:
then:
if:
condition:
switch.is_on: relay
then:
- output.turn_on: led1
else:
- output.turn_off: led1
这样确实是可以了。
请问还有更好的实现办法吗?
|