|
发表于 2021-7-11 17:10:17
|
显示全部楼层
每个触发器传递的参数当中,除了value,还有old_value这个值。
用HA内置的automation能否抓到old_value,我不清楚。我一直是用pysript写自动化脚本的。NodeRed也是可以抓到这个值的。
例如,下面这段脚本,就是我拉出去给电车充电的插座,实现耗电统计,电费统计,功率变化邮件通知,自动通断电等等。这样我就不怕插座晚上被人偷电了。
@state_trigger("sensor.cp2_power")
def cp2_power_changed(value=None, old_value=None, **kwargs):
task.unique("cp2_power_changed")
datetime_now = datetime.now()
hour_now = int(datetime.strftime(datetime_now,'%H'))
if (6 <= hour_now < 22):
pyscript.power_consumption.peak_charger = float(sensor.chong_dian_qiang_zong_ji_hao_neng) - pyscript.power_consumption.valley_charger
else:
pyscript.power_consumption.valley_charger = float(sensor.chong_dian_qiang_zong_ji_hao_neng) - pyscript.power_consumption.peak_charger
pyscript.power_consumption = pyscript.power_consumption.peak_charger + pyscript.power_consumption.valley_charger
datetime_now_str = datetime.strftime(datetime_now,'%Y-%m-%d %H:%M:%S')
value = float(value)
old_value = float(old_value)
if old_value == 0:
service.call("notify","email",title="充电枪已通电", message=f"{datetime_now_str} 功率从 {old_value} 变为 {value}")
elif value == 0:
if switch.cp2_cloud == "on":
service.call("notify","email",title="充电枪被拔出", message=f"{datetime_now_str} 功率从 {old_value} 变为 {value}")
else:
service.call("notify","email",title="充电枪已断电", message=f"{datetime_now_str} 功率从 {old_value} 变为 {value}")
elif ((0 < value < 6) and (old_value > 6)):
service.call("notify","email",title="充电已经完成", message=f"{datetime_now_str} 功率从 {old_value} 变为 {value}")
elif ((old_value <= 2200) and (value > 2200)):
service.call("notify","email",title="充电枪进入16A模式", message=f"{datetime_now_str} 功率从 {old_value} 变为 {value}")
elif (((old_value <= 6) or (old_value > 2200)) and (6 < value <= 2200)):
service.call("notify","email",title="充电枪进入10A模式", message=f"{datetime_now_str} 功率从 {old_value} 变为 {value}")
else:
pass |
|